Repeat.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 Repeat : AsyncEnumerableExTests
  12. {
  13. [Fact]
  14. public void Repeat_Null()
  15. {
  16. AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.Repeat(default(IAsyncEnumerable<int>)));
  17. AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.Repeat(default(IAsyncEnumerable<int>), 3));
  18. AssertThrows<ArgumentOutOfRangeException>(() => AsyncEnumerableEx.Repeat(AsyncEnumerable.Return(42), -1));
  19. }
  20. [Fact]
  21. public void Repeat1()
  22. {
  23. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Repeat();
  24. var e = xs.GetAsyncEnumerator();
  25. HasNext(e, 1);
  26. HasNext(e, 2);
  27. HasNext(e, 3);
  28. HasNext(e, 1);
  29. HasNext(e, 2);
  30. HasNext(e, 3);
  31. HasNext(e, 1);
  32. HasNext(e, 2);
  33. HasNext(e, 3);
  34. }
  35. [Fact]
  36. public void Repeat2()
  37. {
  38. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Repeat(3);
  39. var e = xs.GetAsyncEnumerator();
  40. HasNext(e, 1);
  41. HasNext(e, 2);
  42. HasNext(e, 3);
  43. HasNext(e, 1);
  44. HasNext(e, 2);
  45. HasNext(e, 3);
  46. HasNext(e, 1);
  47. HasNext(e, 2);
  48. HasNext(e, 3);
  49. NoNext(e);
  50. }
  51. [Fact]
  52. public void Repeat3()
  53. {
  54. var i = 0;
  55. var xs = RepeatXs(() => i++).ToAsyncEnumerable().Repeat(3);
  56. var e = xs.GetAsyncEnumerator();
  57. HasNext(e, 1);
  58. HasNext(e, 2);
  59. HasNext(e, 1);
  60. HasNext(e, 2);
  61. HasNext(e, 1);
  62. HasNext(e, 2);
  63. NoNext(e);
  64. Assert.Equal(3, i);
  65. }
  66. [Fact]
  67. public void Repeat0()
  68. {
  69. var i = 0;
  70. var xs = RepeatXs(() => i++).ToAsyncEnumerable().Repeat(0);
  71. var e = xs.GetAsyncEnumerator();
  72. NoNext(e);
  73. }
  74. [Fact]
  75. public async Task Repeat6()
  76. {
  77. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Repeat(3);
  78. await SequenceIdentity(xs);
  79. }
  80. static IEnumerable<int> RepeatXs(Action started)
  81. {
  82. started();
  83. yield return 1;
  84. yield return 2;
  85. }
  86. [Fact]
  87. public void Repeat4()
  88. {
  89. var xs = new FailRepeat().ToAsyncEnumerable().Repeat();
  90. var e = xs.GetAsyncEnumerator();
  91. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is NotImplementedException);
  92. }
  93. [Fact]
  94. public void Repeat5()
  95. {
  96. var xs = new FailRepeat().ToAsyncEnumerable().Repeat(3);
  97. var e = xs.GetAsyncEnumerator();
  98. AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is NotImplementedException);
  99. }
  100. private sealed class FailRepeat : IEnumerable<int>
  101. {
  102. public IEnumerator<int> GetEnumerator()
  103. {
  104. throw new NotImplementedException();
  105. }
  106. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  107. {
  108. throw new NotImplementedException();
  109. }
  110. }
  111. }
  112. }