TakeLast.cs 1.8 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. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.TakeLast(default(IAsyncEnumerable<int>), 5));
  17. }
  18. [Fact]
  19. public void TakeLast0()
  20. {
  21. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().TakeLast(-2);
  22. var e = xs.GetAsyncEnumerator();
  23. NoNext(e);
  24. }
  25. [Fact]
  26. public void TakeLast1()
  27. {
  28. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().TakeLast(2);
  29. var e = xs.GetAsyncEnumerator();
  30. HasNext(e, 3);
  31. HasNext(e, 4);
  32. NoNext(e);
  33. }
  34. [Fact]
  35. public void TakeLast2()
  36. {
  37. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().TakeLast(5);
  38. var e = xs.GetAsyncEnumerator();
  39. HasNext(e, 1);
  40. HasNext(e, 2);
  41. HasNext(e, 3);
  42. HasNext(e, 4);
  43. NoNext(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 void TakeLast_BugFix_TakeLast_Zero_TakesForever()
  53. {
  54. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().TakeLast(0);
  55. var e = xs.GetAsyncEnumerator();
  56. NoNext(e);
  57. }
  58. }
  59. }