SequenceEqual.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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;
  8. using System.Threading.Tasks;
  9. using Xunit;
  10. namespace Tests
  11. {
  12. public class SequenceEqual : AsyncEnumerableTests
  13. {
  14. [Fact]
  15. public async Task SequenceEqualAsync_Null()
  16. {
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.SequenceEqualAsync(default, Return42).AsTask());
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.SequenceEqualAsync(Return42, default).AsTask());
  19. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.SequenceEqualAsync(default, Return42, new Eq()).AsTask());
  20. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.SequenceEqualAsync(Return42, default, new Eq()).AsTask());
  21. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.SequenceEqualAsync(default, Return42, CancellationToken.None).AsTask());
  22. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.SequenceEqualAsync(Return42, default, CancellationToken.None).AsTask());
  23. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.SequenceEqualAsync(default, Return42, new Eq(), CancellationToken.None).AsTask());
  24. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerable.SequenceEqualAsync(Return42, default, new Eq(), CancellationToken.None).AsTask());
  25. }
  26. [Fact]
  27. public async Task SequenceEqualAsync_Same()
  28. {
  29. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Select(x => x);
  30. var res = xs.SequenceEqualAsync(xs);
  31. Assert.True(await res);
  32. }
  33. [Fact]
  34. public async Task SequenceEqualAsync_Same_IList()
  35. {
  36. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  37. var res = xs.SequenceEqualAsync(xs);
  38. Assert.True(await res);
  39. }
  40. [Fact]
  41. public async Task SequenceEqualAsync_Same_Empty()
  42. {
  43. var xs = AsyncEnumerable.Empty<int>();
  44. var res = xs.SequenceEqualAsync(xs);
  45. Assert.True(await res);
  46. }
  47. [Fact]
  48. public async Task SequenceEqualAsync_NotSame()
  49. {
  50. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Select(x => x);
  51. var ys = new[] { 1, 3, 2 }.ToAsyncEnumerable().Select(x => x);
  52. var res = xs.SequenceEqualAsync(ys);
  53. Assert.False(await res);
  54. }
  55. [Fact]
  56. public async Task SequenceEqualAsync_NotSame_IList()
  57. {
  58. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  59. var ys = new[] { 1, 3, 2 }.ToAsyncEnumerable();
  60. var res = xs.SequenceEqualAsync(ys);
  61. Assert.False(await res);
  62. }
  63. [Fact]
  64. public async Task SequenceEqualAsync_NotSame_DifferentLength()
  65. {
  66. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  67. var ys = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  68. var res = xs.SequenceEqualAsync(ys);
  69. Assert.False(await res);
  70. }
  71. [Fact]
  72. public async Task SequenceEqualAsync_NotSame_DifferentLength_IList()
  73. {
  74. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Select(x => x);
  75. var ys = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().Select(x => x);
  76. var res = xs.SequenceEqualAsync(ys);
  77. Assert.False(await res);
  78. }
  79. [Fact]
  80. public async Task SequenceEqualAsync_Throws_Second()
  81. {
  82. var ex = new Exception("Bang!");
  83. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  84. var ys = Throw<int>(ex);
  85. var res = xs.SequenceEqualAsync(ys);
  86. await AssertThrowsAsync(res, ex);
  87. }
  88. [Fact]
  89. public async Task SequenceEqualAsync_Throws_First()
  90. {
  91. var ex = new Exception("Bang!");
  92. var xs = Throw<int>(ex);
  93. var ys = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  94. var res = xs.SequenceEqualAsync(ys);
  95. await AssertThrowsAsync(res, ex);
  96. }
  97. [Fact]
  98. public async Task SequenceEqualAsync_Comparer_Empty()
  99. {
  100. var xs = AsyncEnumerable.Empty<int>();
  101. var res = xs.SequenceEqualAsync(xs, new Eq());
  102. Assert.True(await res);
  103. }
  104. [Fact]
  105. public async Task SequenceEqualAsync_Comparer_Same1()
  106. {
  107. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  108. var res = xs.SequenceEqualAsync(xs, new Eq());
  109. Assert.True(await res);
  110. }
  111. [Fact]
  112. public async Task SequenceEqualAsync_Comparer_Same2()
  113. {
  114. var xs = new[] { 1, 2, -3, 4 }.ToAsyncEnumerable().Select(x => x);
  115. var ys = new[] { 1, -2, 3, 4 }.ToAsyncEnumerable().Select(x => x);
  116. var res = xs.SequenceEqualAsync(ys, new Eq());
  117. Assert.True(await res);
  118. }
  119. [Fact]
  120. public async Task SequenceEqualAsync_Comparer_NotSame()
  121. {
  122. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Select(x => x);
  123. var ys = new[] { 1, 3, 2 }.ToAsyncEnumerable().Select(x => x);
  124. var res = xs.SequenceEqualAsync(ys, new Eq());
  125. Assert.False(await res);
  126. }
  127. [Fact]
  128. public async Task SequenceEqualAsync_Comparer_NotSame_IList()
  129. {
  130. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  131. var ys = new[] { 1, 3, 2 }.ToAsyncEnumerable();
  132. var res = xs.SequenceEqualAsync(ys, new Eq());
  133. Assert.False(await res);
  134. }
  135. [Fact]
  136. public async Task SequenceEqualAsync_Comparer_NotSame_DifferentLength()
  137. {
  138. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable().Select(x => x);
  139. var ys = new[] { 1, 2, 3 }.ToAsyncEnumerable().Select(x => x);
  140. var res = xs.SequenceEqualAsync(ys, new Eq());
  141. Assert.False(await res);
  142. }
  143. [Fact]
  144. public async Task SequenceEqualAsync_Comparer_NotSame_DifferentLength_IList()
  145. {
  146. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  147. var ys = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  148. var res = xs.SequenceEqualAsync(ys, new Eq());
  149. Assert.False(await res);
  150. }
  151. [Fact]
  152. public async Task SequenceEqualAsync_Comparer_Throws_Second()
  153. {
  154. var ex = new Exception("Bang!");
  155. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  156. var ys = Throw<int>(ex);
  157. var res = xs.SequenceEqualAsync(ys, new Eq());
  158. await AssertThrowsAsync(res, ex);
  159. }
  160. [Fact]
  161. public async Task SequenceEqualAsync_Comparer_Throws_First()
  162. {
  163. var ex = new Exception("Bang!");
  164. var xs = Throw<int>(ex);
  165. var ys = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  166. var res = xs.SequenceEqualAsync(ys, new Eq());
  167. await AssertThrowsAsync(res, ex);
  168. }
  169. [Fact]
  170. public async Task SequenceEqualAsync_Comparer_Same_IList()
  171. {
  172. var xs = new[] { 1, 2, -3, 4 }.ToAsyncEnumerable();
  173. var ys = new[] { 1, -2, 3, 4 }.ToAsyncEnumerable();
  174. var res = xs.SequenceEqualAsync(ys, new Eq());
  175. Assert.True(await res);
  176. }
  177. [Fact]
  178. public async Task SequenceEqualAsync_Comparer_Throws_Equals_IList()
  179. {
  180. var xs = new[] { 1, 2, -3, 4 }.ToAsyncEnumerable();
  181. var ys = new[] { 1, -2, 3, 4 }.ToAsyncEnumerable();
  182. await Assert.ThrowsAsync<NotImplementedException>(() => xs.SequenceEqualAsync(ys, new EqEx()).AsTask());
  183. }
  184. [Fact]
  185. public async Task SequenceEqualAsync_Comparer_Throws_Equals()
  186. {
  187. var xs = new[] { 1, 2, -3, 4 }.Select(x => x * 2).ToAsyncEnumerable();
  188. var ys = new[] { 1, -2, 3, 4 }.Select(x => x * 2).ToAsyncEnumerable();
  189. var res = xs.SequenceEqualAsync(ys, new EqEx());
  190. await AssertThrowsAsync<NotImplementedException>(res.AsTask());
  191. }
  192. private sealed class EqEx : IEqualityComparer<int>
  193. {
  194. public bool Equals(int x, int y)
  195. {
  196. throw new NotImplementedException();
  197. }
  198. public int GetHashCode(int obj)
  199. {
  200. throw new NotSupportedException();
  201. }
  202. }
  203. private sealed class Eq : IEqualityComparer<int>
  204. {
  205. public bool Equals(int x, int y)
  206. {
  207. return EqualityComparer<int>.Default.Equals(Math.Abs(x), Math.Abs(y));
  208. }
  209. public int GetHashCode(int obj)
  210. {
  211. return EqualityComparer<int>.Default.GetHashCode(Math.Abs(obj));
  212. }
  213. }
  214. }
  215. }