Skip to content
Permalink
Browse files
cleanup code for xxHash integration.
  • Loading branch information
leerho committed Dec 3, 2018
1 parent 6fdd588 commit c2ed55fc477bde6d6bbc70aff63095eec0238303
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
@@ -206,6 +206,13 @@
<version>${findbugs.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- Used for xxHash testing -->
<groupId>net.openhft</groupId>
<artifactId>zero-allocation-hashing</artifactId>
<version>0.8</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
@@ -228,12 +228,7 @@ public final boolean hasArray() {
*/
@Override
public final int hashCode() {
checkValid();
final Object unsafeObj = getUnsafeObject();
final long cumOffsetBytes = getCumulativeOffset();
final long lengthBytes = getCapacity();
final long seed = 0;
return (int) XxHash64.hash(unsafeObj, cumOffsetBytes, lengthBytes, seed);
return (int) xxHash64(0, getCapacity(), 0);
}

/**
@@ -9,13 +9,16 @@

import org.testng.annotations.Test;

import com.yahoo.memory.WritableMemory;

/**
* @author Lee Rhodes
*/
public class XxHash64LoopingTest {

/*
* This test is modeled from
* <a href="https://github.com/OpenHFT/Zero-Allocation-Hashing/blob/master/src/test/java/net/openhft/hashing/XxHashTest.java">
* OpenHFT/Zero-Allocation-Hashing</a> to test hash compatibility with that implementation.
*/
@Test
public void testWithSeed() {
long seed = 42L;
@@ -8,10 +8,12 @@
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

import org.testng.annotations.Test;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import com.yahoo.memory.WritableMemory;
import org.testng.annotations.Test;

import net.openhft.hashing.LongHashFunction;

/**
* @author Lee Rhodes
@@ -53,6 +55,11 @@ public void byteArrChecks() {
}
}

/*
* This test is modeled from
* <a href="https://github.com/OpenHFT/Zero-Allocation-Hashing/blob/master/src/test/java/net/openhft/hashing/XxHashTest.java">
* OpenHFT/Zero-Allocation-Hashing</a> to test hash compatibility with that implementation.
*/
@Test
public void collisionTest() {
WritableMemory wmem = WritableMemory.allocate(128);
@@ -73,4 +80,27 @@ public void collisionTest() {
assertEquals(h2, h3);
}

/**
* This simple test compares the output of {@link BaseState#xxHash64(long, long, long)} with the
* output of {@link net.openhft.hashing.LongHashFunction}, that itself is tested against the
* reference implementation in C. This increase confidence that the xxHash function implemented
* in this package is in fact the same xxHash function implemented in C.
*
* @author Roman Leventov
* @author Lee Rhodes
*/
@Test
public void testXxHash() {
Random random = ThreadLocalRandom.current();
for (int len = 0; len < 100; len++) {
byte[] bytes = new byte[len];
for (int i = 0; i < 10; i++) {
long zahXxHash = LongHashFunction.xx().hashBytes(bytes);
long memoryXxHash = Memory.wrap(bytes).xxHash64(0, len, 0);
assertEquals(memoryXxHash, zahXxHash);
random.nextBytes(bytes);
}
}
}

}

0 comments on commit c2ed55f

Please sign in to comment.