Take.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 Take : AsyncEnumerableTests
  11. {
  12. [Fact]
  13. public void Take_Null()
  14. {
  15. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Take<int>(default, 5));
  16. }
  17. [Fact]
  18. public async Task Take_Simple_TakeNegative()
  19. {
  20. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().Where(x => true);
  21. var ys = xs.Take(-2);
  22. var e = ys.GetAsyncEnumerator();
  23. await NoNextAsync(e);
  24. }
  25. [Fact]
  26. public async Task Take_Simple_TakeNegative_IList()
  27. {
  28. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  29. var ys = xs.Take(-2);
  30. var e = ys.GetAsyncEnumerator();
  31. await NoNextAsync(e);
  32. }
  33. [Fact]
  34. public async Task Take_Simple_TakeSome()
  35. {
  36. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().Where(x => true);
  37. var ys = xs.Take(2);
  38. var e = ys.GetAsyncEnumerator();
  39. await HasNextAsync(e, 1);
  40. await HasNextAsync(e, 2);
  41. await NoNextAsync(e);
  42. }
  43. [Fact]
  44. public async Task Take_Simple_TakeSome_IList()
  45. {
  46. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  47. var ys = xs.Take(2);
  48. var e = ys.GetAsyncEnumerator();
  49. await HasNextAsync(e, 1);
  50. await HasNextAsync(e, 2);
  51. await NoNextAsync(e);
  52. }
  53. [Fact]
  54. public async Task Take_Simple_TakeAll()
  55. {
  56. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().Where(x => true);
  57. var ys = xs.Take(10);
  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 Take_Simple_TakeAll_IList()
  67. {
  68. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  69. var ys = xs.Take(10);
  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 Take_Simple_TakeZero()
  79. {
  80. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().Where(x => true);
  81. var ys = xs.Take(0);
  82. var e = ys.GetAsyncEnumerator();
  83. await NoNextAsync(e);
  84. }
  85. [Fact]
  86. public async Task Take_Simple_TakeZero_IList()
  87. {
  88. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  89. var ys = xs.Take(0);
  90. var e = ys.GetAsyncEnumerator();
  91. await NoNextAsync(e);
  92. }
  93. [Fact]
  94. public async Task Take_Throws_Source()
  95. {
  96. var ex = new Exception("Bang");
  97. var xs = Throw<int>(ex);
  98. var ys = xs.Take(2);
  99. var e = ys.GetAsyncEnumerator();
  100. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  101. }
  102. [Fact]
  103. public async Task Take_SequenceIdentity()
  104. {
  105. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  106. var ys = xs.Take(2);
  107. await SequenceIdentity(ys);
  108. }
  109. [Fact]
  110. public async Task Take_IAsyncPartition_NonEmpty_Take()
  111. {
  112. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().Where(x => true);
  113. var ys = xs.Take(2);
  114. int[] expected = [1, 2];
  115. Assert.Equal(2, await ys.CountAsync());
  116. Assert.Equal(1, await ys.FirstAsync());
  117. Assert.Equal(2, await ys.LastAsync());
  118. Assert.Equal(1, await ys.ElementAtAsync(0));
  119. Assert.Equal(2, await ys.ElementAtAsync(1));
  120. Assert.Equal(expected, await ys.ToArrayAsync());
  121. Assert.Equal(expected, await ys.ToListAsync());
  122. }
  123. [Fact]
  124. public async Task Take_IAsyncPartition_NonEmpty_TakeTake()
  125. {
  126. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().Where(x => true);
  127. var ys = xs.Take(3).Take(2);
  128. int[] expected = [1, 2];
  129. Assert.Equal(2, await ys.CountAsync());
  130. Assert.Equal(1, await ys.FirstAsync());
  131. Assert.Equal(2, await ys.LastAsync());
  132. Assert.Equal(1, await ys.ElementAtAsync(0));
  133. Assert.Equal(2, await ys.ElementAtAsync(1));
  134. Assert.Equal(expected, await ys.ToArrayAsync());
  135. Assert.Equal(expected, await ys.ToListAsync());
  136. }
  137. [Fact]
  138. public async Task Take_IAsyncPartition_NonEmpty_TakeSkip()
  139. {
  140. var xs = new[] { 2, 3, 4, 5 }.ToAsyncEnumerable().Where(x => true);
  141. var ys = xs.Take(3).Skip(1);
  142. int[] expected = [3, 4];
  143. Assert.Equal(2, await ys.CountAsync());
  144. Assert.Equal(3, await ys.FirstAsync());
  145. Assert.Equal(4, await ys.LastAsync());
  146. Assert.Equal(3, await ys.ElementAtAsync(0));
  147. Assert.Equal(4, await ys.ElementAtAsync(1));
  148. Assert.Equal(expected, await ys.ToArrayAsync());
  149. Assert.Equal(expected, await ys.ToListAsync());
  150. }
  151. [Fact]
  152. public async Task Take_IAsyncPartition_Empty_Take()
  153. {
  154. var xs = Array.Empty<int>().ToAsyncEnumerable().Where(x => true);
  155. var ys = xs.Take(2);
  156. Assert.Equal(0, await ys.CountAsync());
  157. await AssertThrowsAsync<InvalidOperationException>(ys.FirstAsync().AsTask());
  158. await AssertThrowsAsync<InvalidOperationException>(ys.LastAsync().AsTask());
  159. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(0).AsTask());
  160. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(1).AsTask());
  161. Assert.Empty(await ys.ToArrayAsync());
  162. Assert.Empty(await ys.ToListAsync());
  163. }
  164. [Fact]
  165. public async Task Take_IAsyncPartition_Empty_TakeSkip()
  166. {
  167. var xs = new[] { 1, 2, 3, 4, 5 }.ToAsyncEnumerable().Where(x => true);
  168. var ys = xs.Take(7).Skip(5);
  169. Assert.Equal(0, await ys.CountAsync());
  170. await AssertThrowsAsync<InvalidOperationException>(ys.FirstAsync().AsTask());
  171. await AssertThrowsAsync<InvalidOperationException>(ys.LastAsync().AsTask());
  172. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(0).AsTask());
  173. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(1).AsTask());
  174. Assert.Empty(await ys.ToArrayAsync());
  175. Assert.Empty(await ys.ToListAsync());
  176. }
  177. [Fact]
  178. public async Task Take_IAsyncPartition_IList_NonEmpty_Take()
  179. {
  180. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  181. var ys = xs.Take(2);
  182. int[] expected = [1, 2];
  183. Assert.Equal(2, await ys.CountAsync());
  184. Assert.Equal(1, await ys.FirstAsync());
  185. Assert.Equal(2, await ys.LastAsync());
  186. Assert.Equal(1, await ys.ElementAtAsync(0));
  187. Assert.Equal(2, await ys.ElementAtAsync(1));
  188. Assert.Equal(expected, await ys.ToArrayAsync());
  189. Assert.Equal(expected, await ys.ToListAsync());
  190. }
  191. [Fact]
  192. public async Task Take_IAsyncPartition_IList_NonEmpty_TakeTake()
  193. {
  194. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  195. var ys = xs.Take(3).Take(2);
  196. int[] expected = [1, 2];
  197. Assert.Equal(2, await ys.CountAsync());
  198. Assert.Equal(1, await ys.FirstAsync());
  199. Assert.Equal(2, await ys.LastAsync());
  200. Assert.Equal(1, await ys.ElementAtAsync(0));
  201. Assert.Equal(2, await ys.ElementAtAsync(1));
  202. Assert.Equal(expected, await ys.ToArrayAsync());
  203. Assert.Equal(expected, await ys.ToListAsync());
  204. }
  205. [Fact]
  206. public async Task Take_IAsyncPartition_IList_NonEmpty_TakeSkip()
  207. {
  208. var xs = new[] { 2, 3, 4, 5 }.ToAsyncEnumerable();
  209. var ys = xs.Take(3).Skip(1);
  210. int[] expected = [3, 4];
  211. Assert.Equal(2, await ys.CountAsync());
  212. Assert.Equal(3, await ys.FirstAsync());
  213. Assert.Equal(4, await ys.LastAsync());
  214. Assert.Equal(3, await ys.ElementAtAsync(0));
  215. Assert.Equal(4, await ys.ElementAtAsync(1));
  216. Assert.Equal(expected, await ys.ToArrayAsync());
  217. Assert.Equal(expected, await ys.ToListAsync());
  218. }
  219. [Fact]
  220. public async Task Take_IAsyncPartition_IList_Empty_Take()
  221. {
  222. var xs = Array.Empty<int>().ToAsyncEnumerable();
  223. var ys = xs.Take(2);
  224. Assert.Equal(0, await ys.CountAsync());
  225. await AssertThrowsAsync<InvalidOperationException>(ys.FirstAsync().AsTask());
  226. await AssertThrowsAsync<InvalidOperationException>(ys.LastAsync().AsTask());
  227. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(0).AsTask());
  228. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(1).AsTask());
  229. Assert.Empty(await ys.ToArrayAsync());
  230. Assert.Empty(await ys.ToListAsync());
  231. }
  232. [Fact]
  233. public async Task Take_IAsyncPartition_IList_Empty_TakeSkip()
  234. {
  235. var xs = new[] { 1, 2, 3, 4, 5 }.ToAsyncEnumerable();
  236. var ys = xs.Take(7).Skip(5);
  237. Assert.Equal(0, await ys.CountAsync());
  238. await AssertThrowsAsync<InvalidOperationException>(ys.FirstAsync().AsTask());
  239. await AssertThrowsAsync<InvalidOperationException>(ys.LastAsync().AsTask());
  240. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(0).AsTask());
  241. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(1).AsTask());
  242. Assert.Empty(await ys.ToArrayAsync());
  243. Assert.Empty(await ys.ToListAsync());
  244. }
  245. }
  246. }