GroupJoin.cs 5.6 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 System.Threading.Tasks;
  8. using Xunit;
  9. namespace Tests
  10. {
  11. public class GroupJoin : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public void GroupJoin_Null()
  15. {
  16. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(default, Return42, x => x, x => x, (x, y) => x));
  17. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(Return42, default, x => x, x => x, (x, y) => x));
  18. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.GroupJoin(Return42, Return42, default, x => x, (x, y) => x));
  19. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.GroupJoin(Return42, Return42, x => x, default, (x, y) => x));
  20. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(Return42, Return42, x => x, x => x, default));
  21. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(default, Return42, x => x, x => x, (x, y) => x, EqualityComparer<int>.Default));
  22. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(Return42, default, x => x, x => x, (x, y) => x, EqualityComparer<int>.Default));
  23. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.GroupJoin(Return42, Return42, default, x => x, (x, y) => x, EqualityComparer<int>.Default));
  24. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.GroupJoin(Return42, Return42, x => x, default, (x, y) => x, EqualityComparer<int>.Default));
  25. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.GroupJoin<int, int, int, int>(Return42, Return42, x => x, x => x, default, EqualityComparer<int>.Default));
  26. }
  27. [Fact]
  28. public async Task 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.AggregateAsync("", (s, j) => s + j).Result);
  33. var e = res.GetAsyncEnumerator();
  34. await HasNextAsync(e, "0 - 639");
  35. await HasNextAsync(e, "1 - 474");
  36. await HasNextAsync(e, "2 - 28");
  37. await NoNextAsync(e);
  38. }
  39. [Fact]
  40. public async Task 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.AggregateAsync("", (s, j) => s + j).Result);
  45. var e = res.GetAsyncEnumerator();
  46. await HasNextAsync(e, "0 - 36");
  47. await HasNextAsync(e, "1 - 4");
  48. await HasNextAsync(e, "2 - ");
  49. await NoNextAsync(e);
  50. }
  51. [Fact]
  52. public async Task GroupJoin3Async()
  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.AggregateAsync("", (s, j) => s + j).Result);
  58. var e = res.GetAsyncEnumerator();
  59. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  60. }
  61. [Fact]
  62. public async Task GroupJoin4Async()
  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.AggregateAsync("", (s, j) => s + j).Result);
  68. var e = res.GetAsyncEnumerator();
  69. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  70. }
  71. [Fact]
  72. public async Task GroupJoin5Async()
  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.AggregateAsync("", (s, j) => s + j).Result);
  78. var e = res.GetAsyncEnumerator();
  79. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  80. }
  81. [Fact]
  82. public async Task GroupJoin6Async()
  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.AggregateAsync("", (s, j) => s + j).Result);
  88. var e = res.GetAsyncEnumerator();
  89. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  90. }
  91. [Fact]
  92. public async Task 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.AggregateAsync("", (s, j) => s + j).Result;
  102. });
  103. var e = res.GetAsyncEnumerator();
  104. await HasNextAsync(e, "0 - 36");
  105. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  106. }
  107. }
  108. }