Zip.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.Collections.Generic;
  6. using System.Linq;
  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. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Zip<int, int, int>(default, Return42, (x, y) => x + y));
  17. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Zip<int, int, int>(Return42, default, (x, y) => x + y));
  18. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Zip<int, int, int>(Return42, Return42, default(Func<int, int, int>)));
  19. }
  20. [Fact]
  21. public void Zip1()
  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. HasNext(e, 1 * 4);
  28. HasNext(e, 2 * 5);
  29. HasNext(e, 3 * 6);
  30. NoNext(e);
  31. }
  32. [Fact]
  33. public void Zip2()
  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. HasNext(e, 1 * 4);
  40. HasNext(e, 2 * 5);
  41. HasNext(e, 3 * 6);
  42. NoNext(e);
  43. }
  44. [Fact]
  45. public void Zip3()
  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. HasNext(e, 1 * 4);
  52. HasNext(e, 2 * 5);
  53. HasNext(e, 3 * 6);
  54. NoNext(e);
  55. }
  56. [Fact]
  57. public void Zip4()
  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. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), SingleInnerExceptionMatches(ex));
  65. }
  66. [Fact]
  67. public void Zip5()
  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. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), SingleInnerExceptionMatches(ex));
  75. }
  76. [Fact]
  77. public void Zip6()
  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. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), SingleInnerExceptionMatches(ex));
  85. }
  86. [Fact]
  87. public async Task Zip7()
  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. }
  95. }