// 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.Collections.Generic; using System.Linq; using Xunit; using System.Collections; using System.Threading; using System.Threading.Tasks; // ReSharper disable InvokeAsExtensionMethod // ReSharper disable InconsistentNaming // ReSharper disable RedundantTypeArgumentsOfMethod namespace Tests { public partial class AsyncTests { private const int WaitTimeoutMs = 5000; [Fact] public async Task IsEmpty_Null() { await Assert.ThrowsAsync(() => AsyncEnumerableEx.IsEmpty(null)); await Assert.ThrowsAsync(() => AsyncEnumerableEx.IsEmpty(null, CancellationToken.None)); } [Fact] public async Task ToDictionary_Null() { await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(default(IAsyncEnumerable), x => 0)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), default(Func))); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(default(IAsyncEnumerable), x => 0, EqualityComparer.Default)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), default(Func), EqualityComparer.Default)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, default(IEqualityComparer))); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(default(IAsyncEnumerable), x => 0, x => 0)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), default(Func), x => 0)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, default(Func))); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(default(IAsyncEnumerable), x => 0, x => 0, EqualityComparer.Default)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), default(Func), x => 0, EqualityComparer.Default)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, default(Func), EqualityComparer.Default)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, x => 0, default(IEqualityComparer))); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(default(IAsyncEnumerable), x => 0, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), default(Func), CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(default(IAsyncEnumerable), x => 0, EqualityComparer.Default, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), default(Func), EqualityComparer.Default, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, default(IEqualityComparer), CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(default(IAsyncEnumerable), x => 0, x => 0, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), default(Func), x => 0, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, default(Func), CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(default(IAsyncEnumerable), x => 0, x => 0, EqualityComparer.Default, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), default(Func), x => 0, EqualityComparer.Default, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, default(Func), EqualityComparer.Default, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToDictionary(AsyncEnumerable.Return(42), x => 0, x => 0, default(IEqualityComparer), CancellationToken.None)); } [Fact] public void ToDictionary1() { var xs = new[] { 1, 4 }.ToAsyncEnumerable(); var res = xs.ToDictionary(x => x % 2).Result; Assert.True(res[0] == 4); Assert.True(res[1] == 1); } [Fact] public void ToDictionary2() { var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable(); AssertThrows(() => xs.ToDictionary(x => x % 2).Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is ArgumentException); } [Fact] public void ToDictionary3() { var xs = new[] { 1, 4 }.ToAsyncEnumerable(); var res = xs.ToDictionary(x => x % 2, x => x + 1).Result; Assert.True(res[0] == 5); Assert.True(res[1] == 2); } [Fact] public void ToDictionary4() { var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable(); AssertThrows(() => xs.ToDictionary(x => x % 2, x => x + 1).Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is ArgumentException); } [Fact] public void ToDictionary5() { var xs = new[] { 1, 4 }.ToAsyncEnumerable(); var res = xs.ToDictionary(x => x % 2, new Eq()).Result; Assert.True(res[0] == 4); Assert.True(res[1] == 1); } [Fact] public void ToDictionary6() { var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable(); AssertThrows(() => xs.ToDictionary(x => x % 2, new Eq()).Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is ArgumentException); } [Fact] public void ToDictionary7() { var xs = new[] { 1, 4 }.ToAsyncEnumerable(); var res = xs.ToDictionary(x => x % 2, x => x, new Eq()).Result; Assert.True(res[0] == 4); Assert.True(res[1] == 1); } [Fact] public async Task ToLookup_Null() { await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(default(IAsyncEnumerable), x => 0)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), default(Func))); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(default(IAsyncEnumerable), x => 0, EqualityComparer.Default)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), default(Func), EqualityComparer.Default)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, default(IEqualityComparer))); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(default(IAsyncEnumerable), x => 0, x => 0)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), default(Func), x => 0)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, default(Func))); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(default(IAsyncEnumerable), x => 0, x => 0, EqualityComparer.Default)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), default(Func), x => 0, EqualityComparer.Default)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, default(Func), EqualityComparer.Default)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, x => 0, default(IEqualityComparer))); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(default(IAsyncEnumerable), x => 0, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), default(Func), CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(default(IAsyncEnumerable), x => 0, EqualityComparer.Default, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), default(Func), EqualityComparer.Default, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, default(IEqualityComparer), CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(default(IAsyncEnumerable), x => 0, x => 0, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), default(Func), x => 0, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, default(Func), CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(default(IAsyncEnumerable), x => 0, x => 0, EqualityComparer.Default, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), default(Func), x => 0, EqualityComparer.Default, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, default(Func), EqualityComparer.Default, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerable.ToLookup(AsyncEnumerable.Return(42), x => 0, x => 0, default(IEqualityComparer), CancellationToken.None)); } [Fact] public void ToLookup1() { var xs = new[] { 1, 4 }.ToAsyncEnumerable(); var res = xs.ToLookup(x => x % 2).Result; Assert.True(res.Contains(0)); Assert.True(res.Contains(1)); Assert.True(res[0].Contains(4)); Assert.True(res[1].Contains(1)); Assert.True(res.Count == 2); } [Fact] public void ToLookup2() { var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable(); var res = xs.ToLookup(x => x % 2).Result; Assert.True(res.Contains(0)); Assert.True(res.Contains(1)); Assert.True(res[0].Contains(4)); Assert.True(res[0].Contains(2)); Assert.True(res[1].Contains(1)); Assert.True(res.Count == 2); } [Fact] public void ToLookup3() { var xs = new[] { 1, 4 }.ToAsyncEnumerable(); var res = xs.ToLookup(x => x % 2, x => x + 1).Result; Assert.True(res.Contains(0)); Assert.True(res.Contains(1)); Assert.True(res[0].Contains(5)); Assert.True(res[1].Contains(2)); Assert.True(res.Count == 2); } [Fact] public void ToLookup4() { var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable(); var res = xs.ToLookup(x => x % 2, x => x + 1).Result; Assert.True(res.Contains(0)); Assert.True(res.Contains(1)); Assert.True(res[0].Contains(5)); Assert.True(res[0].Contains(3)); Assert.True(res[1].Contains(2)); Assert.True(res.Count == 2); } [Fact] public void ToLookup5() { var xs = new[] { 1, 4 }.ToAsyncEnumerable(); var res = xs.ToLookup(x => x % 2, new Eq()).Result; Assert.True(res.Contains(0)); Assert.True(res.Contains(1)); Assert.True(res[0].Contains(4)); Assert.True(res[1].Contains(1)); Assert.True(res.Count == 2); } [Fact] public void ToLookup6() { var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable(); var res = xs.ToLookup(x => x % 2, new Eq()).Result; Assert.True(res.Contains(0)); Assert.True(res.Contains(1)); Assert.True(res[0].Contains(4)); Assert.True(res[0].Contains(2)); Assert.True(res[1].Contains(1)); Assert.True(res.Count == 2); } [Fact] public void ToLookup7() { var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable(); var res = xs.ToLookup(x => x % 2).Result; foreach (var g in res) Assert.True(g.Key == 0 || g.Key == 1); } [Fact] public void ToLookup8() { var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable(); var res = xs.ToLookup(x => x % 2).Result; #pragma warning disable IDE0007 // Use implicit type foreach (IGrouping g in (IEnumerable)res) Assert.True(g.Key == 0 || g.Key == 1); #pragma warning restore IDE0007 // Use implicit type } [Fact] public void ToLookup9() { var xs = new[] { 1, 4, 2 }.ToAsyncEnumerable(); var res = xs.ToLookup(x => x % 2, x => x, new Eq()).Result; Assert.True(res.Contains(0)); Assert.True(res.Contains(1)); Assert.True(res[0].Contains(4)); Assert.True(res[0].Contains(2)); Assert.True(res[1].Contains(1)); Assert.True(res.Count == 2); } [Fact] public async Task Min_Null() { await Assert.ThrowsAsync(() => AsyncEnumerableEx.Min(default(IAsyncEnumerable), Comparer.Default)); await Assert.ThrowsAsync(() => AsyncEnumerableEx.Min(AsyncEnumerable.Empty(), default(IComparer))); await Assert.ThrowsAsync(() => AsyncEnumerableEx.Min(default(IAsyncEnumerable), Comparer.Default, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerableEx.Min(AsyncEnumerable.Empty(), default(IComparer), CancellationToken.None)); } [Fact] public async Task Max_Null() { await Assert.ThrowsAsync(() => AsyncEnumerableEx.Max(default(IAsyncEnumerable), Comparer.Default)); await Assert.ThrowsAsync(() => AsyncEnumerableEx.Max(AsyncEnumerable.Empty(), default(IComparer))); await Assert.ThrowsAsync(() => AsyncEnumerableEx.Max(default(IAsyncEnumerable), Comparer.Default, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerableEx.Max(AsyncEnumerable.Empty(), default(IComparer), CancellationToken.None)); } [Fact] public async Task MinBy_Null() { await Assert.ThrowsAsync(() => AsyncEnumerableEx.MinBy(default(IAsyncEnumerable), x => x)); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MinBy(AsyncEnumerable.Return(42), default(Func))); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MinBy(default(IAsyncEnumerable), x => x, Comparer.Default)); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MinBy(AsyncEnumerable.Return(42), default(Func), Comparer.Default)); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MinBy(AsyncEnumerable.Return(42), x => x, default(IComparer))); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MinBy(default(IAsyncEnumerable), x => x, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MinBy(AsyncEnumerable.Return(42), default(Func), CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MinBy(default(IAsyncEnumerable), x => x, Comparer.Default, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MinBy(AsyncEnumerable.Return(42), default(Func), Comparer.Default, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MinBy(AsyncEnumerable.Return(42), x => x, default(IComparer), CancellationToken.None)); } [Fact] public void MinBy1() { var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().MinBy(x => x / 2); var res = xs.Result; Assert.True(res.SequenceEqual(new[] { 3, 2 })); } [Fact] public void MinBy2() { var xs = new int[0].ToAsyncEnumerable().MinBy(x => x / 2); AssertThrows(() => xs.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] public void MinBy3() { var ex = new Exception("Bang!"); var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().MinBy(x => { if (x == 3) throw ex; return x; }); AssertThrows(() => xs.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] public void MinBy4() { var ex = new Exception("Bang!"); var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().MinBy(x => { if (x == 4) throw ex; return x; }); AssertThrows(() => xs.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] public void MinBy5() { var ex = new Exception("Bang!"); var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().Concat(AsyncEnumerable.Throw(ex)).MinBy(x => x, Comparer.Default); AssertThrows(() => xs.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] public async Task MaxBy_Null() { await Assert.ThrowsAsync(() => AsyncEnumerableEx.MaxBy(default(IAsyncEnumerable), x => x)); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MaxBy(AsyncEnumerable.Return(42), default(Func))); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MaxBy(default(IAsyncEnumerable), x => x, Comparer.Default)); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MaxBy(AsyncEnumerable.Return(42), default(Func), Comparer.Default)); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MaxBy(AsyncEnumerable.Return(42), x => x, default(IComparer))); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MaxBy(default(IAsyncEnumerable), x => x, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MaxBy(AsyncEnumerable.Return(42), default(Func), CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MaxBy(default(IAsyncEnumerable), x => x, Comparer.Default, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MaxBy(AsyncEnumerable.Return(42), default(Func), Comparer.Default, CancellationToken.None)); await Assert.ThrowsAsync(() => AsyncEnumerableEx.MaxBy(AsyncEnumerable.Return(42), x => x, default(IComparer), CancellationToken.None)); } [Fact] public void MaxBy1() { var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().MaxBy(x => x / 2); var res = xs.Result; Assert.True(res.SequenceEqual(new[] { 7, 6 })); } [Fact] public void MaxBy2() { var xs = new int[0].ToAsyncEnumerable().MaxBy(x => x / 2); AssertThrows(() => xs.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException); } [Fact] public void MaxBy3() { var ex = new Exception("Bang!"); var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().MaxBy(x => { if (x == 3) throw ex; return x; }); AssertThrows(() => xs.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] public void MaxBy4() { var ex = new Exception("Bang!"); var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().MaxBy(x => { if (x == 4) throw ex; return x; }); AssertThrows(() => xs.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } [Fact] public void MaxBy5() { var ex = new Exception("Bang!"); var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().Concat(AsyncEnumerable.Throw(ex)).MaxBy(x => x, Comparer.Default); AssertThrows(() => xs.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex); } private sealed class Eq : IEqualityComparer { public bool Equals(int x, int y) { return EqualityComparer.Default.Equals(Math.Abs(x), Math.Abs(y)); } public int GetHashCode(int obj) { return EqualityComparer.Default.GetHashCode(Math.Abs(obj)); } } } }