TakeLast.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 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 TakeLast_Negative()
  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 TakeLast_Positive()
  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 TakeLast_TooMany()
  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 TakeLast_BreakEarly_Take()
  47. {
  48. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().TakeLast(3).Take(2);
  49. var e = xs.GetAsyncEnumerator();
  50. await HasNextAsync(e, 2);
  51. await HasNextAsync(e, 3);
  52. await NoNextAsync(e);
  53. }
  54. [Fact]
  55. public async Task TakeLast_SequenceIdentity()
  56. {
  57. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().TakeLast(2);
  58. await SequenceIdentity(xs);
  59. }
  60. [Fact]
  61. public async Task TakeLast_BugFix_TakeLast_Zero_TakesForever()
  62. {
  63. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().TakeLast(0);
  64. var e = xs.GetAsyncEnumerator();
  65. await NoNextAsync(e);
  66. }
  67. }
  68. }