Aggregate.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 Aggregate : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public async Task Aggregate_Null()
  15. {
  16. // T
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int>(default, (x, y) => x + y).AsTask());
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, default(Func<int, int, int>)).AsTask());
  19. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int>(default, 0, (x, y) => x + y).AsTask());
  20. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func<int, int, int>)).AsTask());
  21. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int, int>(default, 0, (x, y) => x + y, z => z).AsTask());
  22. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, 0, default, z => z).AsTask());
  23. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int, int>(Return42, 0, (x, y) => x + y, default).AsTask());
  24. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int>(default, (x, y) => x + y, CancellationToken.None).AsTask());
  25. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, default(Func<int, int, int>), CancellationToken.None).AsTask());
  26. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int>(default, 0, (x, y) => x + y, CancellationToken.None).AsTask());
  27. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func<int, int, int>), CancellationToken.None).AsTask());
  28. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int, int>(default, 0, (x, y) => x + y, z => z, CancellationToken.None).AsTask());
  29. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, 0, default, z => z, CancellationToken.None).AsTask());
  30. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int, int>(Return42, 0, (x, y) => x + y, default, CancellationToken.None).AsTask());
  31. // ValueTask<T>
  32. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int>(default, (x, y) => new ValueTask<int>(x + y)).AsTask());
  33. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, default(Func<int, int, ValueTask<int>>)).AsTask());
  34. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int>(default, 0, (x, y) => new ValueTask<int>(x + y)).AsTask());
  35. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func<int, int, ValueTask<int>>)).AsTask());
  36. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int, int>(default, 0, (x, y) => new ValueTask<int>(x + y), z => new ValueTask<int>(z)).AsTask());
  37. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func<int, int, ValueTask<int>>), z => new ValueTask<int>(z)).AsTask());
  38. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int, int>(Return42, 0, (x, y) => new ValueTask<int>(x + y), default).AsTask());
  39. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int>(default, (x, y) => new ValueTask<int>(x + y), CancellationToken.None).AsTask());
  40. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, default(Func<int, int, ValueTask<int>>), CancellationToken.None).AsTask());
  41. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int>(default, 0, (x, y) => new ValueTask<int>(x + y), CancellationToken.None).AsTask());
  42. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func<int, int, ValueTask<int>>), CancellationToken.None).AsTask());
  43. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int, int>(default, 0, (x, y) => new ValueTask<int>(x + y), z => new ValueTask<int>(z), CancellationToken.None).AsTask());
  44. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func<int, int, ValueTask<int>>), z => new ValueTask<int>(z), CancellationToken.None).AsTask());
  45. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int, int>(Return42, 0, (x, y) => new ValueTask<int>(x + y), default, CancellationToken.None).AsTask());
  46. #if !NO_DEEP_CANCELLATION
  47. // CancellationToken, ValueTask<T>
  48. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int>(default, (x, y, ct) => new ValueTask<int>(x + y)).AsTask());
  49. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, default(Func<int, int, CancellationToken, ValueTask<int>>)).AsTask());
  50. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int>(default, 0, (x, y, ct) => new ValueTask<int>(x + y)).AsTask());
  51. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func<int, int, CancellationToken, ValueTask<int>>)).AsTask());
  52. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int, int>(default, 0, (x, y, ct) => new ValueTask<int>(x + y), (z, ct) => new ValueTask<int>(z)).AsTask());
  53. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func<int, int, CancellationToken, ValueTask<int>>), (z, ct) => new ValueTask<int>(z)).AsTask());
  54. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int, int>(Return42, 0, (x, y, ct) => new ValueTask<int>(x + y), default).AsTask());
  55. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int>(default, (x, y, ct) => new ValueTask<int>(x + y), CancellationToken.None).AsTask());
  56. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, default(Func<int, int, CancellationToken, ValueTask<int>>), CancellationToken.None).AsTask());
  57. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int>(default, 0, (x, y, ct) => new ValueTask<int>(x + y), CancellationToken.None).AsTask());
  58. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func<int, int, CancellationToken, ValueTask<int>>), CancellationToken.None).AsTask());
  59. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int, int>(default, 0, (x, y, ct) => new ValueTask<int>(x + y), (z, ct) => new ValueTask<int>(z), CancellationToken.None).AsTask());
  60. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func<int, int, CancellationToken, ValueTask<int>>), (z, ct) => new ValueTask<int>(z), CancellationToken.None).AsTask());
  61. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int, int>(Return42, 0, (x, y, ct) => new ValueTask<int>(x + y), default, CancellationToken.None).AsTask());
  62. #endif
  63. }
  64. [Fact]
  65. public async Task AggregateAsync_Sync_Simple()
  66. {
  67. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  68. var ys = xs.AggregateAsync((x, y) => x * y);
  69. Assert.Equal(24, await ys);
  70. }
  71. [Fact]
  72. public async Task AggregateAsync_Sync_Empty()
  73. {
  74. var xs = new int[0].ToAsyncEnumerable();
  75. var ys = xs.AggregateAsync((x, y) => x * y);
  76. await AssertThrowsAsync<InvalidOperationException>(ys.AsTask());
  77. }
  78. [Fact]
  79. public async Task AggregateAsync_Sync_Throw_Source()
  80. {
  81. var ex = new Exception("Bang!");
  82. var xs = Throw<int>(ex);
  83. var ys = xs.AggregateAsync((x, y) => x * y);
  84. await AssertThrowsAsync(ys, ex);
  85. }
  86. [Fact]
  87. public async Task AggregateAsync_Sync_Throw_Selector()
  88. {
  89. var ex = new Exception("Bang!");
  90. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  91. var ys = xs.AggregateAsync(new Func<int, int, int>((x, y) => { throw ex; }));
  92. await AssertThrowsAsync(ys, ex);
  93. }
  94. [Fact]
  95. public async Task AggregateAsync_Sync_Seed_Simple()
  96. {
  97. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  98. var ys = xs.AggregateAsync(1, (x, y) => x * y);
  99. Assert.Equal(24, await ys);
  100. }
  101. [Fact]
  102. public async Task AggregateAsync_Sync_Seed_Emtpy()
  103. {
  104. var xs = new int[0].ToAsyncEnumerable();
  105. var ys = xs.AggregateAsync(1, (x, y) => x * y);
  106. Assert.Equal(1, await ys);
  107. }
  108. [Fact]
  109. public async Task AggregateAsync_Sync_Seed_Throw_Source()
  110. {
  111. var ex = new Exception("Bang!");
  112. var xs = Throw<int>(ex);
  113. var ys = xs.AggregateAsync(1, (x, y) => x * y);
  114. await AssertThrowsAsync(ys, ex);
  115. }
  116. [Fact]
  117. public async Task AggregateAsync_Sync_Seed_Throw_Selector()
  118. {
  119. var ex = new Exception("Bang!");
  120. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  121. var ys = xs.AggregateAsync(1, new Func<int, int, int>((x, y) => { throw ex; }));
  122. await AssertThrowsAsync(ys, ex);
  123. }
  124. [Fact]
  125. public async Task AggregateAsync_Sync_Seed_Result_Simple()
  126. {
  127. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  128. var ys = xs.AggregateAsync(1, (x, y) => x * y, x => x + 1);
  129. Assert.Equal(25, await ys);
  130. }
  131. [Fact]
  132. public async Task AggregateAsync_Sync_Seed_Result_Empty()
  133. {
  134. var xs = new int[0].ToAsyncEnumerable();
  135. var ys = xs.AggregateAsync(1, (x, y) => x * y, x => x + 1);
  136. Assert.Equal(2, await ys);
  137. }
  138. [Fact]
  139. public async Task AggregateAsync_Sync_Seed_Result_Throw_Source()
  140. {
  141. var ex = new Exception("Bang!");
  142. var xs = Throw<int>(ex);
  143. var ys = xs.AggregateAsync(1, (x, y) => x * y, x => x + 1);
  144. await AssertThrowsAsync(ys, ex);
  145. }
  146. [Fact]
  147. public async Task AggregateAsync_Sync_Seed_Result_Throw_Selector()
  148. {
  149. var ex = new Exception("Bang!");
  150. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  151. var ys = xs.AggregateAsync(1, (x, y) => { throw ex; }, x => x + 1);
  152. await AssertThrowsAsync(ys, ex);
  153. }
  154. [Fact]
  155. public async Task AggregateAsync_Sync_Seed_Result_Throw_ResultSelector()
  156. {
  157. var ex = new Exception("Bang!");
  158. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  159. var ys = xs.AggregateAsync<int, int, int>(1, (x, y) => x * y, x => { throw ex; });
  160. await AssertThrowsAsync(ys, ex);
  161. }
  162. [Fact]
  163. public async Task AggregateAsync_Async_Simple()
  164. {
  165. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  166. var ys = xs.AggregateAsync((x, y) => new ValueTask<int>(x * y));
  167. Assert.Equal(24, await ys);
  168. }
  169. [Fact]
  170. public async Task AggregateAsync_Async_Empty()
  171. {
  172. var xs = new int[0].ToAsyncEnumerable();
  173. var ys = xs.AggregateAsync((x, y) => new ValueTask<int>(x * y));
  174. await AssertThrowsAsync<InvalidOperationException>(ys.AsTask());
  175. }
  176. [Fact]
  177. public async Task AggregateAsync_Async_Throw_Source()
  178. {
  179. var ex = new Exception("Bang!");
  180. var xs = Throw<int>(ex);
  181. var ys = xs.AggregateAsync((x, y) => new ValueTask<int>(x * y));
  182. await AssertThrowsAsync(ys, ex);
  183. }
  184. [Fact]
  185. public async Task AggregateAsync_Async_Throw_Selector()
  186. {
  187. var ex = new Exception("Bang!");
  188. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  189. var ys = xs.AggregateAsync(new Func<int, int, ValueTask<int>>((x, y) => { throw ex; }));
  190. await AssertThrowsAsync(ys, ex);
  191. }
  192. [Fact]
  193. public async Task AggregateAsync_Async_Seed_Simple()
  194. {
  195. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  196. var ys = xs.AggregateAsync(1, (x, y) => new ValueTask<int>(x * y));
  197. Assert.Equal(24, await ys);
  198. }
  199. [Fact]
  200. public async Task AggregateAsync_Async_Seed_Emtpy()
  201. {
  202. var xs = new int[0].ToAsyncEnumerable();
  203. var ys = xs.AggregateAsync(1, (x, y) => new ValueTask<int>(x * y));
  204. Assert.Equal(1, await ys);
  205. }
  206. [Fact]
  207. public async Task AggregateAsync_Async_Seed_Throw_Source()
  208. {
  209. var ex = new Exception("Bang!");
  210. var xs = Throw<int>(ex);
  211. var ys = xs.AggregateAsync(1, (x, y) => new ValueTask<int>(x * y));
  212. await AssertThrowsAsync(ys, ex);
  213. }
  214. [Fact]
  215. public async Task AggregateAsync_Async_Seed_Throw_Selector()
  216. {
  217. var ex = new Exception("Bang!");
  218. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  219. var ys = xs.AggregateAsync(1, new Func<int, int, ValueTask<int>>((x, y) => { throw ex; }));
  220. await AssertThrowsAsync(ys, ex);
  221. }
  222. [Fact]
  223. public async Task AggregateAsync_Async_Seed_Result_Simple()
  224. {
  225. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  226. var ys = xs.AggregateAsync(1, (x, y) => new ValueTask<int>(x * y), x => new ValueTask<int>(x + 1));
  227. Assert.Equal(25, await ys);
  228. }
  229. [Fact]
  230. public async Task AggregateAsync_Async_Seed_Result_Empty()
  231. {
  232. var xs = new int[0].ToAsyncEnumerable();
  233. var ys = xs.AggregateAsync(1, (x, y) => new ValueTask<int>(x * y), x => new ValueTask<int>(x + 1));
  234. Assert.Equal(2, await ys);
  235. }
  236. [Fact]
  237. public async Task AggregateAsync_Async_Seed_Result_Throw_Source()
  238. {
  239. var ex = new Exception("Bang!");
  240. var xs = Throw<int>(ex);
  241. var ys = xs.AggregateAsync(1, (x, y) => new ValueTask<int>(x * y), x => new ValueTask<int>(x + 1));
  242. await AssertThrowsAsync(ys, ex);
  243. }
  244. [Fact]
  245. public async Task AggregateAsync_Async_Seed_Result_Throw_Selector()
  246. {
  247. var ex = new Exception("Bang!");
  248. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  249. var ys = xs.AggregateAsync(1, new Func<int, int, ValueTask<int>>((x, y) => { throw ex; }), x => new ValueTask<int>(x + 1));
  250. await AssertThrowsAsync(ys, ex);
  251. }
  252. [Fact]
  253. public async Task AggregateAsync_Async_Seed_Result_Throw_ResultSelector()
  254. {
  255. var ex = new Exception("Bang!");
  256. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  257. var ys = xs.AggregateAsync<int, int, int>(1, (x, y) => new ValueTask<int>(x * y), x => { throw ex; });
  258. await AssertThrowsAsync(ys, ex);
  259. }
  260. #if !NO_DEEP_CANCELLATION
  261. [Fact]
  262. public async Task AggregateAsyncCancel_AsyncCancel_Simple()
  263. {
  264. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  265. var ys = xs.AggregateAsync((x, y, ct) => new ValueTask<int>(x * y));
  266. Assert.Equal(24, await ys);
  267. }
  268. [Fact]
  269. public async Task AggregateAsyncCancel_AsyncCancel_Empty()
  270. {
  271. var xs = new int[0].ToAsyncEnumerable();
  272. var ys = xs.AggregateAsync((x, y, ct) => new ValueTask<int>(x * y));
  273. await AssertThrowsAsync<InvalidOperationException>(ys.AsTask());
  274. }
  275. [Fact]
  276. public async Task AggregateAsyncCancel_AsyncCancel_Throw_Source()
  277. {
  278. var ex = new Exception("Bang!");
  279. var xs = Throw<int>(ex);
  280. var ys = xs.AggregateAsync((x, y, ct) => new ValueTask<int>(x * y));
  281. await AssertThrowsAsync(ys, ex);
  282. }
  283. [Fact]
  284. public async Task AggregateAsyncCancel_AsyncCancel_Throw_Selector()
  285. {
  286. var ex = new Exception("Bang!");
  287. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  288. var ys = xs.AggregateAsync(new Func<int, int, CancellationToken, ValueTask<int>>((x, y, ct) => { throw ex; }));
  289. await AssertThrowsAsync(ys, ex);
  290. }
  291. [Fact]
  292. public async Task AggregateAsyncCancel_AsyncCancel_Seed_Simple()
  293. {
  294. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  295. var ys = xs.AggregateAsync(1, (x, y, ct) => new ValueTask<int>(x * y));
  296. Assert.Equal(24, await ys);
  297. }
  298. [Fact]
  299. public async Task AggregateAsyncCancel_AsyncCancel_Seed_Emtpy()
  300. {
  301. var xs = new int[0].ToAsyncEnumerable();
  302. var ys = xs.AggregateAsync(1, (x, y, ct) => new ValueTask<int>(x * y));
  303. Assert.Equal(1, await ys);
  304. }
  305. [Fact]
  306. public async Task AggregateAsyncCancel_AsyncCancel_Seed_Throw_Source()
  307. {
  308. var ex = new Exception("Bang!");
  309. var xs = Throw<int>(ex);
  310. var ys = xs.AggregateAsync(1, (x, y, ct) => new ValueTask<int>(x * y));
  311. await AssertThrowsAsync(ys, ex);
  312. }
  313. [Fact]
  314. public async Task AggregateAsyncCancel_AsyncCancel_Seed_Throw_Selector()
  315. {
  316. var ex = new Exception("Bang!");
  317. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  318. var ys = xs.AggregateAsync(1, new Func<int, int, CancellationToken, ValueTask<int>>((x, y, ct) => { throw ex; }));
  319. await AssertThrowsAsync(ys, ex);
  320. }
  321. [Fact]
  322. public async Task AggregateAsyncCancel_AsyncCancel_Seed_Result_Simple()
  323. {
  324. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  325. var ys = xs.AggregateAsync(1, (x, y, ct) => new ValueTask<int>(x * y), (x, ct) => new ValueTask<int>(x + 1));
  326. Assert.Equal(25, await ys);
  327. }
  328. [Fact]
  329. public async Task AggregateAsyncCancel_AsyncCancel_Seed_Result_Empty()
  330. {
  331. var xs = new int[0].ToAsyncEnumerable();
  332. var ys = xs.AggregateAsync(1, (x, y, ct) => new ValueTask<int>(x * y), (x, ct) => new ValueTask<int>(x + 1));
  333. Assert.Equal(2, await ys);
  334. }
  335. [Fact]
  336. public async Task AggregateAsyncCancel_AsyncCancel_Seed_Result_Throw_Source()
  337. {
  338. var ex = new Exception("Bang!");
  339. var xs = Throw<int>(ex);
  340. var ys = xs.AggregateAsync(1, (x, y, ct) => new ValueTask<int>(x * y), (x, ct) => new ValueTask<int>(x + 1));
  341. await AssertThrowsAsync(ys, ex);
  342. }
  343. [Fact]
  344. public async Task AggregateAsyncCancel_AsyncCancel_Seed_Result_Throw_Selector()
  345. {
  346. var ex = new Exception("Bang!");
  347. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  348. var ys = xs.AggregateAsync(1, new Func<int, int, CancellationToken, ValueTask<int>>((x, y, ct) => { throw ex; }), (x, ct) => new ValueTask<int>(x + 1));
  349. await AssertThrowsAsync(ys, ex);
  350. }
  351. [Fact]
  352. public async Task AggregateAsyncCancel_AsyncCancel_Seed_Result_Throw_ResultSelector()
  353. {
  354. var ex = new Exception("Bang!");
  355. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  356. var ys = xs.AggregateAsync<int, int, int>(1, (x, y, ct) => new ValueTask<int>(x * y), (x, ct) => { throw ex; });
  357. await AssertThrowsAsync(ys, ex);
  358. }
  359. #endif
  360. }
  361. }