1
0

Take.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 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. Assert.Equal(2, await ys.CountAsync());
  115. Assert.Equal(1, await ys.FirstAsync());
  116. Assert.Equal(2, await ys.LastAsync());
  117. Assert.Equal(1, await ys.ElementAtAsync(0));
  118. Assert.Equal(2, await ys.ElementAtAsync(1));
  119. Assert.Equal(new[] { 1, 2 }, await ys.ToArrayAsync());
  120. Assert.Equal(new[] { 1, 2 }, await ys.ToListAsync());
  121. }
  122. [Fact]
  123. public async Task Take_IAsyncPartition_NonEmpty_TakeTake()
  124. {
  125. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().Where(x => true);
  126. var ys = xs.Take(3).Take(2);
  127. Assert.Equal(2, await ys.CountAsync());
  128. Assert.Equal(1, await ys.FirstAsync());
  129. Assert.Equal(2, await ys.LastAsync());
  130. Assert.Equal(1, await ys.ElementAtAsync(0));
  131. Assert.Equal(2, await ys.ElementAtAsync(1));
  132. Assert.Equal(new[] { 1, 2 }, await ys.ToArrayAsync());
  133. Assert.Equal(new[] { 1, 2 }, await ys.ToListAsync());
  134. }
  135. [Fact]
  136. public async Task Take_IAsyncPartition_NonEmpty_TakeSkip()
  137. {
  138. var xs = new[] { 2, 3, 4, 5 }.ToAsyncEnumerable().Where(x => true);
  139. var ys = xs.Take(3).Skip(1);
  140. Assert.Equal(2, await ys.CountAsync());
  141. Assert.Equal(3, await ys.FirstAsync());
  142. Assert.Equal(4, await ys.LastAsync());
  143. Assert.Equal(3, await ys.ElementAtAsync(0));
  144. Assert.Equal(4, await ys.ElementAtAsync(1));
  145. Assert.Equal(new[] { 3, 4 }, await ys.ToArrayAsync());
  146. Assert.Equal(new[] { 3, 4 }, await ys.ToListAsync());
  147. }
  148. [Fact]
  149. public async Task Take_IAsyncPartition_Empty_Take()
  150. {
  151. var xs = new int[0].ToAsyncEnumerable().Where(x => true);
  152. var ys = xs.Take(2);
  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 Take_IAsyncPartition_Empty_TakeSkip()
  163. {
  164. var xs = new[] { 1, 2, 3, 4, 5 }.ToAsyncEnumerable().Where(x => true);
  165. var ys = xs.Take(7).Skip(5);
  166. Assert.Equal(0, await ys.CountAsync());
  167. await AssertThrowsAsync<InvalidOperationException>(ys.FirstAsync().AsTask());
  168. await AssertThrowsAsync<InvalidOperationException>(ys.LastAsync().AsTask());
  169. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(0).AsTask());
  170. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(1).AsTask());
  171. Assert.Empty(await ys.ToArrayAsync());
  172. Assert.Empty(await ys.ToListAsync());
  173. }
  174. [Fact]
  175. public async Task Take_IAsyncPartition_IList_NonEmpty_Take()
  176. {
  177. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  178. var ys = xs.Take(2);
  179. Assert.Equal(2, await ys.CountAsync());
  180. Assert.Equal(1, await ys.FirstAsync());
  181. Assert.Equal(2, await ys.LastAsync());
  182. Assert.Equal(1, await ys.ElementAtAsync(0));
  183. Assert.Equal(2, await ys.ElementAtAsync(1));
  184. Assert.Equal(new[] { 1, 2 }, await ys.ToArrayAsync());
  185. Assert.Equal(new[] { 1, 2 }, await ys.ToListAsync());
  186. }
  187. [Fact]
  188. public async Task Take_IAsyncPartition_IList_NonEmpty_TakeTake()
  189. {
  190. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  191. var ys = xs.Take(3).Take(2);
  192. Assert.Equal(2, await ys.CountAsync());
  193. Assert.Equal(1, await ys.FirstAsync());
  194. Assert.Equal(2, await ys.LastAsync());
  195. Assert.Equal(1, await ys.ElementAtAsync(0));
  196. Assert.Equal(2, await ys.ElementAtAsync(1));
  197. Assert.Equal(new[] { 1, 2 }, await ys.ToArrayAsync());
  198. Assert.Equal(new[] { 1, 2 }, await ys.ToListAsync());
  199. }
  200. [Fact]
  201. public async Task Take_IAsyncPartition_IList_NonEmpty_TakeSkip()
  202. {
  203. var xs = new[] { 2, 3, 4, 5 }.ToAsyncEnumerable();
  204. var ys = xs.Take(3).Skip(1);
  205. Assert.Equal(2, await ys.CountAsync());
  206. Assert.Equal(3, await ys.FirstAsync());
  207. Assert.Equal(4, await ys.LastAsync());
  208. Assert.Equal(3, await ys.ElementAtAsync(0));
  209. Assert.Equal(4, await ys.ElementAtAsync(1));
  210. Assert.Equal(new[] { 3, 4 }, await ys.ToArrayAsync());
  211. Assert.Equal(new[] { 3, 4 }, await ys.ToListAsync());
  212. }
  213. [Fact]
  214. public async Task Take_IAsyncPartition_IList_Empty_Take()
  215. {
  216. var xs = new int[0].ToAsyncEnumerable();
  217. var ys = xs.Take(2);
  218. Assert.Equal(0, await ys.CountAsync());
  219. await AssertThrowsAsync<InvalidOperationException>(ys.FirstAsync().AsTask());
  220. await AssertThrowsAsync<InvalidOperationException>(ys.LastAsync().AsTask());
  221. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(0).AsTask());
  222. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(1).AsTask());
  223. Assert.Empty(await ys.ToArrayAsync());
  224. Assert.Empty(await ys.ToListAsync());
  225. }
  226. [Fact]
  227. public async Task Take_IAsyncPartition_IList_Empty_TakeSkip()
  228. {
  229. var xs = new[] { 1, 2, 3, 4, 5 }.ToAsyncEnumerable();
  230. var ys = xs.Take(7).Skip(5);
  231. Assert.Equal(0, await ys.CountAsync());
  232. await AssertThrowsAsync<InvalidOperationException>(ys.FirstAsync().AsTask());
  233. await AssertThrowsAsync<InvalidOperationException>(ys.LastAsync().AsTask());
  234. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(0).AsTask());
  235. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(1).AsTask());
  236. Assert.Empty(await ys.ToArrayAsync());
  237. Assert.Empty(await ys.ToListAsync());
  238. }
  239. }
  240. }