Tests.Aggregates.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 partial class Tests
  11. {
  12. [Fact]
  13. public void IsEmtpy_Arguments()
  14. {
  15. AssertThrows<ArgumentNullException>(() => EnumerableEx.IsEmpty<int>(null));
  16. }
  17. [Fact]
  18. public void IsEmpty_Empty()
  19. {
  20. Assert.True(Enumerable.Empty<int>().IsEmpty());
  21. }
  22. [Fact]
  23. public void IsEmpty_NonEmpty()
  24. {
  25. Assert.False(new[] { 1 }.IsEmpty());
  26. }
  27. [Fact]
  28. public void Min_Arguments()
  29. {
  30. AssertThrows<ArgumentNullException>(() => EnumerableEx.Min(null, Comparer<int>.Default));
  31. AssertThrows<ArgumentNullException>(() => EnumerableEx.Min(new[] { 1 }, null));
  32. }
  33. [Fact]
  34. public void Min()
  35. {
  36. Assert.Equal(3, new[] { 5, 3, 7 }.Min(new Mod3Comparer()));
  37. }
  38. private class Mod3Comparer : IComparer<int>
  39. {
  40. public int Compare(int x, int y)
  41. {
  42. return Comparer<int>.Default.Compare(x % 3, y % 3);
  43. }
  44. }
  45. [Fact]
  46. public void MinBy_Arguments()
  47. {
  48. AssertThrows<ArgumentNullException>(() => EnumerableEx.MinBy(null, (int x) => x));
  49. AssertThrows<ArgumentNullException>(() => EnumerableEx.MinBy(new[] { 1 }, default(Func<int, int>)));
  50. AssertThrows<ArgumentNullException>(() => EnumerableEx.MinBy(null, (int x) => x, Comparer<int>.Default));
  51. AssertThrows<ArgumentNullException>(() => EnumerableEx.MinBy(new[] { 1 }, default, Comparer<int>.Default));
  52. AssertThrows<ArgumentNullException>(() => EnumerableEx.MinBy(new[] { 1 }, (int x) => x, null));
  53. }
  54. [Fact]
  55. public void MinBy()
  56. {
  57. var res = new[] { 2, 5, 0, 7, 4, 3, 6, 2, 1 }.MinBy(x => x % 3);
  58. Assert.True(res.SequenceEqual(new[] { 0, 3, 6 }));
  59. }
  60. [Fact]
  61. public void MinBy_Empty()
  62. {
  63. AssertThrows<InvalidOperationException>(() => Enumerable.Empty<int>().MinBy(x => x));
  64. }
  65. [Fact]
  66. public void Max_Arguments()
  67. {
  68. AssertThrows<ArgumentNullException>(() => EnumerableEx.Max(null, Comparer<int>.Default));
  69. AssertThrows<ArgumentNullException>(() => EnumerableEx.Max(new[] { 1 }, null));
  70. }
  71. [Fact]
  72. public void Max()
  73. {
  74. Assert.Equal(5, new[] { 2, 5, 3, 7 }.Max(new Mod7Comparer()));
  75. }
  76. private class Mod7Comparer : IComparer<int>
  77. {
  78. public int Compare(int x, int y)
  79. {
  80. return Comparer<int>.Default.Compare(x % 7, y % 7);
  81. }
  82. }
  83. [Fact]
  84. public void MaxBy_Arguments()
  85. {
  86. AssertThrows<ArgumentNullException>(() => EnumerableEx.MaxBy(null, (int x) => x));
  87. AssertThrows<ArgumentNullException>(() => EnumerableEx.MaxBy(new[] { 1 }, default(Func<int, int>)));
  88. AssertThrows<ArgumentNullException>(() => EnumerableEx.MaxBy(null, (int x) => x, Comparer<int>.Default));
  89. AssertThrows<ArgumentNullException>(() => EnumerableEx.MaxBy(new[] { 1 }, default, Comparer<int>.Default));
  90. AssertThrows<ArgumentNullException>(() => EnumerableEx.MaxBy(new[] { 1 }, (int x) => x, null));
  91. }
  92. [Fact]
  93. public void MaxBy()
  94. {
  95. var res = new[] { 2, 5, 0, 7, 4, 3, 6, 2, 1 }.MaxBy(x => x % 3);
  96. Assert.True(res.SequenceEqual(new[] { 2, 5, 2 }));
  97. }
  98. [Fact]
  99. public void MaxBy_Empty()
  100. {
  101. AssertThrows<InvalidOperationException>(() => Enumerable.Empty<int>().MaxBy(x => x));
  102. }
  103. }
  104. }