Intersect.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 Intersect : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public void Intersect_Null()
  15. {
  16. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Intersect(default, Return42));
  17. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Intersect(Return42, default));
  18. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Intersect(default, Return42, new Eq()));
  19. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Intersect(Return42, default, new Eq()));
  20. }
  21. [Fact]
  22. public async Task Intersect_Simple()
  23. {
  24. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  25. var ys = new[] { 3, 5, 1, 4 }.ToAsyncEnumerable();
  26. var res = xs.Intersect(ys);
  27. var e = res.GetAsyncEnumerator();
  28. await HasNextAsync(e, 1);
  29. await HasNextAsync(e, 3);
  30. await NoNextAsync(e);
  31. }
  32. [Fact]
  33. public async Task Intersect_EqualityComparer()
  34. {
  35. var xs = new[] { 1, 2, -3 }.ToAsyncEnumerable();
  36. var ys = new[] { 3, 5, -1, 4 }.ToAsyncEnumerable();
  37. var res = xs.Intersect(ys, new Eq());
  38. var e = res.GetAsyncEnumerator();
  39. await HasNextAsync(e, 1);
  40. await HasNextAsync(e, -3);
  41. await NoNextAsync(e);
  42. }
  43. [Fact]
  44. public async Task Intersect_SequenceIdentity()
  45. {
  46. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  47. var ys = new[] { 3, 5, 1, 4 }.ToAsyncEnumerable();
  48. var res = xs.Intersect(ys);
  49. await SequenceIdentity(res);
  50. }
  51. private sealed class Eq : IEqualityComparer<int>
  52. {
  53. public bool Equals(int x, int y)
  54. {
  55. return EqualityComparer<int>.Default.Equals(Math.Abs(x), Math.Abs(y));
  56. }
  57. public int GetHashCode(int obj)
  58. {
  59. return EqualityComparer<int>.Default.GetHashCode(Math.Abs(obj));
  60. }
  61. }
  62. }
  63. }