1
0

ForEachTest.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using System.Linq;
  8. using Xunit;
  9. namespace Tests
  10. {
  11. public class ForEachTest : Tests
  12. {
  13. [Fact]
  14. public void ForEach_Arguments()
  15. {
  16. AssertThrows<ArgumentNullException>(() => EnumerableEx.ForEach<int>(null, x => { }));
  17. AssertThrows<ArgumentNullException>(() => EnumerableEx.ForEach<int>(new[] { 1 }, default(Action<int>)));
  18. AssertThrows<ArgumentNullException>(() => EnumerableEx.ForEach<int>(null, (x, i) => { }));
  19. AssertThrows<ArgumentNullException>(() => EnumerableEx.ForEach<int>(new[] { 1 }, default(Action<int, int>)));
  20. }
  21. [Fact]
  22. public void ForEach1()
  23. {
  24. var n = 0;
  25. Enumerable.Range(5, 3).ForEach(x => n += x);
  26. Assert.Equal(5 + 6 + 7, n);
  27. }
  28. [Fact]
  29. public void ForEach2()
  30. {
  31. var n = 0;
  32. Enumerable.Range(5, 3).ForEach((x, i) => n += x * i);
  33. Assert.Equal(5 * 0 + 6 * 1 + 7 * 2, n);
  34. }
  35. }
  36. }