ForEach.cs 1.2 KB

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