Reverse.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.Linq;
  6. using System.Threading.Tasks;
  7. using Xunit;
  8. namespace Tests
  9. {
  10. public class Reverse : AsyncEnumerableTests
  11. {
  12. [Fact]
  13. public void Reverse_Null()
  14. {
  15. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Reverse<int>(default));
  16. }
  17. [Fact]
  18. public async Task Reverse1Async()
  19. {
  20. var xs = AsyncEnumerable.Empty<int>();
  21. var ys = xs.Reverse();
  22. var e = ys.GetAsyncEnumerator();
  23. await NoNextAsync(e);
  24. }
  25. [Fact]
  26. public async Task Reverse2Async()
  27. {
  28. var xs = Return42;
  29. var ys = xs.Reverse();
  30. var e = ys.GetAsyncEnumerator();
  31. await HasNextAsync(e, 42);
  32. await NoNextAsync(e);
  33. }
  34. [Fact]
  35. public async Task Reverse3Async()
  36. {
  37. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  38. var ys = xs.Reverse();
  39. var e = ys.GetAsyncEnumerator();
  40. await HasNextAsync(e, 3);
  41. await HasNextAsync(e, 2);
  42. await HasNextAsync(e, 1);
  43. await NoNextAsync(e);
  44. }
  45. [Fact]
  46. public async Task Reverse4Async()
  47. {
  48. var ex = new Exception("Bang!");
  49. var xs = Throw<int>(ex);
  50. var ys = xs.Reverse();
  51. var e = ys.GetAsyncEnumerator();
  52. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  53. }
  54. [Fact]
  55. public async Task Reverse5()
  56. {
  57. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  58. var ys = xs.Reverse();
  59. Assert.Equal(new[] { 3, 2, 1 }, await ys.ToArray());
  60. }
  61. [Fact]
  62. public async Task Reverse6()
  63. {
  64. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  65. var ys = xs.Reverse();
  66. Assert.Equal(new[] { 3, 2, 1 }, await ys.ToList());
  67. }
  68. [Fact]
  69. public async Task Reverse7()
  70. {
  71. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  72. var ys = xs.Reverse();
  73. Assert.Equal(3, await ys.Count());
  74. }
  75. [Fact]
  76. public async Task Reverse8()
  77. {
  78. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  79. var ys = xs.Reverse();
  80. await SequenceIdentity(ys);
  81. }
  82. [Fact]
  83. public async Task Reverse9()
  84. {
  85. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  86. var ys = xs.Reverse().Prepend(4); // to trigger onlyIfCheap
  87. Assert.Equal(new[] { 4, 3, 2, 1 }, await ys.ToArray());
  88. }
  89. }
  90. }