Beispiel #1
0
        public void TestComparerEquality()
        {
            Func <int, int> f = i => - i;

            TestEquality(Comparers.Create(f), Comparers.Create(f), Comparers.Create((int i) => i.ToString()));

            TestEquality(Comparers.Reverse <int>(), Comparers.Reverse <int>(), Comparers.Create((int i) => i.ToString()).Reverse());

            var first  = Comparers.Create((string s) => s.Length);
            var second = Comparers.Create((string s) => s[0]);

            TestEquality(first.ThenBy(second), first.ThenBy(second), second.ThenBy(first));

            TestEquality(Comparers.GetSequenceComparer <string>(), Comparers.GetSequenceComparer(Comparer <string> .Default), Comparers.GetSequenceComparer(StringComparer.OrdinalIgnoreCase));
        }
Beispiel #2
0
        public void TestSequenceComparer()
        {
            Assert.Same(Comparers.GetSequenceComparer <int>(), Comparers.GetSequenceComparer <int>());
            Assert.Same(Comparers.GetSequenceComparer <int>(), Comparers.GetSequenceComparer(Comparer <int> .Default));

            var sequence = new[] { new[] { 1, 2, 3 }, new[] { 1, 1, 5 }, new[] { 1, 2, 1 } };

            sequence.OrderBy(i => i, Comparers.GetSequenceComparer <int>())
            .Select(a => string.Join(",", a))
            .SequenceShouldEqual(new[] { "1,1,5", "1,2,1", "1,2,3" });

            sequence.OrderBy(i => i, Comparers.GetSequenceComparer(Comparer <int> .Default.Reverse()))
            .Select(a => string.Join(",", a))
            .SequenceShouldEqual(new[] { "1,2,3", "1,2,1", "1,1,5" });

            Math.Sign(Comparers.GetSequenceComparer <int>().Compare(Empty.Array <int>(), new[] { 1 }))
            .ShouldEqual(-1);
        }