MinBy.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 MinBy : AsyncEnumerableExTests
  13. {
  14. [Fact]
  15. public async Task MinBy_Null()
  16. {
  17. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MinBy(default(IAsyncEnumerable<int>), x => x));
  18. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MinBy(Return42, default(Func<int, int>)));
  19. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MinBy(default(IAsyncEnumerable<int>), x => x, Comparer<int>.Default));
  20. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MinBy(Return42, default(Func<int, int>), Comparer<int>.Default));
  21. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MinBy(Return42, x => x, default(IComparer<int>)));
  22. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MinBy(default(IAsyncEnumerable<int>), x => x, CancellationToken.None));
  23. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MinBy(Return42, default(Func<int, int>), CancellationToken.None));
  24. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MinBy(default(IAsyncEnumerable<int>), x => x, Comparer<int>.Default, CancellationToken.None));
  25. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MinBy(Return42, default(Func<int, int>), Comparer<int>.Default, CancellationToken.None));
  26. await Assert.ThrowsAsync<ArgumentNullException>(() => AsyncEnumerableEx.MinBy(Return42, x => x, default, CancellationToken.None));
  27. }
  28. [Fact]
  29. public void MinBy1()
  30. {
  31. var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().MinBy(x => x / 2);
  32. var res = xs.Result;
  33. Assert.True(res.SequenceEqual(new[] { 3, 2 }));
  34. }
  35. [Fact]
  36. public void MinBy2()
  37. {
  38. var xs = new int[0].ToAsyncEnumerable().MinBy(x => x / 2);
  39. AssertThrows<Exception>(() => xs.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() is InvalidOperationException);
  40. }
  41. [Fact]
  42. public void MinBy3()
  43. {
  44. var ex = new Exception("Bang!");
  45. var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().MinBy(x => { if (x == 3) throw ex; return x; });
  46. AssertThrows<Exception>(() => xs.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  47. }
  48. [Fact]
  49. public void MinBy4()
  50. {
  51. var ex = new Exception("Bang!");
  52. var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().MinBy(x => { if (x == 4) throw ex; return x; });
  53. AssertThrows<Exception>(() => xs.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  54. }
  55. [Fact]
  56. public void MinBy5()
  57. {
  58. var ex = new Exception("Bang!");
  59. var xs = new[] { 3, 5, 7, 6, 4, 2 }.ToAsyncEnumerable().Concat(Throw<int>(ex)).MinBy(x => x, Comparer<int>.Default);
  60. AssertThrows<Exception>(() => xs.Wait(WaitTimeoutMs), ex_ => ((AggregateException)ex_).Flatten().InnerExceptions.Single() == ex);
  61. }
  62. }
  63. }