Aggregate.cs 6.0 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.AggregateAsync<int>(default, (x, y) => x + y));
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, default(Func<int, int, int>)));
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int>(default, 0, (x, y) => x + y));
  19. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func<int, int, int>)));
  20. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int, int>(default, 0, (x, y) => x + y, z => z));
  21. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, 0, default, z => z));
  22. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int, int>(Return42, 0, (x, y) => x + y, default));
  23. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int>(default, (x, y) => x + y, CancellationToken.None));
  24. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, default(Func<int, int, int>), CancellationToken.None));
  25. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int>(default, 0, (x, y) => x + y, CancellationToken.None));
  26. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func<int, int, int>), CancellationToken.None));
  27. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int, int>(default, 0, (x, y) => x + y, z => z, CancellationToken.None));
  28. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, 0, default, z => z, CancellationToken.None));
  29. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int, int>(Return42, 0, (x, y) => x + y, default, CancellationToken.None));
  30. }
  31. [Fact]
  32. public async Task Aggregate1Async()
  33. {
  34. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  35. var ys = xs.AggregateAsync((x, y) => x * y);
  36. Assert.Equal(24, await ys);
  37. }
  38. [Fact]
  39. public async Task Aggregate2Async()
  40. {
  41. var xs = new int[0].ToAsyncEnumerable();
  42. var ys = xs.AggregateAsync((x, y) => x * y);
  43. await AssertThrowsAsync<InvalidOperationException>(ys);
  44. }
  45. [Fact]
  46. public async Task Aggregate3Async()
  47. {
  48. var ex = new Exception("Bang!");
  49. var xs = Throw<int>(ex);
  50. var ys = xs.AggregateAsync((x, y) => x * y);
  51. await AssertThrowsAsync(ys, ex);
  52. }
  53. [Fact]
  54. public async Task Aggregate4Async()
  55. {
  56. var ex = new Exception("Bang!");
  57. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  58. var ys = xs.AggregateAsync(new Func<int, int, int>((x, y) => { throw ex; }));
  59. await AssertThrowsAsync(ys, ex);
  60. }
  61. [Fact]
  62. public async Task Aggregate5Async()
  63. {
  64. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  65. var ys = xs.AggregateAsync(1, (x, y) => x * y);
  66. Assert.Equal(24, await ys);
  67. }
  68. [Fact]
  69. public async Task Aggregate6Async()
  70. {
  71. var xs = new int[0].ToAsyncEnumerable();
  72. var ys = xs.AggregateAsync(1, (x, y) => x * y);
  73. Assert.Equal(1, await ys);
  74. }
  75. [Fact]
  76. public async Task Aggregate7Async()
  77. {
  78. var ex = new Exception("Bang!");
  79. var xs = Throw<int>(ex);
  80. var ys = xs.AggregateAsync(1, (x, y) => x * y);
  81. await AssertThrowsAsync(ys, ex);
  82. }
  83. [Fact]
  84. public async Task Aggregate8Async()
  85. {
  86. var ex = new Exception("Bang!");
  87. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  88. var ys = xs.AggregateAsync(1, new Func<int, int, int>((x, y) => { throw ex; }));
  89. await AssertThrowsAsync(ys, ex);
  90. }
  91. [Fact]
  92. public async Task Aggregate9Async()
  93. {
  94. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  95. var ys = xs.AggregateAsync(1, (x, y) => x * y, x => x + 1);
  96. Assert.Equal(25, await ys);
  97. }
  98. [Fact]
  99. public async Task Aggregate10Async()
  100. {
  101. var xs = new int[0].ToAsyncEnumerable();
  102. var ys = xs.AggregateAsync(1, (x, y) => x * y, x => x + 1);
  103. Assert.Equal(2, await ys);
  104. }
  105. [Fact]
  106. public async Task Aggregate11Async()
  107. {
  108. var ex = new Exception("Bang!");
  109. var xs = Throw<int>(ex);
  110. var ys = xs.AggregateAsync(1, (x, y) => x * y, x => x + 1);
  111. await AssertThrowsAsync(ys, ex);
  112. }
  113. [Fact]
  114. public async Task Aggregate12Async()
  115. {
  116. var ex = new Exception("Bang!");
  117. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  118. var ys = xs.AggregateAsync(1, (x, y) => { throw ex; }, x => x + 1);
  119. await AssertThrowsAsync(ys, ex);
  120. }
  121. [Fact]
  122. public async Task Aggregate13Async()
  123. {
  124. var ex = new Exception("Bang!");
  125. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  126. var ys = xs.AggregateAsync<int, int, int>(1, (x, y) => x * y, x => { throw ex; });
  127. await AssertThrowsAsync(ys, ex);
  128. }
  129. }
  130. }