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 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 : AsyncEnumerableExTests
  12. {
  13. [Fact]
  14. public void Distinct_Null()
  15. {
  16. AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.Distinct<int, int>(Return42, default(Func<int, int>)));
  17. AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.Distinct(default(IAsyncEnumerable<int>), x => x));
  18. AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.Distinct(Return42, default(Func<int, int>)));
  19. AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.Distinct(default(IAsyncEnumerable<int>), x => x, EqualityComparer<int>.Default));
  20. AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.Distinct(Return42, default(Func<int, int>), EqualityComparer<int>.Default));
  21. AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.Distinct(Return42, x => x, default));
  22. }
  23. [Fact]
  24. public void Distinct1()
  25. {
  26. var xs = new[] { 1, 2, 3, 4, 5 }.ToAsyncEnumerable().Distinct(x => x / 2);
  27. var e = xs.GetAsyncEnumerator();
  28. HasNext(e, 1);
  29. HasNext(e, 2);
  30. HasNext(e, 4);
  31. NoNext(e);
  32. }
  33. [Fact]
  34. public async Task Distinct6()
  35. {
  36. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct(k => k);
  37. var res = new[] { 1, 2, 3, 5, 4 };
  38. Assert.True(res.SequenceEqual(await xs.ToArray()));
  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.ToList()));
  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.Count());
  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 void Distinct11()
  61. {
  62. var xs = AsyncEnumerable.Empty<int>().Distinct(k => k);
  63. var e = xs.GetAsyncEnumerator();
  64. NoNext(e);
  65. }
  66. }
  67. }