Esempio n. 1
0
        public static void TestGetHashCode()
        {
            var jsonString = new JsonString("json property value");

            int expectedHashCode = jsonString.GetHashCode();

            Assert.Equal(expectedHashCode, new JsonString("json property value").GetHashCode());
            Assert.NotEqual(jsonString.GetHashCode(), new JsonString("json property value ").GetHashCode());
            Assert.NotEqual(jsonString.GetHashCode(), new JsonString("jsonpropertyvalue").GetHashCode());
            Assert.NotEqual(jsonString.GetHashCode(), new JsonString("Json Property Value").GetHashCode());
            Assert.NotEqual(jsonString.GetHashCode(), new JsonString("SOMETHING COMPLETELY DIFFERENT").GetHashCode());
            Assert.NotEqual(jsonString.GetHashCode(), new JsonString("").GetHashCode());
            Assert.NotEqual(jsonString.GetHashCode(), new JsonString().GetHashCode());

            JsonNode jsonNode = new JsonString("json property value");

            Assert.Equal(jsonString.GetHashCode(), jsonNode.GetHashCode());

            IEquatable <JsonString> jsonStringIEquatable = jsonString;

            Assert.Equal(jsonString.GetHashCode(), jsonStringIEquatable.GetHashCode());

            object jsonNumberCopy   = jsonString;
            object jsonNumberObject = new JsonString("something different");

            Assert.Equal(jsonString.GetHashCode(), jsonNumberCopy.GetHashCode());
            Assert.NotEqual(jsonString.GetHashCode(), jsonNumberObject.GetHashCode());

            Assert.Equal(new JsonString().GetHashCode(), new JsonString().GetHashCode());
        }
Esempio n. 2
0
        public static void TestGetHashCode()
        {
            var jsonNumber = new JsonNumber(123);

            Assert.Equal(jsonNumber.GetHashCode(), new JsonNumber(123).GetHashCode());
            Assert.Equal(jsonNumber.GetHashCode(), new JsonNumber((ushort)123).GetHashCode());
            Assert.Equal(jsonNumber.GetHashCode(), new JsonNumber("123").GetHashCode());
            Assert.NotEqual(jsonNumber.GetHashCode(), new JsonNumber("123e0").GetHashCode());
            Assert.NotEqual(jsonNumber.GetHashCode(), new JsonNumber("123e1").GetHashCode());
            Assert.NotEqual(jsonNumber.GetHashCode(), new JsonNumber(17).GetHashCode());

            JsonNode jsonNode = new JsonNumber(123);

            Assert.Equal(jsonNumber.GetHashCode(), jsonNode.GetHashCode());

            IEquatable <JsonNumber> jsonNumberIEquatable = jsonNumber;

            Assert.Equal(jsonNumber.GetHashCode(), jsonNumberIEquatable.GetHashCode());

            object jsonNumberCopy   = jsonNumber;
            object jsonNumberObject = new JsonNumber(17);

            Assert.Equal(jsonNumber.GetHashCode(), jsonNumberCopy.GetHashCode());
            Assert.NotEqual(jsonNumber.GetHashCode(), jsonNumberObject.GetHashCode());

            Assert.Equal(new JsonNumber().GetHashCode(), new JsonNumber().GetHashCode());
        }
Esempio n. 3
0
        public void HashBasedEquatibleRealValueNodesHashcodeTest()
        {
            var c = new HashBasedEquatibleRealValueNodesConverter();

            IEquatable <RealValueNodes> n1 = c.Covert(new RealValueNodes(new double[] { 1.0, 1.0 }, new double[] { 1.0, 2.0 }, new double[] { 1.0, 5.0 }));
            IEquatable <RealValueNodes> n2 = c.Covert(new RealValueNodes(new double[] { 1.0, 1.0 }, new double[] { 1.0, 2.0 }, new double[] { 1.0, 5.0 }));
            IEquatable <RealValueNodes> n3 = c.Covert(new RealValueNodes(new double[] { 1.0, 1.0 }, new double[] { 1.0, 2.0 }, new double[] { 1.0, 4.0 }));
            IEquatable <RealValueNodes> n4 = c.Covert(new RealValueNodes(new double[] { 1.0, 3.0 }, new double[] { 1.0, 2.0 }, new double[] { 1.0, 5.0 }));

            int h1 = n1.GetHashCode(), h2 = n2.GetHashCode(), h3 = n3.GetHashCode(), h4 = n4.GetHashCode();

            Assert.IsTrue(h1.Equals(h2));
            Assert.IsTrue(h1.Equals(c.Covert(new RealValueNodes(new double[] { 1.0, 1.0 }, new double[] { 1.0, 2.0 }, new double[] { 1.0, 5.0 })).GetHashCode()));
            Assert.IsFalse(h1.Equals(h3));
            Assert.IsFalse(h1.Equals(h4));
            Assert.IsFalse(n4.Equals(h3));
            Assert.IsFalse(n2.Equals(h4));
        }
