Tests.Aggregates.cs 4.0 KB

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