// 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 System.Threading.Tasks; using Xunit; namespace Tests { public class OrderBy : AsyncEnumerableTests { [Fact] public void OrderBy_Null() { AssertThrows(() => AsyncEnumerable.OrderBy(default, x => x)); AssertThrows(() => AsyncEnumerable.OrderBy(Return42, default(Func))); AssertThrows(() => AsyncEnumerable.OrderBy(default, x => x, Comparer.Default)); AssertThrows(() => AsyncEnumerable.OrderBy(Return42, default(Func), Comparer.Default)); AssertThrows(() => AsyncEnumerable.OrderByDescending(default, x => x)); AssertThrows(() => AsyncEnumerable.OrderByDescending(Return42, default(Func))); AssertThrows(() => AsyncEnumerable.OrderByDescending(default, x => x, Comparer.Default)); AssertThrows(() => AsyncEnumerable.OrderByDescending(Return42, default(Func), Comparer.Default)); var xs = Return42.OrderBy(x => x); AssertThrows(() => AsyncEnumerable.ThenBy(default, x => x)); AssertThrows(() => AsyncEnumerable.ThenBy(xs, default(Func))); AssertThrows(() => AsyncEnumerable.ThenBy(default, x => x, Comparer.Default)); AssertThrows(() => AsyncEnumerable.ThenBy(xs, default(Func), Comparer.Default)); AssertThrows(() => AsyncEnumerable.ThenByDescending(default, x => x)); AssertThrows(() => AsyncEnumerable.ThenByDescending(xs, default(Func))); AssertThrows(() => AsyncEnumerable.ThenByDescending(default, x => x, Comparer.Default)); AssertThrows(() => AsyncEnumerable.ThenByDescending(xs, default(Func), Comparer.Default)); } [Fact] public void OrderBy1() { var xs = new[] { 2, 6, 1, 5, 7, 8, 9, 3, 4, 0 }.ToAsyncEnumerable(); var ys = xs.OrderBy(x => x); var e = ys.GetAsyncEnumerator(); for (var i = 0; i < 10; i++) HasNext(e, i); NoNext(e); } [Fact] public void OrderBy2() { var ex = new Exception("Bang!"); var xs = new[] { 2, 6, 1, 5, 7, 8, 9, 3, 4, 0 }.ToAsyncEnumerable(); var ys = xs.OrderBy(new Func(x => { throw ex; })); var e = ys.GetAsyncEnumerator(); AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), SingleInnerExceptionMatches(ex)); } [Fact] public async Task OrderBy3() { var xs = new[] { 2, 6, 1, 5, 7, 8, 9, 3, 4, 0 }.ToAsyncEnumerable(); var ys = xs.OrderBy(x => x); await SequenceIdentity(ys); } [Fact] public void ThenBy2() { var ex = new Exception("Bang!"); var xs = new[] { 2, 6, 1, 5, 7, 8, 9, 3, 4, 0 }.ToAsyncEnumerable(); var ys = xs.OrderBy(x => x).ThenBy(new Func(x => { throw ex; })); var e = ys.GetAsyncEnumerator(); AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), SingleInnerExceptionMatches(ex)); } [Fact] public void OrderByDescending1() { var xs = new[] { 2, 6, 1, 5, 7, 8, 9, 3, 4, 0 }.ToAsyncEnumerable(); var ys = xs.OrderByDescending(x => x); var e = ys.GetAsyncEnumerator(); for (var i = 9; i >= 0; i--) HasNext(e, i); NoNext(e); } [Fact] public void OrderByDescending2() { var ex = new Exception("Bang!"); var xs = new[] { 2, 6, 1, 5, 7, 8, 9, 3, 4, 0 }.ToAsyncEnumerable(); var ys = xs.OrderByDescending(new Func(x => { throw ex; })); var e = ys.GetAsyncEnumerator(); AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), SingleInnerExceptionMatches(ex)); } [Fact] public async Task OrderByDescending3() { var xs = new[] { 2, 6, 1, 5, 7, 8, 9, 3, 4, 0 }.ToAsyncEnumerable(); var ys = xs.OrderByDescending(x => x); await SequenceIdentity(ys); } [Fact] public void ThenByDescending2() { var ex = new Exception("Bang!"); var xs = new[] { 2, 6, 1, 5, 7, 8, 9, 3, 4, 0 }.ToAsyncEnumerable(); var ys = xs.OrderBy(x => x).ThenByDescending(new Func(x => { throw ex; })); var e = ys.GetAsyncEnumerator(); AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), SingleInnerExceptionMatches(ex)); } [Fact] public void OrderByThenBy1() { var xs = new[] { new { Name = "Bart", Age = 27 }, new { Name = "John", Age = 62 }, new { Name = "Eric", Age = 27 }, new { Name = "Lisa", Age = 14 }, new { Name = "Brad", Age = 27 }, new { Name = "Lisa", Age = 23 }, new { Name = "Eric", Age = 42 }, }; var ys = xs.ToAsyncEnumerable(); var ress = xs.OrderBy(x => x.Name).ThenBy(x => x.Age); var resa = ys.OrderBy(x => x.Name).ThenBy(x => x.Age); Assert.True(ress.SequenceEqual(resa.ToEnumerable())); } [Fact] public void OrderByThenBy2() { var xs = new[] { new { Name = "Bart", Age = 27 }, new { Name = "John", Age = 62 }, new { Name = "Eric", Age = 27 }, new { Name = "Lisa", Age = 14 }, new { Name = "Brad", Age = 27 }, new { Name = "Lisa", Age = 23 }, new { Name = "Eric", Age = 42 }, }; var ys = xs.ToAsyncEnumerable(); var ress = xs.OrderBy(x => x.Name).ThenByDescending(x => x.Age); var resa = ys.OrderBy(x => x.Name).ThenByDescending(x => x.Age); Assert.True(ress.SequenceEqual(resa.ToEnumerable())); } [Fact] public void OrderByThenBy3() { var xs = new[] { new { Name = "Bart", Age = 27 }, new { Name = "John", Age = 62 }, new { Name = "Eric", Age = 27 }, new { Name = "Lisa", Age = 14 }, new { Name = "Brad", Age = 27 }, new { Name = "Lisa", Age = 23 }, new { Name = "Eric", Age = 42 }, }; var ys = xs.ToAsyncEnumerable(); var ress = xs.OrderByDescending(x => x.Name).ThenBy(x => x.Age); var resa = ys.OrderByDescending(x => x.Name).ThenBy(x => x.Age); Assert.True(ress.SequenceEqual(resa.ToEnumerable())); } [Fact] public void OrderByThenBy4() { var xs = new[] { new { Name = "Bart", Age = 27 }, new { Name = "John", Age = 62 }, new { Name = "Eric", Age = 27 }, new { Name = "Lisa", Age = 14 }, new { Name = "Brad", Age = 27 }, new { Name = "Lisa", Age = 23 }, new { Name = "Eric", Age = 42 }, }; var ys = xs.ToAsyncEnumerable(); var ress = xs.OrderByDescending(x => x.Name).ThenByDescending(x => x.Age); var resa = ys.OrderByDescending(x => x.Name).ThenByDescending(x => x.Age); Assert.True(ress.SequenceEqual(resa.ToEnumerable())); } } }