Skip.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 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. Assert.Equal(2, await ys.CountAsync());
  99. Assert.Equal(3, await ys.FirstAsync());
  100. Assert.Equal(4, await ys.LastAsync());
  101. Assert.Equal(3, await ys.ElementAtAsync(0));
  102. Assert.Equal(4, await ys.ElementAtAsync(1));
  103. Assert.Equal(new[] { 3, 4 }, await ys.ToArrayAsync());
  104. Assert.Equal(new[] { 3, 4 }, await ys.ToListAsync());
  105. }
  106. [Fact]
  107. public async Task Skip_IAsyncPartition_NonEmpty_SkipSkip()
  108. {
  109. var xs = new[] { -2, -1, 0, 1, 2, 3, 4 }.ToAsyncEnumerable().Where(x => true);
  110. var ys = xs.Skip(2).Skip(3);
  111. Assert.Equal(2, await ys.CountAsync());
  112. Assert.Equal(3, await ys.FirstAsync());
  113. Assert.Equal(4, await ys.LastAsync());
  114. Assert.Equal(3, await ys.ElementAtAsync(0));
  115. Assert.Equal(4, await ys.ElementAtAsync(1));
  116. Assert.Equal(new[] { 3, 4 }, await ys.ToArrayAsync());
  117. Assert.Equal(new[] { 3, 4 }, await ys.ToListAsync());
  118. }
  119. [Fact]
  120. public async Task Skip_IAsyncPartition_NonEmpty_SkipTake()
  121. {
  122. var xs = new[] { 2, 3, 4, 5 }.ToAsyncEnumerable().Where(x => true);
  123. var ys = xs.Skip(1).Take(2);
  124. Assert.Equal(2, await ys.CountAsync());
  125. Assert.Equal(3, await ys.FirstAsync());
  126. Assert.Equal(4, await ys.LastAsync());
  127. Assert.Equal(3, await ys.ElementAtAsync(0));
  128. Assert.Equal(4, await ys.ElementAtAsync(1));
  129. Assert.Equal(new[] { 3, 4 }, await ys.ToArrayAsync());
  130. Assert.Equal(new[] { 3, 4 }, await ys.ToListAsync());
  131. }
  132. [Fact]
  133. public async Task Skip_IAsyncPartition_Empty_Skip()
  134. {
  135. var xs = new int[0].ToAsyncEnumerable().Where(x => true);
  136. var ys = xs.Skip(2);
  137. Assert.Equal(0, await ys.CountAsync());
  138. await AssertThrowsAsync<InvalidOperationException>(ys.FirstAsync().AsTask());
  139. await AssertThrowsAsync<InvalidOperationException>(ys.LastAsync().AsTask());
  140. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(0).AsTask());
  141. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(1).AsTask());
  142. Assert.Empty(await ys.ToArrayAsync());
  143. Assert.Empty(await ys.ToListAsync());
  144. }
  145. [Fact]
  146. public async Task Skip_IAsyncPartition_Empty_SkipSkip()
  147. {
  148. var xs = new[] { 1, 2 }.ToAsyncEnumerable().Where(x => true);
  149. var ys = xs.Skip(2);
  150. Assert.Equal(0, await ys.CountAsync());
  151. await AssertThrowsAsync<InvalidOperationException>(ys.FirstAsync().AsTask());
  152. await AssertThrowsAsync<InvalidOperationException>(ys.LastAsync().AsTask());
  153. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(0).AsTask());
  154. await AssertThrowsAsync<ArgumentOutOfRangeException>(ys.ElementAtAsync(1).AsTask());
  155. Assert.Empty(await ys.ToArrayAsync());
  156. Assert.Empty(await ys.ToListAsync());
  157. }
  158. }
  159. }