public void ChainedCompare(int nLeft, int nRight, int nResult)
        {
            var comparer = ComparisonUtility.CreateComparer <int>((x, y) => (x % 10).CompareTo(y % 10), (x, y) => (x / 10).CompareTo(y / 10));

            Assert.AreEqual(nResult, NormalizeResult(comparer.Compare(nLeft, nRight)));

            Assert.AreEqual(nResult, NormalizeResult(ComparisonUtility.ChainedCompare(nLeft, nRight, (x, y) => (x % 10).CompareTo(y % 10), (x, y) => (x / 10).CompareTo(y / 10))));

            var comparison = ComparisonUtility.CreateChainedComparison <int>((x, y) => (x % 10).CompareTo(y % 10), (x, y) => (x / 10).CompareTo(y / 10));

            Assert.AreEqual(nResult, NormalizeResult(comparison(nLeft, nRight)));
        }
Example #2
0
        public void SequenceCompare()
        {
            var ascending = new[] { 1, 2, 3, 4, 5, 6 };
            var neither   = new[] { 1, 6, 5, 2, 3, 4 };

            Assert.Less(ascending.SequenceCompare(neither), 0);
            Assert.Less(0, neither.SequenceCompare(ascending));

            Assert.AreEqual(0, ascending.SequenceCompare(neither.OrderBy(x => x)));
            Assert.AreEqual(0, neither.OrderBy(x => x).SequenceCompare(ascending));

            Assert.AreEqual(0, ascending.SequenceCompare(neither, ComparisonUtility.CreateComparer <int>((x, y) => (x % 2).CompareTo(y % 2))));
        }
Example #3
0
        public void IsSorted()
        {
            var ascending  = new[] { 1, 2, 3, 4, 5, 6 };
            var descending = new[] { 6, 5, 4, 3, 2, 1 };
            var neither    = new[] { 6, 1, 5, 2, 4, 3 };

            Assert.IsTrue(ascending.IsSorted());
            Assert.IsFalse(descending.IsSorted());
            Assert.IsFalse(neither.IsSorted());

            var inverse = ComparisonUtility.CreateComparer <int>((x, y) => - x.CompareTo(y));

            Assert.IsFalse(ascending.IsSorted(inverse));
            Assert.IsTrue(descending.IsSorted(inverse));
            Assert.IsFalse(neither.IsSorted(inverse));
        }
 public void NoComparers()
 {
     Assert.Throws <ArgumentException>(() => ComparisonUtility.CreateComparer <int>());
 }
        public void CreateComparer(int nLeft, int nRight, int nResult)
        {
            var comparer = ComparisonUtility.CreateComparer <int>((x, y) => - x.CompareTo(y));

            Assert.AreEqual(nResult, NormalizeResult(comparer.Compare(nLeft, nRight)));
        }