Skip.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. using System;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Xunit;
  8. namespace Tests
  9. {
  10. public class Skip : AsyncEnumerableTests
  11. {
  12. [Fact]
  13. public void Skip_Null()
  14. {
  15. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Skip<int>(default, 5));
  16. }
  17. [Fact]
  18. public async Task Skip_Simple_SkipSome()
  19. {
  20. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().Where(x => true);
  21. var ys = xs.Skip(2);
  22. var e = ys.GetAsyncEnumerator();
  23. await HasNextAsync(e, 3);
  24. await HasNextAsync(e, 4);
  25. await NoNextAsync(e);
  26. }
  27. [Fact]
  28. public async Task Skip_Simple_SkipSome_IList()
  29. {
  30. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  31. var ys = xs.Skip(2);
  32. var e = ys.GetAsyncEnumerator();
  33. await HasNextAsync(e, 3);
  34. await HasNextAsync(e, 4);
  35. await NoNextAsync(e);
  36. }
  37. [Fact]
  38. public async Task Skip_Simple_SkipAll()
  39. {
  40. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().Where(x => true);
  41. var ys = xs.Skip(10);
  42. var e = ys.GetAsyncEnumerator();
  43. await NoNextAsync(e);
  44. }
  45. [Fact]
  46. public async Task Skip_Simple_SkipAll_IList()
  47. {
  48. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  49. var ys = xs.Skip(10);
  50. var e = ys.GetAsyncEnumerator();
  51. await NoNextAsync(e);
  52. }
  53. [Fact]
  54. public async Task Skip_Simple_SkipNone()
  55. {
  56. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().Where(x => true);
  57. var ys = xs.Skip(0);
  58. var e = ys.GetAsyncEnumerator();
  59. await HasNextAsync(e, 1);
  60. await HasNextAsync(e, 2);
  61. await HasNextAsync(e, 3);
  62. await HasNextAsync(e, 4);
  63. await NoNextAsync(e);
  64. }
  65. [Fact]
  66. public async Task Skip_Simple_SkipNone_IList()
  67. {
  68. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  69. var ys = xs.Skip(0);
  70. var e = ys.GetAsyncEnumerator();
  71. await HasNextAsync(e, 1);
  72. await HasNextAsync(e, 2);
  73. await HasNextAsync(e, 3);
  74. await HasNextAsync(e, 4);
  75. await NoNextAsync(e);
  76. }
  77. [Fact]
  78. public async Task Skip_Throws_Source()
  79. {
  80. var ex = new Exception("Bang");
  81. var xs = Throw<int>(ex);
  82. var ys = xs.Skip(2);
  83. var e = ys.GetAsyncEnumerator();
  84. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  85. }
  86. [Fact]
  87. public async Task Skip_SequenceIdentity()
  88. {
  89. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  90. var ys = xs.Skip(2);
  91. await SequenceIdentity(ys);
  92. }
  93. [Fact]
  94. public async Task Skip_IAsyncPartition_NonEmpty_Skip()
  95. {
  96. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().Where(x => true);
  97. var ys = xs.Skip(2);
  98. int[] expected = [3, 4];
  99. Assert.Equal(2, await ys.CountAsync());
  100. Assert.Equal(3, await ys.FirstAsync());
  101. Assert.Equal(4, await ys.LastAsync());
  102. Assert.Equal(3, await ys.ElementAtAsync(0));
  103. Assert.Equal(4, await ys.ElementAtAsync(1));
  104. Assert.Equal(expected, await ys.ToArrayAsync());
  105. Assert.Equal(expected, await ys.ToListAsync());
  106. }
  107. [Fact]
  108. public async Task Skip_IAsyncPartition_NonEmpty_SkipSkip()
  109. {
  110. var xs = new[] { -2, -1, 0, 1, 2, 3, 4 }.ToAsyncEnumerable().Where(x => true);
  111. var ys = xs.Skip(2).Skip(3);
  112. int[] expected = [3, 4];
  113. Assert.Equal(2, await ys.CountAsync());
  114. Assert.Equal(3, await ys.FirstAsync());
  115. Assert.Equal(4, await ys.LastAsync());
  116. Assert.Equal(3, await ys.ElementAtAsync(0));
  117. Assert.Equal(4, await ys.ElementAtAsync(1));
  118. Assert.Equal(expected, await ys.ToArrayAsync());
  119. Assert.Equal(expected, await ys.ToListAsync());
  120. }
  121. [Fact]
  122. public async Task Skip_IAsyncPartition_NonEmpty_SkipTake()
  123. {
  124. var xs = new[] { 2, 3, 4, 5 }.ToAsyncEnumerable().Where(x => true);
  125. var ys = xs.Skip(1).Take(2);
  126. int[] expected = [3, 4];
  127. Assert.Equal(2, await ys.CountAsync());
  128. Assert.Equal(3, await ys.FirstAsync());
  129. Assert.Equal(4, await ys.LastAsync());
  130. Assert.Equal(3, await ys.ElementAtAsync(0));
  131. Assert.Equal(4, await ys.ElementAtAsync(1));
  132. Assert.Equal(expected, await ys.ToArrayAsync());
  133. Assert.Equal(expected, await ys.ToListAsync());
  134. }
  135. [Fact]
  136. public async Task Skip_IAsyncPartition_Empty_Skip()
  137. {
  138. var xs = Array.Empty<int>().ToAsyncEnumerable().Where(x => true);
  139. var ys = xs.Skip(2);
  140. Assert.Equal(0, await ys.CountAsync());
  141. await AssertThrowsAsync<InvalidOperationException>(ys.FirstAsync().AsTask());
  142. await AssertThrowsAsync<InvalidOperationException>(ys.LastAsync().AsTask());
  143. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(0).AsTask());
  144. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(1).AsTask());
  145. Assert.Empty(await ys.ToArrayAsync());
  146. Assert.Empty(await ys.ToListAsync());
  147. }
  148. [Fact]
  149. public async Task Skip_IAsyncPartition_Empty_SkipSkip()
  150. {
  151. var xs = new[] { 1, 2 }.ToAsyncEnumerable().Where(x => true);
  152. var ys = xs.Skip(1).Skip(1);
  153. Assert.Equal(0, await ys.CountAsync());
  154. await AssertThrowsAsync<InvalidOperationException>(ys.FirstAsync().AsTask());
  155. await AssertThrowsAsync<InvalidOperationException>(ys.LastAsync().AsTask());
  156. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(0).AsTask());
  157. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(1).AsTask());
  158. Assert.Empty(await ys.ToArrayAsync());
  159. Assert.Empty(await ys.ToListAsync());
  160. }
  161. [Fact]
  162. public async Task Skip_IAsyncPartition_IList_NonEmpty_Skip()
  163. {
  164. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  165. var ys = xs.Skip(2);
  166. int[] expected = [3, 4];
  167. Assert.Equal(2, await ys.CountAsync());
  168. Assert.Equal(3, await ys.FirstAsync());
  169. Assert.Equal(4, await ys.LastAsync());
  170. Assert.Equal(3, await ys.ElementAtAsync(0));
  171. Assert.Equal(4, await ys.ElementAtAsync(1));
  172. Assert.Equal(expected, await ys.ToArrayAsync());
  173. Assert.Equal(expected, await ys.ToListAsync());
  174. }
  175. [Fact]
  176. public async Task Skip_IAsyncPartition_IList_NonEmpty_SkipSkip()
  177. {
  178. var xs = new[] { -2, -1, 0, 1, 2, 3, 4 }.ToAsyncEnumerable();
  179. var ys = xs.Skip(2).Skip(3);
  180. int[] expected = [3, 4];
  181. Assert.Equal(2, await ys.CountAsync());
  182. Assert.Equal(3, await ys.FirstAsync());
  183. Assert.Equal(4, await ys.LastAsync());
  184. Assert.Equal(3, await ys.ElementAtAsync(0));
  185. Assert.Equal(4, await ys.ElementAtAsync(1));
  186. Assert.Equal(expected, await ys.ToArrayAsync());
  187. Assert.Equal(expected, await ys.ToListAsync());
  188. }
  189. [Fact]
  190. public async Task Skip_IAsyncPartition_IList_NonEmpty_SkipTake()
  191. {
  192. var xs = new[] { 2, 3, 4, 5 }.ToAsyncEnumerable();
  193. var ys = xs.Skip(1).Take(2);
  194. int[] expected = [3, 4];
  195. Assert.Equal(2, await ys.CountAsync());
  196. Assert.Equal(3, await ys.FirstAsync());
  197. Assert.Equal(4, await ys.LastAsync());
  198. Assert.Equal(3, await ys.ElementAtAsync(0));
  199. Assert.Equal(4, await ys.ElementAtAsync(1));
  200. Assert.Equal(expected, await ys.ToArrayAsync());
  201. Assert.Equal(expected, await ys.ToListAsync());
  202. }
  203. [Fact]
  204. public async Task Skip_IAsyncPartition_IList_Empty_Skip()
  205. {
  206. var xs = Array.Empty<int>().ToAsyncEnumerable();
  207. var ys = xs.Skip(2);
  208. Assert.Equal(0, await ys.CountAsync());
  209. await AssertThrowsAsync<InvalidOperationException>(ys.FirstAsync().AsTask());
  210. await AssertThrowsAsync<InvalidOperationException>(ys.LastAsync().AsTask());
  211. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(0).AsTask());
  212. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(1).AsTask());
  213. Assert.Empty(await ys.ToArrayAsync());
  214. Assert.Empty(await ys.ToListAsync());
  215. }
  216. [Fact]
  217. public async Task Skip_IAsyncPartition_IList_Empty_SkipSkip()
  218. {
  219. var xs = new[] { 1, 2 }.ToAsyncEnumerable();
  220. var ys = xs.Skip(1).Skip(1);
  221. Assert.Equal(0, await ys.CountAsync());
  222. await AssertThrowsAsync<InvalidOperationException>(ys.FirstAsync().AsTask());
  223. await AssertThrowsAsync<InvalidOperationException>(ys.LastAsync().AsTask());
  224. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(0).AsTask());
  225. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(1).AsTask());
  226. Assert.Empty(await ys.ToArrayAsync());
  227. Assert.Empty(await ys.ToListAsync());
  228. }
  229. }
  230. }