Reverse.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 Reverse : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public void Reverse_Null()
  15. {
  16. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Reverse<int>(default(IAsyncEnumerable<int>)));
  17. }
  18. [Fact]
  19. public void Reverse1()
  20. {
  21. var xs = AsyncEnumerable.Empty<int>();
  22. var ys = xs.Reverse();
  23. var e = ys.GetAsyncEnumerator();
  24. NoNext(e);
  25. }
  26. [Fact]
  27. public void Reverse2()
  28. {
  29. var xs = Return42;
  30. var ys = xs.Reverse();
  31. var e = ys.GetAsyncEnumerator();
  32. HasNext(e, 42);
  33. NoNext(e);
  34. }
  35. [Fact]
  36. public void Reverse3()
  37. {
  38. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  39. var ys = xs.Reverse();
  40. var e = ys.GetAsyncEnumerator();
  41. HasNext(e, 3);
  42. HasNext(e, 2);
  43. HasNext(e, 1);
  44. NoNext(e);
  45. }
  46. [Fact]
  47. public void Reverse4()
  48. {
  49. var ex = new Exception("Bang!");
  50. var xs = Throw<int>(ex);
  51. var ys = xs.Reverse();
  52. var e = ys.GetAsyncEnumerator();
  53. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), SingleInnerExceptionMatches(ex));
  54. }
  55. [Fact]
  56. public async Task Reverse5()
  57. {
  58. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  59. var ys = xs.Reverse();
  60. Assert.Equal(new[] { 3, 2, 1 }, await ys.ToArray());
  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.ToList());
  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.Count());
  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. Assert.Equal(new[] { 4, 3, 2, 1 }, await ys.ToArray());
  89. }
  90. }
  91. }