MaxBy.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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;
  8. using System.Threading.Tasks;
  9. using Xunit;
  10. namespace Tests
  11. {
  12. public class MaxBy : AsyncEnumerableExTests
  13. {
  14. [Fact]
  15. public async Task MaxBy_Null()
  16. {
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MaxByAsync(default(IAsyncEnumerable<int>), x => x));
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MaxByAsync(Return42, default(Func<int, int>)));
  19. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MaxByAsync(default(IAsyncEnumerable<int>), x => x, Comparer<int>.Default));
  20. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MaxByAsync(Return42, default(Func<int, int>), Comparer<int>.Default));
  21. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MaxByAsync(default(IAsyncEnumerable<int>), x => x, CancellationToken.None));
  22. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MaxByAsync(Return42, default(Func<int, int>), CancellationToken.None));
  23. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MaxByAsync(default(IAsyncEnumerable<int>), x => x, Comparer<int>.Default, CancellationToken.None));
  24. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MaxByAsync(Return42, default(Func<int, int>), Comparer<int>.Default, CancellationToken.None));
  25. }
  26. [Fact]
  27. public async Task MaxBy1Async()
  28. {
  29. var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().MaxByAsync(x => x / 2);
  30. var res = await xs;
  31. Assert.True(res.SequenceEqual(new[] { 7, 6 }));
  32. }
  33. [Fact]
  34. public async Task MaxBy2()
  35. {
  36. var xs = new int[0].ToAsyncEnumerable().MaxByAsync(x => x / 2);
  37. await AssertThrowsAsync<InvalidOperationException>(xs);
  38. }
  39. [Fact]
  40. public async Task MaxBy3()
  41. {
  42. var ex = new Exception("Bang!");
  43. var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().MaxByAsync(x => { if (x == 3) throw ex; return x; });
  44. await AssertThrowsAsync(xs, ex);
  45. }
  46. [Fact]
  47. public async Task MaxBy4()
  48. {
  49. var ex = new Exception("Bang!");
  50. var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().MaxByAsync(x => { if (x == 4) throw ex; return x; });
  51. await AssertThrowsAsync(xs, ex);
  52. }
  53. [Fact]
  54. public async Task MaxBy5()
  55. {
  56. var ex = new Exception("Bang!");
  57. var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().Concat(Throw<int>(ex)).MaxByAsync(x => x, Comparer<int>.Default);
  58. await AssertThrowsAsync(xs, ex);
  59. }
  60. }
  61. }