1
0

GroupJoin.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. #if NET6_0_OR_GREATER
  5. #pragma warning disable CA2012 // Use ValueTasks correctly. These tests need to use Result to verify correct operation, so we can't avoid breaking this rule.
  6. #endif
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. using Xunit;
  12. namespace Tests
  13. {
  14. public class GroupJoin : AsyncEnumerableTests
  15. {
  16. [Fact]
  17. public void GroupJoin_Null()
  18. {
  19. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(default, Return42, x => x, x => x, (x, y) => x));
  20. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(Return42, default, x => x, x => x, (x, y) => x));
  21. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.GroupJoin(Return42, Return42, default, x => x, (x, y) => x));
  22. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.GroupJoin(Return42, Return42, x => x, default, (x, y) => x));
  23. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(Return42, Return42, x => x, x => x, default));
  24. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(default, Return42, x => x, x => x, (x, y) => x, EqualityComparer<int>.Default));
  25. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(Return42, default, x => x, x => x, (x, y) => x, EqualityComparer<int>.Default));
  26. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.GroupJoin(Return42, Return42, default, x => x, (x, y) => x, EqualityComparer<int>.Default));
  27. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.GroupJoin(Return42, Return42, x => x, default, (x, y) => x, EqualityComparer<int>.Default));
  28. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(Return42, Return42, x => x, x => x, default, EqualityComparer<int>.Default));
  29. }
  30. [Fact]
  31. public async Task GroupJoin1()
  32. {
  33. var xs = new[] { 0, 1, 2 }.ToAsyncEnumerable();
  34. var ys = new[] { 4, 7, 6, 2, 3, 4, 8, 9 }.ToAsyncEnumerable();
  35. var res = xs.GroupJoin(ys, x => x % 3, y => y % 3, (x, i) => x + " - " + i.AggregateAsync("", (s, j) => s + j).Result);
  36. var e = res.GetAsyncEnumerator();
  37. await HasNextAsync(e, "0 - 639");
  38. await HasNextAsync(e, "1 - 474");
  39. await HasNextAsync(e, "2 - 28");
  40. await NoNextAsync(e);
  41. }
  42. [Fact]
  43. public async Task GroupJoin2()
  44. {
  45. var xs = new[] { 0, 1, 2 }.ToAsyncEnumerable();
  46. var ys = new[] { 3, 6, 4 }.ToAsyncEnumerable();
  47. var res = xs.GroupJoin(ys, x => x % 3, y => y % 3, (x, i) => x + " - " + i.AggregateAsync("", (s, j) => s + j).Result);
  48. var e = res.GetAsyncEnumerator();
  49. await HasNextAsync(e, "0 - 36");
  50. await HasNextAsync(e, "1 - 4");
  51. await HasNextAsync(e, "2 - ");
  52. await NoNextAsync(e);
  53. }
  54. [Fact]
  55. public async Task GroupJoin3Async()
  56. {
  57. var ex = new Exception("Bang!");
  58. var xs = Throw<int>(ex);
  59. var ys = new[] { 3, 6, 4 }.ToAsyncEnumerable();
  60. var res = xs.GroupJoin(ys, x => x % 3, y => y % 3, (x, i) => x + " - " + i.AggregateAsync("", (s, j) => s + j).Result);
  61. var e = res.GetAsyncEnumerator();
  62. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  63. }
  64. [Fact]
  65. public async Task GroupJoin4Async()
  66. {
  67. var ex = new Exception("Bang!");
  68. var xs = new[] { 0, 1, 2 }.ToAsyncEnumerable();
  69. var ys = Throw<int>(ex);
  70. var res = xs.GroupJoin(ys, x => x % 3, y => y % 3, (x, i) => x + " - " + i.AggregateAsync("", (s, j) => s + j).Result);
  71. var e = res.GetAsyncEnumerator();
  72. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  73. }
  74. [Fact]
  75. public async Task GroupJoin5Async()
  76. {
  77. var ex = new Exception("Bang!");
  78. var xs = new[] { 0, 1, 2 }.ToAsyncEnumerable();
  79. var ys = new[] { 3, 6, 4 }.ToAsyncEnumerable();
  80. var res = xs.GroupJoin(ys, x => { throw ex; }, y => y % 3, (x, i) => x + " - " + i.AggregateAsync("", (s, j) => s + j).Result);
  81. var e = res.GetAsyncEnumerator();
  82. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  83. }
  84. [Fact]
  85. public async Task GroupJoin6Async()
  86. {
  87. var ex = new Exception("Bang!");
  88. var xs = new[] { 0, 1, 2 }.ToAsyncEnumerable();
  89. var ys = new[] { 3, 6, 4 }.ToAsyncEnumerable();
  90. var res = xs.GroupJoin(ys, x => x % 3, y => { throw ex; }, (x, i) => x + " - " + i.AggregateAsync("", (s, j) => s + j).Result);
  91. var e = res.GetAsyncEnumerator();
  92. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  93. }
  94. [Fact]
  95. public async Task GroupJoin7()
  96. {
  97. var ex = new Exception("Bang!");
  98. var xs = new[] { 0, 1, 2 }.ToAsyncEnumerable();
  99. var ys = new[] { 3, 6, 4 }.ToAsyncEnumerable();
  100. var res = xs.GroupJoin(ys, x => x % 3, y => y % 3, (x, i) =>
  101. {
  102. if (x == 1)
  103. throw ex;
  104. return x + " - " + i.AggregateAsync("", (s, j) => s + j).Result;
  105. });
  106. var e = res.GetAsyncEnumerator();
  107. await HasNextAsync(e, "0 - 36");
  108. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  109. }
  110. }
  111. }