private void checkConsistentHashCorrectness(long hashCode) { int last = 0; for (int shards = 1; shards <= 100000; shards++) { int b = Hashing.consistentHash(hashCode, shards); if (b != last) { assertEquals(shards - 1, b); last = b; } } }
public void testCombineOrdered() { HashCode hash31 = HashCode.fromInt(31); HashCode hash32 = HashCode.fromInt(32); assertEquals(hash32, Hashing.combineOrdered(ImmutableList.of(hash32))); assertEquals( HashCode.fromBytes(new byte[] {(byte) 0x80, 0, 0, 0}), Hashing.combineOrdered(ImmutableList.of(hash32, hash32))); assertEquals( HashCode.fromBytes(new byte[] {(byte) 0xa0, 0, 0, 0}), Hashing.combineOrdered(ImmutableList.of(hash32, hash32, hash32))); assertFalse( Hashing.combineOrdered(ImmutableList.of(hash31, hash32)) .equals(Hashing.combineOrdered(ImmutableList.of(hash32, hash31)))); }
static void assertSeedlessHashFunctionEquals(Class<?> clazz) throws Exception { for (Method method : clazz.getDeclaredMethods()) { if (method.getReturnType().equals(HashFunction.class) // must return HashFunction && Modifier.isPublic(method.getModifiers()) // only the public methods && method.getParameterTypes().length == 0) { // only the seed-less hash functions HashFunction hashFunction1a = (HashFunction) method.invoke(clazz); HashFunction hashFunction1b = (HashFunction) method.invoke(clazz); new EqualsTester().addEqualityGroup(hashFunction1a, hashFunction1b).testEquals(); // Make sure we're returning not only equal instances, but constants. assertSame(hashFunction1a, hashFunction1b); assertEquals(hashFunction1a.toString(), hashFunction1b.toString()); } } }
public void testConsistentHash_probabilities() { AtomicLongMap<Integer> map = AtomicLongMap.create(); Random r = new Random(9); for (int i = 0; i < ITERS; i++) { countRemaps(r.nextLong(), map); } for (int shard = 2; shard <= MAX_SHARDS; shard++) { // Rough: don't exceed 1.2x the expected number of remaps by more than 20 assertTrue(map.get(shard) <= 1.2 * ITERS / shard + 20); } }
public void testConsistentHash_outOfRange() { try { Hashing.consistentHash(5L, 0); fail(); } catch (IllegalArgumentException expected) { } }
params2[i] = random.nextLong(); } else { fail("Unable to create a random parameter for " + method.getParameterTypes()[i]); .testEquals(); assertEquals(hashFunction1a.toString(), hashFunction1b.toString());
public void testAllHashFunctionsHaveKnownHashes() throws Exception { // The following legacy hashing function methods have been covered by unit testing already. List<String> legacyHashingMethodNames = ImmutableList.of("murmur2_64", "fprint96"); for (Method method : Hashing.class.getDeclaredMethods()) { if (method.getReturnType().equals(HashFunction.class) // must return HashFunction && Modifier.isPublic(method.getModifiers()) // only the public methods && method.getParameterTypes().length == 0 // only the seed-less grapes^W hash functions && !legacyHashingMethodNames.contains(method.getName())) { HashFunction hashFunction = (HashFunction) method.invoke(Hashing.class); assertTrue( "There should be at least 3 entries in KNOWN_HASHES for " + hashFunction, KNOWN_HASHES.row(hashFunction).size() >= 3); } } }
public void testCombineOrdered_randomHashCodes() { Random random = new Random(7); List<HashCode> hashCodes = Lists.newArrayList(); for (int i = 0; i < 10; i++) { hashCodes.add(HashCode.fromLong(random.nextLong())); } HashCode hashCode1 = Hashing.combineOrdered(hashCodes); Collections.shuffle(hashCodes, random); HashCode hashCode2 = Hashing.combineOrdered(hashCodes); assertFalse(hashCode1.equals(hashCode2)); }
public void testConsistentHash_correctness() { long[] interestingValues = {-1, 0, 1, 2, Long.MAX_VALUE, Long.MIN_VALUE}; for (long h : interestingValues) { checkConsistentHashCorrectness(h); } Random r = new Random(7); for (int i = 0; i < 20; i++) { checkConsistentHashCorrectness(r.nextLong()); } }
public void testConsistentHash_ofHashCode() { checkSameResult(HashCode.fromLong(1), 1); checkSameResult(HashCode.fromLong(0x9999999999999999L), 0x9999999999999999L); checkSameResult(HashCode.fromInt(0x99999999), 0x0000000099999999L); }
public void testSeededHashFunctionEquals() throws Exception { assertSeededHashFunctionEquals(Hashing.class); }
public void testSeedlessHashFunctionEquals() throws Exception { assertSeedlessHashFunctionEquals(Hashing.class); }
public void testCombineOrdered_empty() { try { Hashing.combineOrdered(Collections.<HashCode>emptySet()); fail(); } catch (IllegalArgumentException expected) { } }
public void testConsistentHash_probabilities() { AtomicLongMap<Integer> map = AtomicLongMap.create(); Random r = new Random(9); for (int i = 0; i < ITERS; i++) { countRemaps(r.nextLong(), map); } for (int shard = 2; shard <= MAX_SHARDS; shard++) { // Rough: don't exceed 1.2x the expected number of remaps by more than 20 assertTrue(map.get(shard) <= 1.2 * ITERS / shard + 20); } }
params2[i] = random.nextLong(); } else { fail("Unable to create a random parameter for " + method.getParameterTypes()[i]); .testEquals(); assertEquals(hashFunction1a.toString(), hashFunction1b.toString());
@AndroidIncompatible // slow TODO(cpovirk): Maybe just reduce iterations under Android. public void testGoodFastHash() { for (int i = 1; i < 200; i += 17) { HashFunction hasher = Hashing.goodFastHash(i); assertTrue(hasher.bits() >= i); HashTestUtils.assertInvariants(hasher); } }
public void testCombineOrdered_randomHashCodes() { Random random = new Random(7); List<HashCode> hashCodes = Lists.newArrayList(); for (int i = 0; i < 10; i++) { hashCodes.add(HashCode.fromLong(random.nextLong())); } HashCode hashCode1 = Hashing.combineOrdered(hashCodes); Collections.shuffle(hashCodes, random); HashCode hashCode2 = Hashing.combineOrdered(hashCodes); assertFalse(hashCode1.equals(hashCode2)); }
public void testConsistentHash_correctness() { long[] interestingValues = { -1, 0, 1, 2, Long.MAX_VALUE, Long.MIN_VALUE }; for (long h : interestingValues) { checkConsistentHashCorrectness(h); } Random r = new Random(7); for (int i = 0; i < 20; i++) { checkConsistentHashCorrectness(r.nextLong()); } }
public void testConsistentHash_ofHashCode() { checkSameResult(HashCode.fromLong(1), 1); checkSameResult(HashCode.fromLong(0x9999999999999999L), 0x9999999999999999L); checkSameResult(HashCode.fromInt(0x99999999), 0x0000000099999999L); }
public void testSeededHashFunctionEquals() throws Exception { assertSeededHashFunctionEquals(Hashing.class); }