Repeat.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 Repeat : AsyncEnumerableExTests
  12. {
  13. [Fact]
  14. public async Task RepeatValue1()
  15. {
  16. var xs = AsyncEnumerableEx.Repeat(2);
  17. var e = xs.GetAsyncEnumerator();
  18. await HasNextAsync(e, 2);
  19. await HasNextAsync(e, 2);
  20. await HasNextAsync(e, 2);
  21. await HasNextAsync(e, 2);
  22. await HasNextAsync(e, 2);
  23. await e.DisposeAsync();
  24. }
  25. [Fact]
  26. public async Task RepeatValue2()
  27. {
  28. var xs = AsyncEnumerableEx.Repeat(2).Take(5);
  29. await SequenceIdentity(xs);
  30. }
  31. [Fact]
  32. public void RepeatSequence_Null()
  33. {
  34. Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.Repeat(default(IAsyncEnumerable<int>)));
  35. Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.Repeat(default(IAsyncEnumerable<int>), 3));
  36. Assert.Throws<ArgumentOutOfRangeException>(() => AsyncEnumerableEx.Repeat(Return42, -1));
  37. }
  38. [Fact]
  39. public async Task RepeatSequence1Async()
  40. {
  41. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Repeat();
  42. var e = xs.GetAsyncEnumerator();
  43. await HasNextAsync(e, 1);
  44. await HasNextAsync(e, 2);
  45. await HasNextAsync(e, 3);
  46. await HasNextAsync(e, 1);
  47. await HasNextAsync(e, 2);
  48. await HasNextAsync(e, 3);
  49. await HasNextAsync(e, 1);
  50. await HasNextAsync(e, 2);
  51. await HasNextAsync(e, 3);
  52. }
  53. [Fact]
  54. public async Task RepeatSequence2Async()
  55. {
  56. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Repeat(3);
  57. var e = xs.GetAsyncEnumerator();
  58. await HasNextAsync(e, 1);
  59. await HasNextAsync(e, 2);
  60. await HasNextAsync(e, 3);
  61. await HasNextAsync(e, 1);
  62. await HasNextAsync(e, 2);
  63. await HasNextAsync(e, 3);
  64. await HasNextAsync(e, 1);
  65. await HasNextAsync(e, 2);
  66. await HasNextAsync(e, 3);
  67. await NoNextAsync(e);
  68. }
  69. [Fact]
  70. public async Task RepeatSequence3Async()
  71. {
  72. var i = 0;
  73. var xs = RepeatXs(() => i++).ToAsyncEnumerable().Repeat(3);
  74. var e = xs.GetAsyncEnumerator();
  75. await HasNextAsync(e, 1);
  76. await HasNextAsync(e, 2);
  77. await HasNextAsync(e, 1);
  78. await HasNextAsync(e, 2);
  79. await HasNextAsync(e, 1);
  80. await HasNextAsync(e, 2);
  81. await NoNextAsync(e);
  82. Assert.Equal(3, i);
  83. }
  84. [Fact]
  85. public async Task RepeatSequence4Async()
  86. {
  87. var i = 0;
  88. var xs = RepeatXs(() => i++).ToAsyncEnumerable().Repeat(0);
  89. var e = xs.GetAsyncEnumerator();
  90. await NoNextAsync(e);
  91. }
  92. [Fact]
  93. public async Task RepeatSequence5()
  94. {
  95. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Repeat(3);
  96. await SequenceIdentity(xs);
  97. }
  98. [Fact]
  99. public async Task RepeatSequence6Async()
  100. {
  101. var xs = new FailRepeat().ToAsyncEnumerable().Repeat();
  102. var e = xs.GetAsyncEnumerator();
  103. await AssertThrowsAsync<NotImplementedException>(e.MoveNextAsync().AsTask());
  104. }
  105. [Fact]
  106. public async Task RepeatSequence7Async()
  107. {
  108. var xs = new FailRepeat().ToAsyncEnumerable().Repeat(3);
  109. var e = xs.GetAsyncEnumerator();
  110. await AssertThrowsAsync<NotImplementedException>(e.MoveNextAsync().AsTask());
  111. }
  112. private static IEnumerable<int> RepeatXs(Action started)
  113. {
  114. started();
  115. yield return 1;
  116. yield return 2;
  117. }
  118. private sealed class FailRepeat : IEnumerable<int>
  119. {
  120. public IEnumerator<int> GetEnumerator()
  121. {
  122. throw new NotImplementedException();
  123. }
  124. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  125. {
  126. throw new NotImplementedException();
  127. }
  128. }
  129. }
  130. }