WhileTest.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 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 WhileTest : Tests
  12. {
  13. [Fact]
  14. public void While_Arguments()
  15. {
  16. AssertThrows<ArgumentNullException>(() => EnumerableEx.While<int>(null, new[] { 1 }));
  17. AssertThrows<ArgumentNullException>(() => EnumerableEx.While<int>(() => true, null));
  18. }
  19. [Fact]
  20. public void While1()
  21. {
  22. var x = 5;
  23. var res = EnumerableEx.While(() => x > 0, EnumerableEx.Defer(() => new[] { x }).Do(_ => x--)).ToList();
  24. Assert.True(Enumerable.SequenceEqual(res, new[] { 5, 4, 3, 2, 1 }));
  25. }
  26. [Fact]
  27. public void While2()
  28. {
  29. var x = 0;
  30. var res = EnumerableEx.While(() => x > 0, EnumerableEx.Defer(() => new[] { x }).Do(_ => x--)).ToList();
  31. Assert.True(Enumerable.SequenceEqual(res, new int[0]));
  32. }
  33. }
  34. }