123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- // 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.Tasks;
- using Xunit;
- namespace Tests
- {
- public class Reverse : AsyncEnumerableTests
- {
- [Fact]
- public void Reverse_Null()
- {
- AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Reverse<int>(null));
- }
- [Fact]
- public void Reverse1()
- {
- var xs = AsyncEnumerable.Empty<int>();
- var ys = xs.Reverse();
- var e = ys.GetAsyncEnumerator();
- NoNext(e);
- }
- [Fact]
- public void Reverse2()
- {
- var xs = AsyncEnumerable.Return(42);
- var ys = xs.Reverse();
- var e = ys.GetAsyncEnumerator();
- HasNext(e, 42);
- NoNext(e);
- }
- [Fact]
- public void Reverse3()
- {
- var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
- var ys = xs.Reverse();
- var e = ys.GetAsyncEnumerator();
- HasNext(e, 3);
- HasNext(e, 2);
- HasNext(e, 1);
- NoNext(e);
- }
- [Fact]
- public void Reverse4()
- {
- var ex = new Exception("Bang!");
- var xs = AsyncEnumerable.Throw<int>(ex);
- var ys = xs.Reverse();
- var e = ys.GetAsyncEnumerator();
- AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
- }
- [Fact]
- public async Task Reverse5()
- {
- var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
- var ys = xs.Reverse();
- Assert.Equal(new[] { 3, 2, 1 }, await ys.ToArray());
- }
- [Fact]
- public async Task Reverse6()
- {
- var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
- var ys = xs.Reverse();
- Assert.Equal(new[] { 3, 2, 1 }, await ys.ToList());
- }
- [Fact]
- public async Task Reverse7()
- {
- var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
- var ys = xs.Reverse();
- Assert.Equal(3, await ys.Count());
- }
- [Fact]
- public async Task Reverse8()
- {
- var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
- var ys = xs.Reverse();
- await SequenceIdentity(ys);
- }
- [Fact]
- public async Task Reverse9()
- {
- var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
- var ys = xs.Reverse().Prepend(4); // to trigger onlyIfCheap
- Assert.Equal(new[] { 4, 3, 2, 1 }, await ys.ToArray());
- }
- }
- }
|