Esempio n. 4
0
        public static void TestGetHashCode()
        {
            var jsonBooleanTrue  = new JsonBoolean(true);
            var jsonBooleanFalse = new JsonBoolean(false);

            Assert.Equal(jsonBooleanTrue.GetHashCode(), jsonBooleanTrue.GetHashCode());
            Assert.Equal(jsonBooleanTrue.GetHashCode(), new JsonBoolean(true).GetHashCode());
            Assert.Equal(jsonBooleanFalse.GetHashCode(), jsonBooleanFalse.GetHashCode());
            Assert.Equal(jsonBooleanFalse.GetHashCode(), new JsonBoolean(false).GetHashCode());
            Assert.Equal(jsonBooleanFalse.GetHashCode(), new JsonBoolean().GetHashCode());
            Assert.NotEqual(jsonBooleanTrue.GetHashCode(), new JsonBoolean().GetHashCode());

            JsonNode jsonNodeTrue = new JsonBoolean(true);

            Assert.Equal(jsonBooleanTrue.GetHashCode(), jsonNodeTrue.GetHashCode());
            JsonNode jsonNodeFalse = new JsonBoolean(false);

            Assert.Equal(jsonBooleanFalse.GetHashCode(), jsonNodeFalse.GetHashCode());

            Assert.NotEqual(jsonBooleanTrue.GetHashCode(), jsonBooleanFalse.GetHashCode());

            IEquatable <JsonBoolean> jsonBooleanIEquatableTrue = jsonBooleanTrue;

            Assert.Equal(jsonBooleanIEquatableTrue.GetHashCode(), jsonBooleanTrue.GetHashCode());

            IEquatable <JsonBoolean> jsonBooleanIEquatableFalse = jsonBooleanFalse;

            Assert.Equal(jsonBooleanIEquatableFalse.GetHashCode(), jsonBooleanFalse.GetHashCode());

            object jsonBooleanCopyTrue   = jsonBooleanTrue;
            object jsonBooleanObjectTrue = new JsonBoolean(true);

            Assert.Equal(jsonBooleanTrue.GetHashCode(), jsonBooleanCopyTrue.GetHashCode());
            Assert.Equal(jsonBooleanTrue.GetHashCode(), jsonBooleanObjectTrue.GetHashCode());

            object jsonBooleanCopyFalse   = jsonBooleanFalse;
            object jsonBooleanObjectFalse = new JsonBoolean(false);

            Assert.Equal(jsonBooleanCopyFalse.GetHashCode(), jsonBooleanFalse.GetHashCode());
            Assert.Equal(jsonBooleanObjectFalse.GetHashCode(), jsonBooleanFalse.GetHashCode());

            Assert.Equal(0, jsonBooleanFalse.GetHashCode());
            Assert.Equal(1, jsonBooleanTrue.GetHashCode());
        }
 public static void AreEqual <T>(this IEquatableGetHashCodeAssert assert, IEquatable <T> left, IEquatable <T> right)
 {
     Assert.AreEqual(left.GetHashCode(), right.GetHashCode(), $"{nameof(IEquatable<T>.GetHashCode)} failed for {left.Stringify()} and {right.Stringify()}.");
 }
Esempio n. 6
0
 public static void AssertEqual <T>(IEquatable <T> x, IEquatable <T> y)
 {
     Assert.True(x.Equals(y));
     Assert.True(((object)x).Equals(y));
     Assert.Equal(x.GetHashCode(), y.GetHashCode());
 }
            int IEqualityComparer <T> .GetHashCode(T obj)
            {
                IEquatable <TProp> tProp = this.getter(obj);

                return(tProp.GetHashCode());
            }