MaxByTest.cs 1.3 KB

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