Aggregate.cs 6.6 KB

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