コード例 #1
0
        public void testHashCode()
        {
            // as lifted from the JDK Object JavaDocs:
            // Whenever it is invoked on the same object more than once
            // during an execution of a Java application, the hashCode
            // method must consistently return the same integer, provided
            // no information used in Equals comparisons on the object is
            // modified. This integer need not remain consistent from one
            // execution of an application to another execution of the
            // same application
            UUID x = new UUID(VALID_UUID_BYTE_ARRAY);
            assertTrue("x.Equals(x) didn't return true",
                        x.Equals(x));
            assertEquals("x.hashCode() didn't equal x.hashCode()",
                        x.GetHashCode(),
                        x.GetHashCode());
            assertEquals("x.hashCode() didn't equal x.hashCode()",
                        x.GetHashCode(),
                        x.GetHashCode());

            // as lifted from the JDK Object JavaDocs:
            // If two objects are equal according to the Equals(Object) method,
            // then calling the hashCode method on each of the two objects
            // must produce the same integer result
            UUID y = new UUID(VALID_UUID_BYTE_ARRAY);
            assertFalse("x == y didn't return false",
                        x == y);
            assertTrue("x.Equals(y) didn't return true",
                        x.Equals(y));
            assertEquals("x.hashCode() didn't equal y.hashCode()",
                        x.GetHashCode(),
                        y.GetHashCode());

            // it is not REQUIRED that hashCode return different ints for different
            // objects where x.Equals(z) is not true.
            // So, there is no test for that here
        }
コード例 #2
0
        public void testEquals()
        {
            // test passing null to Equals returns false
            // (as specified in the JDK docs for Object)
            UUID x = new UUID(VALID_UUID_BYTE_ARRAY);
            assertFalse("Equals(null) didn't return false",
                    x.Equals((Object)null));

            // test that passing an object which is not a UUID returns false
            assertFalse("x.Equals(non_UUID_object) didn't return false",
                        x.Equals(new Object()));

            // test a case where two UUIDs are definitly not equal
            UUID w = new UUID(ANOTHER_VALID_UUID_BYTE_ARRAY);
            assertFalse("x == w didn't return false",
                        x == w);
            assertFalse("x.Equals(w) didn't return false",
                        x.Equals(w));

            // test refelexivity
            assertTrue("x.Equals(x) didn't return true",
                        x.Equals(x));

            // test symmetry
            UUID y = new UUID(VALID_UUID_BYTE_ARRAY);
            assertFalse("x == y didn't return false",
                        x == y);
            assertTrue("y.Equals(x) didn't return true",
                        y.Equals(x));
            assertTrue("x.Equals(y) didn't return true",
                        x.Equals(y));

            // now we'll test transitivity
            UUID z = new UUID(VALID_UUID_BYTE_ARRAY);
            assertFalse("x == y didn't return false",
                        x == y);
            assertFalse("x == y didn't return false",
                        y == z);
            assertFalse("x == y didn't return false",
                        x == z);
            assertTrue("x.Equals(y) didn't return true",
                        x.Equals(y));
            assertTrue("y.Equals(z) didn't return true",
                        y.Equals(z));
            assertTrue("x.Equals(z) didn't return true",
                        x.Equals(z));

            // test consistancy (this test is just calling Equals multiple times)
            assertFalse("x == y didn't return false",
                        x == y);
            assertTrue("x.Equals(y) didn't return true",
                        x.Equals(y));
            assertTrue("x.Equals(y) didn't return true",
                        x.Equals(y));
            assertTrue("x.Equals(y) didn't return true",
                        x.Equals(y));
        }