コード例 #1
0
        public void Compare()
        {
            using (new CultureSwapper(PredefinedCulture.EnglishUnitedStates))
            {
                var message  = "'ñ' should be less than 't' for default comparison (current culture, en-US).";
                var comparer = KeyComparer.Create((City city) => city.Name);
                CompareAssert.IsLessThan(añasco, athens, comparer, message);
                CompareAssert.IsEqualTo(añasco, añasco, comparer);
                CompareAssert.IsGreaterThan(athens, añasco, comparer, message);

                message  = "'ñ' should greater less than 't' for ordinal comparison.";
                comparer = KeyComparer.Create((City city) => city.Name, StringComparer.Ordinal);
                CompareAssert.IsGreaterThan(añasco, athens, comparer, message);
                CompareAssert.IsEqualTo(añasco, añasco, comparer);
                CompareAssert.IsLessThan(athens, añasco, comparer, message);
            }
        }
コード例 #2
0
        public void Compare_Shortlex_Enumerables()
        {
            var comparer = SequenceComparer <int> .Shortlex;

            CompareAssert.IsEqualTo(Enumerable.Empty <int>(), Enumerable.Empty <int>(), comparer);
            CompareAssert.IsLessThan(Enumerable.Empty <int>(), EnumerableUtility.Yield(42), comparer);
            CompareAssert.IsGreaterThan(EnumerableUtility.Yield(59), Enumerable.Empty <int>(), comparer);
            CompareAssert.IsGreaterThan(EnumerableUtility.Yield(59), EnumerableUtility.Yield(42), comparer);
            CompareAssert.IsLessThan(EnumerableUtility.Yield(59), EnumerableUtility.Yield(42, 98), comparer);
            CompareAssert.IsLessThan(EnumerableUtility.Yield(59), EnumerableUtility.Yield(42, 98, 11), comparer);
            CompareAssert.IsLessThan(EnumerableUtility.Yield(59, 23), EnumerableUtility.Yield(42, 98, 11), comparer);
            CompareAssert.IsGreaterThan(EnumerableUtility.Yield(59, 23, 34), EnumerableUtility.Yield(42, 98, 11), comparer);
            CompareAssert.IsGreaterThan(EnumerableUtility.Yield(59, 23, 34, 16), EnumerableUtility.Yield(42, 98, 11), comparer);
            CompareAssert.IsGreaterThan(EnumerableUtility.Yield(59, 23, 34, 16), EnumerableUtility.Yield(59, 98, 11), comparer);
            CompareAssert.IsGreaterThan(EnumerableUtility.Yield(59, 23, 34, 16), EnumerableUtility.Yield(59, 23, 11), comparer);
            CompareAssert.IsGreaterThan(EnumerableUtility.Yield(59, 23, 34, 16), EnumerableUtility.Yield(59, 23, 34), comparer);
            CompareAssert.IsEqualTo(EnumerableUtility.Yield(59, 23, 34, 16), EnumerableUtility.Yield(59, 23, 34, 16), comparer);
            CompareAssert.IsLessThan(EnumerableUtility.Yield(59, 23, 34, 16), EnumerableUtility.Yield(59, 23, 34, 16, 65), comparer);
        }
