TakeLast.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 TakeLast : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public void TakeLast_Null()
  15. {
  16. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.TakeLast(default(IAsyncEnumerable<int>), 5));
  17. }
  18. [Fact]
  19. public async Task TakeLast0()
  20. {
  21. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().TakeLast(-2);
  22. var e = xs.GetAsyncEnumerator();
  23. await NoNextAsync(e);
  24. }
  25. [Fact]
  26. public async Task TakeLast1()
  27. {
  28. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().TakeLast(2);
  29. var e = xs.GetAsyncEnumerator();
  30. await HasNextAsync(e, 3);
  31. await HasNextAsync(e, 4);
  32. await NoNextAsync(e);
  33. }
  34. [Fact]
  35. public async Task TakeLast2()
  36. {
  37. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().TakeLast(5);
  38. var e = xs.GetAsyncEnumerator();
  39. await HasNextAsync(e, 1);
  40. await HasNextAsync(e, 2);
  41. await HasNextAsync(e, 3);
  42. await HasNextAsync(e, 4);
  43. await NoNextAsync(e);
  44. }
  45. [Fact]
  46. public async Task TakeLast3()
  47. {
  48. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().TakeLast(2);
  49. await SequenceIdentity(xs);
  50. }
  51. [Fact]
  52. public async Task TakeLast_BugFix_TakeLast_Zero_TakesForever()
  53. {
  54. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().TakeLast(0);
  55. var e = xs.GetAsyncEnumerator();
  56. await NoNextAsync(e);
  57. }
  58. }
  59. }