Union.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 Xunit;
  8. namespace Tests
  9. {
  10. public class Union : AsyncEnumerableTests
  11. {
  12. [Fact]
  13. public void Union_Null()
  14. {
  15. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Union<int>(null, AsyncEnumerable.Return(42)));
  16. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Union<int>(AsyncEnumerable.Return(42), null));
  17. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Union<int>(null, AsyncEnumerable.Return(42), new Eq()));
  18. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Union<int>(AsyncEnumerable.Return(42), null, new Eq()));
  19. AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Union<int>(AsyncEnumerable.Return(42), AsyncEnumerable.Return(42), null));
  20. }
  21. [Fact]
  22. public void Union1()
  23. {
  24. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  25. var ys = new[] { 3, 5, 1, 4 }.ToAsyncEnumerable();
  26. var res = xs.Union(ys);
  27. var e = res.GetAsyncEnumerator();
  28. HasNext(e, 1);
  29. HasNext(e, 2);
  30. HasNext(e, 3);
  31. HasNext(e, 5);
  32. HasNext(e, 4);
  33. NoNext(e);
  34. }
  35. [Fact]
  36. public void Union2()
  37. {
  38. var xs = new[] { 1, 2, -3 }.ToAsyncEnumerable();
  39. var ys = new[] { 3, 5, -1, 4 }.ToAsyncEnumerable();
  40. var res = xs.Union(ys, new Eq());
  41. var e = res.GetAsyncEnumerator();
  42. HasNext(e, 1);
  43. HasNext(e, 2);
  44. HasNext(e, -3);
  45. HasNext(e, 5);
  46. HasNext(e, 4);
  47. NoNext(e);
  48. }
  49. private sealed class Eq : IEqualityComparer<int>
  50. {
  51. public bool Equals(int x, int y)
  52. {
  53. return EqualityComparer<int>.Default.Equals(Math.Abs(x), Math.Abs(y));
  54. }
  55. public int GetHashCode(int obj)
  56. {
  57. return EqualityComparer<int>.Default.GetHashCode(Math.Abs(obj));
  58. }
  59. }
  60. }
  61. }