GroupJoin.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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;
  8. using System.Threading.Tasks;
  9. using Xunit;
  10. namespace Tests
  11. {
  12. public class GroupJoin : AsyncEnumerableTests
  13. {
  14. [Fact]
  15. public void GroupJoin_Null()
  16. {
  17. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(null, AsyncEnumerable.Return(42), x => x, x => x, (x, y) => x));
  18. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(AsyncEnumerable.Return(42), null, x => x, x => x, (x, y) => x));
  19. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(AsyncEnumerable.Return(42), AsyncEnumerable.Return(42), null, x => x, (x, y) => x));
  20. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(AsyncEnumerable.Return(42), AsyncEnumerable.Return(42), x => x, null, (x, y) => x));
  21. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(AsyncEnumerable.Return(42), AsyncEnumerable.Return(42), x => x, x => x, null));
  22. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(null, AsyncEnumerable.Return(42), x => x, x => x, (x, y) => x, EqualityComparer<int>.Default));
  23. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(AsyncEnumerable.Return(42), null, x => x, x => x, (x, y) => x, EqualityComparer<int>.Default));
  24. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(AsyncEnumerable.Return(42), AsyncEnumerable.Return(42), null, x => x, (x, y) => x, EqualityComparer<int>.Default));
  25. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(AsyncEnumerable.Return(42), AsyncEnumerable.Return(42), x => x, null, (x, y) => x, EqualityComparer<int>.Default));
  26. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(AsyncEnumerable.Return(42), AsyncEnumerable.Return(42), x => x, x => x, null, EqualityComparer<int>.Default));
  27. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(AsyncEnumerable.Return(42), AsyncEnumerable.Return(42), x => x, x => x, (x, y) => x, default(IEqualityComparer<int>)));
  28. }
  29. [Fact]
  30. public void GroupJoin1()
  31. {
  32. var xs = new[] { 0, 1, 2 }.ToAsyncEnumerable();
  33. var ys = new[] { 4, 7, 6, 2, 3, 4, 8, 9 }.ToAsyncEnumerable();
  34. var res = xs.GroupJoin(ys, x => x % 3, y => y % 3, (x, i) => x + " - " + i.Aggregate("", (s, j) => s + j).Result);
  35. var e = res.GetAsyncEnumerator();
  36. HasNext(e, "0 - 639");
  37. HasNext(e, "1 - 474");
  38. HasNext(e, "2 - 28");
  39. NoNext(e);
  40. }
  41. [Fact]
  42. public void GroupJoin2()
  43. {
  44. var xs = new[] { 0, 1, 2 }.ToAsyncEnumerable();
  45. var ys = new[] { 3, 6, 4 }.ToAsyncEnumerable();
  46. var res = xs.GroupJoin(ys, x => x % 3, y => y % 3, (x, i) => x + " - " + i.Aggregate("", (s, j) => s + j).Result);
  47. var e = res.GetAsyncEnumerator();
  48. HasNext(e, "0 - 36");
  49. HasNext(e, "1 - 4");
  50. HasNext(e, "2 - ");
  51. NoNext(e);
  52. }
  53. [Fact]
  54. public void GroupJoin3()
  55. {
  56. var ex = new Exception("Bang!");
  57. var xs = AsyncEnumerable.Throw<int>(ex);
  58. var ys = new[] { 3, 6, 4 }.ToAsyncEnumerable();
  59. var res = xs.GroupJoin(ys, x => x % 3, y => y % 3, (x, i) => x + " - " + i.Aggregate("", (s, j) => s + j).Result);
  60. var e = res.GetAsyncEnumerator();
  61. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  62. }
  63. [Fact]
  64. public void GroupJoin4()
  65. {
  66. var ex = new Exception("Bang!");
  67. var xs = new[] { 0, 1, 2 }.ToAsyncEnumerable();
  68. var ys = AsyncEnumerable.Throw<int>(ex);
  69. var res = xs.GroupJoin(ys, x => x % 3, y => y % 3, (x, i) => x + " - " + i.Aggregate("", (s, j) => s + j).Result);
  70. var e = res.GetAsyncEnumerator();
  71. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  72. }
  73. [Fact]
  74. public void GroupJoin5()
  75. {
  76. var ex = new Exception("Bang!");
  77. var xs = new[] { 0, 1, 2 }.ToAsyncEnumerable();
  78. var ys = new[] { 3, 6, 4 }.ToAsyncEnumerable();
  79. var res = xs.GroupJoin(ys, x => { throw ex; }, y => y % 3, (x, i) => x + " - " + i.Aggregate("", (s, j) => s + j).Result);
  80. var e = res.GetAsyncEnumerator();
  81. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  82. }
  83. [Fact]
  84. public void GroupJoin6()
  85. {
  86. var ex = new Exception("Bang!");
  87. var xs = new[] { 0, 1, 2 }.ToAsyncEnumerable();
  88. var ys = new[] { 3, 6, 4 }.ToAsyncEnumerable();
  89. var res = xs.GroupJoin(ys, x => x % 3, y => { throw ex; }, (x, i) => x + " - " + i.Aggregate("", (s, j) => s + j).Result);
  90. var e = res.GetAsyncEnumerator();
  91. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  92. }
  93. [Fact]
  94. public void GroupJoin7()
  95. {
  96. var ex = new Exception("Bang!");
  97. var xs = new[] { 0, 1, 2 }.ToAsyncEnumerable();
  98. var ys = new[] { 3, 6, 4 }.ToAsyncEnumerable();
  99. var res = xs.GroupJoin(ys, x => x % 3, y => y % 3, (x, i) =>
  100. {
  101. if (x == 1)
  102. throw ex;
  103. return x + " - " + i.Aggregate("", (s, j) => s + j).Result;
  104. });
  105. var e = res.GetAsyncEnumerator();
  106. HasNext(e, "0 - 36");
  107. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  108. }
  109. }
  110. }