Skip to content

Commit

Permalink
added string hashcode contract
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenvinju committed Oct 15, 2023
1 parent b915d3c commit 84dcfbe
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import java.io.IOException;
import java.io.StringReader;
import java.util.PrimitiveIterator.OfInt;
import java.util.function.IntConsumer;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;
Expand Down Expand Up @@ -92,7 +94,24 @@ public void testFingerprintStabilityIntegersDoNotChangeTheTest(IValueFactory vf,
public void testFingerprintStabilityStringDoNotChangeTheTest(IString string) {
assertEquals(string.length() == 0 ? "str".hashCode() : string.hashCode(), string.getMatchFingerprint());

// we copied the generic hashCode implementation here, to check the contract.
int h = 0;
OfInt it = string.iterator();

while (it.hasNext()) {
int c = it.nextInt();

if (!Character.isBmpCodePoint(c)) {
h = 31 * h + Character.highSurrogate(c);
h = 31 * h + Character.lowSurrogate(c);
} else {
h = 31 * h + ((char) c);
}
}

if (string.length() != 0) {
assertEquals(h, string.getMatchFingerprint());
}
}

@ParameterizedTest @ArgumentsSource(ValueProvider.class)
Expand Down

0 comments on commit 84dcfbe

Please sign in to comment.