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 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); } }
@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 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 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); } }
@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); } }