Where.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.Linq;
  6. using System.Threading.Tasks;
  7. using Xunit;
  8. namespace Tests
  9. {
  10. public class Where : AsyncEnumerableTests
  11. {
  12. [Fact]
  13. public void Where_Null()
  14. {
  15. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Where<int>(null, x => true));
  16. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Where<int>(null, (x, i) => true));
  17. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Where<int>(AsyncEnumerable.Return<int>(42), default(Func<int, bool>)));
  18. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Where<int>(AsyncEnumerable.Return<int>(42), default(Func<int, int, bool>)));
  19. }
  20. [Fact]
  21. public void Where1()
  22. {
  23. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  24. var ys = xs.Where(x => x % 2 == 0);
  25. var e = ys.GetAsyncEnumerator();
  26. HasNext(e, 8);
  27. HasNext(e, 4);
  28. HasNext(e, 6);
  29. HasNext(e, 2);
  30. HasNext(e, 0);
  31. NoNext(e);
  32. }
  33. [Fact]
  34. public void Where2()
  35. {
  36. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  37. var ys = xs.Where((x, i) => i % 2 == 0);
  38. var e = ys.GetAsyncEnumerator();
  39. HasNext(e, 8);
  40. HasNext(e, 7);
  41. HasNext(e, 6);
  42. HasNext(e, 2);
  43. HasNext(e, 0);
  44. NoNext(e);
  45. }
  46. [Fact]
  47. public void Where3()
  48. {
  49. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  50. var ex = new Exception("Bang");
  51. var ys = xs.Where(x => { if (x == 4) throw ex; return true; });
  52. var e = ys.GetAsyncEnumerator();
  53. HasNext(e, 8);
  54. HasNext(e, 5);
  55. HasNext(e, 7);
  56. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  57. }
  58. [Fact]
  59. public void Where4()
  60. {
  61. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  62. var ex = new Exception("Bang");
  63. var ys = xs.Where((x, i) => { if (i == 3) throw ex; return true; });
  64. var e = ys.GetAsyncEnumerator();
  65. HasNext(e, 8);
  66. HasNext(e, 5);
  67. HasNext(e, 7);
  68. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  69. }
  70. [Fact]
  71. public void Where5()
  72. {
  73. var ex = new Exception("Bang");
  74. var xs = AsyncEnumerable.Throw<int>(ex);
  75. var ys = xs.Where(x => true);
  76. var e = ys.GetAsyncEnumerator();
  77. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  78. }
  79. [Fact]
  80. public void Where6()
  81. {
  82. var ex = new Exception("Bang");
  83. var xs = AsyncEnumerable.Throw<int>(ex);
  84. var ys = xs.Where((x, i) => true);
  85. var e = ys.GetAsyncEnumerator();
  86. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  87. }
  88. [Fact]
  89. public void Where7()
  90. {
  91. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  92. var ys = xs.Where(x => x % 2 == 0).Where(x => x > 5);
  93. var e = ys.GetAsyncEnumerator();
  94. HasNext(e, 8);
  95. HasNext(e, 6);
  96. NoNext(e);
  97. }
  98. [Fact]
  99. public async Task Where8()
  100. {
  101. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  102. var ys = xs.Where(x => x % 2 == 0);
  103. await SequenceIdentity(ys);
  104. }
  105. [Fact]
  106. public async Task Where9()
  107. {
  108. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  109. var ys = xs.Where((x, i) => i % 2 == 0);
  110. await SequenceIdentity(ys);
  111. }
  112. }
  113. }