Distinct.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 : AsyncEnumerableExTests
  12. {
  13. [Fact]
  14. public void Distinct_Null()
  15. {
  16. Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.Distinct<int, int>(Return42, default(Func<int, int>)));
  17. Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.Distinct(default(IAsyncEnumerable<int>), x => x));
  18. Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.Distinct(Return42, default(Func<int, int>)));
  19. Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.Distinct(default(IAsyncEnumerable<int>), x => x, EqualityComparer<int>.Default));
  20. Assert.Throws<ArgumentNullException>(() => AsyncEnumerableEx.Distinct(Return42, default(Func<int, int>), EqualityComparer<int>.Default));
  21. }
  22. [Fact]
  23. public async Task Distinct1Async()
  24. {
  25. var xs = new[] { 1, 2, 3, 4, 5 }.ToAsyncEnumerable().Distinct(x => x / 2);
  26. var e = xs.GetAsyncEnumerator();
  27. await HasNextAsync(e, 1);
  28. await HasNextAsync(e, 2);
  29. await HasNextAsync(e, 4);
  30. await NoNextAsync(e);
  31. }
  32. [Fact]
  33. public async Task Distinct6()
  34. {
  35. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct(k => k);
  36. var expected = new[] { 1, 2, 3, 5, 4 };
  37. var actual = await xs.ToArrayAsync();
  38. Assert.True(expected.SequenceEqual(actual));
  39. }
  40. [Fact]
  41. public async Task Distinct7()
  42. {
  43. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct(k => k);
  44. var res = new List<int> { 1, 2, 3, 5, 4 };
  45. Assert.True(res.SequenceEqual(await xs.ToListAsync()));
  46. }
  47. [Fact]
  48. public async Task Distinct8()
  49. {
  50. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct(k => k);
  51. Assert.Equal(5, await xs.CountAsync());
  52. }
  53. [Fact]
  54. public async Task Distinct9()
  55. {
  56. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct(k => k);
  57. await SequenceIdentity(xs);
  58. }
  59. [Fact]
  60. public async Task Distinct11Async()
  61. {
  62. var xs = AsyncEnumerable.Empty<int>().Distinct(k => k);
  63. var e = xs.GetAsyncEnumerator();
  64. await NoNextAsync(e);
  65. }
  66. }
  67. }