Tests.Aggregates.cs 3.9 KB

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