Distinct.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 Distinct : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public void Distinct_Null()
  15. {
  16. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Distinct<int>(default));
  17. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.Distinct(default, EqualityComparer<int>.Default));
  18. }
  19. [Fact]
  20. public async Task Distinct1()
  21. {
  22. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct();
  23. var e = xs.GetAsyncEnumerator();
  24. await HasNextAsync(e, 1);
  25. await HasNextAsync(e, 2);
  26. await HasNextAsync(e, 3);
  27. await HasNextAsync(e, 5);
  28. await HasNextAsync(e, 4);
  29. await NoNextAsync(e);
  30. }
  31. [Fact]
  32. public async Task Distinct2()
  33. {
  34. var xs = new[] { 1, -2, -1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct(new Eq());
  35. var e = xs.GetAsyncEnumerator();
  36. await HasNextAsync(e, 1);
  37. await HasNextAsync(e, -2);
  38. await HasNextAsync(e, 3);
  39. await HasNextAsync(e, 5);
  40. await HasNextAsync(e, 4);
  41. await NoNextAsync(e);
  42. }
  43. [Fact]
  44. public async Task Distinct3()
  45. {
  46. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct();
  47. var res = new[] { 1, 2, 3, 5, 4 };
  48. Assert.True(res.SequenceEqual(await xs.ToArrayAsync()));
  49. }
  50. [Fact]
  51. public async Task Distinct4()
  52. {
  53. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct();
  54. var res = new List<int> { 1, 2, 3, 5, 4 };
  55. Assert.True(res.SequenceEqual(await xs.ToListAsync()));
  56. }
  57. [Fact]
  58. public async Task Distinct5()
  59. {
  60. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct();
  61. Assert.Equal(5, await xs.CountAsync());
  62. }
  63. [Fact]
  64. public async Task Distinct10()
  65. {
  66. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct();
  67. await SequenceIdentity(xs);
  68. }
  69. [Fact]
  70. public async Task Distinct12()
  71. {
  72. var xs = AsyncEnumerable.Empty<int>().Distinct();
  73. var e = xs.GetAsyncEnumerator();
  74. await NoNextAsync(e);
  75. }
  76. private sealed class Eq : IEqualityComparer<int>
  77. {
  78. public bool Equals(int x, int y)
  79. {
  80. return EqualityComparer<int>.Default.Equals(Math.Abs(x), Math.Abs(y));
  81. }
  82. public int GetHashCode(int obj)
  83. {
  84. return EqualityComparer<int>.Default.GetHashCode(Math.Abs(obj));
  85. }
  86. }
  87. }
  88. }