SkipLast.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Xunit;
  9. namespace Tests
  10. {
  11. public class SkipLast : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public void SkipLast_Null()
  15. {
  16. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.SkipLast(default(IAsyncEnumerable<int>), 5));
  17. }
  18. [Fact]
  19. public async Task SkipLast_Few()
  20. {
  21. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().SkipLast(2);
  22. var e = xs.GetAsyncEnumerator();
  23. await HasNextAsync(e, 1);
  24. await HasNextAsync(e, 2);
  25. await NoNextAsync(e);
  26. }
  27. [Fact]
  28. public async Task SkipLast_All()
  29. {
  30. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().SkipLast(5);
  31. var e = xs.GetAsyncEnumerator();
  32. await NoNextAsync(e);
  33. }
  34. [Fact]
  35. public async Task SkipLast_SequenceIdentity()
  36. {
  37. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().SkipLast(2);
  38. await SequenceIdentity(xs);
  39. }
  40. [Fact]
  41. public async Task SkipLast_Zero()
  42. {
  43. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  44. var ys = xs.SkipLast(0);
  45. Assert.Same(xs, ys);
  46. var e = ys.GetAsyncEnumerator();
  47. await HasNextAsync(e, 1);
  48. await HasNextAsync(e, 2);
  49. await HasNextAsync(e, 3);
  50. await HasNextAsync(e, 4);
  51. await NoNextAsync(e);
  52. }
  53. #if USE_ASYNC_ITERATOR
  54. [Fact]
  55. public void SkipLast_Zero_NoAlias()
  56. {
  57. var xs = Xs();
  58. var ys = xs.SkipLast(0);
  59. Assert.NotSame(xs, ys);
  60. }
  61. private async IAsyncEnumerable<int> Xs()
  62. {
  63. await Task.Yield();
  64. yield return 1;
  65. }
  66. #endif
  67. }
  68. }