DoWhile.cs 1.1 KB

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