Distinct.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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>(AsyncEnumerable.Return(42), (Func<int, int>)null));
  17. }
  18. [Fact]
  19. public async Task Distinct6()
  20. {
  21. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct(k => k);
  22. var res = new[] { 1, 2, 3, 5, 4 };
  23. Assert.True(res.SequenceEqual(await xs.ToArray()));
  24. }
  25. [Fact]
  26. public async Task Distinct7()
  27. {
  28. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct(k => k);
  29. var res = new List<int> { 1, 2, 3, 5, 4 };
  30. Assert.True(res.SequenceEqual(await xs.ToList()));
  31. }
  32. [Fact]
  33. public async Task Distinct8()
  34. {
  35. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct(k => k);
  36. Assert.Equal(5, await xs.Count());
  37. }
  38. [Fact]
  39. public async Task Distinct9()
  40. {
  41. var xs = new[] { 1, 2, 1, 3, 5, 2, 1, 4 }.ToAsyncEnumerable().Distinct(k => k);
  42. await SequenceIdentity(xs);
  43. }
  44. [Fact]
  45. public void Distinct11()
  46. {
  47. var xs = AsyncEnumerable.Empty<int>().Distinct(k => k);
  48. var e = xs.GetAsyncEnumerator();
  49. NoNext(e);
  50. }
  51. }
  52. }