123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- // 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.Linq;
- using System.Threading.Tasks;
- using Xunit;
- namespace Tests
- {
- public class Take : AsyncEnumerableTests
- {
- [Fact]
- public void Take_Null()
- {
- Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Take<int>(default, 5));
- }
- [Fact]
- public async Task Take_Simple_TakeNegative()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().Where(x => true);
- var ys = xs.Take(-2);
- var e = ys.GetAsyncEnumerator();
- await NoNextAsync(e);
- }
- [Fact]
- public async Task Take_Simple_TakeNegative_IList()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.Take(-2);
- var e = ys.GetAsyncEnumerator();
- await NoNextAsync(e);
- }
- [Fact]
- public async Task Take_Simple_TakeSome()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().Where(x => true);
- var ys = xs.Take(2);
- var e = ys.GetAsyncEnumerator();
- await HasNextAsync(e, 1);
- await HasNextAsync(e, 2);
- await NoNextAsync(e);
- }
- [Fact]
- public async Task Take_Simple_TakeSome_IList()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.Take(2);
- var e = ys.GetAsyncEnumerator();
- await HasNextAsync(e, 1);
- await HasNextAsync(e, 2);
- await NoNextAsync(e);
- }
- [Fact]
- public async Task Take_Simple_TakeAll()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().Where(x => true);
- var ys = xs.Take(10);
- var e = ys.GetAsyncEnumerator();
- await HasNextAsync(e, 1);
- await HasNextAsync(e, 2);
- await HasNextAsync(e, 3);
- await HasNextAsync(e, 4);
- await NoNextAsync(e);
- }
- [Fact]
- public async Task Take_Simple_TakeAll_IList()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.Take(10);
- var e = ys.GetAsyncEnumerator();
- await HasNextAsync(e, 1);
- await HasNextAsync(e, 2);
- await HasNextAsync(e, 3);
- await HasNextAsync(e, 4);
- await NoNextAsync(e);
- }
- [Fact]
- public async Task Take_Simple_TakeZero()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().Where(x => true);
- var ys = xs.Take(0);
- var e = ys.GetAsyncEnumerator();
- await NoNextAsync(e);
- }
- [Fact]
- public async Task Take_Simple_TakeZero_IList()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.Take(0);
- var e = ys.GetAsyncEnumerator();
- await NoNextAsync(e);
- }
- [Fact]
- public async Task Take_Throws_Source()
- {
- var ex = new Exception("Bang");
- var xs = Throw<int>(ex);
- var ys = xs.Take(2);
- var e = ys.GetAsyncEnumerator();
- await AssertThrowsAsync(e.MoveNextAsync(), ex);
- }
- [Fact]
- public async Task Take_SequenceIdentity()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.Take(2);
- await SequenceIdentity(ys);
- }
- [Fact]
- public async Task Take_IAsyncPartition_NonEmpty_Take()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().Where(x => true);
- var ys = xs.Take(2);
- int[] expected = [1, 2];
- Assert.Equal(2, await ys.CountAsync());
- Assert.Equal(1, await ys.FirstAsync());
- Assert.Equal(2, await ys.LastAsync());
- Assert.Equal(1, await ys.ElementAtAsync(0));
- Assert.Equal(2, await ys.ElementAtAsync(1));
- Assert.Equal(expected, await ys.ToArrayAsync());
- Assert.Equal(expected, await ys.ToListAsync());
- }
- [Fact]
- public async Task Take_IAsyncPartition_NonEmpty_TakeTake()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().Where(x => true);
- var ys = xs.Take(3).Take(2);
- int[] expected = [1, 2];
- Assert.Equal(2, await ys.CountAsync());
- Assert.Equal(1, await ys.FirstAsync());
- Assert.Equal(2, await ys.LastAsync());
- Assert.Equal(1, await ys.ElementAtAsync(0));
- Assert.Equal(2, await ys.ElementAtAsync(1));
- Assert.Equal(expected, await ys.ToArrayAsync());
- Assert.Equal(expected, await ys.ToListAsync());
- }
- [Fact]
- public async Task Take_IAsyncPartition_NonEmpty_TakeSkip()
- {
- var xs = new[] { 2, 3, 4, 5 }.ToAsyncEnumerable().Where(x => true);
- var ys = xs.Take(3).Skip(1);
- int[] expected = [3, 4];
- Assert.Equal(2, await ys.CountAsync());
- Assert.Equal(3, await ys.FirstAsync());
- Assert.Equal(4, await ys.LastAsync());
- Assert.Equal(3, await ys.ElementAtAsync(0));
- Assert.Equal(4, await ys.ElementAtAsync(1));
- Assert.Equal(expected, await ys.ToArrayAsync());
- Assert.Equal(expected, await ys.ToListAsync());
- }
- [Fact]
- public async Task Take_IAsyncPartition_Empty_Take()
- {
- var xs = Array.Empty<int>().ToAsyncEnumerable().Where(x => true);
- var ys = xs.Take(2);
- Assert.Equal(0, await ys.CountAsync());
- await AssertThrowsAsync<InvalidOperationException>(ys.FirstAsync().AsTask());
- await AssertThrowsAsync<InvalidOperationException>(ys.LastAsync().AsTask());
- await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(0).AsTask());
- await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(1).AsTask());
- Assert.Empty(await ys.ToArrayAsync());
- Assert.Empty(await ys.ToListAsync());
- }
- [Fact]
- public async Task Take_IAsyncPartition_Empty_TakeSkip()
- {
- var xs = new[] { 1, 2, 3, 4, 5 }.ToAsyncEnumerable().Where(x => true);
- var ys = xs.Take(7).Skip(5);
- Assert.Equal(0, await ys.CountAsync());
- await AssertThrowsAsync<InvalidOperationException>(ys.FirstAsync().AsTask());
- await AssertThrowsAsync<InvalidOperationException>(ys.LastAsync().AsTask());
- await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(0).AsTask());
- await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(1).AsTask());
- Assert.Empty(await ys.ToArrayAsync());
- Assert.Empty(await ys.ToListAsync());
- }
- [Fact]
- public async Task Take_IAsyncPartition_IList_NonEmpty_Take()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.Take(2);
- int[] expected = [1, 2];
- Assert.Equal(2, await ys.CountAsync());
- Assert.Equal(1, await ys.FirstAsync());
- Assert.Equal(2, await ys.LastAsync());
- Assert.Equal(1, await ys.ElementAtAsync(0));
- Assert.Equal(2, await ys.ElementAtAsync(1));
- Assert.Equal(expected, await ys.ToArrayAsync());
- Assert.Equal(expected, await ys.ToListAsync());
- }
- [Fact]
- public async Task Take_IAsyncPartition_IList_NonEmpty_TakeTake()
- {
- var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
- var ys = xs.Take(3).Take(2);
- int[] expected = [1, 2];
- Assert.Equal(2, await ys.CountAsync());
- Assert.Equal(1, await ys.FirstAsync());
- Assert.Equal(2, await ys.LastAsync());
- Assert.Equal(1, await ys.ElementAtAsync(0));
- Assert.Equal(2, await ys.ElementAtAsync(1));
- Assert.Equal(expected, await ys.ToArrayAsync());
- Assert.Equal(expected, await ys.ToListAsync());
- }
- [Fact]
- public async Task Take_IAsyncPartition_IList_NonEmpty_TakeSkip()
- {
- var xs = new[] { 2, 3, 4, 5 }.ToAsyncEnumerable();
- var ys = xs.Take(3).Skip(1);
- int[] expected = [3, 4];
- Assert.Equal(2, await ys.CountAsync());
- Assert.Equal(3, await ys.FirstAsync());
- Assert.Equal(4, await ys.LastAsync());
- Assert.Equal(3, await ys.ElementAtAsync(0));
- Assert.Equal(4, await ys.ElementAtAsync(1));
- Assert.Equal(expected, await ys.ToArrayAsync());
- Assert.Equal(expected, await ys.ToListAsync());
- }
- [Fact]
- public async Task Take_IAsyncPartition_IList_Empty_Take()
- {
- var xs = Array.Empty<int>().ToAsyncEnumerable();
- var ys = xs.Take(2);
- Assert.Equal(0, await ys.CountAsync());
- await AssertThrowsAsync<InvalidOperationException>(ys.FirstAsync().AsTask());
- await AssertThrowsAsync<InvalidOperationException>(ys.LastAsync().AsTask());
- await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(0).AsTask());
- await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(1).AsTask());
- Assert.Empty(await ys.ToArrayAsync());
- Assert.Empty(await ys.ToListAsync());
- }
- [Fact]
- public async Task Take_IAsyncPartition_IList_Empty_TakeSkip()
- {
- var xs = new[] { 1, 2, 3, 4, 5 }.ToAsyncEnumerable();
- var ys = xs.Take(7).Skip(5);
- Assert.Equal(0, await ys.CountAsync());
- await AssertThrowsAsync<InvalidOperationException>(ys.FirstAsync().AsTask());
- await AssertThrowsAsync<InvalidOperationException>(ys.LastAsync().AsTask());
- await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(0).AsTask());
- await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(1).AsTask());
- Assert.Empty(await ys.ToArrayAsync());
- Assert.Empty(await ys.ToListAsync());
- }
- }
- }
|