// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT License. // See the LICENSE file in the project root for more information. using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Xunit; namespace Tests { public class MaxByWithTies : AsyncEnumerableExTests { [Fact] public async Task MaxByWithTies_Null() { await Assert.ThrowsAsync(() => AsyncEnumerableEx.MaxByWithTiesAsync(default(IAsyncEnumerable), x => x).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MaxByWithTiesAsync(Return42, default(Func)).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MaxByWithTiesAsync(default(IAsyncEnumerable), x => x, Comparer.Default).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MaxByWithTiesAsync(Return42, default(Func), Comparer.Default).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MaxByWithTiesAsync(default(IAsyncEnumerable), x => x, CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MaxByWithTiesAsync(Return42, default(Func), CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MaxByWithTiesAsync(default(IAsyncEnumerable), x => x, Comparer.Default, CancellationToken.None).AsTask()); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MaxByWithTiesAsync(Return42, default(Func), Comparer.Default, CancellationToken.None).AsTask()); } [Fact] public async Task MaxByWithTies1Async() { var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().MaxByWithTiesAsync(x => x / 2); var res = await xs; Assert.True(res.SequenceEqual([7, 6])); } [Fact] public async Task MaxByWithTies2() { var xs = Array.Empty().ToAsyncEnumerable().MaxByWithTiesAsync(x => x / 2); await AssertThrowsAsync(xs.AsTask()); } [Fact] public async Task MaxByWithTies3() { var ex = new Exception("Bang!"); var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().MaxByWithTiesAsync(x => { if (x == 3) throw ex; return x; }); await AssertThrowsAsync(xs, ex); } [Fact] public async Task MaxByWithTies4() { var ex = new Exception("Bang!"); var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().MaxByWithTiesAsync(x => { if (x == 4) throw ex; return x; }); await AssertThrowsAsync(xs, ex); } [Fact] public async Task MaxByWithTies5() { var ex = new Exception("Bang!"); var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().Concat(Throw(ex)).MaxByWithTiesAsync(x => x, Comparer.Default); await AssertThrowsAsync(xs, ex); } } }