// 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_Sync_Null() { await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(default, (x, y) => x + y).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, default(Func)).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(default, 0, (x, y) => x + y).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func)).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(default, 0, (x, y) => x + y, z => z).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, 0, default, z => z).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, 0, (x, y) => x + y, default).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(default, (x, y) => x + y, CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, default(Func), CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(default, 0, (x, y) => x + y, CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func), CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(default, 0, (x, y) => x + y, z => z, CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, 0, default, z => z, CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, 0, (x, y) => x + y, default, CancellationToken.None).AsTask()); } [Fact] public async Task AggregateAsync_Sync_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_Sync_Empty() { var xs = new int[0].ToAsyncEnumerable(); var ys = xs.AggregateAsync((x, y) => x * y); await AssertThrowsAsync(ys.AsTask()); } [Fact] public async Task AggregateAsync_Sync_Throw_Source() { var ex = new Exception("Bang!"); var xs = Throw(ex); var ys = xs.AggregateAsync((x, y) => x * y); await AssertThrowsAsync(ys, ex); } [Fact] public async Task AggregateAsync_Sync_Throw_Selector() { var ex = new Exception("Bang!"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.AggregateAsync(new Func((x, y) => { throw ex; })); await AssertThrowsAsync(ys, ex); } [Fact] public async Task AggregateAsync_Sync_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_Sync_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_Sync_Seed_Throw_Source() { var ex = new Exception("Bang!"); var xs = Throw(ex); var ys = xs.AggregateAsync(1, (x, y) => x * y); await AssertThrowsAsync(ys, ex); } [Fact] public async Task AggregateAsync_Sync_Seed_Throw_Selector() { var ex = new Exception("Bang!"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.AggregateAsync(1, new Func((x, y) => { throw ex; })); await AssertThrowsAsync(ys, ex); } [Fact] public async Task AggregateAsync_Sync_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_Sync_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_Sync_Seed_Result_Throw_Source() { var ex = new Exception("Bang!"); var xs = Throw(ex); var ys = xs.AggregateAsync(1, (x, y) => x * y, x => x + 1); await AssertThrowsAsync(ys, ex); } [Fact] public async Task AggregateAsync_Sync_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_Sync_Seed_Result_Throw_ResultSelector() { var ex = new Exception("Bang!"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.AggregateAsync(1, (x, y) => x * y, x => { throw ex; }); await AssertThrowsAsync(ys, ex); } [Fact] public async Task AggregateAsync_Async_Null() { await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(default, (x, y) => new ValueTask(x + y)).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, default(Func>)).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(default, 0, (x, y) => new ValueTask(x + y)).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func>)).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(default, 0, (x, y) => new ValueTask(x + y), z => new ValueTask(z)).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func>), z => new ValueTask(z)).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, 0, (x, y) => new ValueTask(x + y), default).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(default, (x, y) => new ValueTask(x + y), CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, default(Func>), CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(default, 0, (x, y) => new ValueTask(x + y), CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func>), CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(default, 0, (x, y) => new ValueTask(x + y), z => new ValueTask(z), CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func>), z => new ValueTask(z), CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, 0, (x, y) => new ValueTask(x + y), default, CancellationToken.None).AsTask()); } [Fact] public async Task AggregateAsync_Async_Simple() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.AggregateAsync((x, y) => new ValueTask(x * y)); Assert.Equal(24, await ys); } [Fact] public async Task AggregateAsync_Async_Empty() { var xs = new int[0].ToAsyncEnumerable(); var ys = xs.AggregateAsync((x, y) => new ValueTask(x * y)); await AssertThrowsAsync(ys.AsTask()); } [Fact] public async Task AggregateAsync_Async_Throw_Source() { var ex = new Exception("Bang!"); var xs = Throw(ex); var ys = xs.AggregateAsync((x, y) => new ValueTask(x * y)); await AssertThrowsAsync(ys, ex); } [Fact] public async Task AggregateAsync_Async_Throw_Selector() { var ex = new Exception("Bang!"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.AggregateAsync(new Func>((x, y) => { throw ex; })); await AssertThrowsAsync(ys, ex); } [Fact] public async Task AggregateAsync_Async_Seed_Simple() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.AggregateAsync(1, (x, y) => new ValueTask(x * y)); Assert.Equal(24, await ys); } [Fact] public async Task AggregateAsync_Async_Seed_Emtpy() { var xs = new int[0].ToAsyncEnumerable(); var ys = xs.AggregateAsync(1, (x, y) => new ValueTask(x * y)); Assert.Equal(1, await ys); } [Fact] public async Task AggregateAsync_Async_Seed_Throw_Source() { var ex = new Exception("Bang!"); var xs = Throw(ex); var ys = xs.AggregateAsync(1, (x, y) => new ValueTask(x * y)); await AssertThrowsAsync(ys, ex); } [Fact] public async Task AggregateAsync_Async_Seed_Throw_Selector() { var ex = new Exception("Bang!"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.AggregateAsync(1, new Func>((x, y) => { throw ex; })); await AssertThrowsAsync(ys, ex); } [Fact] public async Task AggregateAsync_Async_Seed_Result_Simple() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.AggregateAsync(1, (x, y) => new ValueTask(x * y), x => new ValueTask(x + 1)); Assert.Equal(25, await ys); } [Fact] public async Task AggregateAsync_Async_Seed_Result_Empty() { var xs = new int[0].ToAsyncEnumerable(); var ys = xs.AggregateAsync(1, (x, y) => new ValueTask(x * y), x => new ValueTask(x + 1)); Assert.Equal(2, await ys); } [Fact] public async Task AggregateAsync_Async_Seed_Result_Throw_Source() { var ex = new Exception("Bang!"); var xs = Throw(ex); var ys = xs.AggregateAsync(1, (x, y) => new ValueTask(x * y), x => new ValueTask(x + 1)); await AssertThrowsAsync(ys, ex); } [Fact] public async Task AggregateAsync_Async_Seed_Result_Throw_Selector() { var ex = new Exception("Bang!"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.AggregateAsync(1, new Func>((x, y) => { throw ex; }), x => new ValueTask(x + 1)); await AssertThrowsAsync(ys, ex); } [Fact] public async Task AggregateAsync_Async_Seed_Result_Throw_ResultSelector() { var ex = new Exception("Bang!"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.AggregateAsync(1, (x, y) => new ValueTask(x * y), x => { throw ex; }); await AssertThrowsAsync(ys, ex); } #if !NO_DEEP_CANCELLATION [Fact] public async Task AggregateAsync_AsyncCancel_Null() { await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(default, (x, y, ct) => new ValueTask(x + y)).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, default(Func>)).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(default, 0, (x, y, ct) => new ValueTask(x + y)).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func>)).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(default, 0, (x, y, ct) => new ValueTask(x + y), (z, ct) => new ValueTask(z)).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func>), (z, ct) => new ValueTask(z)).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, 0, (x, y, ct) => new ValueTask(x + y), default).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(default, (x, y, ct) => new ValueTask(x + y), CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, default(Func>), CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(default, 0, (x, y, ct) => new ValueTask(x + y), CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func>), CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(default, 0, (x, y, ct) => new ValueTask(x + y), (z, ct) => new ValueTask(z), CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, 0, default(Func>), (z, ct) => new ValueTask(z), CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerable.AggregateAsync(Return42, 0, (x, y, ct) => new ValueTask(x + y), default, CancellationToken.None).AsTask()); } [Fact] public async Task AggregateAsync_AsyncCancel_Simple() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.AggregateAsync((x, y, ct) => new ValueTask(x * y)); Assert.Equal(24, await ys); } [Fact] public async Task AggregateAsync_AsyncCancel_Empty() { var xs = new int[0].ToAsyncEnumerable(); var ys = xs.AggregateAsync((x, y, ct) => new ValueTask(x * y)); await AssertThrowsAsync(ys.AsTask()); } [Fact] public async Task AggregateAsync_AsyncCancel_Throw_Source() { var ex = new Exception("Bang!"); var xs = Throw(ex); var ys = xs.AggregateAsync((x, y, ct) => new ValueTask(x * y)); await AssertThrowsAsync(ys, ex); } [Fact] public async Task AggregateAsync_AsyncCancel_Throw_Selector() { var ex = new Exception("Bang!"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.AggregateAsync(new Func>((x, y, ct) => { throw ex; })); await AssertThrowsAsync(ys, ex); } [Fact] public async Task AggregateAsync_AsyncCancel_Seed_Simple() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.AggregateAsync(1, (x, y, ct) => new ValueTask(x * y)); Assert.Equal(24, await ys); } [Fact] public async Task AggregateAsync_AsyncCancel_Seed_Emtpy() { var xs = new int[0].ToAsyncEnumerable(); var ys = xs.AggregateAsync(1, (x, y, ct) => new ValueTask(x * y)); Assert.Equal(1, await ys); } [Fact] public async Task AggregateAsync_AsyncCancel_Seed_Throw_Source() { var ex = new Exception("Bang!"); var xs = Throw(ex); var ys = xs.AggregateAsync(1, (x, y, ct) => new ValueTask(x * y)); await AssertThrowsAsync(ys, ex); } [Fact] public async Task AggregateAsync_AsyncCancel_Seed_Throw_Selector() { var ex = new Exception("Bang!"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.AggregateAsync(1, new Func>((x, y, ct) => { throw ex; })); await AssertThrowsAsync(ys, ex); } [Fact] public async Task AggregateAsync_AsyncCancel_Seed_Result_Simple() { var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.AggregateAsync(1, (x, y, ct) => new ValueTask(x * y), (x, ct) => new ValueTask(x + 1)); Assert.Equal(25, await ys); } [Fact] public async Task AggregateAsync_AsyncCancel_Seed_Result_Empty() { var xs = new int[0].ToAsyncEnumerable(); var ys = xs.AggregateAsync(1, (x, y, ct) => new ValueTask(x * y), (x, ct) => new ValueTask(x + 1)); Assert.Equal(2, await ys); } [Fact] public async Task AggregateAsync_AsyncCancel_Seed_Result_Throw_Source() { var ex = new Exception("Bang!"); var xs = Throw(ex); var ys = xs.AggregateAsync(1, (x, y, ct) => new ValueTask(x * y), (x, ct) => new ValueTask(x + 1)); await AssertThrowsAsync(ys, ex); } [Fact] public async Task AggregateAsync_AsyncCancel_Seed_Result_Throw_Selector() { var ex = new Exception("Bang!"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.AggregateAsync(1, new Func>((x, y, ct) => { throw ex; }), (x, ct) => new ValueTask(x + 1)); await AssertThrowsAsync(ys, ex); } [Fact] public async Task AggregateAsync_AsyncCancel_Seed_Result_Throw_ResultSelector() { var ex = new Exception("Bang!"); var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable(); var ys = xs.AggregateAsync(1, (x, y, ct) => new ValueTask(x * y), (x, ct) => { throw ex; }); await AssertThrowsAsync(ys, ex); } #endif } }