public void testFromBytes_copyOccurs() { byte[] bytes = new byte[] {(byte) 0xcd, (byte) 0xab, (byte) 0x00, (byte) 0x00}; HashCode hashCode = HashCode.fromBytes(bytes); int expectedInt = 0x0000abcd; String expectedToString = "cdab0000"; assertEquals(expectedInt, hashCode.asInt()); assertEquals(expectedToString, hashCode.toString()); bytes[0] = (byte) 0x00; assertEquals(expectedInt, hashCode.asInt()); assertEquals(expectedToString, hashCode.toString()); }
public void testFromBytesNoCopy_noCopyOccurs() { byte[] bytes = new byte[] {(byte) 0xcd, (byte) 0xab, (byte) 0x00, (byte) 0x00}; HashCode hashCode = HashCode.fromBytesNoCopy(bytes); assertEquals(0x0000abcd, hashCode.asInt()); assertEquals("cdab0000", hashCode.toString()); bytes[0] = (byte) 0x00; assertEquals(0x0000ab00, hashCode.asInt()); assertEquals("00ab0000", hashCode.toString()); }
public void testToString() { byte[] data = new byte[] {127, -128, 5, -1, 14}; assertEquals("7f8005ff0e", HashCode.fromBytes(data).toString()); assertEquals("7f8005ff0e", base16().lowerCase().encode(data)); }
public void testGetBytesInternal_noCloneOccurs() { byte[] bytes = new byte[] {(byte) 0xcd, (byte) 0xab, (byte) 0x00, (byte) 0x00}; HashCode hashCode = HashCode.fromBytes(bytes); assertEquals(0x0000abcd, hashCode.asInt()); assertEquals("cdab0000", hashCode.toString()); hashCode.getBytesInternal()[0] = (byte) 0x00; assertEquals(0x0000ab00, hashCode.asInt()); assertEquals("00ab0000", hashCode.toString()); }
public void testRoundTrip() { for (ExpectedHashCode expected : expectedHashCodes) { String string = HashCode.fromBytes(expected.bytes).toString(); assertEquals(expected.toString, string); assertEquals( expected.toString, HashCode.fromBytes(BaseEncoding.base16().lowerCase().decode(string)).toString()); } }
public void testRoundTripHashCodeUsingFromString() { HashCode hash1 = Hashing.sha1().hashString("foo", Charsets.US_ASCII); HashCode hash2 = HashCode.fromString(hash1.toString()); assertEquals(hash1, hash2); }
public void testRoundTripHashCodeUsingBaseEncoding() { HashCode hash1 = Hashing.sha1().hashString("foo", Charsets.US_ASCII); HashCode hash2 = HashCode.fromBytes(BaseEncoding.base16().lowerCase().decode(hash1.toString())); assertEquals(hash1, hash2); }
public void testObjectHashCode() { HashCode hashCode42 = HashCode.fromInt(42); assertEquals(42, hashCode42.hashCode()); }
public void testPadToLong() { assertEquals(0x1111111111111111L, HashCode.fromLong(0x1111111111111111L).padToLong()); assertEquals(0x9999999999999999L, HashCode.fromLong(0x9999999999999999L).padToLong()); assertEquals(0x0000000011111111L, HashCode.fromInt(0x11111111).padToLong()); assertEquals(0x0000000099999999L, HashCode.fromInt(0x99999999).padToLong()); }
private static void assertExpectedHashCode(ExpectedHashCode expectedHashCode, HashCode hash) { assertTrue(Arrays.equals(expectedHashCode.bytes, hash.asBytes())); byte[] bb = new byte[hash.bits() / 8]; hash.writeBytesTo(bb, 0, bb.length); assertTrue(Arrays.equals(expectedHashCode.bytes, bb)); assertEquals(expectedHashCode.asInt, hash.asInt()); if (expectedHashCode.asLong == null) { try { hash.asLong(); fail(); } catch (IllegalStateException expected) { } } else { assertEquals(expectedHashCode.asLong.longValue(), hash.asLong()); } assertEquals(expectedHashCode.toString, hash.toString()); assertSideEffectFree(hash); assertReadableBytes(hash); }
public void testPadToLongWith6Bytes() { assertEquals(0x0000999999999999L, HashCode.fromBytesNoCopy(byteArrayWith9s(6)).padToLong()); }
public void testPadToLongWith8Bytes() { assertEquals(0x9999999999999999L, HashCode.fromBytesNoCopy(byteArrayWith9s(8)).padToLong()); }
public void testPadToLongWith4Bytes() { assertEquals(0x0000000099999999L, HashCode.fromBytesNoCopy(byteArrayWith9s(4)).padToLong()); }
public void testObjectHashCodeWithSameLowOrderBytes() { // These will have the same first 4 bytes (all 0). byte[] bytesA = new byte[5]; byte[] bytesB = new byte[5]; // Change only the last (5th) byte bytesA[4] = (byte) 0xbe; bytesB[4] = (byte) 0xef; HashCode hashCodeA = HashCode.fromBytes(bytesA); HashCode hashCodeB = HashCode.fromBytes(bytesB); // They aren't equal... assertFalse(hashCodeA.equals(hashCodeB)); // But they still have the same Object#hashCode() value. // Technically not a violation of the equals/hashCode contract, but...? assertEquals(hashCodeA.hashCode(), hashCodeB.hashCode()); }
public void testFromBytes_copyOccurs() { byte[] bytes = new byte[] { (byte) 0xcd, (byte) 0xab, (byte) 0x00, (byte) 0x00 }; HashCode hashCode = HashCode.fromBytes(bytes); int expectedInt = 0x0000abcd; String expectedToString = "cdab0000"; assertEquals(expectedInt, hashCode.asInt()); assertEquals(expectedToString, hashCode.toString()); bytes[0] = (byte) 0x00; assertEquals(expectedInt, hashCode.asInt()); assertEquals(expectedToString, hashCode.toString()); }
public void testFromBytesNoCopy_noCopyOccurs() { byte[] bytes = new byte[] { (byte) 0xcd, (byte) 0xab, (byte) 0x00, (byte) 0x00 }; HashCode hashCode = HashCode.fromBytesNoCopy(bytes); assertEquals(0x0000abcd, hashCode.asInt()); assertEquals("cdab0000", hashCode.toString()); bytes[0] = (byte) 0x00; assertEquals(0x0000ab00, hashCode.asInt()); assertEquals("00ab0000", hashCode.toString()); }
public void testGetBytesInternal_noCloneOccurs() { byte[] bytes = new byte[] { (byte) 0xcd, (byte) 0xab, (byte) 0x00, (byte) 0x00 }; HashCode hashCode = HashCode.fromBytes(bytes); assertEquals(0x0000abcd, hashCode.asInt()); assertEquals("cdab0000", hashCode.toString()); hashCode.getBytesInternal()[0] = (byte) 0x00; assertEquals(0x0000ab00, hashCode.asInt()); assertEquals("00ab0000", hashCode.toString()); }
public void testToString() { byte[] data = new byte[] { 127, -128, 5, -1, 14 }; assertEquals("7f8005ff0e", HashCode.fromBytes(data).toString()); assertEquals("7f8005ff0e", base16().lowerCase().encode(data)); }
public void testRoundTripHashCodeUsingFromString() { HashCode hash1 = Hashing.sha1().hashString("foo", Charsets.US_ASCII); HashCode hash2 = HashCode.fromString(hash1.toString()); assertEquals(hash1, hash2); }
public void testObjectHashCode() { HashCode hashCode42 = HashCode.fromInt(42); assertEquals(42, hashCode42.hashCode()); }