Aggregate.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Xunit;
  9. namespace Tests
  10. {
  11. public class Aggregate : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public async Task Aggregate_Null()
  15. {
  16. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Aggregate<int>(null, (x, y) => x + y));
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Aggregate<int>(AsyncEnumerable.Return(42), default(Func<int, int, int>)));
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Aggregate<int, int>(null, 0, (x, y) => x + y));
  19. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Aggregate<int, int>(AsyncEnumerable.Return(42), 0, default(Func<int, int, int>)));
  20. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Aggregate<int, int, int>(null, 0, (x, y) => x + y, z => z));
  21. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Aggregate<int, int, int>(AsyncEnumerable.Return(42), 0, default(Func<int, int, int>), z => z));
  22. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Aggregate<int, int, int>(AsyncEnumerable.Return(42), 0, (x, y) => x + y, default(Func<int, int>)));
  23. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Aggregate<int>(null, (x, y) => x + y, CancellationToken.None));
  24. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Aggregate<int>(AsyncEnumerable.Return(42), default(Func<int, int, int>), CancellationToken.None));
  25. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Aggregate<int, int>(null, 0, (x, y) => x + y, CancellationToken.None));
  26. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Aggregate<int, int>(AsyncEnumerable.Return(42), 0, default(Func<int, int, int>), CancellationToken.None));
  27. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Aggregate<int, int, int>(null, 0, (x, y) => x + y, z => z, CancellationToken.None));
  28. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Aggregate<int, int, int>(AsyncEnumerable.Return(42), 0, default(Func<int, int, int>), z => z, CancellationToken.None));
  29. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.Aggregate<int, int, int>(AsyncEnumerable.Return(42), 0, (x, y) => x + y, default(Func<int, int>), CancellationToken.None));
  30. }
  31. [Fact]
  32. public void Aggregate1()
  33. {
  34. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  35. var ys = xs.Aggregate((x, y) => x * y);
  36. Assert.Equal(24, ys.Result);
  37. }
  38. [Fact]
  39. public void Aggregate2()
  40. {
  41. var xs = new int[0].ToAsyncEnumerable();
  42. var ys = xs.Aggregate((x, y) => x * y);
  43. AssertThrows<Exception>(() => ys.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException);
  44. }
  45. [Fact]
  46. public void Aggregate3()
  47. {
  48. var ex = new Exception("Bang!");
  49. var xs = AsyncEnumerable.Throw<int>(ex);
  50. var ys = xs.Aggregate((x, y) => x * y);
  51. AssertThrows<Exception>(() => ys.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  52. }
  53. [Fact]
  54. public void Aggregate4()
  55. {
  56. var ex = new Exception("Bang!");
  57. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  58. var ys = xs.Aggregate(new Func<int, int, int>((x, y) => { throw ex; }));
  59. AssertThrows<Exception>(() => ys.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  60. }
  61. [Fact]
  62. public void Aggregate5()
  63. {
  64. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  65. var ys = xs.Aggregate(1, (x, y) => x * y);
  66. Assert.Equal(24, ys.Result);
  67. }
  68. [Fact]
  69. public void Aggregate6()
  70. {
  71. var xs = new int[0].ToAsyncEnumerable();
  72. var ys = xs.Aggregate(1, (x, y) => x * y);
  73. Assert.Equal(1, ys.Result);
  74. }
  75. [Fact]
  76. public void Aggregate7()
  77. {
  78. var ex = new Exception("Bang!");
  79. var xs = AsyncEnumerable.Throw<int>(ex);
  80. var ys = xs.Aggregate(1, (x, y) => x * y);
  81. AssertThrows<Exception>(() => ys.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  82. }
  83. [Fact]
  84. public void Aggregate8()
  85. {
  86. var ex = new Exception("Bang!");
  87. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  88. var ys = xs.Aggregate(1, new Func<int, int, int>((x, y) => { throw ex; }));
  89. AssertThrows<Exception>(() => ys.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  90. }
  91. [Fact]
  92. public void Aggregate9()
  93. {
  94. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  95. var ys = xs.Aggregate(1, (x, y) => x * y, x => x + 1);
  96. Assert.Equal(25, ys.Result);
  97. }
  98. [Fact]
  99. public void Aggregate10()
  100. {
  101. var xs = new int[0].ToAsyncEnumerable();
  102. var ys = xs.Aggregate(1, (x, y) => x * y, x => x + 1);
  103. Assert.Equal(2, ys.Result);
  104. }
  105. [Fact]
  106. public void Aggregate11()
  107. {
  108. var ex = new Exception("Bang!");
  109. var xs = AsyncEnumerable.Throw<int>(ex);
  110. var ys = xs.Aggregate(1, (x, y) => x * y, x => x + 1);
  111. AssertThrows<Exception>(() => ys.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  112. }
  113. [Fact]
  114. public void Aggregate12()
  115. {
  116. var ex = new Exception("Bang!");
  117. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  118. var ys = xs.Aggregate(1, (x, y) => { throw ex; }, x => x + 1);
  119. AssertThrows<Exception>(() => ys.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  120. }
  121. [Fact]
  122. public void Aggregate13()
  123. {
  124. var ex = new Exception("Bang!");
  125. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  126. var ys = xs.Aggregate<int, int, int>(1, (x, y) => x * y, x => { throw ex; });
  127. AssertThrows<Exception>(() => ys.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  128. }
  129. }
  130. }