Distinct.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. 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 res = new[] { 1, 2, 3, 5, 4 };
  37. Assert.True(res.SequenceEqual(await xs.ToArrayAsync()));
  38. }
  39. [Fact]
  40. public async Task Distinct7()
  41. {
  42. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct(k => k);
  43. var res = new List<int> { 1, 2, 3, 5, 4 };
  44. Assert.True(res.SequenceEqual(await xs.ToListAsync()));
  45. }
  46. [Fact]
  47. public async Task Distinct8()
  48. {
  49. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct(k => k);
  50. Assert.Equal(5, await xs.CountAsync());
  51. }
  52. [Fact]
  53. public async Task Distinct9()
  54. {
  55. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct(k => k);
  56. await SequenceIdentity(xs);
  57. }
  58. [Fact]
  59. public async Task Distinct11Async()
  60. {
  61. var xs = AsyncEnumerable.Empty<int>().Distinct(k => k);
  62. var e = xs.GetAsyncEnumerator();
  63. await NoNextAsync(e);
  64. }
  65. }
  66. }