TakeLast.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 isSet = false;
  55. new int[] { 1, 2, 3, 4 }.ToAsyncEnumerable()
  56. .TakeLast(0)
  57. .ForEachAsync(_ => { isSet = true; })
  58. .Wait(WaitTimeoutMs);
  59. Assert.False(isSet);
  60. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().TakeLast(0);
  61. var e = xs.GetAsyncEnumerator();
  62. NoNext(e);
  63. }
  64. }
  65. }