MinByTest.cs 1.3 KB

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