Ejemplo n.º 1
0
        public void CompareInts()
        {
            var comparer = EqualityComparerEx.Func((int a, int b) => a == b);

            Assert.True(comparer.Equals(1, 1));
            Assert.False(comparer.Equals(1, 2));
            // ReSharper disable once EqualExpressionComparison
            Assert.True(comparer.GetHashCode(1) == comparer.GetHashCode(1));
            Assert.False(comparer.GetHashCode(1) == comparer.GetHashCode(2));
        }
Ejemplo n.º 2
0
        public void NonGenericCompareInts()
        {
            var    comparer    = EqualityComparerEx.Func <int>((a, b) => a == b);
            object one         = 1;
            object two         = 2;
            var    comparerObj = comparer as IEqualityComparer;

            Assert.True(comparerObj.Equals(one, 1));
            Assert.False(comparerObj.Equals(one, two));
            // ReSharper disable once EqualExpressionComparison
            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            Assert.Throws <ArgumentException>(() => comparerObj.Equals(one, "1"));
            Assert.True(comparerObj.GetHashCode(one) == comparer.GetHashCode(1));
            Assert.False(comparerObj.GetHashCode(one) == comparerObj.GetHashCode(two));
            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            Assert.Throws <ArgumentException>(() => comparerObj.GetHashCode("1"));
        }
Ejemplo n.º 3
0
        public void CompareIntsUsingSignComparer()
        {
            var array1    = new[] { 1, 2, 3 };
            var array2    = new[] { 2, 3, 5 };
            var array3    = new[] { 1, 3, -3 };
            var array4    = new[] { 1, 2, 3, 4 };
            var comparer  = EqualityComparerEx.Array(EqualityComparerEx.Func <int>((x, y) => Math.Sign(x) == Math.Sign(y), Math.Sign));
            var hashCode1 = comparer.GetHashCode(array1);
            var hashCode2 = comparer.GetHashCode(array2);
            var hashCode3 = comparer.GetHashCode(array3);
            var hashCode4 = comparer.GetHashCode(array4);

            Assert.Equal(hashCode1, hashCode2);
            Assert.NotEqual(hashCode1, hashCode3);
            Assert.NotEqual(hashCode1, hashCode4);
            Assert.True(comparer.Equals(array1, array1));
            Assert.True(comparer.Equals(array1, array2));
            Assert.False(comparer.Equals(array1, array3));
            Assert.False(comparer.Equals(array1, array4));
        }
Ejemplo n.º 4
0
        public void CompareTestClassByOneProp()
        {
            var o1 = new ComparersTestClass {
                A = "test1", B = 1
            };
            var oe1 = new ComparersTestClass {
                A = "test1", B = 2
            };
            var o2 = new ComparersTestClass {
                A = "test2", B = 2
            };
            var comparer = EqualityComparerEx.Func <ComparersTestClass>((a, b) => a.A == b.A, a => a.A.GetHashCode());

            Assert.True(comparer.Equals(o1, o1));
            Assert.True(comparer.Equals(o1, oe1));
            Assert.False(comparer.Equals(o1, o2));
            // ReSharper disable once EqualExpressionComparison
            Assert.True(comparer.GetHashCode(o1) == comparer.GetHashCode(o1));
            Assert.True(comparer.GetHashCode(o1) == comparer.GetHashCode(oe1));
            Assert.False(comparer.GetHashCode(o1) == comparer.GetHashCode(o2));
        }