SkipLast.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.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. [Fact]
  54. public void SkipLast_Zero_NoAlias()
  55. {
  56. var xs = Xs();
  57. var ys = xs.SkipLast(0);
  58. Assert.NotSame(xs, ys);
  59. }
  60. private async IAsyncEnumerable<int> Xs()
  61. {
  62. await Task.Yield();
  63. yield return 1;
  64. }
  65. }
  66. }