MinBy.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 Xunit;
  8. namespace Tests
  9. {
  10. public class MinBy : Tests
  11. {
  12. [Fact]
  13. public void MinBy_Arguments()
  14. {
  15. AssertThrows<ArgumentNullException>(() => EnumerableEx.MinBy(null, (int x) => x));
  16. AssertThrows<ArgumentNullException>(() => EnumerableEx.MinBy(new[] { 1 }, default(Func<int, int>)));
  17. AssertThrows<ArgumentNullException>(() => EnumerableEx.MinBy(null, (int x) => x, Comparer<int>.Default));
  18. AssertThrows<ArgumentNullException>(() => EnumerableEx.MinBy(new[] { 1 }, default(Func<int, int>), Comparer<int>.Default));
  19. AssertThrows<ArgumentNullException>(() => EnumerableEx.MinBy(new[] { 1 }, (int x) => x, null));
  20. }
  21. [Fact]
  22. public void MinBy1()
  23. {
  24. var res = new[] { 2, 5, 0, 7, 4, 3, 6, 2, 1 }.MinBy(x => x % 3);
  25. Assert.True(res.SequenceEqual(new[] { 0, 3, 6 }));
  26. }
  27. [Fact]
  28. public void MinBy_Empty()
  29. {
  30. AssertThrows<InvalidOperationException>(() => Enumerable.Empty<int>().MinBy(x => x));
  31. }
  32. }
  33. }