Zip.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 System.Threading;
  7. using System.Threading.Tasks;
  8. using Xunit;
  9. namespace Tests
  10. {
  11. public class Zip : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public void Zip_Null()
  15. {
  16. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Zip<int, int, int>(default, Return42, (x, y) => x + y));
  17. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Zip<int, int, int>(Return42, default, (x, y) => x + y));
  18. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Zip(Return42, Return42, default(Func<int, int, int>)));
  19. }
  20. [Fact]
  21. public async Task Zip_EqualLength()
  22. {
  23. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  24. var ys = new[] { 4, 5, 6 }.ToAsyncEnumerable();
  25. var res = xs.Zip(ys, (x, y) => x * y);
  26. var e = res.GetAsyncEnumerator();
  27. await HasNextAsync(e, 1 * 4);
  28. await HasNextAsync(e, 2 * 5);
  29. await HasNextAsync(e, 3 * 6);
  30. await NoNextAsync(e);
  31. }
  32. [Fact]
  33. public async Task Zip_LeftShorter()
  34. {
  35. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  36. var ys = new[] { 4, 5, 6, 7 }.ToAsyncEnumerable();
  37. var res = xs.Zip(ys, (x, y) => x * y);
  38. var e = res.GetAsyncEnumerator();
  39. await HasNextAsync(e, 1 * 4);
  40. await HasNextAsync(e, 2 * 5);
  41. await HasNextAsync(e, 3 * 6);
  42. await NoNextAsync(e);
  43. }
  44. [Fact]
  45. public async Task Zip_RightShorter()
  46. {
  47. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  48. var ys = new[] { 4, 5, 6 }.ToAsyncEnumerable();
  49. var res = xs.Zip(ys, (x, y) => x * y);
  50. var e = res.GetAsyncEnumerator();
  51. await HasNextAsync(e, 1 * 4);
  52. await HasNextAsync(e, 2 * 5);
  53. await HasNextAsync(e, 3 * 6);
  54. await NoNextAsync(e);
  55. }
  56. [Fact]
  57. public async Task Zip_Throws_Right()
  58. {
  59. var ex = new Exception("Bang!");
  60. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  61. var ys = Throw<int>(ex);
  62. var res = xs.Zip(ys, (x, y) => x * y);
  63. var e = res.GetAsyncEnumerator();
  64. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  65. }
  66. [Fact]
  67. public async Task Zip_Throws_Left()
  68. {
  69. var ex = new Exception("Bang!");
  70. var xs = Throw<int>(ex);
  71. var ys = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  72. var res = xs.Zip(ys, (x, y) => x * y);
  73. var e = res.GetAsyncEnumerator();
  74. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  75. }
  76. [Fact]
  77. public async Task Zip_Throws_Selector()
  78. {
  79. var ex = new Exception("Bang!");
  80. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  81. var ys = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  82. var res = xs.Zip(ys, (x, y) => { if (x > 0) throw ex; return x * y; });
  83. var e = res.GetAsyncEnumerator();
  84. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  85. }
  86. [Fact]
  87. public async Task Zip_SequenceIdentity()
  88. {
  89. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  90. var ys = new[] { 4, 5, 6 }.ToAsyncEnumerable();
  91. var res = xs.Zip(ys, (x, y) => x * y);
  92. await SequenceIdentity(res);
  93. }
  94. [Fact]
  95. public void ZipAwait_Null()
  96. {
  97. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.ZipAwait<int, int, int>(default, Return42, (x, y) => new ValueTask<int>(x + y)));
  98. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.ZipAwait<int, int, int>(Return42, default, (x, y) => new ValueTask<int>(x + y)));
  99. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.ZipAwait(Return42, Return42, default(Func<int, int, ValueTask<int>>)));
  100. }
  101. [Fact]
  102. public async Task ZipAwait_EqualLength()
  103. {
  104. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  105. var ys = new[] { 4, 5, 6 }.ToAsyncEnumerable();
  106. var res = xs.ZipAwait(ys, (x, y) => new ValueTask<int>(x * y));
  107. var e = res.GetAsyncEnumerator();
  108. await HasNextAsync(e, 1 * 4);
  109. await HasNextAsync(e, 2 * 5);
  110. await HasNextAsync(e, 3 * 6);
  111. await NoNextAsync(e);
  112. }
  113. [Fact]
  114. public async Task ZipAwait_LeftShorter()
  115. {
  116. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  117. var ys = new[] { 4, 5, 6, 7 }.ToAsyncEnumerable();
  118. var res = xs.ZipAwait(ys, (x, y) => new ValueTask<int>(x * y));
  119. var e = res.GetAsyncEnumerator();
  120. await HasNextAsync(e, 1 * 4);
  121. await HasNextAsync(e, 2 * 5);
  122. await HasNextAsync(e, 3 * 6);
  123. await NoNextAsync(e);
  124. }
  125. [Fact]
  126. public async Task ZipAwait_RightShorter()
  127. {
  128. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  129. var ys = new[] { 4, 5, 6 }.ToAsyncEnumerable();
  130. var res = xs.ZipAwait(ys, (x, y) => new ValueTask<int>(x * y));
  131. var e = res.GetAsyncEnumerator();
  132. await HasNextAsync(e, 1 * 4);
  133. await HasNextAsync(e, 2 * 5);
  134. await HasNextAsync(e, 3 * 6);
  135. await NoNextAsync(e);
  136. }
  137. [Fact]
  138. public async Task ZipAwait_Throws_Right()
  139. {
  140. var ex = new Exception("Bang!");
  141. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  142. var ys = Throw<int>(ex);
  143. var res = xs.ZipAwait(ys, (x, y) => new ValueTask<int>(x * y));
  144. var e = res.GetAsyncEnumerator();
  145. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  146. }
  147. [Fact]
  148. public async Task ZipAwait_Throws_Left()
  149. {
  150. var ex = new Exception("Bang!");
  151. var xs = Throw<int>(ex);
  152. var ys = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  153. var res = xs.ZipAwait(ys, (x, y) => new ValueTask<int>(x * y));
  154. var e = res.GetAsyncEnumerator();
  155. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  156. }
  157. [Fact]
  158. public async Task ZipAwait_Throws_Selector()
  159. {
  160. var ex = new Exception("Bang!");
  161. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  162. var ys = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  163. var res = xs.ZipAwait(ys, (x, y) => { if (x > 0) throw ex; return new ValueTask<int>(x * y); });
  164. var e = res.GetAsyncEnumerator();
  165. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  166. }
  167. [Fact]
  168. public async Task ZipAwait_SequenceIdentity()
  169. {
  170. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  171. var ys = new[] { 4, 5, 6 }.ToAsyncEnumerable();
  172. var res = xs.ZipAwait(ys, (x, y) => new ValueTask<int>(x * y));
  173. await SequenceIdentity(res);
  174. }
  175. #if !NO_DEEP_CANCELLATION
  176. [Fact]
  177. public void ZipAwaitWithCancellation_Null()
  178. {
  179. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.ZipAwaitWithCancellation<int, int, int>(default, Return42, (x, y, ct) => new ValueTask<int>(x + y)));
  180. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.ZipAwaitWithCancellation<int, int, int>(Return42, default, (x, y, ct) => new ValueTask<int>(x + y)));
  181. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.ZipAwaitWithCancellation(Return42, Return42, default(Func<int, int, CancellationToken, ValueTask<int>>)));
  182. }
  183. [Fact]
  184. public async Task ZipAwaitWithCancellation_EqualLength()
  185. {
  186. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  187. var ys = new[] { 4, 5, 6 }.ToAsyncEnumerable();
  188. var res = xs.ZipAwaitWithCancellation(ys, (x, y, ct) => new ValueTask<int>(x * y));
  189. var e = res.GetAsyncEnumerator();
  190. await HasNextAsync(e, 1 * 4);
  191. await HasNextAsync(e, 2 * 5);
  192. await HasNextAsync(e, 3 * 6);
  193. await NoNextAsync(e);
  194. }
  195. [Fact]
  196. public async Task ZipAwaitWithCancellation_LeftShorter()
  197. {
  198. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  199. var ys = new[] { 4, 5, 6, 7 }.ToAsyncEnumerable();
  200. var res = xs.ZipAwaitWithCancellation(ys, (x, y, ct) => new ValueTask<int>(x * y));
  201. var e = res.GetAsyncEnumerator();
  202. await HasNextAsync(e, 1 * 4);
  203. await HasNextAsync(e, 2 * 5);
  204. await HasNextAsync(e, 3 * 6);
  205. await NoNextAsync(e);
  206. }
  207. [Fact]
  208. public async Task ZipAwaitWithCancellation_RightShorter()
  209. {
  210. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  211. var ys = new[] { 4, 5, 6 }.ToAsyncEnumerable();
  212. var res = xs.ZipAwaitWithCancellation(ys, (x, y, ct) => new ValueTask<int>(x * y));
  213. var e = res.GetAsyncEnumerator();
  214. await HasNextAsync(e, 1 * 4);
  215. await HasNextAsync(e, 2 * 5);
  216. await HasNextAsync(e, 3 * 6);
  217. await NoNextAsync(e);
  218. }
  219. [Fact]
  220. public async Task ZipAwaitWithCancellation_Throws_Right()
  221. {
  222. var ex = new Exception("Bang!");
  223. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  224. var ys = Throw<int>(ex);
  225. var res = xs.ZipAwaitWithCancellation(ys, (x, y, ct) => new ValueTask<int>(x * y));
  226. var e = res.GetAsyncEnumerator();
  227. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  228. }
  229. [Fact]
  230. public async Task ZipAwaitWithCancellation_Throws_Left()
  231. {
  232. var ex = new Exception("Bang!");
  233. var xs = Throw<int>(ex);
  234. var ys = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  235. var res = xs.ZipAwaitWithCancellation(ys, (x, y, ct) => new ValueTask<int>(x * y));
  236. var e = res.GetAsyncEnumerator();
  237. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  238. }
  239. [Fact]
  240. public async Task ZipAwaitWithCancellation_Throws_Selector()
  241. {
  242. var ex = new Exception("Bang!");
  243. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  244. var ys = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  245. var res = xs.ZipAwaitWithCancellation(ys, (x, y, ct) => { if (x > 0) throw ex; return new ValueTask<int>(x * y); });
  246. var e = res.GetAsyncEnumerator();
  247. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  248. }
  249. [Fact]
  250. public async Task ZipAwaitWithCancellation_SequenceIdentity()
  251. {
  252. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  253. var ys = new[] { 4, 5, 6 }.ToAsyncEnumerable();
  254. var res = xs.ZipAwaitWithCancellation(ys, (x, y, ct) => new ValueTask<int>(x * y));
  255. await SequenceIdentity(res);
  256. }
  257. #endif
  258. [Fact]
  259. public void Zip_Tuple_Null()
  260. {
  261. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Zip<int, int>(default, Return42));
  262. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Zip<int, int>(Return42, default));
  263. }
  264. [Fact]
  265. public async Task Zip_Tuple_EqualLength()
  266. {
  267. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  268. var ys = new[] { 4, 5, 6 }.ToAsyncEnumerable();
  269. var res = xs.Zip(ys);
  270. var e = res.GetAsyncEnumerator();
  271. await HasNextAsync(e, (1, 4));
  272. await HasNextAsync(e, (2, 5));
  273. await HasNextAsync(e, (3, 6));
  274. await NoNextAsync(e);
  275. }
  276. [Fact]
  277. public async Task Zip_Tuple_LeftShorter()
  278. {
  279. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  280. var ys = new[] { 4, 5, 6, 7 }.ToAsyncEnumerable();
  281. var res = xs.Zip(ys);
  282. var e = res.GetAsyncEnumerator();
  283. await HasNextAsync(e, (1, 4));
  284. await HasNextAsync(e, (2, 5));
  285. await HasNextAsync(e, (3, 6));
  286. await NoNextAsync(e);
  287. }
  288. [Fact]
  289. public async Task Zip_Tuple_RightShorter()
  290. {
  291. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  292. var ys = new[] { 4, 5, 6 }.ToAsyncEnumerable();
  293. var res = xs.Zip(ys);
  294. var e = res.GetAsyncEnumerator();
  295. await HasNextAsync(e, (1, 4));
  296. await HasNextAsync(e, (2, 5));
  297. await HasNextAsync(e, (3, 6));
  298. await NoNextAsync(e);
  299. }
  300. [Fact]
  301. public async Task Zip_Tuple_Throws_Right()
  302. {
  303. var ex = new Exception("Bang!");
  304. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  305. var ys = Throw<int>(ex);
  306. var res = xs.Zip(ys);
  307. var e = res.GetAsyncEnumerator();
  308. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  309. }
  310. [Fact]
  311. public async Task Zip_Tuple_Throws_Left()
  312. {
  313. var ex = new Exception("Bang!");
  314. var xs = Throw<int>(ex);
  315. var ys = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  316. var res = xs.Zip(ys);
  317. var e = res.GetAsyncEnumerator();
  318. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  319. }
  320. [Fact]
  321. public async Task Zip_Tuple_SequenceIdentity()
  322. {
  323. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  324. var ys = new[] { 4, 5, 6 }.ToAsyncEnumerable();
  325. var res = xs.Zip(ys);
  326. await SequenceIdentity(res);
  327. }
  328. }
  329. }