| 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 |
#if NUNIT |
| 7 |
using NUnit.Framework; |
| 8 |
using TestClassAttribute = NUnit.Framework.TestFixtureAttribute; |
| 9 |
using TestMethodAttribute = NUnit.Framework.TestAttribute; |
| 10 |
using TestInitializeAttribute = NUnit.Framework.SetUpAttribute; |
| 11 |
#else |
| 12 |
using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 13 |
#endif |
| 14 |
|
| 15 |
namespace Tests |
| 16 |
{ |
| 17 |
public partial class Tests |
| 18 |
{ |
| 19 |
[TestMethod] |
| 20 |
public void IsEmtpy_Arguments() |
| 21 |
{ |
| 22 |
AssertThrows<ArgumentNullException>(() => EnumerableEx.IsEmpty<int>(null)); |
| 23 |
} |
| 24 |
|
| 25 |
[TestMethod] |
| 26 |
public void IsEmpty_Empty() |
| 27 |
{ |
| 28 |
Assert.IsTrue(Enumerable.Empty<int>().IsEmpty()); |
| 29 |
} |
| 30 |
|
| 31 |
[TestMethod] |
| 32 |
public void IsEmpty_NonEmpty() |
| 33 |
{ |
| 34 |
Assert.IsFalse(new[] { 1 }.IsEmpty()); |
| 35 |
} |
| 36 |
|
| 37 |
[TestMethod] |
| 38 |
public void Min_Arguments() |
| 39 |
{ |
| 40 |
AssertThrows<ArgumentNullException>(() => EnumerableEx.Min(null, Comparer<int>.Default)); |
| 41 |
AssertThrows<ArgumentNullException>(() => EnumerableEx.Min(new[] { 1 }, null)); |
| 42 |
} |
| 43 |
|
| 44 |
[TestMethod] |
| 45 |
public void Min() |
| 46 |
{ |
| 47 |
Assert.AreEqual(3, new[] { 5, 3, 7 }.Min(new Mod3Comparer())); |
| 48 |
} |
| 49 |
|
| 50 |
class Mod3Comparer : IComparer<int> |
| 51 |
{ |
| 52 |
public int Compare(int x, int y) |
| 53 |
{ |
| 54 |
return Comparer<int>.Default.Compare(x % 3, y % 3); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
[TestMethod] |
| 59 |
public void MinBy_Arguments() |
| 60 |
{ |
| 61 |
AssertThrows<ArgumentNullException>(() => EnumerableEx.MinBy(null, (int x) => x)); |
| 62 |
AssertThrows<ArgumentNullException>(() => EnumerableEx.MinBy(new[] { 1 }, default(Func<int, int>))); |
| 63 |
AssertThrows<ArgumentNullException>(() => EnumerableEx.MinBy(null, (int x) => x, Comparer<int>.Default)); |
| 64 |
AssertThrows<ArgumentNullException>(() => EnumerableEx.MinBy(new[] { 1 }, default(Func<int, int>), Comparer<int>.Default)); |
| 65 |
AssertThrows<ArgumentNullException>(() => EnumerableEx.MinBy(new[] { 1 }, (int x) => x, null)); |
| 66 |
} |
| 67 |
|
| 68 |
[TestMethod] |
| 69 |
public void MinBy() |
| 70 |
{ |
| 71 |
var res = new[] { 2, 5, 0, 7, 4, 3, 6, 2, 1 }.MinBy(x => x % 3); |
| 72 |
Assert.IsTrue(res.SequenceEqual(new[] { 0, 3, 6 })); |
| 73 |
} |
| 74 |
|
| 75 |
[TestMethod] |
| 76 |
public void MinBy_Empty() |
| 77 |
{ |
| 78 |
AssertThrows<InvalidOperationException>(() => Enumerable.Empty<int>().MinBy(x => x)); |
| 79 |
} |
| 80 |
|
| 81 |
[TestMethod] |
| 82 |
public void Max_Arguments() |
| 83 |
{ |
| 84 |
AssertThrows<ArgumentNullException>(() => EnumerableEx.Max(null, Comparer<int>.Default)); |
| 85 |
AssertThrows<ArgumentNullException>(() => EnumerableEx.Max(new[] { 1 }, null)); |
| 86 |
} |
| 87 |
|
| 88 |
[TestMethod] |
| 89 |
public void Max() |
| 90 |
{ |
| 91 |
Assert.AreEqual(5, new[] { 2, 5, 3, 7 }.Max(new Mod7Comparer())); |
| 92 |
} |
| 93 |
|
| 94 |
class Mod7Comparer : IComparer<int> |
| 95 |
{ |
| 96 |
public int Compare(int x, int y) |
| 97 |
{ |
| 98 |
return Comparer<int>.Default.Compare(x % 7, y % 7); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
[TestMethod] |
| 103 |
public void MaxBy_Arguments() |
| 104 |
{ |
| 105 |
AssertThrows<ArgumentNullException>(() => EnumerableEx.MaxBy(null, (int x) => x)); |
| 106 |
AssertThrows<ArgumentNullException>(() => EnumerableEx.MaxBy(new[] { 1 }, default(Func<int, int>))); |
| 107 |
AssertThrows<ArgumentNullException>(() => EnumerableEx.MaxBy(null, (int x) => x, Comparer<int>.Default)); |
| 108 |
AssertThrows<ArgumentNullException>(() => EnumerableEx.MaxBy(new[] { 1 }, default(Func<int, int>), Comparer<int>.Default)); |
| 109 |
AssertThrows<ArgumentNullException>(() => EnumerableEx.MaxBy(new[] { 1 }, (int x) => x, null)); |
| 110 |
} |
| 111 |
|
| 112 |
[TestMethod] |
| 113 |
public void MaxBy() |
| 114 |
{ |
| 115 |
var res = new[] { 2, 5, 0, 7, 4, 3, 6, 2, 1 }.MaxBy(x => x % 3); |
| 116 |
Assert.IsTrue(res.SequenceEqual(new[] { 2, 5, 2 })); |
| 117 |
} |
| 118 |
|
| 119 |
[TestMethod] |
| 120 |
public void MaxBy_Empty() |
| 121 |
{ |
| 122 |
AssertThrows<InvalidOperationException>(() => Enumerable.Empty<int>().MaxBy(x => x)); |
| 123 |
} |
| 124 |
} |
| 125 |
} |