GroupJoin.cs 5.8 KB

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