Skip to content

Commit

Permalink
Rename HighwayHash64FunctionTest to HighwayHashFunctionTest. Simp…
Browse files Browse the repository at this point in the history
…lify some testing setup.

RELNOTES=n/a
PiperOrigin-RevId: 650679959
  • Loading branch information
kluever authored and Google Java Core Libraries committed Jul 9, 2024
1 parent 558c2be commit c28e652
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 60 deletions.
58 changes: 28 additions & 30 deletions android/guava-tests/test/com/google/common/hash/HashingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.google.common.hash;

import static com.google.common.base.Charsets.UTF_8;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertThrows;

Expand All @@ -41,7 +41,7 @@
/**
* Unit tests for {@link Hashing}.
*
* <p>TODO(b/33919189): Migrate repeated testing methods to {@link #HashTestUtils} and tweak unit
* <p>TODO(b/33919189): Migrate repeated testing methods to {@link HashTestUtils} and tweak unit
* tests to reference them from there.
*
* @author Dimitris Andreou
Expand Down Expand Up @@ -419,33 +419,32 @@ public void testHashIntVsForLoop() {
assertEquals(expected, actual);
}

private static final String EMPTY_STRING = "";
private static final String TQBFJOTLD = "The quick brown fox jumps over the lazy dog";
private static final String TQBFJOTLDP = "The quick brown fox jumps over the lazy dog.";

private static final ImmutableTable<HashFunction, String, String> KNOWN_HASHES =
ImmutableTable.<HashFunction, String, String>builder()
.put(Hashing.adler32(), EMPTY_STRING, "01000000")
.put(Hashing.adler32(), "", "01000000")
.put(Hashing.adler32(), TQBFJOTLD, "da0fdc5b")
.put(Hashing.adler32(), TQBFJOTLDP, "0810e46b")
.put(Hashing.md5(), EMPTY_STRING, "d41d8cd98f00b204e9800998ecf8427e")
.put(Hashing.md5(), "", "d41d8cd98f00b204e9800998ecf8427e")
.put(Hashing.md5(), TQBFJOTLD, "9e107d9d372bb6826bd81d3542a419d6")
.put(Hashing.md5(), TQBFJOTLDP, "e4d909c290d0fb1ca068ffaddf22cbd0")
.put(Hashing.murmur3_128(), EMPTY_STRING, "00000000000000000000000000000000")
.put(Hashing.murmur3_128(), "", "00000000000000000000000000000000")
.put(Hashing.murmur3_128(), TQBFJOTLD, "6c1b07bc7bbc4be347939ac4a93c437a")
.put(Hashing.murmur3_128(), TQBFJOTLDP, "c902e99e1f4899cde7b68789a3a15d69")
.put(Hashing.murmur3_32(), EMPTY_STRING, "00000000")
.put(Hashing.murmur3_32(), "", "00000000")
.put(Hashing.murmur3_32(), TQBFJOTLD, "23f74f2e")
.put(Hashing.murmur3_32(), TQBFJOTLDP, "fc8bc4d5")
.put(Hashing.murmur3_32_fixed(), EMPTY_STRING, "00000000")
.put(Hashing.murmur3_32_fixed(), "", "00000000")
.put(Hashing.murmur3_32_fixed(), TQBFJOTLD, "23f74f2e")
.put(Hashing.murmur3_32_fixed(), TQBFJOTLDP, "fc8bc4d5")
.put(Hashing.sha1(), EMPTY_STRING, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
.put(Hashing.sha1(), "", "da39a3ee5e6b4b0d3255bfef95601890afd80709")
.put(Hashing.sha1(), TQBFJOTLD, "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12")
.put(Hashing.sha1(), TQBFJOTLDP, "408d94384216f890ff7a0c3528e8bed1e0b01621")
.put(
Hashing.sha256(),
EMPTY_STRING,
"",
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
.put(
Hashing.sha256(),
Expand All @@ -457,7 +456,7 @@ public void testHashIntVsForLoop() {
"ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c")
.put(
Hashing.sha384(),
EMPTY_STRING,
"",
"38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da2"
+ "74edebfe76f65fbd51ad2f14898b95b")
.put(
Expand All @@ -472,7 +471,7 @@ public void testHashIntVsForLoop() {
+ "a7af2819a021c2fc34e91bdb63409d7")
.put(
Hashing.sha512(),
EMPTY_STRING,
"",
"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"
+ "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e")
.put(
Expand All @@ -485,34 +484,26 @@ public void testHashIntVsForLoop() {
TQBFJOTLDP,
"91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bb"
+ "c6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed")
.put(Hashing.crc32(), EMPTY_STRING, "00000000")
.put(Hashing.crc32(), "", "00000000")
.put(Hashing.crc32(), TQBFJOTLD, "39a34f41")
.put(Hashing.crc32(), TQBFJOTLDP, "e9259051")
.put(Hashing.sipHash24(), EMPTY_STRING, "310e0edd47db6f72")
.put(Hashing.sipHash24(), "", "310e0edd47db6f72")
.put(Hashing.sipHash24(), TQBFJOTLD, "e46f1fdc05612752")
.put(Hashing.sipHash24(), TQBFJOTLDP, "9b602581fce4d4f8")
.put(Hashing.crc32c(), EMPTY_STRING, "00000000")
.put(Hashing.crc32c(), "", "00000000")
.put(Hashing.crc32c(), TQBFJOTLD, "04046222")
.put(Hashing.crc32c(), TQBFJOTLDP, "b3970019")
.put(Hashing.farmHashFingerprint64(), EMPTY_STRING, "4f40902f3b6ae19a")
.put(Hashing.farmHashFingerprint64(), "", "4f40902f3b6ae19a")
.put(Hashing.farmHashFingerprint64(), TQBFJOTLD, "34511b3bf383beab")
.put(Hashing.farmHashFingerprint64(), TQBFJOTLDP, "737d7e5f8660653e")
.put(Hashing.fingerprint2011(), EMPTY_STRING, "e365a64a907cad23")
.put(Hashing.fingerprint2011(), "", "e365a64a907cad23")
.put(Hashing.fingerprint2011(), TQBFJOTLD, "c9688c84e813b089")
.put(Hashing.fingerprint2011(), TQBFJOTLDP, "a714d70f1d569cd0")
.build();

// TODO(kak): provide a private utility method to filter the declared hashing methods

public void testAllHashFunctionsHaveKnownHashes() throws Exception {
// The following legacy hashing function methods have been covered by unit testing already.
ImmutableSet<String> legacyHashingMethodNames =
ImmutableSet.of("murmur2_64", "fprint96", "highwayFingerprint64", "highwayFingerprint128");
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())) {
if (shouldHaveKnownHashes(method)) {
HashFunction hashFunction = (HashFunction) method.invoke(Hashing.class);
assertTrue(
"There should be at least 3 entries in KNOWN_HASHES for " + hashFunction,
Expand Down Expand Up @@ -581,10 +572,7 @@ public void testGoodFastHashEquals() throws Exception {

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.getName().contains("highwayFingerprint") // tested in HighwayHash64FunctionTest
&& method.getParameterTypes().length == 0) { // only the seed-less hash functions
if (shouldHaveKnownHashes(method)) {
HashFunction hashFunction1a = (HashFunction) method.invoke(clazz);
HashFunction hashFunction1b = (HashFunction) method.invoke(clazz);

Expand All @@ -598,6 +586,16 @@ static void assertSeedlessHashFunctionEquals(Class<?> clazz) throws Exception {
}
}

private static boolean shouldHaveKnownHashes(Method method) {
// The following legacy hashing function methods have been covered by unit testing already.
ImmutableSet<String> legacyHashingMethodNames =
ImmutableSet.of("murmur2_64", "fprint96", "highwayFingerprint64", "highwayFingerprint128");
return method.getReturnType().equals(HashFunction.class) // must return HashFunction
&& Modifier.isPublic(method.getModifiers()) // only the public methods
&& method.getParameterTypes().length == 0 // only the seedless hash functions
&& !legacyHashingMethodNames.contains(method.getName());
}

static void assertSeededHashFunctionEquals(Class<?> clazz) throws Exception {
Random random = new Random(RANDOM_SEED);
for (Method method : clazz.getDeclaredMethods()) {
Expand Down
58 changes: 28 additions & 30 deletions guava-tests/test/com/google/common/hash/HashingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.google.common.hash;

import static com.google.common.base.Charsets.UTF_8;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertThrows;

Expand All @@ -41,7 +41,7 @@
/**
* Unit tests for {@link Hashing}.
*
* <p>TODO(b/33919189): Migrate repeated testing methods to {@link #HashTestUtils} and tweak unit
* <p>TODO(b/33919189): Migrate repeated testing methods to {@link HashTestUtils} and tweak unit
* tests to reference them from there.
*
* @author Dimitris Andreou
Expand Down Expand Up @@ -419,33 +419,32 @@ public void testHashIntVsForLoop() {
assertEquals(expected, actual);
}

private static final String EMPTY_STRING = "";
private static final String TQBFJOTLD = "The quick brown fox jumps over the lazy dog";
private static final String TQBFJOTLDP = "The quick brown fox jumps over the lazy dog.";

private static final ImmutableTable<HashFunction, String, String> KNOWN_HASHES =
ImmutableTable.<HashFunction, String, String>builder()
.put(Hashing.adler32(), EMPTY_STRING, "01000000")
.put(Hashing.adler32(), "", "01000000")
.put(Hashing.adler32(), TQBFJOTLD, "da0fdc5b")
.put(Hashing.adler32(), TQBFJOTLDP, "0810e46b")
.put(Hashing.md5(), EMPTY_STRING, "d41d8cd98f00b204e9800998ecf8427e")
.put(Hashing.md5(), "", "d41d8cd98f00b204e9800998ecf8427e")
.put(Hashing.md5(), TQBFJOTLD, "9e107d9d372bb6826bd81d3542a419d6")
.put(Hashing.md5(), TQBFJOTLDP, "e4d909c290d0fb1ca068ffaddf22cbd0")
.put(Hashing.murmur3_128(), EMPTY_STRING, "00000000000000000000000000000000")
.put(Hashing.murmur3_128(), "", "00000000000000000000000000000000")
.put(Hashing.murmur3_128(), TQBFJOTLD, "6c1b07bc7bbc4be347939ac4a93c437a")
.put(Hashing.murmur3_128(), TQBFJOTLDP, "c902e99e1f4899cde7b68789a3a15d69")
.put(Hashing.murmur3_32(), EMPTY_STRING, "00000000")
.put(Hashing.murmur3_32(), "", "00000000")
.put(Hashing.murmur3_32(), TQBFJOTLD, "23f74f2e")
.put(Hashing.murmur3_32(), TQBFJOTLDP, "fc8bc4d5")
.put(Hashing.murmur3_32_fixed(), EMPTY_STRING, "00000000")
.put(Hashing.murmur3_32_fixed(), "", "00000000")
.put(Hashing.murmur3_32_fixed(), TQBFJOTLD, "23f74f2e")
.put(Hashing.murmur3_32_fixed(), TQBFJOTLDP, "fc8bc4d5")
.put(Hashing.sha1(), EMPTY_STRING, "da39a3ee5e6b4b0d3255bfef95601890afd80709")
.put(Hashing.sha1(), "", "da39a3ee5e6b4b0d3255bfef95601890afd80709")
.put(Hashing.sha1(), TQBFJOTLD, "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12")
.put(Hashing.sha1(), TQBFJOTLDP, "408d94384216f890ff7a0c3528e8bed1e0b01621")
.put(
Hashing.sha256(),
EMPTY_STRING,
"",
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
.put(
Hashing.sha256(),
Expand All @@ -457,7 +456,7 @@ public void testHashIntVsForLoop() {
"ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c")
.put(
Hashing.sha384(),
EMPTY_STRING,
"",
"38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da2"
+ "74edebfe76f65fbd51ad2f14898b95b")
.put(
Expand All @@ -472,7 +471,7 @@ public void testHashIntVsForLoop() {
+ "a7af2819a021c2fc34e91bdb63409d7")
.put(
Hashing.sha512(),
EMPTY_STRING,
"",
"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"
+ "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e")
.put(
Expand All @@ -485,34 +484,26 @@ public void testHashIntVsForLoop() {
TQBFJOTLDP,
"91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bb"
+ "c6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed")
.put(Hashing.crc32(), EMPTY_STRING, "00000000")
.put(Hashing.crc32(), "", "00000000")
.put(Hashing.crc32(), TQBFJOTLD, "39a34f41")
.put(Hashing.crc32(), TQBFJOTLDP, "e9259051")
.put(Hashing.sipHash24(), EMPTY_STRING, "310e0edd47db6f72")
.put(Hashing.sipHash24(), "", "310e0edd47db6f72")
.put(Hashing.sipHash24(), TQBFJOTLD, "e46f1fdc05612752")
.put(Hashing.sipHash24(), TQBFJOTLDP, "9b602581fce4d4f8")
.put(Hashing.crc32c(), EMPTY_STRING, "00000000")
.put(Hashing.crc32c(), "", "00000000")
.put(Hashing.crc32c(), TQBFJOTLD, "04046222")
.put(Hashing.crc32c(), TQBFJOTLDP, "b3970019")
.put(Hashing.farmHashFingerprint64(), EMPTY_STRING, "4f40902f3b6ae19a")
.put(Hashing.farmHashFingerprint64(), "", "4f40902f3b6ae19a")
.put(Hashing.farmHashFingerprint64(), TQBFJOTLD, "34511b3bf383beab")
.put(Hashing.farmHashFingerprint64(), TQBFJOTLDP, "737d7e5f8660653e")
.put(Hashing.fingerprint2011(), EMPTY_STRING, "e365a64a907cad23")
.put(Hashing.fingerprint2011(), "", "e365a64a907cad23")
.put(Hashing.fingerprint2011(), TQBFJOTLD, "c9688c84e813b089")
.put(Hashing.fingerprint2011(), TQBFJOTLDP, "a714d70f1d569cd0")
.build();

// TODO(kak): provide a private utility method to filter the declared hashing methods

public void testAllHashFunctionsHaveKnownHashes() throws Exception {
// The following legacy hashing function methods have been covered by unit testing already.
ImmutableSet<String> legacyHashingMethodNames =
ImmutableSet.of("murmur2_64", "fprint96", "highwayFingerprint64", "highwayFingerprint128");
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())) {
if (shouldHaveKnownHashes(method)) {
HashFunction hashFunction = (HashFunction) method.invoke(Hashing.class);
assertTrue(
"There should be at least 3 entries in KNOWN_HASHES for " + hashFunction,
Expand Down Expand Up @@ -581,10 +572,7 @@ public void testGoodFastHashEquals() throws Exception {

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.getName().contains("highwayFingerprint") // tested in HighwayHash64FunctionTest
&& method.getParameterTypes().length == 0) { // only the seed-less hash functions
if (shouldHaveKnownHashes(method)) {
HashFunction hashFunction1a = (HashFunction) method.invoke(clazz);
HashFunction hashFunction1b = (HashFunction) method.invoke(clazz);

Expand All @@ -598,6 +586,16 @@ static void assertSeedlessHashFunctionEquals(Class<?> clazz) throws Exception {
}
}

private static boolean shouldHaveKnownHashes(Method method) {
// The following legacy hashing function methods have been covered by unit testing already.
ImmutableSet<String> legacyHashingMethodNames =
ImmutableSet.of("murmur2_64", "fprint96", "highwayFingerprint64", "highwayFingerprint128");
return method.getReturnType().equals(HashFunction.class) // must return HashFunction
&& Modifier.isPublic(method.getModifiers()) // only the public methods
&& method.getParameterTypes().length == 0 // only the seedless hash functions
&& !legacyHashingMethodNames.contains(method.getName());
}

static void assertSeededHashFunctionEquals(Class<?> clazz) throws Exception {
Random random = new Random(RANDOM_SEED);
for (Method method : clazz.getDeclaredMethods()) {
Expand Down

0 comments on commit c28e652

Please sign in to comment.