Where.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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;
  7. using System.Threading.Tasks;
  8. using Xunit;
  9. namespace Tests
  10. {
  11. public class Where : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public void Where_Sync_Null()
  15. {
  16. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Where<int>(default, x => true));
  17. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Where<int>(default, (x, i) => true));
  18. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Where(Return42, default(Func<int, bool>)));
  19. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Where(Return42, default(Func<int, int, bool>)));
  20. }
  21. [Fact]
  22. public async Task Where_Sync_Simple()
  23. {
  24. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  25. var ys = xs.Where(x => x % 2 == 0);
  26. var e = ys.GetAsyncEnumerator();
  27. await HasNextAsync(e, 8);
  28. await HasNextAsync(e, 4);
  29. await HasNextAsync(e, 6);
  30. await HasNextAsync(e, 2);
  31. await HasNextAsync(e, 0);
  32. await NoNextAsync(e);
  33. }
  34. [Fact]
  35. public async Task Where_Sync_Indexed()
  36. {
  37. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  38. var ys = xs.Where((x, i) => i % 2 == 0);
  39. var e = ys.GetAsyncEnumerator();
  40. await HasNextAsync(e, 8);
  41. await HasNextAsync(e, 7);
  42. await HasNextAsync(e, 6);
  43. await HasNextAsync(e, 2);
  44. await HasNextAsync(e, 0);
  45. await NoNextAsync(e);
  46. }
  47. [Fact]
  48. public async Task Where_Sync_Throws_Predicate()
  49. {
  50. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  51. var ex = new Exception("Bang");
  52. var ys = xs.Where(x => { if (x == 4) throw ex; return true; });
  53. var e = ys.GetAsyncEnumerator();
  54. await HasNextAsync(e, 8);
  55. await HasNextAsync(e, 5);
  56. await HasNextAsync(e, 7);
  57. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  58. }
  59. [Fact]
  60. public async Task Where_Sync_Indexed_Throws_Predicate()
  61. {
  62. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  63. var ex = new Exception("Bang");
  64. var ys = xs.Where((x, i) => { if (i == 3) throw ex; return true; });
  65. var e = ys.GetAsyncEnumerator();
  66. await HasNextAsync(e, 8);
  67. await HasNextAsync(e, 5);
  68. await HasNextAsync(e, 7);
  69. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  70. }
  71. [Fact]
  72. public async Task Where_Sync_Throws_Source()
  73. {
  74. var ex = new Exception("Bang");
  75. var xs = Throw<int>(ex);
  76. var ys = xs.Where(x => true);
  77. var e = ys.GetAsyncEnumerator();
  78. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  79. }
  80. [Fact]
  81. public async Task Where_Sync_Indexed_Throws_Source()
  82. {
  83. var ex = new Exception("Bang");
  84. var xs = Throw<int>(ex);
  85. var ys = xs.Where((x, i) => true);
  86. var e = ys.GetAsyncEnumerator();
  87. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  88. }
  89. [Fact]
  90. public async Task Where_Sync_WhereWhere()
  91. {
  92. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  93. var ys = xs.Where(x => x % 2 == 0).Where(x => x > 5);
  94. var e = ys.GetAsyncEnumerator();
  95. await HasNextAsync(e, 8);
  96. await HasNextAsync(e, 6);
  97. await NoNextAsync(e);
  98. }
  99. [Fact]
  100. public async Task Where_Sync_SequenceIdentity()
  101. {
  102. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  103. var ys = xs.Where(x => x % 2 == 0);
  104. await SequenceIdentity(ys);
  105. }
  106. [Fact]
  107. public async Task Where_Sync_Indexed_SequenceIdentity()
  108. {
  109. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  110. var ys = xs.Where((x, i) => i % 2 == 0);
  111. await SequenceIdentity(ys);
  112. }
  113. [Fact]
  114. public void Where_Async_Null()
  115. {
  116. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Where<int>(default, x => new ValueTask<bool>(true)));
  117. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Where<int>(default, (int x, int i) => new ValueTask<bool>(true)));
  118. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Where(Return42, default(Func<int, ValueTask<bool>>)));
  119. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Where(Return42, default(Func<int, int, ValueTask<bool>>)));
  120. }
  121. [Fact]
  122. public async Task Where_Async_Simple()
  123. {
  124. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  125. var ys = xs.Where(x => new ValueTask<bool>(x % 2 == 0));
  126. var e = ys.GetAsyncEnumerator();
  127. await HasNextAsync(e, 8);
  128. await HasNextAsync(e, 4);
  129. await HasNextAsync(e, 6);
  130. await HasNextAsync(e, 2);
  131. await HasNextAsync(e, 0);
  132. await NoNextAsync(e);
  133. }
  134. [Fact]
  135. public async Task Where_Async_Indexed()
  136. {
  137. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  138. var ys = xs.Where((x, i) => new ValueTask<bool>(i % 2 == 0));
  139. var e = ys.GetAsyncEnumerator();
  140. await HasNextAsync(e, 8);
  141. await HasNextAsync(e, 7);
  142. await HasNextAsync(e, 6);
  143. await HasNextAsync(e, 2);
  144. await HasNextAsync(e, 0);
  145. await NoNextAsync(e);
  146. }
  147. [Fact]
  148. public async Task Where_Async_Throws_Predicate()
  149. {
  150. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  151. var ex = new Exception("Bang");
  152. var ys = xs.Where(x => { if (x == 4) throw ex; return new ValueTask<bool>(true); });
  153. var e = ys.GetAsyncEnumerator();
  154. await HasNextAsync(e, 8);
  155. await HasNextAsync(e, 5);
  156. await HasNextAsync(e, 7);
  157. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  158. }
  159. [Fact]
  160. public async Task Where_Async_Indexed_Throws_Predicate()
  161. {
  162. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  163. var ex = new Exception("Bang");
  164. var ys = xs.Where((x, i) => { if (i == 3) throw ex; return new ValueTask<bool>(true); });
  165. var e = ys.GetAsyncEnumerator();
  166. await HasNextAsync(e, 8);
  167. await HasNextAsync(e, 5);
  168. await HasNextAsync(e, 7);
  169. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  170. }
  171. [Fact]
  172. public async Task Where_Async_Throws_Source()
  173. {
  174. var ex = new Exception("Bang");
  175. var xs = Throw<int>(ex);
  176. var ys = xs.Where(x => new ValueTask<bool>(true));
  177. var e = ys.GetAsyncEnumerator();
  178. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  179. }
  180. [Fact]
  181. public async Task Where_Async_Indexed_Throws_Source()
  182. {
  183. var ex = new Exception("Bang");
  184. var xs = Throw<int>(ex);
  185. var ys = xs.Where((int x, int i) => new ValueTask<bool>(true));
  186. var e = ys.GetAsyncEnumerator();
  187. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  188. }
  189. [Fact]
  190. public async Task Where_Async_WhereWhere()
  191. {
  192. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  193. var ys = xs.Where(x => new ValueTask<bool>(x % 2 == 0)).Where(x => new ValueTask<bool>(x > 5));
  194. var e = ys.GetAsyncEnumerator();
  195. await HasNextAsync(e, 8);
  196. await HasNextAsync(e, 6);
  197. await NoNextAsync(e);
  198. }
  199. [Fact]
  200. public async Task Where_Async_SequenceIdentity()
  201. {
  202. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  203. var ys = xs.Where(x => new ValueTask<bool>(x % 2 == 0));
  204. await SequenceIdentity(ys);
  205. }
  206. [Fact]
  207. public async Task Where_Async_Indexed_SequenceIdentity()
  208. {
  209. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  210. var ys = xs.Where((x, i) => new ValueTask<bool>(i % 2 == 0));
  211. await SequenceIdentity(ys);
  212. }
  213. #if !NO_DEEP_CANCELLATION
  214. // REVIEW: These overloads are problematic for type inference. E.g. xs.Where((x, ct) => ...) is ambiguous.
  215. [Fact]
  216. public void Where_Async_Cancel_Null()
  217. {
  218. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Where<int>(default, (int x, CancellationToken ct) => new ValueTask<bool>(true)));
  219. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Where<int>(default, (x, i, ct) => new ValueTask<bool>(true)));
  220. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Where(Return42, default(Func<int, CancellationToken, ValueTask<bool>>)));
  221. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Where(Return42, default(Func<int, int, CancellationToken, ValueTask<bool>>)));
  222. }
  223. [Fact]
  224. public async Task Where_Async_Cancel_Simple()
  225. {
  226. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  227. var ys = xs.Where((int x, CancellationToken ct) => new ValueTask<bool>(x % 2 == 0));
  228. var e = ys.GetAsyncEnumerator();
  229. await HasNextAsync(e, 8);
  230. await HasNextAsync(e, 4);
  231. await HasNextAsync(e, 6);
  232. await HasNextAsync(e, 2);
  233. await HasNextAsync(e, 0);
  234. await NoNextAsync(e);
  235. }
  236. [Fact]
  237. public async Task Where_Async_Cancel_Indexed()
  238. {
  239. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  240. var ys = xs.Where((x, i, ct) => new ValueTask<bool>(i % 2 == 0));
  241. var e = ys.GetAsyncEnumerator();
  242. await HasNextAsync(e, 8);
  243. await HasNextAsync(e, 7);
  244. await HasNextAsync(e, 6);
  245. await HasNextAsync(e, 2);
  246. await HasNextAsync(e, 0);
  247. await NoNextAsync(e);
  248. }
  249. [Fact]
  250. public async Task Where_Async_Cancel_Throws_Predicate()
  251. {
  252. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  253. var ex = new Exception("Bang");
  254. var ys = xs.Where((int x, CancellationToken ct) => { if (x == 4) throw ex; return new ValueTask<bool>(true); });
  255. var e = ys.GetAsyncEnumerator();
  256. await HasNextAsync(e, 8);
  257. await HasNextAsync(e, 5);
  258. await HasNextAsync(e, 7);
  259. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  260. }
  261. [Fact]
  262. public async Task Where_Async_Cancel_Indexed_Throws_Predicate()
  263. {
  264. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  265. var ex = new Exception("Bang");
  266. var ys = xs.Where((x, i, ct) => { if (i == 3) throw ex; return new ValueTask<bool>(true); });
  267. var e = ys.GetAsyncEnumerator();
  268. await HasNextAsync(e, 8);
  269. await HasNextAsync(e, 5);
  270. await HasNextAsync(e, 7);
  271. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  272. }
  273. [Fact]
  274. public async Task Where_Async_Cancel_Throws_Source()
  275. {
  276. var ex = new Exception("Bang");
  277. var xs = Throw<int>(ex);
  278. var ys = xs.Where((int x, CancellationToken ct) => new ValueTask<bool>(true));
  279. var e = ys.GetAsyncEnumerator();
  280. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  281. }
  282. [Fact]
  283. public async Task Where_Async_Cancel_Indexed_Throws_Source()
  284. {
  285. var ex = new Exception("Bang");
  286. var xs = Throw<int>(ex);
  287. var ys = xs.Where((x, i, ct) => new ValueTask<bool>(true));
  288. var e = ys.GetAsyncEnumerator();
  289. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  290. }
  291. [Fact]
  292. public async Task Where_Async_Cancel_WhereWhere()
  293. {
  294. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  295. var ys = xs.Where((int x, CancellationToken ct) => new ValueTask<bool>(x % 2 == 0)).Where((int x, CancellationToken ct) => new ValueTask<bool>(x > 5));
  296. var e = ys.GetAsyncEnumerator();
  297. await HasNextAsync(e, 8);
  298. await HasNextAsync(e, 6);
  299. await NoNextAsync(e);
  300. }
  301. [Fact]
  302. public async Task Where_Async_Cancel_SequenceIdentity()
  303. {
  304. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  305. var ys = xs.Where((int x, CancellationToken ct) => new ValueTask<bool>(x % 2 == 0));
  306. await SequenceIdentity(ys);
  307. }
  308. [Fact]
  309. public async Task Where_Async_Cancel_Indexed_SequenceIdentity()
  310. {
  311. var xs = new[] { 8, 5, 7, 4, 6, 9, 2, 1, 0 }.ToAsyncEnumerable();
  312. var ys = xs.Where((x, i, ct) => new ValueTask<bool>(i % 2 == 0));
  313. await SequenceIdentity(ys);
  314. }
  315. #endif
  316. }
  317. }