Intersect.cs 2.4 KB

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