コード例 #3
0
        public void Compare_Shortlex_Arrays()
        {
            var comparer = SequenceComparer <int> .Shortlex;

            CompareAssert.IsEqualTo(new int[] { }, new int[] { }, comparer);
            CompareAssert.IsLessThan(new int[] { }, new int[] { 42 }, comparer);
            CompareAssert.IsGreaterThan(new int[] { 59 }, new int[] { }, comparer);
            CompareAssert.IsGreaterThan(new int[] { 59 }, new int[] { 42 }, comparer);
            CompareAssert.IsLessThan(new int[] { 59 }, new int[] { 42, 98 }, comparer);
            CompareAssert.IsLessThan(new int[] { 59 }, new int[] { 42, 98, 11 }, comparer);
            CompareAssert.IsLessThan(new int[] { 59, 23 }, new int[] { 42, 98, 11 }, comparer);
            CompareAssert.IsGreaterThan(new int[] { 59, 23, 34 }, new int[] { 42, 98, 11 }, comparer);
            CompareAssert.IsGreaterThan(new int[] { 59, 23, 34, 16 }, new int[] { 42, 98, 11 }, comparer);
            CompareAssert.IsGreaterThan(new int[] { 59, 23, 34, 16 }, new int[] { 59, 98, 11 }, comparer);
            CompareAssert.IsGreaterThan(new int[] { 59, 23, 34, 16 }, new int[] { 59, 23, 11 }, comparer);
            CompareAssert.IsGreaterThan(new int[] { 59, 23, 34, 16 }, new int[] { 59, 23, 34 }, comparer);
            CompareAssert.IsEqualTo(new int[] { 59, 23, 34, 16 }, new int[] { 59, 23, 34, 16 }, comparer);
            CompareAssert.IsLessThan(new int[] { 59, 23, 34, 16 }, new int[] { 59, 23, 34, 16, 65 }, comparer);
        }
コード例 #4
0
        public void Serialize_BinaryFormatter()
        {
            var x = new[] { "abc", "def" };
            var y = new[] { "abc", "DEF" };
            var z = new[] { "xyz" };

            var binaryFormatter = new BinaryFormatter();

            using (var stream = new MemoryStream())
            {
                var original = SequenceComparer.Create(SequenceComparison.Shortlex, StringComparer.OrdinalIgnoreCase);
                binaryFormatter.Serialize(stream, original);
                stream.Position = 0;

                var comparer = (SequenceComparer <string>)binaryFormatter.Deserialize(stream);
                CompareAssert.IsEqualTo(x, y, comparer);
                CompareAssert.IsGreaterThan(x, z, comparer);
            }
        }
コード例 #5
0
        public void Compare_SameLength_Enumerables()
        {
            var comparer = SequenceComparer <int> .SameLength;

            CompareAssert.IsEqualTo(Enumerable.Empty <int>(), Enumerable.Empty <int>(), comparer);
            ExceptionAssert.Throws <ArgumentException>(() => comparer.Compare(Enumerable.Empty <int>(), EnumerableUtility.Yield(42)));
            ExceptionAssert.Throws <ArgumentException>(() => comparer.Compare(EnumerableUtility.Yield(59), Enumerable.Empty <int>()));
            CompareAssert.IsGreaterThan(EnumerableUtility.Yield(59), EnumerableUtility.Yield(42), comparer);
            ExceptionAssert.Throws <ArgumentException>(() => comparer.Compare(EnumerableUtility.Yield(59), EnumerableUtility.Yield(42, 98)));
            ExceptionAssert.Throws <ArgumentException>(() => comparer.Compare(EnumerableUtility.Yield(59), EnumerableUtility.Yield(42, 98, 11)));
            ExceptionAssert.Throws <ArgumentException>(() => comparer.Compare(EnumerableUtility.Yield(59, 23), EnumerableUtility.Yield(42, 98, 11)));
            CompareAssert.IsGreaterThan(EnumerableUtility.Yield(59, 23, 34), EnumerableUtility.Yield(42, 98, 11), comparer);
            ExceptionAssert.Throws <ArgumentException>(() => comparer.Compare(EnumerableUtility.Yield(59, 23, 34, 16), EnumerableUtility.Yield(42, 98, 11)));
            ExceptionAssert.Throws <ArgumentException>(() => comparer.Compare(EnumerableUtility.Yield(59, 23, 34, 16), EnumerableUtility.Yield(59, 98, 11)));
            ExceptionAssert.Throws <ArgumentException>(() => comparer.Compare(EnumerableUtility.Yield(59, 23, 34, 16), EnumerableUtility.Yield(59, 23, 11)));
            ExceptionAssert.Throws <ArgumentException>(() => comparer.Compare(EnumerableUtility.Yield(59, 23, 34, 16), EnumerableUtility.Yield(59, 23, 34)));
            CompareAssert.IsEqualTo(EnumerableUtility.Yield(59, 23, 34, 16), EnumerableUtility.Yield(59, 23, 34, 16), comparer);
            ExceptionAssert.Throws <ArgumentException>(() => comparer.Compare(EnumerableUtility.Yield(59, 23, 34, 16), EnumerableUtility.Yield(59, 23, 34, 16, 65)));
        }
