|
|
e63c38 |
diff --git a/.classpath b/.classpath
|
|
|
e63c38 |
index 058b340..c48898b 100644
|
|
|
e63c38 |
--- a/.classpath
|
|
|
e63c38 |
+++ b/.classpath
|
|
|
e63c38 |
@@ -7,7 +7,5 @@
|
|
|
e63c38 |
<classpathentry kind="src" path="src/test"/>
|
|
|
e63c38 |
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
|
|
e63c38 |
<classpathentry kind="lib" path="lib/hamcrest-core-1.1.jar"/>
|
|
|
e63c38 |
- <classpathentry kind="lib" path="lib/junit-4.10.jar"/>
|
|
|
e63c38 |
- <classpathentry kind="lib" path="lib/randomizedtesting-runner-2.0.9.jar"/>
|
|
|
e63c38 |
<classpathentry kind="output" path="bin"/>
|
|
|
e63c38 |
</classpath>
|
|
|
e63c38 |
|
|
|
e63c38 |
diff --git a/build.xml b/build.xml
|
|
|
e63c38 |
index 1bdc702..919386f 100644
|
|
|
e63c38 |
--- a/build.xml
|
|
|
e63c38 |
+++ b/build.xml
|
|
|
e63c38 |
@@ -14,7 +14,6 @@
|
|
|
e63c38 |
|
|
|
e63c38 |
|
|
|
e63c38 |
xmlns:ivy="antlib:org.apache.ivy.ant"
|
|
|
e63c38 |
- xmlns:junit4="antlib:com.carrotsearch.junit4"
|
|
|
e63c38 |
xmlns:mvn="antlib:org.apache.maven.artifact.ant">
|
|
|
e63c38 |
|
|
|
e63c38 |
<property name="src" location="src"/>
|
|
|
e63c38 |
@@ -105,15 +104,6 @@
|
|
|
e63c38 |
<ivy:retrieve />
|
|
|
e63c38 |
</target>
|
|
|
e63c38 |
|
|
|
e63c38 |
- <target name="install-junit4" depends="init" unless="junit4.available">
|
|
|
e63c38 |
- <taskdef uri="antlib:com.carrotsearch.junit4">
|
|
|
e63c38 |
- <classpath>
|
|
|
e63c38 |
- <fileset dir="${lib}" includes="*.jar" />
|
|
|
e63c38 |
- </classpath>
|
|
|
e63c38 |
- </taskdef>
|
|
|
e63c38 |
- <property name="junit4.available" value="true" />
|
|
|
e63c38 |
- </target>
|
|
|
e63c38 |
-
|
|
|
e63c38 |
<target name="generate-sources" depends="init">
|
|
|
e63c38 |
|
|
|
e63c38 |
inline="true" conf="default" transitive="true" pathid="mvel.classpath"/>
|
|
|
e63c38 |
@@ -236,37 +226,14 @@
|
|
|
e63c38 |
<antcall target="-test" />
|
|
|
e63c38 |
</target>
|
|
|
e63c38 |
|
|
|
e63c38 |
- <target name="-test" depends="compile-tests, install-junit4">
|
|
|
e63c38 |
- <mkdir dir="${build}/tests" />
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- dir="${java.io.tmpdir}"
|
|
|
e63c38 |
- maxmemory="300m"
|
|
|
e63c38 |
- parallelism="auto">
|
|
|
e63c38 |
- <assertions>
|
|
|
e63c38 |
- <enable package="net.jpountz.lz4"/>
|
|
|
e63c38 |
- </assertions>
|
|
|
e63c38 |
+ <target name="-test" depends="compile-tests">
|
|
|
e63c38 |
+ <junit printsummary="on" showoutput="true" failureProperty="test.failure">
|
|
|
e63c38 |
<classpath refid="${prop.test.classpath}" />
|
|
|
e63c38 |
- <fileset dir="${build}/test-classes/">
|
|
|
e63c38 |
- <include name="**/*Test.class" />
|
|
|
e63c38 |
- <exclude name="**/*$*" />
|
|
|
e63c38 |
- <exclude name="**/Abstract*" />
|
|
|
e63c38 |
- </fileset>
|
|
|
e63c38 |
- <listeners>
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- showThrowable="true"
|
|
|
e63c38 |
- showStackTraces="true"
|
|
|
e63c38 |
- showOutput="never"
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- showStatusOk="true"
|
|
|
e63c38 |
- showStatusError="true"
|
|
|
e63c38 |
- showStatusFailure="true"
|
|
|
e63c38 |
- showStatusIgnored="true"
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- showSuiteSummary="false" />
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- <junit4:report-ant-xml dir="${build}/tests" />
|
|
|
e63c38 |
- </listeners>
|
|
|
e63c38 |
- </junit4:junit4>
|
|
|
e63c38 |
+ <test name="net.jpountz.lz4.LZ4FrameIOStreamTest" />
|
|
|
e63c38 |
+ <test name="net.jpountz.lz4.LZ4FactoryTest" />
|
|
|
e63c38 |
+ <test name="net.jpountz.xxhash.XXHashFactoryTest" />
|
|
|
e63c38 |
+ </junit>
|
|
|
e63c38 |
+ <fail message="tests failed" if="test.failure" />
|
|
|
e63c38 |
</target>
|
|
|
e63c38 |
|
|
|
e63c38 |
<target name="test-skip-jni" description="run tests without building JNI">
|
|
|
e63c38 |
diff --git a/ivy-pure-java.xml b/ivy-pure-java.xml
|
|
|
e63c38 |
index 6e99e19..9c1c99b 100644
|
|
|
e63c38 |
--- a/ivy-pure-java.xml
|
|
|
e63c38 |
+++ b/ivy-pure-java.xml
|
|
|
e63c38 |
@@ -21,6 +21,6 @@
|
|
|
e63c38 |
</configurations>
|
|
|
e63c38 |
|
|
|
e63c38 |
<dependencies>
|
|
|
e63c38 |
- <dependency org="com.carrotsearch.randomizedtesting" name="junit4-ant" rev="2.7.3" transitive="true" conf="test->*,!sources,!javadoc" />
|
|
|
e63c38 |
+ <dependency org="junit" name="junit" rev="4.12" conf="test->default" />
|
|
|
e63c38 |
</dependencies>
|
|
|
e63c38 |
</ivy-module>
|
|
|
e63c38 |
diff --git a/ivy.xml b/ivy.xml
|
|
|
e63c38 |
index 6643da7..e5a9620 100644
|
|
|
e63c38 |
--- a/ivy.xml
|
|
|
e63c38 |
+++ b/ivy.xml
|
|
|
e63c38 |
@@ -21,6 +21,6 @@
|
|
|
e63c38 |
</configurations>
|
|
|
e63c38 |
|
|
|
e63c38 |
<dependencies>
|
|
|
e63c38 |
- <dependency org="com.carrotsearch.randomizedtesting" name="junit4-ant" rev="2.7.3" transitive="true" conf="test->*,!sources,!javadoc" />
|
|
|
e63c38 |
+ <dependency org="junit" name="junit" rev="4.12" conf="test->default" />
|
|
|
e63c38 |
</dependencies>
|
|
|
e63c38 |
</ivy-module>
|
|
|
e63c38 |
diff --git a/src/test/net/jpountz/lz4/AbstractLZ4Test.java b/src/test/net/jpountz/lz4/AbstractLZ4Test.java
|
|
|
e63c38 |
deleted file mode 100644
|
|
|
e63c38 |
index 8c5cd38..0000000
|
|
|
e63c38 |
--- a/src/test/net/jpountz/lz4/AbstractLZ4Test.java
|
|
|
e63c38 |
+++ /dev/null
|
|
|
e63c38 |
@@ -1,257 +0,0 @@
|
|
|
e63c38 |
-package net.jpountz.lz4;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-/*
|
|
|
e63c38 |
- * Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
e63c38 |
- * you may not use this file except in compliance with the License.
|
|
|
e63c38 |
- * You may obtain a copy of the License at
|
|
|
e63c38 |
- *
|
|
|
e63c38 |
- * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
e63c38 |
- *
|
|
|
e63c38 |
- * Unless required by applicable law or agreed to in writing, software
|
|
|
e63c38 |
- * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
e63c38 |
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
e63c38 |
- * See the License for the specific language governing permissions and
|
|
|
e63c38 |
- * limitations under the License.
|
|
|
e63c38 |
- */
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-import java.io.ByteArrayOutputStream;
|
|
|
e63c38 |
-import java.io.IOException;
|
|
|
e63c38 |
-import java.io.InputStream;
|
|
|
e63c38 |
-import java.nio.ByteBuffer;
|
|
|
e63c38 |
-import java.nio.ByteOrder;
|
|
|
e63c38 |
-import java.util.Arrays;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-import com.carrotsearch.randomizedtesting.RandomizedTest;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-public abstract class AbstractLZ4Test extends RandomizedTest {
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- public interface Tester<T> {
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- T allocate(int length);
|
|
|
e63c38 |
- T copyOf(byte[] array);
|
|
|
e63c38 |
- byte[] copyOf(T data, int off, int len);
|
|
|
e63c38 |
- int maxCompressedLength(int len);
|
|
|
e63c38 |
- int compress(LZ4Compressor compressor, T src, int srcOff, int srcLen, T dest, int destOff, int maxDestLen);
|
|
|
e63c38 |
- int decompress(LZ4FastDecompressor decompressor, T src, int srcOff, T dest, int destOff, int destLen);
|
|
|
e63c38 |
- int decompress(LZ4SafeDecompressor decompressor, T src, int srcOff, int srcLen, T dest, int destOff, int maxDestLen);
|
|
|
e63c38 |
- void fill(T instance, byte b);
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- public static class ByteArrayTester implements Tester<byte[]> {
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public byte[] allocate(int length) {
|
|
|
e63c38 |
- return new byte[length];
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public byte[] copyOf(byte[] array) {
|
|
|
e63c38 |
- return Arrays.copyOf(array, array.length);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public byte[] copyOf(byte[] data, int off, int len) {
|
|
|
e63c38 |
- return Arrays.copyOfRange(data, off, off + len);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public int maxCompressedLength(int len) {
|
|
|
e63c38 |
- return LZ4Utils.maxCompressedLength(len);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public int compress(LZ4Compressor compressor, byte[] src, int srcOff,
|
|
|
e63c38 |
- int srcLen, byte[] dest, int destOff, int maxDestLen) {
|
|
|
e63c38 |
- return compressor.compress(src, srcOff, srcLen, dest, destOff, maxDestLen);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public int decompress(LZ4FastDecompressor decompressor,
|
|
|
e63c38 |
- byte[] src, int srcOff, byte[] dest, int destOff, int destLen) {
|
|
|
e63c38 |
- return decompressor.decompress(src, srcOff, dest, destOff, destLen);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public int decompress(LZ4SafeDecompressor decompressor,
|
|
|
e63c38 |
- byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen) {
|
|
|
e63c38 |
- return decompressor.decompress(src, srcOff, srcLen, dest, destOff, maxDestLen);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public void fill(byte[] instance, byte b) {
|
|
|
e63c38 |
- Arrays.fill(instance, b);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- public static final Tester<byte[]> BYTE_ARRAY = new ByteArrayTester();
|
|
|
e63c38 |
- public static final Tester<byte[]> BYTE_ARRAY_WITH_LENGTH = new ByteArrayTester() {
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public int compress(LZ4Compressor compressor, byte[] src, int srcOff,
|
|
|
e63c38 |
- int srcLen, byte[] dest, int destOff, int maxDestLen) {
|
|
|
e63c38 |
- return new LZ4CompressorWithLength(compressor).compress(src, srcOff, srcLen, dest, destOff, maxDestLen);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public int decompress(LZ4FastDecompressor decompressor,
|
|
|
e63c38 |
- byte[] src, int srcOff, byte[] dest, int destOff, int destLen) {
|
|
|
e63c38 |
- return new LZ4DecompressorWithLength(decompressor).decompress(src, srcOff, dest, destOff);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public int decompress(LZ4SafeDecompressor decompressor,
|
|
|
e63c38 |
- byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen) {
|
|
|
e63c38 |
- return -1;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- };
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- public static class ByteBufferTester implements Tester<ByteBuffer> {
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public ByteBuffer allocate(int length) {
|
|
|
e63c38 |
- ByteBuffer bb;
|
|
|
e63c38 |
- int slice = randomInt(5);
|
|
|
e63c38 |
- if (randomBoolean()) {
|
|
|
e63c38 |
- bb = ByteBuffer.allocate(length + slice);
|
|
|
e63c38 |
- } else {
|
|
|
e63c38 |
- bb = ByteBuffer.allocateDirect(length + slice);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- bb.position(slice);
|
|
|
e63c38 |
- bb = bb.slice();
|
|
|
e63c38 |
- if (randomBoolean()) {
|
|
|
e63c38 |
- bb.order(ByteOrder.LITTLE_ENDIAN);
|
|
|
e63c38 |
- } else {
|
|
|
e63c38 |
- bb.order(ByteOrder.BIG_ENDIAN);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- return bb;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public ByteBuffer copyOf(byte[] array) {
|
|
|
e63c38 |
- ByteBuffer bb = allocate(array.length).put(array);
|
|
|
e63c38 |
- if (randomBoolean()) {
|
|
|
e63c38 |
- bb = bb.asReadOnlyBuffer();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- bb.position(0);
|
|
|
e63c38 |
- return bb;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public byte[] copyOf(ByteBuffer data, int off, int len) {
|
|
|
e63c38 |
- byte[] copy = new byte[len];
|
|
|
e63c38 |
- data.position(off);
|
|
|
e63c38 |
- data.get(copy);
|
|
|
e63c38 |
- return copy;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public int maxCompressedLength(int len) {
|
|
|
e63c38 |
- return LZ4Utils.maxCompressedLength(len);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public int compress(LZ4Compressor compressor, ByteBuffer src, int srcOff,
|
|
|
e63c38 |
- int srcLen, ByteBuffer dest, int destOff, int maxDestLen) {
|
|
|
e63c38 |
- return compressor.compress(src, srcOff, srcLen, dest, destOff, maxDestLen);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public int decompress(LZ4FastDecompressor decompressor, ByteBuffer src,
|
|
|
e63c38 |
- int srcOff, ByteBuffer dest, int destOff, int destLen) {
|
|
|
e63c38 |
- return decompressor.decompress(src, srcOff, dest, destOff, destLen);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public int decompress(LZ4SafeDecompressor decompressor, ByteBuffer src,
|
|
|
e63c38 |
- int srcOff, int srcLen, ByteBuffer dest, int destOff, int maxDestLen) {
|
|
|
e63c38 |
- return decompressor.decompress(src, srcOff, srcLen, dest, destOff, maxDestLen);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public void fill(ByteBuffer instance, byte b) {
|
|
|
e63c38 |
- for (int i = 0; i < instance.capacity(); ++i) {
|
|
|
e63c38 |
- instance.put(i, b);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- public static final Tester<ByteBuffer> BYTE_BUFFER = new ByteBufferTester();
|
|
|
e63c38 |
- public static final Tester<ByteBuffer> BYTE_BUFFER_WITH_LENGTH = new ByteBufferTester() {
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public int compress(LZ4Compressor compressor, ByteBuffer src, int srcOff,
|
|
|
e63c38 |
- int srcLen, ByteBuffer dest, int destOff, int maxDestLen) {
|
|
|
e63c38 |
- return new LZ4CompressorWithLength(compressor).compress(src, srcOff, srcLen, dest, destOff, maxDestLen);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public int decompress(LZ4FastDecompressor decompressor, ByteBuffer src,
|
|
|
e63c38 |
- int srcOff, ByteBuffer dest, int destOff, int destLen) {
|
|
|
e63c38 |
- return new LZ4DecompressorWithLength(decompressor).decompress(src, srcOff, dest, destOff);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public int decompress(LZ4SafeDecompressor decompressor, ByteBuffer src,
|
|
|
e63c38 |
- int srcOff, int srcLen, ByteBuffer dest, int destOff, int maxDestLen) {
|
|
|
e63c38 |
- return -1;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- };
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- protected class RandomBytes {
|
|
|
e63c38 |
- private final byte[] bytes;
|
|
|
e63c38 |
- RandomBytes(int n) {
|
|
|
e63c38 |
- assert n > 0 && n <= 256;
|
|
|
e63c38 |
- bytes = new byte[n];
|
|
|
e63c38 |
- for (int i = 0; i < n; ++i) {
|
|
|
e63c38 |
- bytes[i] = (byte) randomInt(255);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- byte next() {
|
|
|
e63c38 |
- final int i = randomInt(bytes.length - 1);
|
|
|
e63c38 |
- return bytes[i];
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- protected static byte[] readResource(String resource) throws IOException {
|
|
|
e63c38 |
- InputStream is = LZ4Test.class.getResourceAsStream(resource);
|
|
|
e63c38 |
- if (is == null) {
|
|
|
e63c38 |
- throw new IllegalStateException("Cannot find " + resource);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- byte[] buf = new byte[4096];
|
|
|
e63c38 |
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- while (true) {
|
|
|
e63c38 |
- final int read = is.read(buf);
|
|
|
e63c38 |
- if (read == -1) {
|
|
|
e63c38 |
- break;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- baos.write(buf, 0, read);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- } finally {
|
|
|
e63c38 |
- is.close();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- return baos.toByteArray();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- protected byte[] randomArray(int len, int n) {
|
|
|
e63c38 |
- byte[] result = new byte[len];
|
|
|
e63c38 |
- RandomBytes randomBytes = new RandomBytes(n);
|
|
|
e63c38 |
- for (int i = 0; i < result.length; ++i) {
|
|
|
e63c38 |
- result[i] = randomBytes.next();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- return result;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- protected ByteBuffer copyOf(byte[] bytes, int offset, int len) {
|
|
|
e63c38 |
- ByteBuffer buffer;
|
|
|
e63c38 |
- if (randomBoolean()) {
|
|
|
e63c38 |
- buffer = ByteBuffer.allocate(bytes.length);
|
|
|
e63c38 |
- } else {
|
|
|
e63c38 |
- buffer = ByteBuffer.allocateDirect(bytes.length);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- buffer.put(bytes);
|
|
|
e63c38 |
- buffer.position(offset);
|
|
|
e63c38 |
- buffer.limit(offset + len);
|
|
|
e63c38 |
- if (randomBoolean()) {
|
|
|
e63c38 |
- buffer = buffer.asReadOnlyBuffer();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- return buffer;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-}
|
|
|
e63c38 |
diff --git a/src/test/net/jpountz/lz4/LZ4BlockStreamingTest.java b/src/test/net/jpountz/lz4/LZ4BlockStreamingTest.java
|
|
|
e63c38 |
deleted file mode 100644
|
|
|
e63c38 |
index 9d6fc5b..0000000
|
|
|
e63c38 |
--- a/src/test/net/jpountz/lz4/LZ4BlockStreamingTest.java
|
|
|
e63c38 |
+++ /dev/null
|
|
|
e63c38 |
@@ -1,347 +0,0 @@
|
|
|
e63c38 |
-package net.jpountz.lz4;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-/*
|
|
|
e63c38 |
- * Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
e63c38 |
- * you may not use this file except in compliance with the License.
|
|
|
e63c38 |
- * You may obtain a copy of the License at
|
|
|
e63c38 |
- *
|
|
|
e63c38 |
- * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
e63c38 |
- *
|
|
|
e63c38 |
- * Unless required by applicable law or agreed to in writing, software
|
|
|
e63c38 |
- * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
e63c38 |
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
e63c38 |
- * See the License for the specific language governing permissions and
|
|
|
e63c38 |
- * limitations under the License.
|
|
|
e63c38 |
- */
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-import java.io.ByteArrayInputStream;
|
|
|
e63c38 |
-import java.io.ByteArrayOutputStream;
|
|
|
e63c38 |
-import java.io.FilterInputStream;
|
|
|
e63c38 |
-import java.io.FilterOutputStream;
|
|
|
e63c38 |
-import java.io.IOException;
|
|
|
e63c38 |
-import java.io.InputStream;
|
|
|
e63c38 |
-import java.io.OutputStream;
|
|
|
e63c38 |
-import java.nio.charset.Charset;
|
|
|
e63c38 |
-import java.util.Arrays;
|
|
|
e63c38 |
-import java.util.zip.Adler32;
|
|
|
e63c38 |
-import java.util.zip.CRC32;
|
|
|
e63c38 |
-import java.util.zip.Checksum;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-import net.jpountz.xxhash.XXHashFactory;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-import org.junit.Test;
|
|
|
e63c38 |
-import static org.junit.Assert.*;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-import com.carrotsearch.randomizedtesting.annotations.Repeat;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-public class LZ4BlockStreamingTest extends AbstractLZ4Test {
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- // An input stream that might read less data than it is able to
|
|
|
e63c38 |
- class MockInputStream extends FilterInputStream {
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- MockInputStream(InputStream in) {
|
|
|
e63c38 |
- super(in);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public int read(byte[] b, int off, int len) throws IOException {
|
|
|
e63c38 |
- return super.read(b, off, randomIntBetween(0, len));
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public long skip(long n) throws IOException {
|
|
|
e63c38 |
- return super.skip(randomInt((int) Math.min(n, Integer.MAX_VALUE)));
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- // an output stream that delays the actual writes
|
|
|
e63c38 |
- class MockOutputStream extends FilterOutputStream {
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- private final byte[] buffer;
|
|
|
e63c38 |
- private int delayedBytes;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- MockOutputStream(OutputStream out) {
|
|
|
e63c38 |
- super(out);
|
|
|
e63c38 |
- buffer = new byte[randomIntBetween(10, 1000)];
|
|
|
e63c38 |
- delayedBytes = 0;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- private void flushIfNecessary() throws IOException {
|
|
|
e63c38 |
- if (delayedBytes == buffer.length) {
|
|
|
e63c38 |
- flushPendingData();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- private void flushPendingData() throws IOException {
|
|
|
e63c38 |
- out.write(buffer, 0, delayedBytes);
|
|
|
e63c38 |
- delayedBytes = 0;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public void write(int b) throws IOException {
|
|
|
e63c38 |
- if (rarely()) {
|
|
|
e63c38 |
- flushPendingData();
|
|
|
e63c38 |
- } else {
|
|
|
e63c38 |
- flushIfNecessary();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- buffer[delayedBytes++] = (byte) b;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public void write(byte[] b, int off, int len) throws IOException {
|
|
|
e63c38 |
- if (rarely()) {
|
|
|
e63c38 |
- flushPendingData();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- if (len + delayedBytes > buffer.length) {
|
|
|
e63c38 |
- flushPendingData();
|
|
|
e63c38 |
- delayedBytes = randomInt(Math.min(len, buffer.length));
|
|
|
e63c38 |
- out.write(b, off, len - delayedBytes);
|
|
|
e63c38 |
- System.arraycopy(b, off + len - delayedBytes, buffer, 0, delayedBytes);
|
|
|
e63c38 |
- } else {
|
|
|
e63c38 |
- System.arraycopy(b, off, buffer, delayedBytes, len);
|
|
|
e63c38 |
- delayedBytes += len;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public void write(byte[] b) throws IOException {
|
|
|
e63c38 |
- write(b, 0, b.length);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public void flush() throws IOException {
|
|
|
e63c38 |
- // no-op
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public void close() throws IOException {
|
|
|
e63c38 |
- flushPendingData();
|
|
|
e63c38 |
- out.close();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- private InputStream open(byte[] data) {
|
|
|
e63c38 |
- return new MockInputStream(new ByteArrayInputStream(data));
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- private OutputStream wrap(OutputStream other) {
|
|
|
e63c38 |
- return new MockOutputStream(other);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- @Repeat(iterations=5)
|
|
|
e63c38 |
- public void testRoundtripGeo() throws IOException {
|
|
|
e63c38 |
- testRoundTrip("/calgary/geo");
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- @Repeat(iterations=5)
|
|
|
e63c38 |
- public void testRoundtripBook1() throws IOException {
|
|
|
e63c38 |
- testRoundTrip("/calgary/book1");
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- @Repeat(iterations=5)
|
|
|
e63c38 |
- public void testRoundtripPic() throws IOException {
|
|
|
e63c38 |
- testRoundTrip("/calgary/pic");
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- public void testRoundTrip(String resource) throws IOException {
|
|
|
e63c38 |
- testRoundTrip(readResource(resource));
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- public void testRoundTrip(byte[] data) throws IOException {
|
|
|
e63c38 |
- final ByteArrayOutputStream compressed = new ByteArrayOutputStream();
|
|
|
e63c38 |
- final int blockSize;
|
|
|
e63c38 |
- switch (randomInt(2)) {
|
|
|
e63c38 |
- case 0:
|
|
|
e63c38 |
- blockSize = LZ4BlockOutputStream.MIN_BLOCK_SIZE;
|
|
|
e63c38 |
- break;
|
|
|
e63c38 |
- case 1:
|
|
|
e63c38 |
- blockSize = LZ4BlockOutputStream.MAX_BLOCK_SIZE;
|
|
|
e63c38 |
- break;
|
|
|
e63c38 |
- default:
|
|
|
e63c38 |
- blockSize = randomIntBetween(LZ4BlockOutputStream.MIN_BLOCK_SIZE, LZ4BlockOutputStream.MAX_BLOCK_SIZE);
|
|
|
e63c38 |
- break;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- final LZ4Compressor compressor = randomBoolean()
|
|
|
e63c38 |
- ? LZ4Factory.fastestInstance().fastCompressor()
|
|
|
e63c38 |
- : LZ4Factory.fastestInstance().highCompressor();
|
|
|
e63c38 |
- final Checksum checksum;
|
|
|
e63c38 |
- switch (randomInt(2)) {
|
|
|
e63c38 |
- case 0:
|
|
|
e63c38 |
- checksum = new Adler32();
|
|
|
e63c38 |
- break;
|
|
|
e63c38 |
- case 1:
|
|
|
e63c38 |
- checksum = new CRC32();
|
|
|
e63c38 |
- break;
|
|
|
e63c38 |
- default:
|
|
|
e63c38 |
- checksum = XXHashFactory.fastestInstance().newStreamingHash32(randomInt()).asChecksum();
|
|
|
e63c38 |
- break;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- final boolean syncFlush = randomBoolean();
|
|
|
e63c38 |
- final LZ4BlockOutputStream os = new LZ4BlockOutputStream(wrap(compressed), blockSize, compressor, checksum, syncFlush);
|
|
|
e63c38 |
- final int half = data.length / 2;
|
|
|
e63c38 |
- switch (randomInt(2)) {
|
|
|
e63c38 |
- case 0:
|
|
|
e63c38 |
- os.write(data, 0, half);
|
|
|
e63c38 |
- for (int i = half; i < data.length; ++i) {
|
|
|
e63c38 |
- os.write(data[i]);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- break;
|
|
|
e63c38 |
- case 1:
|
|
|
e63c38 |
- for (int i = 0; i < half; ++i) {
|
|
|
e63c38 |
- os.write(data[i]);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- os.write(data, half, data.length - half);
|
|
|
e63c38 |
- break;
|
|
|
e63c38 |
- case 2:
|
|
|
e63c38 |
- os.write(data, 0, data.length);
|
|
|
e63c38 |
- break;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- os.close();
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- final LZ4FastDecompressor decompressor = LZ4Factory.fastestInstance().fastDecompressor();
|
|
|
e63c38 |
- InputStream is = new LZ4BlockInputStream(open(compressed.toByteArray()), decompressor, checksum);
|
|
|
e63c38 |
- assertFalse(is.markSupported());
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- is.mark(1);
|
|
|
e63c38 |
- is.reset();
|
|
|
e63c38 |
- assertFalse(true);
|
|
|
e63c38 |
- } catch (IOException e) {
|
|
|
e63c38 |
- // OK
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- byte[] restored = new byte[data.length + 1000];
|
|
|
e63c38 |
- int read = 0;
|
|
|
e63c38 |
- while (true) {
|
|
|
e63c38 |
- if (randomFloat() < 0.01f) {
|
|
|
e63c38 |
- final int r = is.read(restored, read, restored.length - read);
|
|
|
e63c38 |
- if (r == -1) {
|
|
|
e63c38 |
- break;
|
|
|
e63c38 |
- } else {
|
|
|
e63c38 |
- read += r;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- } else {
|
|
|
e63c38 |
- final int b = is.read();
|
|
|
e63c38 |
- if (b == -1) {
|
|
|
e63c38 |
- break;
|
|
|
e63c38 |
- } else {
|
|
|
e63c38 |
- restored[read++] = (byte) b;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- is.close();
|
|
|
e63c38 |
- assertEquals(data.length, read);
|
|
|
e63c38 |
- assertArrayEquals(data, Arrays.copyOf(restored, read));
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- // test skip
|
|
|
e63c38 |
- final int offset = data.length <= 1 ? 0 : randomInt(data.length - 1);
|
|
|
e63c38 |
- final int length = randomInt(data.length - offset);
|
|
|
e63c38 |
- is = new LZ4BlockInputStream(open(compressed.toByteArray()), decompressor, checksum);
|
|
|
e63c38 |
- restored = new byte[length + 1000];
|
|
|
e63c38 |
- read = 0;
|
|
|
e63c38 |
- while (read < offset) {
|
|
|
e63c38 |
- final long skipped = is.skip(offset - read);
|
|
|
e63c38 |
- assertTrue(skipped >= 0);
|
|
|
e63c38 |
- read += skipped;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- read = 0;
|
|
|
e63c38 |
- while (read < length) {
|
|
|
e63c38 |
- final int r = is.read(restored, read, length - read);
|
|
|
e63c38 |
- assertTrue(r >= 0);
|
|
|
e63c38 |
- read += r;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- is.close();
|
|
|
e63c38 |
- assertArrayEquals(Arrays.copyOfRange(data, offset, offset + length), Arrays.copyOfRange(restored, 0, length));
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- @Repeat(iterations=20)
|
|
|
e63c38 |
- public void testRoundtripRandom() throws IOException {
|
|
|
e63c38 |
- final int size = randomFloat() < 0.1f ? randomInt(5) : randomInt(1 << 20);
|
|
|
e63c38 |
- final byte[] data = randomArray(size, randomBoolean() ? randomIntBetween(1, 5) : randomIntBetween(6, 100));
|
|
|
e63c38 |
- testRoundTrip(data);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void testRoundtripEmpty() throws IOException {
|
|
|
e63c38 |
- testRoundTrip(new byte[0]);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void testDoubleClose() throws IOException {
|
|
|
e63c38 |
- final byte[] testBytes = "Testing!".getBytes(Charset.forName("UTF-8"));
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
|
|
e63c38 |
- LZ4BlockOutputStream out = new LZ4BlockOutputStream(bytes);
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- out.write(testBytes);
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- out.close();
|
|
|
e63c38 |
- out.close();
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- LZ4BlockInputStream in = new LZ4BlockInputStream(new ByteArrayInputStream(bytes.toByteArray()));
|
|
|
e63c38 |
- byte[] actual = new byte[testBytes.length];
|
|
|
e63c38 |
- in.read(actual);
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- assertArrayEquals(testBytes, actual);
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- in.close();
|
|
|
e63c38 |
- in.close();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- private static int readFully(InputStream in, byte[] b) throws IOException {
|
|
|
e63c38 |
- int total;
|
|
|
e63c38 |
- int result;
|
|
|
e63c38 |
- for (total = 0; total < b.length; total += result) {
|
|
|
e63c38 |
- result = in.read(b, total, b.length - total);
|
|
|
e63c38 |
- if(result == -1) {
|
|
|
e63c38 |
- break;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- return total;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void testConcatenationOfSerializedStreams() throws IOException {
|
|
|
e63c38 |
- final byte[] testBytes1 = randomArray(64, 256);
|
|
|
e63c38 |
- final byte[] testBytes2 = randomArray(64, 256);
|
|
|
e63c38 |
- byte[] expected = new byte[128];
|
|
|
e63c38 |
- System.arraycopy(testBytes1, 0, expected, 0, 64);
|
|
|
e63c38 |
- System.arraycopy(testBytes2, 0, expected, 64, 64);
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- ByteArrayOutputStream bytes1os = new ByteArrayOutputStream();
|
|
|
e63c38 |
- LZ4BlockOutputStream out1 = new LZ4BlockOutputStream(bytes1os);
|
|
|
e63c38 |
- out1.write(testBytes1);
|
|
|
e63c38 |
- out1.close();
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- ByteArrayOutputStream bytes2os = new ByteArrayOutputStream();
|
|
|
e63c38 |
- LZ4BlockOutputStream out2 = new LZ4BlockOutputStream(bytes2os);
|
|
|
e63c38 |
- out2.write(testBytes2);
|
|
|
e63c38 |
- out2.close();
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- byte[] bytes1 = bytes1os.toByteArray();
|
|
|
e63c38 |
- byte[] bytes2 = bytes2os.toByteArray();
|
|
|
e63c38 |
- byte[] concatenatedBytes = new byte[bytes1.length + bytes2.length];
|
|
|
e63c38 |
- System.arraycopy(bytes1, 0, concatenatedBytes, 0, bytes1.length);
|
|
|
e63c38 |
- System.arraycopy(bytes2, 0, concatenatedBytes, bytes1.length, bytes2.length);
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- // In a default behaviour, we can read the first block of the concatenated bytes only
|
|
|
e63c38 |
- LZ4BlockInputStream in1 = new LZ4BlockInputStream(new ByteArrayInputStream(concatenatedBytes));
|
|
|
e63c38 |
- byte[] actual1 = new byte[128];
|
|
|
e63c38 |
- assertEquals(64, readFully(in1, actual1));
|
|
|
e63c38 |
- assertEquals(-1, in1.read());
|
|
|
e63c38 |
- in1.close();
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- // Check if we can read concatenated byte stream
|
|
|
e63c38 |
- LZ4BlockInputStream in2 = new LZ4BlockInputStream(new ByteArrayInputStream(concatenatedBytes), false);
|
|
|
e63c38 |
- byte[] actual2 = new byte[128];
|
|
|
e63c38 |
- assertEquals(128, readFully(in2, actual2));
|
|
|
e63c38 |
- assertEquals(-1, in2.read());
|
|
|
e63c38 |
- in2.close();
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- assertArrayEquals(expected, actual2);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-}
|
|
|
e63c38 |
diff --git a/src/test/net/jpountz/lz4/LZ4Test.java b/src/test/net/jpountz/lz4/LZ4Test.java
|
|
|
e63c38 |
deleted file mode 100644
|
|
|
e63c38 |
index 161899c..0000000
|
|
|
e63c38 |
--- a/src/test/net/jpountz/lz4/LZ4Test.java
|
|
|
e63c38 |
+++ /dev/null
|
|
|
e63c38 |
@@ -1,562 +0,0 @@
|
|
|
e63c38 |
-package net.jpountz.lz4;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-/*
|
|
|
e63c38 |
- * Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
e63c38 |
- * you may not use this file except in compliance with the License.
|
|
|
e63c38 |
- * You may obtain a copy of the License at
|
|
|
e63c38 |
- *
|
|
|
e63c38 |
- * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
e63c38 |
- *
|
|
|
e63c38 |
- * Unless required by applicable law or agreed to in writing, software
|
|
|
e63c38 |
- * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
e63c38 |
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
e63c38 |
- * See the License for the specific language governing permissions and
|
|
|
e63c38 |
- * limitations under the License.
|
|
|
e63c38 |
- */
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-import static net.jpountz.lz4.Instances.COMPRESSORS;
|
|
|
e63c38 |
-import static net.jpountz.lz4.Instances.FAST_DECOMPRESSORS;
|
|
|
e63c38 |
-import static net.jpountz.lz4.Instances.SAFE_DECOMPRESSORS;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-import java.io.ByteArrayOutputStream;
|
|
|
e63c38 |
-import java.io.IOException;
|
|
|
e63c38 |
-import java.nio.ByteBuffer;
|
|
|
e63c38 |
-import java.nio.ReadOnlyBufferException;
|
|
|
e63c38 |
-import java.util.Arrays;
|
|
|
e63c38 |
-import java.io.File;
|
|
|
e63c38 |
-import java.io.FilenameFilter;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-import org.junit.Test;
|
|
|
e63c38 |
-import static org.junit.Assert.*;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-import com.carrotsearch.randomizedtesting.annotations.Repeat;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-public class LZ4Test extends AbstractLZ4Test {
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void testLockFileOfTemporaryNativeLibrary() {
|
|
|
e63c38 |
- // Load the native library
|
|
|
e63c38 |
- LZ4JNI.LZ4_compressBound(100);
|
|
|
e63c38 |
- String tempFolder = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath();
|
|
|
e63c38 |
- File tempDir = new File(new File(System.getProperty("java.io.tmpdir")).getAbsolutePath());
|
|
|
e63c38 |
- if (!System.getProperty("os.name").contains("Windows")) {
|
|
|
e63c38 |
- // A temporary library must be accompanied by a lock file.
|
|
|
e63c38 |
- // On Windows, JVM does not remove a temporary library on exit.
|
|
|
e63c38 |
- // This means on Windows, there might be a temporary library
|
|
|
e63c38 |
- // that is not accompanied by a lock file when there was
|
|
|
e63c38 |
- // a Java process using lz4-java that was running concurrently
|
|
|
e63c38 |
- // to this test process.
|
|
|
e63c38 |
- File[] tempLibFiles = tempDir.listFiles(new FilenameFilter() {
|
|
|
e63c38 |
- public boolean accept(File dir, String name) {
|
|
|
e63c38 |
- return name.startsWith("liblz4-java-") && !name.endsWith(".lck");
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- });
|
|
|
e63c38 |
- if (tempLibFiles != null) {
|
|
|
e63c38 |
- for (File tempLibFile : tempLibFiles) {
|
|
|
e63c38 |
- File lckFile = new File(tempLibFile.getAbsolutePath() + ".lck");
|
|
|
e63c38 |
- assertTrue(tempLibFile.getAbsolutePath(), lckFile.exists());
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- // A lock file must be accompanied by a temporary library.
|
|
|
e63c38 |
- File[] tempLockFiles = tempDir.listFiles(new FilenameFilter() {
|
|
|
e63c38 |
- public boolean accept(File dir, String name) {
|
|
|
e63c38 |
- return name.startsWith("liblz4-java-") && name.endsWith(".lck");
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- });
|
|
|
e63c38 |
- if (tempLockFiles != null) {
|
|
|
e63c38 |
- for (File tempLockFile : tempLockFiles) {
|
|
|
e63c38 |
- String tempLockFilePath = tempLockFile.getAbsolutePath();
|
|
|
e63c38 |
- File libFile = new File(tempLockFilePath.substring(0, tempLockFilePath.length() - 4));
|
|
|
e63c38 |
- assertTrue(tempLockFilePath, libFile.exists());
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- @Repeat(iterations=50)
|
|
|
e63c38 |
- public void testMaxCompressedLength() {
|
|
|
e63c38 |
- final int len = randomBoolean() ? randomInt(16) : randomInt(1 << 30);
|
|
|
e63c38 |
- for (LZ4Compressor compressor : COMPRESSORS) {
|
|
|
e63c38 |
- assertEquals(LZ4JNI.LZ4_compressBound(len), compressor.maxCompressedLength(len));
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- private static byte[] getCompressedWorstCase(byte[] decompressed) {
|
|
|
e63c38 |
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
e63c38 |
- int len = decompressed.length;
|
|
|
e63c38 |
- if (len >= LZ4Constants.RUN_MASK) {
|
|
|
e63c38 |
- baos.write(LZ4Constants.RUN_MASK << LZ4Constants.ML_BITS);
|
|
|
e63c38 |
- len -= LZ4Constants.RUN_MASK;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- while (len >= 255) {
|
|
|
e63c38 |
- baos.write(255);
|
|
|
e63c38 |
- len -= 255;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- baos.write(len);
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- baos.write(decompressed);
|
|
|
e63c38 |
- } catch (IOException e) {
|
|
|
e63c38 |
- throw new AssertionError();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- return baos.toByteArray();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void testEmpty() {
|
|
|
e63c38 |
- testRoundTrip(new byte[0]);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- public void testUncompressWorstCase(LZ4FastDecompressor decompressor) {
|
|
|
e63c38 |
- final int len = randomInt(100 * 1024);
|
|
|
e63c38 |
- final int max = randomIntBetween(1, 255);
|
|
|
e63c38 |
- byte[] decompressed = randomArray(len, max);
|
|
|
e63c38 |
- byte[] compressed = getCompressedWorstCase(decompressed);
|
|
|
e63c38 |
- byte[] restored = new byte[decompressed.length];
|
|
|
e63c38 |
- int cpLen = decompressor.decompress(compressed, 0, restored, 0, decompressed.length);
|
|
|
e63c38 |
- assertEquals(compressed.length, cpLen);
|
|
|
e63c38 |
- assertArrayEquals(decompressed, restored);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void testUncompressWorstCase() {
|
|
|
e63c38 |
- for (LZ4FastDecompressor decompressor : FAST_DECOMPRESSORS) {
|
|
|
e63c38 |
- testUncompressWorstCase(decompressor);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- public void testUncompressWorstCase(LZ4SafeDecompressor decompressor) {
|
|
|
e63c38 |
- final int len = randomInt(100 * 1024);
|
|
|
e63c38 |
- final int max = randomIntBetween(1, 256);
|
|
|
e63c38 |
- byte[] decompressed = randomArray(len, max);
|
|
|
e63c38 |
- byte[] compressed = getCompressedWorstCase(decompressed);
|
|
|
e63c38 |
- byte[] restored = new byte[decompressed.length];
|
|
|
e63c38 |
- int uncpLen = decompressor.decompress(compressed, 0, compressed.length, restored, 0);
|
|
|
e63c38 |
- assertEquals(decompressed.length, uncpLen);
|
|
|
e63c38 |
- assertArrayEquals(decompressed, restored);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void testUncompressSafeWorstCase() {
|
|
|
e63c38 |
- for (LZ4SafeDecompressor decompressor : SAFE_DECOMPRESSORS) {
|
|
|
e63c38 |
- testUncompressWorstCase(decompressor);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- public void testRoundTrip(byte[] data, int off, int len,
|
|
|
e63c38 |
- LZ4Compressor compressor,
|
|
|
e63c38 |
- LZ4FastDecompressor decompressor,
|
|
|
e63c38 |
- LZ4SafeDecompressor decompressor2) {
|
|
|
e63c38 |
- for (Tester tester : Arrays.asList(Tester.BYTE_ARRAY, Tester.BYTE_BUFFER, Tester.BYTE_ARRAY_WITH_LENGTH, Tester.BYTE_BUFFER_WITH_LENGTH)) {
|
|
|
e63c38 |
- testRoundTrip(tester, data, off, len, compressor, decompressor, decompressor2);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- public <T> void testRoundTrip(
|
|
|
e63c38 |
- Tester<T> tester,
|
|
|
e63c38 |
- byte[] data, int off, int len,
|
|
|
e63c38 |
- LZ4Compressor compressor,
|
|
|
e63c38 |
- LZ4FastDecompressor decompressor,
|
|
|
e63c38 |
- LZ4SafeDecompressor decompressor2) {
|
|
|
e63c38 |
- final int maxCompressedLength = tester.maxCompressedLength(len);
|
|
|
e63c38 |
- // "maxCompressedLength + 1" for the over-estimated compressed length test below
|
|
|
e63c38 |
- final T compressed = tester.allocate(maxCompressedLength + 1);
|
|
|
e63c38 |
- final int compressedLen = tester.compress(compressor,
|
|
|
e63c38 |
- tester.copyOf(data), off, len,
|
|
|
e63c38 |
- compressed, 0, maxCompressedLength);
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- // test decompression
|
|
|
e63c38 |
- final T restored = tester.allocate(len);
|
|
|
e63c38 |
- assertEquals(compressedLen, tester.decompress(decompressor, compressed, 0, restored, 0, len));
|
|
|
e63c38 |
- assertArrayEquals(Arrays.copyOfRange(data, off, off + len), tester.copyOf(restored, 0, len));
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- // make sure it fails if the compression dest is not large enough
|
|
|
e63c38 |
- tester.fill(restored, randomByte());
|
|
|
e63c38 |
- final T compressed2 = tester.allocate(compressedLen-1);
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- final int compressedLen2 = tester.compress(compressor,
|
|
|
e63c38 |
- tester.copyOf(data), off, len,
|
|
|
e63c38 |
- compressed2, 0, compressedLen - 1);
|
|
|
e63c38 |
- // Compression can succeed even with the smaller dest
|
|
|
e63c38 |
- // because the compressor is allowed to return different compression results
|
|
|
e63c38 |
- // even when it is invoked with the same input data.
|
|
|
e63c38 |
- // In this case, just make sure the compressed data can be successfully decompressed.
|
|
|
e63c38 |
- assertEquals(compressedLen2, tester.decompress(decompressor, compressed2, 0, restored, 0, len));
|
|
|
e63c38 |
- assertArrayEquals(Arrays.copyOfRange(data, off, off + len), tester.copyOf(restored, 0, len));
|
|
|
e63c38 |
- } catch (LZ4Exception e) {
|
|
|
e63c38 |
- // OK
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- if (tester != Tester.BYTE_ARRAY_WITH_LENGTH && tester != Tester.BYTE_BUFFER_WITH_LENGTH) {
|
|
|
e63c38 |
- if (len > 0) {
|
|
|
e63c38 |
- // decompression dest is too small
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- tester.decompress(decompressor, compressed, 0, restored, 0, len - 1);
|
|
|
e63c38 |
- fail();
|
|
|
e63c38 |
- } catch (LZ4Exception e) {
|
|
|
e63c38 |
- // OK
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- // decompression dest is too large
|
|
|
e63c38 |
- final T restored2 = tester.allocate(len+1);
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- final int cpLen = tester.decompress(decompressor, compressed, 0, restored2, 0, len + 1);
|
|
|
e63c38 |
- fail("compressedLen=" + cpLen);
|
|
|
e63c38 |
- } catch (LZ4Exception e) {
|
|
|
e63c38 |
- // OK
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- // try decompression when only the size of the compressed buffer is known
|
|
|
e63c38 |
- if (len > 0) {
|
|
|
e63c38 |
- tester.fill(restored, randomByte());
|
|
|
e63c38 |
- assertEquals(len, tester.decompress(decompressor2, compressed, 0, compressedLen, restored, 0, len));
|
|
|
e63c38 |
- assertArrayEquals(Arrays.copyOfRange(data, off, off + len), tester.copyOf(restored, 0, len));
|
|
|
e63c38 |
- tester.fill(restored, randomByte());
|
|
|
e63c38 |
- } else {
|
|
|
e63c38 |
- assertEquals(0, tester.decompress(decompressor2, compressed, 0, compressedLen, tester.allocate(1), 0, 1));
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- // over-estimated compressed length
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- final int decompressedLen = tester.decompress(decompressor2, compressed, 0, compressedLen + 1, tester.allocate(len + 100), 0, len + 100);
|
|
|
e63c38 |
- fail("decompressedLen=" + decompressedLen);
|
|
|
e63c38 |
- } catch (LZ4Exception e) {
|
|
|
e63c38 |
- // OK
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- // under-estimated compressed length
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- final int decompressedLen = tester.decompress(decompressor2, compressed, 0, compressedLen - 1, tester.allocate(len + 100), 0, len + 100);
|
|
|
e63c38 |
- if (!(decompressor2 instanceof LZ4JNISafeDecompressor)) {
|
|
|
e63c38 |
- fail("decompressedLen=" + decompressedLen);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- } catch (LZ4Exception e) {
|
|
|
e63c38 |
- // OK
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- public void testRoundTrip(byte[] data, int off, int len, LZ4Factory compressorFactory, LZ4Factory decompressorFactory) {
|
|
|
e63c38 |
- for (LZ4Compressor compressor : Arrays.asList(
|
|
|
e63c38 |
- compressorFactory.fastCompressor(), compressorFactory.highCompressor())) {
|
|
|
e63c38 |
- testRoundTrip(data, off, len, compressor, decompressorFactory.fastDecompressor(), decompressorFactory.safeDecompressor());
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- public void testRoundTrip(byte[] data, int off, int len) {
|
|
|
e63c38 |
- for (LZ4Factory compressorFactory : Arrays.asList(
|
|
|
e63c38 |
- LZ4Factory.nativeInstance(),
|
|
|
e63c38 |
- LZ4Factory.unsafeInstance(),
|
|
|
e63c38 |
- LZ4Factory.safeInstance())) {
|
|
|
e63c38 |
- for (LZ4Factory decompressorFactory : Arrays.asList(
|
|
|
e63c38 |
- LZ4Factory.nativeInstance(),
|
|
|
e63c38 |
- LZ4Factory.unsafeInstance(),
|
|
|
e63c38 |
- LZ4Factory.safeInstance())) {
|
|
|
e63c38 |
- testRoundTrip(data, off, len, compressorFactory, decompressorFactory);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- public void testRoundTrip(byte[] data) {
|
|
|
e63c38 |
- testRoundTrip(data, 0, data.length);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- public void testRoundTrip(String resource) throws IOException {
|
|
|
e63c38 |
- final byte[] data = readResource(resource);
|
|
|
e63c38 |
- testRoundTrip(data);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void testRoundtripGeo() throws IOException {
|
|
|
e63c38 |
- testRoundTrip("/calgary/geo");
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void testRoundtripBook1() throws IOException {
|
|
|
e63c38 |
- testRoundTrip("/calgary/book1");
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void testRoundtripPic() throws IOException {
|
|
|
e63c38 |
- testRoundTrip("/calgary/pic");
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void testNullMatchDec() {
|
|
|
e63c38 |
- // 1 literal, 4 matchs with matchDec=0, 8 literals
|
|
|
e63c38 |
- final byte[] invalid = new byte[] { 16, 42, 0, 0, (byte) 128, 42, 42, 42, 42, 42, 42, 42, 42 };
|
|
|
e63c38 |
- // decompression should neither throw an exception nor loop indefinitely
|
|
|
e63c38 |
- for (LZ4FastDecompressor decompressor : FAST_DECOMPRESSORS) {
|
|
|
e63c38 |
- decompressor.decompress(invalid, 0, new byte[13], 0, 13);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- for (LZ4SafeDecompressor decompressor : SAFE_DECOMPRESSORS) {
|
|
|
e63c38 |
- decompressor.decompress(invalid, 0, invalid.length, new byte[20], 0);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void testEndsWithMatch() {
|
|
|
e63c38 |
- // 6 literals, 4 matchs
|
|
|
e63c38 |
- final byte[] invalid = new byte[] { 96, 42, 43, 44, 45, 46, 47, 5, 0 };
|
|
|
e63c38 |
- final int decompressedLength = 10;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- for (LZ4FastDecompressor decompressor : FAST_DECOMPRESSORS) {
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- // it is invalid to end with a match, should be at least 5 literals
|
|
|
e63c38 |
- decompressor.decompress(invalid, 0, new byte[decompressedLength], 0, decompressedLength);
|
|
|
e63c38 |
- assertTrue(decompressor.toString(), false);
|
|
|
e63c38 |
- } catch (LZ4Exception e) {
|
|
|
e63c38 |
- // OK
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- for (LZ4SafeDecompressor decompressor : SAFE_DECOMPRESSORS) {
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- // it is invalid to end with a match, should be at least 5 literals
|
|
|
e63c38 |
- decompressor.decompress(invalid, 0, invalid.length, new byte[20], 0);
|
|
|
e63c38 |
- assertTrue(false);
|
|
|
e63c38 |
- } catch (LZ4Exception e) {
|
|
|
e63c38 |
- // OK
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void testEndsWithLessThan5Literals() {
|
|
|
e63c38 |
- // 6 literals, 4 matchs
|
|
|
e63c38 |
- final byte[] invalidBase = new byte[] { 96, 42, 43, 44, 45, 46, 47, 5, 0 };
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- for (int i = 1; i < 5; ++i) {
|
|
|
e63c38 |
- final byte[] invalid = Arrays.copyOf(invalidBase, invalidBase.length + 1 + i);
|
|
|
e63c38 |
- invalid[invalidBase.length] = (byte) (i << 4); // i literals at the end
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- for (LZ4FastDecompressor decompressor : FAST_DECOMPRESSORS) {
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- // it is invalid to end with a match, should be at least 5 literals
|
|
|
e63c38 |
- decompressor.decompress(invalid, 0, new byte[20], 0, 20);
|
|
|
e63c38 |
- assertTrue(decompressor.toString(), false);
|
|
|
e63c38 |
- } catch (LZ4Exception e) {
|
|
|
e63c38 |
- // OK
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- for (LZ4SafeDecompressor decompressor : SAFE_DECOMPRESSORS) {
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- // it is invalid to end with a match, should be at least 5 literals
|
|
|
e63c38 |
- decompressor.decompress(invalid, 0, invalid.length, new byte[20], 0);
|
|
|
e63c38 |
- assertTrue(false);
|
|
|
e63c38 |
- } catch (LZ4Exception e) {
|
|
|
e63c38 |
- // OK
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void testWriteToReadOnlyBuffer() {
|
|
|
e63c38 |
- for (LZ4Compressor compressor : COMPRESSORS) {
|
|
|
e63c38 |
- ByteBuffer in = Tester.BYTE_BUFFER.copyOf(new byte[] {2, 3});
|
|
|
e63c38 |
- ByteBuffer out = Tester.BYTE_BUFFER.allocate(100).asReadOnlyBuffer();
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- compressor.compress(in, out);
|
|
|
e63c38 |
- fail();
|
|
|
e63c38 |
- } catch (ReadOnlyBufferException e) {
|
|
|
e63c38 |
- // ok
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- for (LZ4SafeDecompressor decompressor : SAFE_DECOMPRESSORS) {
|
|
|
e63c38 |
- ByteBuffer in = Tester.BYTE_BUFFER.copyOf(COMPRESSORS[0].compress(new byte[] {2, 3}));
|
|
|
e63c38 |
- ByteBuffer out = Tester.BYTE_BUFFER.allocate(100).asReadOnlyBuffer();
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- decompressor.decompress(in, out);
|
|
|
e63c38 |
- fail();
|
|
|
e63c38 |
- } catch (ReadOnlyBufferException e) {
|
|
|
e63c38 |
- // ok
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- for (LZ4FastDecompressor decompressor : FAST_DECOMPRESSORS) {
|
|
|
e63c38 |
- ByteBuffer in = Tester.BYTE_BUFFER.copyOf(COMPRESSORS[0].compress(new byte[] {2, 3}));
|
|
|
e63c38 |
- ByteBuffer out = Tester.BYTE_BUFFER.allocate(100).asReadOnlyBuffer();
|
|
|
e63c38 |
- out.limit(2);
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- decompressor.decompress(in, out);
|
|
|
e63c38 |
- fail();
|
|
|
e63c38 |
- } catch (ReadOnlyBufferException e) {
|
|
|
e63c38 |
- // ok
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- @Repeat(iterations=5)
|
|
|
e63c38 |
- public void testAllEqual() {
|
|
|
e63c38 |
- final int len = randomBoolean() ? randomInt(20) : randomInt(100000);
|
|
|
e63c38 |
- final byte[] buf = new byte[len];
|
|
|
e63c38 |
- Arrays.fill(buf, randomByte());
|
|
|
e63c38 |
- testRoundTrip(buf);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void testMaxDistance() {
|
|
|
e63c38 |
- final int len = randomIntBetween(1 << 17, 1 << 18);
|
|
|
e63c38 |
- final int off = randomInt(len - (1 << 16) - (1 << 15));
|
|
|
e63c38 |
- final byte[] buf = new byte[len];
|
|
|
e63c38 |
- for (int i = 0; i < (1 << 15); ++i) {
|
|
|
e63c38 |
- buf[off + i] = randomByte();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- System.arraycopy(buf, off, buf, off + 65535, 1 << 15);
|
|
|
e63c38 |
- testRoundTrip(buf);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- @Repeat(iterations=10)
|
|
|
e63c38 |
- public void testRandomData() {
|
|
|
e63c38 |
- final int n = randomIntBetween(1, 15);
|
|
|
e63c38 |
- final int off = randomInt(1000);
|
|
|
e63c38 |
- final int len = randomBoolean() ? randomInt(1 << 16) : randomInt(1 << 20);
|
|
|
e63c38 |
- final byte[] data = randomArray(off + len + randomInt(100), n);
|
|
|
e63c38 |
- testRoundTrip(data, off, len);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- // https://github.com/jpountz/lz4-java/issues/12
|
|
|
e63c38 |
- public void testRoundtripIssue12() {
|
|
|
e63c38 |
- byte[] data = new byte[]{
|
|
|
e63c38 |
- 14, 72, 14, 85, 3, 72, 14, 85, 3, 72, 14, 72, 14, 72, 14, 85, 3, 72, 14, 72, 14, 72, 14, 72, 14, 72, 14, 72, 14, 85, 3, 72,
|
|
|
e63c38 |
- 14, 85, 3, 72, 14, 85, 3, 72, 14, 85, 3, 72, 14, 85, 3, 72, 14, 85, 3, 72, 14, 50, 64, 0, 46, -1, 0, 0, 0, 29, 3, 85,
|
|
|
e63c38 |
- 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3,
|
|
|
e63c38 |
- 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113,
|
|
|
e63c38 |
- 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113,
|
|
|
e63c38 |
- 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 50, 64, 0, 47, -105, 0, 0, 0, 30, 3, -97, 6, 0, 68, -113,
|
|
|
e63c38 |
- 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, 85,
|
|
|
e63c38 |
- 8, -113, 0, 68, -97, 3, 0, 2, -97, 6, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97,
|
|
|
e63c38 |
- 6, 0, 68, -113, 0, 120, 64, 0, 48, 4, 0, 0, 0, 31, 34, 72, 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72,
|
|
|
e63c38 |
- 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72,
|
|
|
e63c38 |
- 43, 72, 19, 72, 34, 72, 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72,
|
|
|
e63c38 |
- 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72, 43, 72, 19, 72, 34, 72, 29, 72, 37, 72,
|
|
|
e63c38 |
- 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72,
|
|
|
e63c38 |
- 41, 72, 32, 72, 18, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
e63c38 |
- 0, 39, 24, 32, 34, 124, 0, 120, 64, 0, 48, 80, 0, 0, 0, 31, 30, 72, 22, 72, 31, 72, 43, 72, 19, 72, 34, 72, 29, 72, 37, 72,
|
|
|
e63c38 |
- 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72,
|
|
|
e63c38 |
- 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72, 43, 72, 19, 72, 34, 72, 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72,
|
|
|
e63c38 |
- 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72,
|
|
|
e63c38 |
- 31, 72, 43, 72, 19, 72, 34, 72, 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72,
|
|
|
e63c38 |
- 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72, 43, 72, 19, 72, 34, 72, 29, 72,
|
|
|
e63c38 |
- 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72,
|
|
|
e63c38 |
- 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72, 43, 72, 19, 72, 34, 72, 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72,
|
|
|
e63c38 |
- 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72,
|
|
|
e63c38 |
- 22, 72, 31, 72, 43, 72, 19, 72, 34, 72, 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72,
|
|
|
e63c38 |
- 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72, 43, 72, 19, 72, 34, 72,
|
|
|
e63c38 |
- 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72,
|
|
|
e63c38 |
- 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72, 43, 72, 19, 50, 64, 0, 49, 20, 0, 0, 0, 32, 3, -97, 6, 0,
|
|
|
e63c38 |
- 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97,
|
|
|
e63c38 |
- 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2,
|
|
|
e63c38 |
- 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2,
|
|
|
e63c38 |
- 3, -97, 6, 0, 50, 64, 0, 50, 53, 0, 0, 0, 34, 3, -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -113, 0, 2, 3, -97,
|
|
|
e63c38 |
- 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3,
|
|
|
e63c38 |
- -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97,
|
|
|
e63c38 |
- 3, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3,
|
|
|
e63c38 |
- 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0,
|
|
|
e63c38 |
- 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3,
|
|
|
e63c38 |
- -97, 6, 0, 50, 64, 0, 51, 85, 0, 0, 0, 36, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97,
|
|
|
e63c38 |
- 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, -97, 5, 0, 2, 3, 85, 8, -113, 0, 68,
|
|
|
e63c38 |
- -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0,
|
|
|
e63c38 |
- 68, -113, 0, 2, 3, -97, 6, 0, 50, -64, 0, 51, -45, 0, 0, 0, 37, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6,
|
|
|
e63c38 |
- 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -113, 0, 2, 3, -97,
|
|
|
e63c38 |
- 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 120, 64, 0, 52, -88, 0, 0,
|
|
|
e63c38 |
- 0, 39, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72,
|
|
|
e63c38 |
- 13, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 72, 13, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85,
|
|
|
e63c38 |
- 5, 72, 13, 85, 5, 72, 13, 72, 13, 72, 13, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85,
|
|
|
e63c38 |
- 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85,
|
|
|
e63c38 |
- 5, 72, 13, 85, 5, 72, 13, 72, 13, 72, 13, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 72, 13, 85, 5, 72, 13, 72,
|
|
|
e63c38 |
- 13, 85, 5, 72, 13, 72, 13, 85, 5, 72, 13, -19, -24, -101, -35
|
|
|
e63c38 |
- };
|
|
|
e63c38 |
- testRoundTrip(data, 9, data.length - 9);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- private static void assertCompressedArrayEquals(String message, byte[] expected, byte[] actual) {
|
|
|
e63c38 |
- int off = 0;
|
|
|
e63c38 |
- int decompressedOff = 0;
|
|
|
e63c38 |
- while (true) {
|
|
|
e63c38 |
- if (off == expected.length) {
|
|
|
e63c38 |
- break;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- final Sequence sequence1 = readSequence(expected, off);
|
|
|
e63c38 |
- final Sequence sequence2 = readSequence(actual, off);
|
|
|
e63c38 |
- assertEquals(message + ", off=" + off + ", decompressedOff=" + decompressedOff, sequence1, sequence2);
|
|
|
e63c38 |
- off += sequence1.length;
|
|
|
e63c38 |
- decompressedOff += sequence1.literalLen + sequence1.matchLen;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- private static Sequence readSequence(byte[] buf, int off) {
|
|
|
e63c38 |
- final int start = off;
|
|
|
e63c38 |
- final int token = buf[off++] & 0xFF;
|
|
|
e63c38 |
- int literalLen = token >>> 4;
|
|
|
e63c38 |
- if (literalLen >= 0x0F) {
|
|
|
e63c38 |
- int len;
|
|
|
e63c38 |
- while ((len = buf[off++] & 0xFF) == 0xFF) {
|
|
|
e63c38 |
- literalLen += 0xFF;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- literalLen += len;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- off += literalLen;
|
|
|
e63c38 |
- if (off == buf.length) {
|
|
|
e63c38 |
- return new Sequence(literalLen, -1, -1, off - start);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- int matchDec = (buf[off++] & 0xFF) | ((buf[off++] & 0xFF) << 8);
|
|
|
e63c38 |
- int matchLen = token & 0x0F;
|
|
|
e63c38 |
- if (matchLen >= 0x0F) {
|
|
|
e63c38 |
- int len;
|
|
|
e63c38 |
- while ((len = buf[off++] & 0xFF) == 0xFF) {
|
|
|
e63c38 |
- matchLen += 0xFF;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- matchLen += len;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- matchLen += 4;
|
|
|
e63c38 |
- return new Sequence(literalLen, matchDec, matchLen, off - start);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- private static class Sequence {
|
|
|
e63c38 |
- final int literalLen, matchDec, matchLen, length;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- public Sequence(int literalLen, int matchDec, int matchLen, int length) {
|
|
|
e63c38 |
- this.literalLen = literalLen;
|
|
|
e63c38 |
- this.matchDec = matchDec;
|
|
|
e63c38 |
- this.matchLen = matchLen;
|
|
|
e63c38 |
- this.length = length;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public String toString() {
|
|
|
e63c38 |
- return "Sequence [literalLen=" + literalLen + ", matchDec=" + matchDec
|
|
|
e63c38 |
- + ", matchLen=" + matchLen + "]";
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public int hashCode() {
|
|
|
e63c38 |
- return 42;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public boolean equals(Object obj) {
|
|
|
e63c38 |
- if (this == obj)
|
|
|
e63c38 |
- return true;
|
|
|
e63c38 |
- if (obj == null)
|
|
|
e63c38 |
- return false;
|
|
|
e63c38 |
- if (getClass() != obj.getClass())
|
|
|
e63c38 |
- return false;
|
|
|
e63c38 |
- Sequence other = (Sequence) obj;
|
|
|
e63c38 |
- if (literalLen != other.literalLen)
|
|
|
e63c38 |
- return false;
|
|
|
e63c38 |
- if (matchDec != other.matchDec)
|
|
|
e63c38 |
- return false;
|
|
|
e63c38 |
- if (matchLen != other.matchLen)
|
|
|
e63c38 |
- return false;
|
|
|
e63c38 |
- return true;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-}
|
|
|
e63c38 |
diff --git a/src/test/net/jpountz/xxhash/XXHash32Test.java b/src/test/net/jpountz/xxhash/XXHash32Test.java
|
|
|
e63c38 |
deleted file mode 100644
|
|
|
e63c38 |
index 98c9436..0000000
|
|
|
e63c38 |
--- a/src/test/net/jpountz/xxhash/XXHash32Test.java
|
|
|
e63c38 |
+++ /dev/null
|
|
|
e63c38 |
@@ -1,189 +0,0 @@
|
|
|
e63c38 |
-package net.jpountz.xxhash;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-/*
|
|
|
e63c38 |
- * Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
e63c38 |
- * you may not use this file except in compliance with the License.
|
|
|
e63c38 |
- * You may obtain a copy of the License at
|
|
|
e63c38 |
- *
|
|
|
e63c38 |
- * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
e63c38 |
- *
|
|
|
e63c38 |
- * Unless required by applicable law or agreed to in writing, software
|
|
|
e63c38 |
- * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
e63c38 |
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
e63c38 |
- * See the License for the specific language governing permissions and
|
|
|
e63c38 |
- * limitations under the License.
|
|
|
e63c38 |
- */
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-import java.nio.ByteBuffer;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-import net.jpountz.lz4.AbstractLZ4Test;
|
|
|
e63c38 |
-import net.jpountz.util.SafeUtils;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-import org.junit.Test;
|
|
|
e63c38 |
-import static org.junit.Assert.*;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-import com.carrotsearch.randomizedtesting.annotations.Repeat;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-public class XXHash32Test extends AbstractLZ4Test {
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- private static abstract class StreamingXXHash32Adapter extends XXHash32 {
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- protected abstract StreamingXXHash32 streamingHash(int seed);
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public int hash(byte[] buf, int off, int len, int seed) {
|
|
|
e63c38 |
- SafeUtils.checkRange(buf, off, len);
|
|
|
e63c38 |
- int originalOff = off;
|
|
|
e63c38 |
- int remainingPasses = randomInt(5);
|
|
|
e63c38 |
- StreamingXXHash32 h = streamingHash(seed);
|
|
|
e63c38 |
- final int end = off + len;
|
|
|
e63c38 |
- while (off < end) {
|
|
|
e63c38 |
- final int l = randomIntBetween(off, end) - off;
|
|
|
e63c38 |
- h.update(buf, off, l);
|
|
|
e63c38 |
- off += l;
|
|
|
e63c38 |
- if (remainingPasses > 0 && randomInt(5) == 0) {
|
|
|
e63c38 |
- h.reset();
|
|
|
e63c38 |
- --remainingPasses;
|
|
|
e63c38 |
- off = originalOff;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- if (randomBoolean()) {
|
|
|
e63c38 |
- h.getValue();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- return h.getValue();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public int hash(ByteBuffer buf, int off, int len, int seed) {
|
|
|
e63c38 |
- byte[] bytes = new byte[len];
|
|
|
e63c38 |
- int originalPosition = buf.position();
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- buf.position(off);
|
|
|
e63c38 |
- buf.get(bytes, 0, len);
|
|
|
e63c38 |
- return hash(bytes, 0, len, seed);
|
|
|
e63c38 |
- } finally {
|
|
|
e63c38 |
- buf.position(originalPosition);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- public String toString() {
|
|
|
e63c38 |
- return streamingHash(0).toString();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- private static XXHash32[] INSTANCES = new XXHash32[] {
|
|
|
e63c38 |
- XXHashFactory.nativeInstance().hash32(),
|
|
|
e63c38 |
- XXHashFactory.unsafeInstance().hash32(),
|
|
|
e63c38 |
- XXHashFactory.safeInstance().hash32(),
|
|
|
e63c38 |
- new StreamingXXHash32Adapter() {
|
|
|
e63c38 |
- protected StreamingXXHash32 streamingHash(int seed) {
|
|
|
e63c38 |
- return XXHashFactory.nativeInstance().newStreamingHash32(seed);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- },
|
|
|
e63c38 |
- new StreamingXXHash32Adapter() {
|
|
|
e63c38 |
- protected StreamingXXHash32 streamingHash(int seed) {
|
|
|
e63c38 |
- return XXHashFactory.unsafeInstance().newStreamingHash32(seed);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- },
|
|
|
e63c38 |
- new StreamingXXHash32Adapter() {
|
|
|
e63c38 |
- protected StreamingXXHash32 streamingHash(int seed) {
|
|
|
e63c38 |
- return XXHashFactory.safeInstance().newStreamingHash32(seed);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- };
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void testEmpty() {
|
|
|
e63c38 |
- final int seed = randomInt();
|
|
|
e63c38 |
- for (XXHash32 xxHash : INSTANCES) {
|
|
|
e63c38 |
- xxHash.hash(new byte[0], 0, 0, seed);
|
|
|
e63c38 |
- xxHash.hash(copyOf(new byte[0], 0, 0), 0, 0, seed);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- @Repeat(iterations = 20)
|
|
|
e63c38 |
- public void testAIOOBE() {
|
|
|
e63c38 |
- final int seed = randomInt();
|
|
|
e63c38 |
- final int max = randomBoolean() ? 32 : 1000;
|
|
|
e63c38 |
- final int bufLen = randomIntBetween(1, max);
|
|
|
e63c38 |
- final byte[] buf = randomArray(bufLen, 256);
|
|
|
e63c38 |
- final int off = randomInt(buf.length - 1);
|
|
|
e63c38 |
- final int len = randomInt(buf.length - off);
|
|
|
e63c38 |
- for (XXHash32 xxHash : INSTANCES) {
|
|
|
e63c38 |
- xxHash.hash(buf, off, len, seed);
|
|
|
e63c38 |
- xxHash.hash(copyOf(buf, off, len), off, len, seed);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- @Repeat(iterations=40)
|
|
|
e63c38 |
- public void testInstances() {
|
|
|
e63c38 |
- final int maxLenLog = randomInt(20);
|
|
|
e63c38 |
- final int bufLen = randomInt(1 << maxLenLog);
|
|
|
e63c38 |
- byte[] buf = randomArray(bufLen, 256);
|
|
|
e63c38 |
- final int seed = randomInt();
|
|
|
e63c38 |
- final int off = randomIntBetween(0, Math.max(0, bufLen - 1));
|
|
|
e63c38 |
- final int len = randomIntBetween(0, bufLen - off);
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- final int ref = XXHashFactory.nativeInstance().hash32().hash(buf, off, len, seed);
|
|
|
e63c38 |
- for (XXHash32 hash : INSTANCES) {
|
|
|
e63c38 |
- final int h = hash.hash(buf, off, len, seed);
|
|
|
e63c38 |
- assertEquals(hash.toString(), ref, h);
|
|
|
e63c38 |
- final ByteBuffer copy = copyOf(buf, off, len);
|
|
|
e63c38 |
- final int h2 = hash.hash(copy, off, len, seed);
|
|
|
e63c38 |
- assertEquals(off, copy.position());
|
|
|
e63c38 |
- assertEquals(len, copy.remaining());
|
|
|
e63c38 |
- assertEquals(hash.toString(), ref, h2);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void test4GB() {
|
|
|
e63c38 |
- byte[] bytes = new byte[randomIntBetween(1 << 22, 1 << 26)];
|
|
|
e63c38 |
- for (int i = 0; i < bytes.length; ++i) {
|
|
|
e63c38 |
- bytes[i] = randomByte();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- final int off = randomInt(5);
|
|
|
e63c38 |
- final int len = randomIntBetween(bytes.length - off - 1024, bytes.length - off);
|
|
|
e63c38 |
- long totalLen = 0;
|
|
|
e63c38 |
- final int seed = randomInt();
|
|
|
e63c38 |
- StreamingXXHash32 hash1 = XXHashFactory.nativeInstance().newStreamingHash32(seed);
|
|
|
e63c38 |
- StreamingXXHash32 hash2 = XXHashFactory.unsafeInstance().newStreamingHash32(seed);
|
|
|
e63c38 |
- StreamingXXHash32 hash3 = XXHashFactory.safeInstance().newStreamingHash32(seed);
|
|
|
e63c38 |
- while (totalLen < (1L << 33)) {
|
|
|
e63c38 |
- hash1.update(bytes, off, len);
|
|
|
e63c38 |
- hash2.update(bytes, off, len);
|
|
|
e63c38 |
- hash3.update(bytes, off, len);
|
|
|
e63c38 |
- assertEquals(hash2.toString() + " " + totalLen, hash1.getValue(), hash2.getValue());
|
|
|
e63c38 |
- assertEquals(hash3.toString() + " " + totalLen, hash1.getValue(), hash3.getValue());
|
|
|
e63c38 |
- totalLen += len;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void testClose() {
|
|
|
e63c38 |
- StreamingXXHash32 hash = XXHashFactory.nativeInstance().newStreamingHash32(randomInt());
|
|
|
e63c38 |
- hash.close();
|
|
|
e63c38 |
- hash.close();
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- hash.getValue();
|
|
|
e63c38 |
- assertTrue(hash.toString(), false);
|
|
|
e63c38 |
- } catch (AssertionError e) {
|
|
|
e63c38 |
- // OK
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- hash.update(null, 0, 0);
|
|
|
e63c38 |
- assertTrue(hash.toString(), false);
|
|
|
e63c38 |
- } catch (AssertionError e) {
|
|
|
e63c38 |
- // OK
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- hash.reset();
|
|
|
e63c38 |
- assertTrue(hash.toString(), false);
|
|
|
e63c38 |
- } catch (AssertionError e) {
|
|
|
e63c38 |
- // OK
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-}
|
|
|
e63c38 |
diff --git a/src/test/net/jpountz/xxhash/XXHash64Test.java b/src/test/net/jpountz/xxhash/XXHash64Test.java
|
|
|
e63c38 |
deleted file mode 100644
|
|
|
e63c38 |
index dadbeda..0000000
|
|
|
e63c38 |
--- a/src/test/net/jpountz/xxhash/XXHash64Test.java
|
|
|
e63c38 |
+++ /dev/null
|
|
|
e63c38 |
@@ -1,194 +0,0 @@
|
|
|
e63c38 |
-package net.jpountz.xxhash;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-/*
|
|
|
e63c38 |
- * Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
e63c38 |
- * you may not use this file except in compliance with the License.
|
|
|
e63c38 |
- * You may obtain a copy of the License at
|
|
|
e63c38 |
- *
|
|
|
e63c38 |
- * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
e63c38 |
- *
|
|
|
e63c38 |
- * Unless required by applicable law or agreed to in writing, software
|
|
|
e63c38 |
- * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
e63c38 |
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
e63c38 |
- * See the License for the specific language governing permissions and
|
|
|
e63c38 |
- * limitations under the License.
|
|
|
e63c38 |
- */
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-import java.nio.ByteBuffer;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-import net.jpountz.lz4.AbstractLZ4Test;
|
|
|
e63c38 |
-import net.jpountz.util.SafeUtils;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-import org.junit.Test;
|
|
|
e63c38 |
-import static org.junit.Assert.*;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-import com.carrotsearch.randomizedtesting.annotations.Repeat;
|
|
|
e63c38 |
-
|
|
|
e63c38 |
-public class XXHash64Test extends AbstractLZ4Test {
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- private static abstract class StreamingXXHash64Adapter extends XXHash64 {
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- protected abstract StreamingXXHash64 streamingHash(long seed);
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public long hash(byte[] buf, int off, int len, long seed) {
|
|
|
e63c38 |
- SafeUtils.checkRange(buf, off, len);
|
|
|
e63c38 |
- int originalOff = off;
|
|
|
e63c38 |
- int remainingPasses = randomInt(5);
|
|
|
e63c38 |
- StreamingXXHash64 h = streamingHash(seed);
|
|
|
e63c38 |
- final int end = off + len;
|
|
|
e63c38 |
- while (off < end) {
|
|
|
e63c38 |
- final int l = randomIntBetween(off, end) - off;
|
|
|
e63c38 |
- h.update(buf, off, l);
|
|
|
e63c38 |
- off += l;
|
|
|
e63c38 |
- if (remainingPasses > 0 && randomInt(5) == 0) {
|
|
|
e63c38 |
- h.reset();
|
|
|
e63c38 |
- --remainingPasses;
|
|
|
e63c38 |
- off = originalOff;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- if (randomBoolean()) {
|
|
|
e63c38 |
- h.getValue();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- return h.getValue();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Override
|
|
|
e63c38 |
- public long hash(ByteBuffer buf, int off, int len, long seed) {
|
|
|
e63c38 |
- byte[] bytes = new byte[len];
|
|
|
e63c38 |
- int originalPosition = buf.position();
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- buf.position(off);
|
|
|
e63c38 |
- buf.get(bytes, 0, len);
|
|
|
e63c38 |
- return hash(bytes, 0, len, seed);
|
|
|
e63c38 |
- } finally {
|
|
|
e63c38 |
- buf.position(originalPosition);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- public String toString() {
|
|
|
e63c38 |
- return streamingHash(0).toString();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- private static XXHash64[] INSTANCES = new XXHash64[] {
|
|
|
e63c38 |
- XXHashFactory.nativeInstance().hash64(),
|
|
|
e63c38 |
- XXHashFactory.unsafeInstance().hash64(),
|
|
|
e63c38 |
- XXHashFactory.safeInstance().hash64(),
|
|
|
e63c38 |
- new StreamingXXHash64Adapter() {
|
|
|
e63c38 |
- protected StreamingXXHash64 streamingHash(long seed) {
|
|
|
e63c38 |
- return XXHashFactory.nativeInstance().newStreamingHash64(seed);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- },
|
|
|
e63c38 |
- new StreamingXXHash64Adapter() {
|
|
|
e63c38 |
- protected StreamingXXHash64 streamingHash(long seed) {
|
|
|
e63c38 |
- return XXHashFactory.unsafeInstance().newStreamingHash64(seed);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- },
|
|
|
e63c38 |
- new StreamingXXHash64Adapter() {
|
|
|
e63c38 |
- protected StreamingXXHash64 streamingHash(long seed) {
|
|
|
e63c38 |
- return XXHashFactory.safeInstance().newStreamingHash64(seed);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- };
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void testEmpty() {
|
|
|
e63c38 |
- final long seed = randomLong();
|
|
|
e63c38 |
- for (XXHash64 xxHash : INSTANCES) {
|
|
|
e63c38 |
- xxHash.hash(new byte[0], 0, 0, seed);
|
|
|
e63c38 |
- xxHash.hash(copyOf(new byte[0], 0, 0), 0, 0, seed);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- @Repeat(iterations = 20)
|
|
|
e63c38 |
- public void testAIOOBE() {
|
|
|
e63c38 |
- final long seed = randomLong();
|
|
|
e63c38 |
- final int max = randomBoolean() ? 64 : 1000;
|
|
|
e63c38 |
- final int bufLen = randomIntBetween(1, max);
|
|
|
e63c38 |
- final byte[] buf = new byte[bufLen];
|
|
|
e63c38 |
- for (int i = 0; i < buf.length; ++i) {
|
|
|
e63c38 |
- buf[i] = randomByte();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- final int off = randomInt(buf.length - 1);
|
|
|
e63c38 |
- final int len = randomInt(buf.length - off);
|
|
|
e63c38 |
- for (XXHash64 xxHash : INSTANCES) {
|
|
|
e63c38 |
- xxHash.hash(buf, off, len, seed);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- @Repeat(iterations=40)
|
|
|
e63c38 |
- public void testInstances() {
|
|
|
e63c38 |
- final int maxLenLog = randomInt(20);
|
|
|
e63c38 |
- final int bufLen = randomInt(1 << maxLenLog);
|
|
|
e63c38 |
- byte[] buf = new byte[bufLen];
|
|
|
e63c38 |
- for (int i = 0; i < bufLen; ++i) {
|
|
|
e63c38 |
- buf[i] = randomByte();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- final long seed = randomLong();
|
|
|
e63c38 |
- final int off = randomIntBetween(0, Math.max(0, bufLen - 1));
|
|
|
e63c38 |
- final int len = randomIntBetween(0, bufLen - off);
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- final long ref = XXHashFactory.nativeInstance().hash64().hash(buf, off, len, seed);
|
|
|
e63c38 |
- for (XXHash64 hash : INSTANCES) {
|
|
|
e63c38 |
- final long h = hash.hash(buf, off, len, seed);
|
|
|
e63c38 |
- assertEquals(hash.toString(), ref, h);
|
|
|
e63c38 |
- final ByteBuffer copy = copyOf(buf, off, len);
|
|
|
e63c38 |
- final long h2 = hash.hash(copy, off, len, seed);
|
|
|
e63c38 |
- assertEquals(off, copy.position());
|
|
|
e63c38 |
- assertEquals(len, copy.remaining());
|
|
|
e63c38 |
- assertEquals(hash.toString(), ref, h2);
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void test4GB() {
|
|
|
e63c38 |
- byte[] bytes = new byte[randomIntBetween(1 << 22, 1 << 26)];
|
|
|
e63c38 |
- for (int i = 0; i < bytes.length; ++i) {
|
|
|
e63c38 |
- bytes[i] = randomByte();
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- final int off = randomInt(5);
|
|
|
e63c38 |
- final int len = randomIntBetween(bytes.length - off - 1024, bytes.length - off);
|
|
|
e63c38 |
- long totalLen = 0;
|
|
|
e63c38 |
- final long seed = randomLong();
|
|
|
e63c38 |
- StreamingXXHash64 hash1 = XXHashFactory.nativeInstance().newStreamingHash64(seed);
|
|
|
e63c38 |
- StreamingXXHash64 hash2 = XXHashFactory.unsafeInstance().newStreamingHash64(seed);
|
|
|
e63c38 |
- StreamingXXHash64 hash3 = XXHashFactory.safeInstance().newStreamingHash64(seed);
|
|
|
e63c38 |
- while (totalLen < (1L << 33)) {
|
|
|
e63c38 |
- hash1.update(bytes, off, len);
|
|
|
e63c38 |
- hash2.update(bytes, off, len);
|
|
|
e63c38 |
- hash3.update(bytes, off, len);
|
|
|
e63c38 |
- assertEquals(hash2.toString() + " " + totalLen, hash1.getValue(), hash2.getValue());
|
|
|
e63c38 |
- assertEquals(hash3.toString() + " " + totalLen, hash1.getValue(), hash3.getValue());
|
|
|
e63c38 |
- totalLen += len;
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-
|
|
|
e63c38 |
- @Test
|
|
|
e63c38 |
- public void testClose() {
|
|
|
e63c38 |
- StreamingXXHash64 hash = XXHashFactory.nativeInstance().newStreamingHash64(randomInt());
|
|
|
e63c38 |
- hash.close();
|
|
|
e63c38 |
- hash.close();
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- hash.getValue();
|
|
|
e63c38 |
- assertTrue(hash.toString(), false);
|
|
|
e63c38 |
- } catch (AssertionError e) {
|
|
|
e63c38 |
- // OK
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- hash.update(null, 0, 0);
|
|
|
e63c38 |
- assertTrue(hash.toString(), false);
|
|
|
e63c38 |
- } catch (AssertionError e) {
|
|
|
e63c38 |
- // OK
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- try {
|
|
|
e63c38 |
- hash.reset();
|
|
|
e63c38 |
- assertTrue(hash.toString(), false);
|
|
|
e63c38 |
- } catch (AssertionError e) {
|
|
|
e63c38 |
- // OK
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
- }
|
|
|
e63c38 |
-}
|