123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- using System;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using Xunit;
- namespace Tests
- {
- public class Aggregate : AsyncEnumerableTests
- {
- [Fact]
- public async Task AggregateAsync_Null()
- {
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int>(default, (x, y) => x + y).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, default).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int>(default, 0, (x, y) => x + y).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, 0, default).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int, int>(default, 0, (x, y) => x + y, z => z).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, 0, default, z => z).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int, int>(Return42, 0, (x, y) => x + y, default).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int>(default, (x, y) => x + y, CancellationToken.None).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, default, CancellationToken.None).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int>(default, 0, (x, y) => x + y, CancellationToken.None).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, 0, default, CancellationToken.None).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int, int>(default, 0, (x, y) => x + y, z => z, CancellationToken.None).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync(Return42, 0, default, z => z, CancellationToken.None).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAsync<int, int, int>(Return42, 0, (x, y) => x + y, default, CancellationToken.None).AsTask());
- }
- [Fact]
- public async Task AggregateAsync_Simple()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAsync((x, y) => x * y);
- Assert.Equal(24, await ys);
- }
- [Fact]
- public async Task AggregateAsync_Empty()
- {
- var xs = new int[0].ToAsyncEnumerable();
- var ys = xs.AggregateAsync((x, y) => x * y);
- await AssertThrowsAsync<InvalidOperationException>(ys.AsTask());
- }
- [Fact]
- public async Task AggregateAsync_Throw_Source()
- {
- var ex = new Exception("Bang!");
- var xs = Throw<int>(ex);
- var ys = xs.AggregateAsync((x, y) => x * y);
- await AssertThrowsAsync(ys, ex);
- }
- [Fact]
- public async Task AggregateAsync_Throw_Selector()
- {
- var ex = new Exception("Bang!");
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAsync(new Func<int, int, int>((x, y) => { throw ex; }));
- await AssertThrowsAsync(ys, ex);
- }
- [Fact]
- public async Task AggregateAsync_Seed_Simple()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAsync(1, (x, y) => x * y);
- Assert.Equal(24, await ys);
- }
- [Fact]
- public async Task AggregateAsync_Seed_Emtpy()
- {
- var xs = new int[0].ToAsyncEnumerable();
- var ys = xs.AggregateAsync(1, (x, y) => x * y);
- Assert.Equal(1, await ys);
- }
- [Fact]
- public async Task AggregateAsync_Seed_Throw_Source()
- {
- var ex = new Exception("Bang!");
- var xs = Throw<int>(ex);
- var ys = xs.AggregateAsync(1, (x, y) => x * y);
- await AssertThrowsAsync(ys, ex);
- }
- [Fact]
- public async Task AggregateAsync_Seed_Throw_Selector()
- {
- var ex = new Exception("Bang!");
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAsync(1, new Func<int, int, int>((x, y) => { throw ex; }));
- await AssertThrowsAsync(ys, ex);
- }
- [Fact]
- public async Task AggregateAsync_Seed_Result_Simple()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAsync(1, (x, y) => x * y, x => x + 1);
- Assert.Equal(25, await ys);
- }
- [Fact]
- public async Task AggregateAsync_Seed_Result_Empty()
- {
- var xs = new int[0].ToAsyncEnumerable();
- var ys = xs.AggregateAsync(1, (x, y) => x * y, x => x + 1);
- Assert.Equal(2, await ys);
- }
- [Fact]
- public async Task AggregateAsync_Seed_Result_Throw_Source()
- {
- var ex = new Exception("Bang!");
- var xs = Throw<int>(ex);
- var ys = xs.AggregateAsync(1, (x, y) => x * y, x => x + 1);
- await AssertThrowsAsync(ys, ex);
- }
- [Fact]
- public async Task AggregateAsync_Seed_Result_Throw_Selector()
- {
- var ex = new Exception("Bang!");
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAsync(1, (x, y) => { throw ex; }, x => x + 1);
- await AssertThrowsAsync(ys, ex);
- }
- [Fact]
- public async Task AggregateAsync_Seed_Result_Throw_ResultSelector()
- {
- var ex = new Exception("Bang!");
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAsync<int, int, int>(1, (x, y) => x * y, x => { throw ex; });
- await AssertThrowsAsync(ys, ex);
- }
- [Fact]
- public async Task AggregateAwaitAsync_Null()
- {
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitAsync<int>(default, (x, y) => new ValueTask<int>(x + y)).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitAsync(Return42, default).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitAsync<int, int>(default, 0, (x, y) => new ValueTask<int>(x + y)).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitAsync(Return42, 0, default).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitAsync<int, int, int>(default, 0, (x, y) => new ValueTask<int>(x + y), z => new ValueTask<int>(z)).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitAsync(Return42, 0, default, z => new ValueTask<int>(z)).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitAsync<int, int, int>(Return42, 0, (x, y) => new ValueTask<int>(x + y), default).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitAsync<int>(default, (x, y) => new ValueTask<int>(x + y), CancellationToken.None).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitAsync(Return42, default, CancellationToken.None).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitAsync<int, int>(default, 0, (x, y) => new ValueTask<int>(x + y), CancellationToken.None).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitAsync(Return42, 0, default, CancellationToken.None).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitAsync<int, int, int>(default, 0, (x, y) => new ValueTask<int>(x + y), z => new ValueTask<int>(z), CancellationToken.None).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitAsync(Return42, 0, default, z => new ValueTask<int>(z), CancellationToken.None).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitAsync<int, int, int>(Return42, 0, (x, y) => new ValueTask<int>(x + y), default, CancellationToken.None).AsTask());
- }
- [Fact]
- public async Task AggregateAwaitAsync_Simple()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAwaitAsync((x, y) => new ValueTask<int>(x * y));
- Assert.Equal(24, await ys);
- }
- [Fact]
- public async Task AggregateAwaitAsync_Empty()
- {
- var xs = new int[0].ToAsyncEnumerable();
- var ys = xs.AggregateAwaitAsync((x, y) => new ValueTask<int>(x * y));
- await AssertThrowsAsync<InvalidOperationException>(ys.AsTask());
- }
- [Fact]
- public async Task AggregateAwaitAsync_Throw_Source()
- {
- var ex = new Exception("Bang!");
- var xs = Throw<int>(ex);
- var ys = xs.AggregateAwaitAsync((x, y) => new ValueTask<int>(x * y));
- await AssertThrowsAsync(ys, ex);
- }
- [Fact]
- public async Task AggregateAwaitAsync_Throw_Selector()
- {
- var ex = new Exception("Bang!");
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAwaitAsync(new Func<int, int, ValueTask<int>>((x, y) => { throw ex; }));
- await AssertThrowsAsync(ys, ex);
- }
- [Fact]
- public async Task AggregateAwaitAsync_Seed_Simple()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAwaitAsync(1, (x, y) => new ValueTask<int>(x * y));
- Assert.Equal(24, await ys);
- }
- [Fact]
- public async Task AggregateAwaitAsync_Seed_Emtpy()
- {
- var xs = new int[0].ToAsyncEnumerable();
- var ys = xs.AggregateAwaitAsync(1, (x, y) => new ValueTask<int>(x * y));
- Assert.Equal(1, await ys);
- }
- [Fact]
- public async Task AggregateAwaitAsync_Seed_Throw_Source()
- {
- var ex = new Exception("Bang!");
- var xs = Throw<int>(ex);
- var ys = xs.AggregateAwaitAsync(1, (x, y) => new ValueTask<int>(x * y));
- await AssertThrowsAsync(ys, ex);
- }
- [Fact]
- public async Task AggregateAwaitAsync_Seed_Throw_Selector()
- {
- var ex = new Exception("Bang!");
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAwaitAsync(1, new Func<int, int, ValueTask<int>>((x, y) => { throw ex; }));
- await AssertThrowsAsync(ys, ex);
- }
- [Fact]
- public async Task AggregateAwaitAsync_Seed_Result_Simple()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAwaitAsync(1, (x, y) => new ValueTask<int>(x * y), x => new ValueTask<int>(x + 1));
- Assert.Equal(25, await ys);
- }
- [Fact]
- public async Task AggregateAwaitAsync_Seed_Result_Empty()
- {
- var xs = new int[0].ToAsyncEnumerable();
- var ys = xs.AggregateAwaitAsync(1, (x, y) => new ValueTask<int>(x * y), x => new ValueTask<int>(x + 1));
- Assert.Equal(2, await ys);
- }
- [Fact]
- public async Task AggregateAwaitAsync_Seed_Result_Throw_Source()
- {
- var ex = new Exception("Bang!");
- var xs = Throw<int>(ex);
- var ys = xs.AggregateAwaitAsync(1, (x, y) => new ValueTask<int>(x * y), x => new ValueTask<int>(x + 1));
- await AssertThrowsAsync(ys, ex);
- }
- [Fact]
- public async Task AggregateAwaitAsync_Seed_Result_Throw_Selector()
- {
- var ex = new Exception("Bang!");
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAwaitAsync(1, new Func<int, int, ValueTask<int>>((x, y) => { throw ex; }), x => new ValueTask<int>(x + 1));
- await AssertThrowsAsync(ys, ex);
- }
- [Fact]
- public async Task AggregateAwaitAsync_Seed_Result_Throw_ResultSelector()
- {
- var ex = new Exception("Bang!");
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAwaitAsync<int, int, int>(1, (x, y) => new ValueTask<int>(x * y), x => { throw ex; });
- await AssertThrowsAsync(ys, ex);
- }
- #if !NO_DEEP_CANCELLATION
- [Fact]
- public async Task AggregateAwaitWithCancellationAsync_Null()
- {
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitWithCancellationAsync<int>(default, (x, y, ct) => new ValueTask<int>(x + y)).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitWithCancellationAsync(Return42, default).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitWithCancellationAsync<int, int>(default, 0, (x, y, ct) => new ValueTask<int>(x + y)).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitWithCancellationAsync(Return42, 0, default).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitWithCancellationAsync<int, int, int>(default, 0, (x, y, ct) => new ValueTask<int>(x + y), (z, ct) => new ValueTask<int>(z)).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitWithCancellationAsync(Return42, 0, default, (z, ct) => new ValueTask<int>(z)).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitWithCancellationAsync<int, int, int>(Return42, 0, (x, y, ct) => new ValueTask<int>(x + y), default).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitWithCancellationAsync<int>(default, (x, y, ct) => new ValueTask<int>(x + y), CancellationToken.None).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitWithCancellationAsync(Return42, default, CancellationToken.None).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitWithCancellationAsync<int, int>(default, 0, (x, y, ct) => new ValueTask<int>(x + y), CancellationToken.None).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitWithCancellationAsync(Return42, 0, default, CancellationToken.None).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitWithCancellationAsync<int, int, int>(default, 0, (x, y, ct) => new ValueTask<int>(x + y), (z, ct) => new ValueTask<int>(z), CancellationToken.None).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitWithCancellationAsync(Return42, 0, default, (z, ct) => new ValueTask<int>(z), CancellationToken.None).AsTask());
- await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.AggregateAwaitWithCancellationAsync<int, int, int>(Return42, 0, (x, y, ct) => new ValueTask<int>(x + y), default, CancellationToken.None).AsTask());
- }
- [Fact]
- public async Task AggregateAwaitWithCancellationAsync_Simple()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAwaitWithCancellationAsync((x, y, ct) => new ValueTask<int>(x * y));
- Assert.Equal(24, await ys);
- }
- [Fact]
- public async Task AggregateAwaitWithCancellationAsync_Empty()
- {
- var xs = new int[0].ToAsyncEnumerable();
- var ys = xs.AggregateAwaitWithCancellationAsync((x, y, ct) => new ValueTask<int>(x * y));
- await AssertThrowsAsync<InvalidOperationException>(ys.AsTask());
- }
- [Fact]
- public async Task AggregateAwaitWithCancellationAsync_Throw_Source()
- {
- var ex = new Exception("Bang!");
- var xs = Throw<int>(ex);
- var ys = xs.AggregateAwaitWithCancellationAsync((x, y, ct) => new ValueTask<int>(x * y));
- await AssertThrowsAsync(ys, ex);
- }
- [Fact]
- public async Task AggregateAwaitWithCancellationAsync_Throw_Selector()
- {
- var ex = new Exception("Bang!");
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAwaitWithCancellationAsync(new Func<int, int, CancellationToken, ValueTask<int>>((x, y, ct) => { throw ex; }));
- await AssertThrowsAsync(ys, ex);
- }
- [Fact]
- public async Task AggregateAwaitWithCancellationAsync_Seed_Simple()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAwaitWithCancellationAsync(1, (x, y, ct) => new ValueTask<int>(x * y));
- Assert.Equal(24, await ys);
- }
- [Fact]
- public async Task AggregateAwaitWithCancellationAsync_Seed_Emtpy()
- {
- var xs = new int[0].ToAsyncEnumerable();
- var ys = xs.AggregateAwaitWithCancellationAsync(1, (x, y, ct) => new ValueTask<int>(x * y));
- Assert.Equal(1, await ys);
- }
- [Fact]
- public async Task AggregateAwaitWithCancellationAsync_Seed_Throw_Source()
- {
- var ex = new Exception("Bang!");
- var xs = Throw<int>(ex);
- var ys = xs.AggregateAwaitWithCancellationAsync(1, (x, y, ct) => new ValueTask<int>(x * y));
- await AssertThrowsAsync(ys, ex);
- }
- [Fact]
- public async Task AggregateAwaitWithCancellationAsync_Seed_Throw_Selector()
- {
- var ex = new Exception("Bang!");
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAwaitWithCancellationAsync(1, new Func<int, int, CancellationToken, ValueTask<int>>((x, y, ct) => { throw ex; }));
- await AssertThrowsAsync(ys, ex);
- }
- [Fact]
- public async Task AggregateAwaitWithCancellationAsync_Seed_Result_Simple()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAwaitWithCancellationAsync(1, (x, y, ct) => new ValueTask<int>(x * y), (x, ct) => new ValueTask<int>(x + 1));
- Assert.Equal(25, await ys);
- }
- [Fact]
- public async Task AggregateAwaitWithCancellationAsync_Seed_Result_Empty()
- {
- var xs = new int[0].ToAsyncEnumerable();
- var ys = xs.AggregateAwaitWithCancellationAsync(1, (x, y, ct) => new ValueTask<int>(x * y), (x, ct) => new ValueTask<int>(x + 1));
- Assert.Equal(2, await ys);
- }
- [Fact]
- public async Task AggregateAwaitWithCancellationAsync_Seed_Result_Throw_Source()
- {
- var ex = new Exception("Bang!");
- var xs = Throw<int>(ex);
- var ys = xs.AggregateAwaitWithCancellationAsync(1, (x, y, ct) => new ValueTask<int>(x * y), (x, ct) => new ValueTask<int>(x + 1));
- await AssertThrowsAsync(ys, ex);
- }
- [Fact]
- public async Task AggregateAwaitWithCancellationAsync_Seed_Result_Throw_Selector()
- {
- var ex = new Exception("Bang!");
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAwaitWithCancellationAsync(1, new Func<int, int, CancellationToken, ValueTask<int>>((x, y, ct) => { throw ex; }), (x, ct) => new ValueTask<int>(x + 1));
- await AssertThrowsAsync(ys, ex);
- }
- [Fact]
- public async Task AggregateAwaitWithCancellationAsync_Seed_Result_Throw_ResultSelector()
- {
- var ex = new Exception("Bang!");
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.AggregateAwaitWithCancellationAsync<int, int, int>(1, (x, y, ct) => new ValueTask<int>(x * y), (x, ct) => { throw ex; });
- await AssertThrowsAsync(ys, ex);
- }
- #endif
- }
- }
|