Esempio 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));
        }
Esempio n. 2
0
        public void TestCreateByKey()
        {
            var comparer = EqualityComparers.Create((string s) => s.Length);

            comparer.Equals("a", "b").ShouldEqual(true);
            comparer.Equals(null, string.Empty).ShouldEqual(false);
            comparer.Equals(string.Empty, null).ShouldEqual(false);
            comparer.Equals("aa", "b").ShouldEqual(false);
            comparer.GetHashCode("abc").ShouldEqual(3);
        }
        public void TestRapidExit()
        {
            var comparer = EqualityComparers.Create(
                (int a, int b) => { throw new InvalidOperationException("should never get here"); },
                i => { throw new InvalidOperationException("should never get here"); }
                );

            Sequence <int>().CollectionEquals(Sequence <int>(), comparer).ShouldEqual(true);
            new int[0].CollectionEquals(new int[0], comparer).ShouldEqual(true);

            new[] { 1, 2, 3 }.CollectionEquals(new[] { 1, 2, 3, 4 }).ShouldEqual(false);
        }
Esempio n. 4
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));
        }
Esempio n. 5
0
        public void TestReferenceComparer()
        {
            var referenceComparer = EqualityComparers.GetReferenceComparer <string>();

            Assert.Same(referenceComparer, EqualityComparers.GetReferenceComparer <string>());

            referenceComparer.Equals(new string('a', 1), new string('a', 1)).ShouldEqual(false);
            var text = new string('a', 1);

            referenceComparer.Equals(text, text).ShouldEqual(true);
            referenceComparer.GetHashCode(text).ShouldEqual(referenceComparer.GetHashCode(text));
            referenceComparer.Equals(text, null).ShouldEqual(false);
            referenceComparer.Equals(null, null).ShouldEqual(true);

            var obj = new object();

            EqualityComparers.GetReferenceComparer <object>().GetHashCode(obj)
            .ShouldEqual(obj.GetHashCode());
        }
Esempio n. 6
0
        public void TestCreate()
        {
            Assert.Throws <ArgumentNullException>(() => EqualityComparers.Create <int>(null));

            var comparer = EqualityComparers.Create <string>((a, b) => a.Length == b.Length);

            comparer.Equals("a", "b").ShouldEqual(true);
            comparer.Equals(null, string.Empty).ShouldEqual(false);
            comparer.Equals(string.Empty, null).ShouldEqual(false);
            comparer.Equals("aa", "b").ShouldEqual(false);
            comparer.GetHashCode("abc").ShouldEqual(-1);

            comparer = EqualityComparers.Create <string>((a, b) => a.Length == b.Length, s => s.Length);
            comparer.Equals("a", "b").ShouldEqual(true);
            comparer.Equals(null, string.Empty).ShouldEqual(false);
            comparer.Equals(string.Empty, null).ShouldEqual(false);
            comparer.Equals("aa", "b").ShouldEqual(false);
            comparer.GetHashCode("abc").ShouldEqual(3);
        }