Ejemplo n.º 1
0
        public void TestSequenceComparer()
        {
            var a = new[] { 1, 2, 3 };
            var b = new[] { 1, 2, 3 };
            var c = new[] { 1, 3, 2 };

            var comparer = EqualityComparers.GetSequenceComparer <int>();

            Assert.Same(comparer, EqualityComparers.GetSequenceComparer <int>());
            Assert.Same(comparer, EqualityComparers.GetSequenceComparer(EqualityComparer <int> .Default));

            comparer.Equals(a, b).ShouldEqual(true);
            comparer.Equals(a, c).ShouldEqual(false);
            comparer.Equals(null, a).ShouldEqual(false);
            comparer.Equals(null, null).ShouldEqual(true);
            comparer.GetHashCode(null).ShouldEqual(((IEqualityComparer)comparer).GetHashCode(null));
            comparer.GetHashCode(a).ShouldEqual(comparer.GetHashCode(b));
            Assert.NotEqual(comparer.GetHashCode(a), comparer.GetHashCode(c));

            var stringComparer = EqualityComparers.GetSequenceComparer(StringComparer.OrdinalIgnoreCase);
            var aa             = new[] { "a", "B", "C" };
            var bb             = new[] { "A", "b", "c" };
            var cc             = new[] { "a", "B", "C", "d" };

            stringComparer.Equals(aa, bb).ShouldEqual(true);
            stringComparer.Equals(aa, cc).ShouldEqual(false);
            stringComparer.GetHashCode(aa).ShouldEqual(stringComparer.GetHashCode(bb));
            Assert.NotEqual(stringComparer.GetHashCode(aa), stringComparer.GetHashCode(cc));
        }
Ejemplo n.º 2
0
        public void TestComparerEquality()
        {
            Func <string, string, bool> sameLength = (a, b) => a.Length == b.Length;
            Func <string, int>          getLength  = s => s.Length;

            ComparersTest.TestEquality(EqualityComparers.Create(sameLength, getLength), EqualityComparers.Create(sameLength, getLength), EqualityComparers.Create(getLength));

            ComparersTest.TestEquality(EqualityComparers.Create(getLength), EqualityComparers.Create(getLength, EqualityComparer <int> .Default), EqualityComparers.Create((string s) => s[0]));

            ComparersTest.TestEquality(EqualityComparers.GetReferenceComparer <string>(), EqualityComparers.GetReferenceComparer <string>(), EqualityComparer <string> .Default);

            ComparersTest.TestEquality(EqualityComparers.GetCollectionComparer(EqualityComparer <string> .Default), EqualityComparers.GetCollectionComparer <string>(), EqualityComparers.GetCollectionComparer(StringComparer.OrdinalIgnoreCase));

            ComparersTest.TestEquality(EqualityComparers.GetSequenceComparer(StringComparer.Ordinal), EqualityComparers.GetSequenceComparer(StringComparer.Ordinal), EqualityComparers.GetCollectionComparer(StringComparer.Ordinal));
        }