Except.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Except(default, Return42));
  17. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Except(Return42, default));
  18. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Except(default, Return42, new Eq()));
  19. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Except(Return42, null, new Eq()));
  20. }
  21. [Fact]
  22. public async Task Except_Simple()
  23. {
  24. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  25. var ys = new[] { 3, 5, 1, 4 }.ToAsyncEnumerable();
  26. var res = xs.Except(ys);
  27. var e = res.GetAsyncEnumerator();
  28. await HasNextAsync(e, 2);
  29. await NoNextAsync(e);
  30. }
  31. [Fact]
  32. public async Task Except_EqualityComparer()
  33. {
  34. var xs = new[] { 1, 2, -3 }.ToAsyncEnumerable();
  35. var ys = new[] { 3, 5, -1, 4 }.ToAsyncEnumerable();
  36. var res = xs.Except(ys, new Eq());
  37. var e = res.GetAsyncEnumerator();
  38. await HasNextAsync(e, 2);
  39. await NoNextAsync(e);
  40. }
  41. [Fact]
  42. public async Task Except_SequenceIdentity()
  43. {
  44. var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
  45. var ys = new[] { 3, 5, 1, 4 }.ToAsyncEnumerable();
  46. var res = xs.Except(ys);
  47. await SequenceIdentity(res);
  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. }