GroupJoin.cs 5.6 KB

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