|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT License. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace Tests |
| 12 | +{ |
| 13 | + public class Chunk : AsyncEnumerableTests |
| 14 | + { |
| 15 | + [Fact] |
| 16 | + public async Task Chunk_Null() |
| 17 | + { |
| 18 | + await Assert.ThrowsAsync<ArgumentNullException>(async () => |
| 19 | + { |
| 20 | + IAsyncEnumerable<int>? xs = null; |
| 21 | + var ys = xs!.ChunkAsync(24); |
| 22 | + |
| 23 | + var e = ys.GetAsyncEnumerator(); |
| 24 | + await e.MoveNextAsync(); |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + [Theory] |
| 29 | + [InlineData(0)] |
| 30 | + [InlineData(-1)] |
| 31 | + public async Task Chunk_NonPositiveSize(int size) |
| 32 | + { |
| 33 | + await Assert.ThrowsAsync<ArgumentOutOfRangeException>("size", async () => |
| 34 | + { |
| 35 | + var xs = new[] { 24 }.ToAsyncEnumerable(); |
| 36 | + var ys = xs.ChunkAsync(size); |
| 37 | + |
| 38 | + var e = ys.GetAsyncEnumerator(); |
| 39 | + await e.MoveNextAsync(); |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public async Task Chunk_Simple_Evenly() |
| 45 | + { |
| 46 | + var xs = new[] { 1, 1, 4, 5, 1, 4 }.ToAsyncEnumerable(); |
| 47 | + var ys = xs.ChunkAsync(3); |
| 48 | + |
| 49 | + var e = ys.GetAsyncEnumerator(); |
| 50 | + await HasNextAsync(e, new[] { 1, 1, 4 }); |
| 51 | + await HasNextAsync(e, new[] { 5, 1, 4 }); |
| 52 | + await NoNextAsync(e); |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public async Task Chunk_Simple_Unevenly() |
| 57 | + { |
| 58 | + var xs = new[] { 1, 9, 1, 9, 8, 1, 0 }.ToAsyncEnumerable(); |
| 59 | + var ys = xs.ChunkAsync(4); |
| 60 | + |
| 61 | + var e = ys.GetAsyncEnumerator(); |
| 62 | + await HasNextAsync(e, new[] { 1, 9, 1, 9 }); |
| 63 | + await HasNextAsync(e, new[] { 8, 1, 0 }); |
| 64 | + await NoNextAsync(e); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public async Task Chunk_SourceSmallerThanChunkSize() |
| 69 | + { |
| 70 | + var xs = new[] { 8, 9, 3 }.ToAsyncEnumerable(); |
| 71 | + var ys = xs.ChunkAsync(4); |
| 72 | + |
| 73 | + var e = ys.GetAsyncEnumerator(); |
| 74 | + await HasNextAsync(e, new[] { 8, 9, 3 }); |
| 75 | + await NoNextAsync(e); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public async Task Chunk_EmptySource() |
| 80 | + { |
| 81 | + var xs = new int[0].ToAsyncEnumerable(); |
| 82 | + var ys = xs.ChunkAsync(24); |
| 83 | + |
| 84 | + var e = ys.GetAsyncEnumerator(); |
| 85 | + await NoNextAsync(e); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments