Zip.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 Zip : AsyncEnumerableTests
  11. {
  12. [Fact]
  13. public void Zip_Null()
  14. {
  15. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Zip<int, int, int>(default, Return42, (x, y) => x + y));
  16. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Zip<int, int, int>(Return42, default, (x, y) => x + y));
  17. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Zip(Return42, Return42, default(Func<int, int, int>)));
  18. }
  19. [Fact]
  20. public async Task Zip1Async()
  21. {
  22. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  23. var ys = new[] { 4, 5, 6 }.ToAsyncEnumerable();
  24. var res = xs.Zip(ys, (x, y) => x * y);
  25. var e = res.GetAsyncEnumerator();
  26. await HasNextAsync(e, 1 * 4);
  27. await HasNextAsync(e, 2 * 5);
  28. await HasNextAsync(e, 3 * 6);
  29. await NoNextAsync(e);
  30. }
  31. [Fact]
  32. public async Task Zip2Async()
  33. {
  34. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  35. var ys = new[] { 4, 5, 6, 7 }.ToAsyncEnumerable();
  36. var res = xs.Zip(ys, (x, y) => x * y);
  37. var e = res.GetAsyncEnumerator();
  38. await HasNextAsync(e, 1 * 4);
  39. await HasNextAsync(e, 2 * 5);
  40. await HasNextAsync(e, 3 * 6);
  41. await NoNextAsync(e);
  42. }
  43. [Fact]
  44. public async Task Zip3Async()
  45. {
  46. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  47. var ys = new[] { 4, 5, 6 }.ToAsyncEnumerable();
  48. var res = xs.Zip(ys, (x, y) => x * y);
  49. var e = res.GetAsyncEnumerator();
  50. await HasNextAsync(e, 1 * 4);
  51. await HasNextAsync(e, 2 * 5);
  52. await HasNextAsync(e, 3 * 6);
  53. await NoNextAsync(e);
  54. }
  55. [Fact]
  56. public async Task Zip4Async()
  57. {
  58. var ex = new Exception("Bang!");
  59. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  60. var ys = Throw<int>(ex);
  61. var res = xs.Zip(ys, (x, y) => x * y);
  62. var e = res.GetAsyncEnumerator();
  63. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  64. }
  65. [Fact]
  66. public async Task Zip5Async()
  67. {
  68. var ex = new Exception("Bang!");
  69. var xs = Throw<int>(ex);
  70. var ys = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  71. var res = xs.Zip(ys, (x, y) => x * y);
  72. var e = res.GetAsyncEnumerator();
  73. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  74. }
  75. [Fact]
  76. public async Task Zip6Async()
  77. {
  78. var ex = new Exception("Bang!");
  79. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  80. var ys = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  81. var res = xs.Zip(ys, (x, y) => { if (x > 0) throw ex; return x * y; });
  82. var e = res.GetAsyncEnumerator();
  83. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  84. }
  85. [Fact]
  86. public async Task Zip7()
  87. {
  88. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  89. var ys = new[] { 4, 5, 6 }.ToAsyncEnumerable();
  90. var res = xs.Zip(ys, (x, y) => x * y);
  91. await SequenceIdentity(res);
  92. }
  93. }
  94. }