MinBy.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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 Xunit;
  8. namespace Tests
  9. {
  10. public class MinBy : Tests
  11. {
  12. #if !NET6_0_OR_GREATER
  13. [Fact]
  14. public void MinBy_Arguments()
  15. {
  16. AssertThrows<ArgumentNullException>(() => EnumerableEx.MinBy(null, (int x) => x));
  17. AssertThrows<ArgumentNullException>(() => EnumerableEx.MinBy([1], default(Func<int, int>)));
  18. AssertThrows<ArgumentNullException>(() => EnumerableEx.MinBy(null, (int x) => x, Comparer<int>.Default));
  19. AssertThrows<ArgumentNullException>(() => EnumerableEx.MinBy([1], default, Comparer<int>.Default));
  20. AssertThrows<ArgumentNullException>(() => EnumerableEx.MinBy([1], (int x) => x, null));
  21. }
  22. [Fact]
  23. public void MinBy1()
  24. {
  25. var res = new[] { 2, 5, 0, 7, 4, 3, 6, 2, 1 }.MinBy(x => x % 3);
  26. Assert.True(res.SequenceEqual([0, 3, 6]));
  27. }
  28. [Fact]
  29. public void MinBy_Empty()
  30. {
  31. AssertThrows<InvalidOperationException>(() => Enumerable.Empty<int>().MinBy(x => x));
  32. }
  33. #endif
  34. }
  35. }