Reverse.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.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. int[] expected = [3, 2, 1];
  60. Assert.Equal(expected, await ys.ToArrayAsync());
  61. }
  62. [Fact]
  63. public async Task Reverse6()
  64. {
  65. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  66. var ys = xs.Reverse();
  67. Assert.Equal(new[] { 3, 2, 1 }, await ys.ToListAsync());
  68. }
  69. [Fact]
  70. public async Task Reverse7()
  71. {
  72. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  73. var ys = xs.Reverse();
  74. Assert.Equal(3, await ys.CountAsync());
  75. }
  76. [Fact]
  77. public async Task Reverse8()
  78. {
  79. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  80. var ys = xs.Reverse();
  81. await SequenceIdentity(ys);
  82. }
  83. [Fact]
  84. public async Task Reverse9()
  85. {
  86. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  87. var ys = xs.Reverse().Prepend(4); // to trigger onlyIfCheap
  88. int[] expected = [4, 3, 2, 1];
  89. Assert.Equal(expected, await ys.ToArrayAsync());
  90. }
  91. }
  92. }