Except.cs 2.3 KB

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