Distinct.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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 Distinct_Simple()
  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 Distinct_Comparer()
  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 Distinct_ToArray()
  45. {
  46. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct();
  47. var expected = new[] { 1, 2, 3, 5, 4 };
  48. var actual = await xs.ToArrayAsync();
  49. Assert.True(expected.SequenceEqual(actual));
  50. }
  51. [Fact]
  52. public async Task Distinct_ToList()
  53. {
  54. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct();
  55. var res = new List<int> { 1, 2, 3, 5, 4 };
  56. Assert.True(res.SequenceEqual(await xs.ToListAsync()));
  57. }
  58. [Fact]
  59. public async Task Distinct_Count()
  60. {
  61. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct();
  62. Assert.Equal(5, await xs.CountAsync());
  63. }
  64. [Fact]
  65. public async Task Distinct_SequenceIdentity()
  66. {
  67. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct();
  68. await SequenceIdentity(xs);
  69. }
  70. [Fact]
  71. public async Task Distinct_Empty()
  72. {
  73. var xs = AsyncEnumerable.Empty<int>().Distinct();
  74. var e = xs.GetAsyncEnumerator();
  75. await NoNextAsync(e);
  76. }
  77. private sealed class Eq : IEqualityComparer<int>
  78. {
  79. public bool Equals(int x, int y)
  80. {
  81. return EqualityComparer<int>.Default.Equals(Math.Abs(x), Math.Abs(y));
  82. }
  83. public int GetHashCode(int obj)
  84. {
  85. return EqualityComparer<int>.Default.GetHashCode(Math.Abs(obj));
  86. }
  87. }
  88. }
  89. }