コード例 #6
0
        public void Compare_SameLength_Arrays()
        {
            var comparer = SequenceComparer <int> .SameLength;

            CompareAssert.IsEqualTo(new int[] { }, new int[] { }, comparer);
            ExceptionAssert.Throws <ArgumentException>(() => comparer.Compare(new int[] { }, new int[] { 42 }));
            ExceptionAssert.Throws <ArgumentException>(() => comparer.Compare(new int[] { 59 }, new int[] { }));
            CompareAssert.IsGreaterThan(new int[] { 59 }, new int[] { 42 }, comparer);
            ExceptionAssert.Throws <ArgumentException>(() => comparer.Compare(new int[] { 59 }, new int[] { 42, 98 }));
            ExceptionAssert.Throws <ArgumentException>(() => comparer.Compare(new int[] { 59 }, new int[] { 42, 98, 11 }));
            ExceptionAssert.Throws <ArgumentException>(() => comparer.Compare(new int[] { 59, 23 }, new int[] { 42, 98, 11 }));
            CompareAssert.IsGreaterThan(new int[] { 59, 23, 34 }, new int[] { 42, 98, 11 }, comparer);
            ExceptionAssert.Throws <ArgumentException>(() => comparer.Compare(new int[] { 59, 23, 34, 16 }, new int[] { 42, 98, 11 }));
            ExceptionAssert.Throws <ArgumentException>(() => comparer.Compare(new int[] { 59, 23, 34, 16 }, new int[] { 59, 98, 11 }));
            ExceptionAssert.Throws <ArgumentException>(() => comparer.Compare(new int[] { 59, 23, 34, 16 }, new int[] { 59, 23, 11 }));
            ExceptionAssert.Throws <ArgumentException>(() => comparer.Compare(new int[] { 59, 23, 34, 16 }, new int[] { 59, 23, 34 }));
            CompareAssert.IsEqualTo(new int[] { 59, 23, 34, 16 }, new int[] { 59, 23, 34, 16 }, comparer);
            ExceptionAssert.Throws <ArgumentException>(() => comparer.Compare(new int[] { 59, 23, 34, 16 }, new int[] { 59, 23, 34, 16, 65 }));
        }
コード例 #7
0
        public void Compare_Strings()
        {
            var x = new[] { "Madrid", "Añasco" };
            var y = new[] { "Madrid", "Athens" };

            using (new CultureSwapper(PredefinedCulture.EnglishUnitedStates))
            {
                foreach (var comparisonType in EnumUtility.GetValues <SequenceComparison>())
                {
                    var message  = "'ñ' should be less than 't' for default comparison (current culture, en-US).";
                    var comparer = SequenceComparer.Create <string>(comparisonType);
                    CompareAssert.IsLessThan(x, y, comparer, message);
                    CompareAssert.IsEqualTo(x, x, comparer);
                    CompareAssert.IsGreaterThan(y, x, comparer, message);

                    message  = "'ñ' should be greater than 't' for ordinal comparison.";
                    comparer = SequenceComparer.Create(comparisonType, StringComparer.Ordinal);
                    CompareAssert.IsGreaterThan(x, y, comparer, message);
                    CompareAssert.IsEqualTo(x, x, comparer);
                    CompareAssert.IsLessThan(y, x, comparer, message);
                }
            }
        }