diff options
author | Eugene Kliuchnikov <eustas@google.com> | 2017-06-01 13:51:18 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-01 13:51:18 +0200 |
commit | 19dc934e391752b78f78eca60c2c1ca5f6ac647b (patch) | |
tree | caae2d5dd9ef9dc54fcd75230d616ecd9b7ae4c9 /java/org/brotli/wrapper/enc/UseCustomDictionaryTest.java | |
parent | 03739d2b113afe60638069c4e1604dc2ac27380d (diff) | |
download | brotli-19dc934e391752b78f78eca60c2c1ca5f6ac647b.zip brotli-19dc934e391752b78f78eca60c2c1ca5f6ac647b.tar.gz brotli-19dc934e391752b78f78eca60c2c1ca5f6ac647b.tar.bz2 |
Add JNI wrappers. (#556)
Diffstat (limited to 'java/org/brotli/wrapper/enc/UseCustomDictionaryTest.java')
-rwxr-xr-x | java/org/brotli/wrapper/enc/UseCustomDictionaryTest.java | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/java/org/brotli/wrapper/enc/UseCustomDictionaryTest.java b/java/org/brotli/wrapper/enc/UseCustomDictionaryTest.java new file mode 100755 index 0000000..d46f997 --- /dev/null +++ b/java/org/brotli/wrapper/enc/UseCustomDictionaryTest.java @@ -0,0 +1,104 @@ +package org.brotli.wrapper.enc; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.brotli.integration.BundleHelper; +import org.brotli.wrapper.common.BrotliCommon; +import org.brotli.wrapper.dec.BrotliInputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.util.List; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import org.junit.runner.RunWith; +import org.junit.runners.AllTests; + +/** Tests for compression / decompression aided with LZ77 dictionary. */ +@RunWith(AllTests.class) +public class UseCustomDictionaryTest { + + // TODO: remove when Bazel get JNI support. + static { + System.load(new java.io.File(new java.io.File(System.getProperty("java.library.path")), + "liblibjni.so").getAbsolutePath()); + } + + static InputStream getBundle() throws IOException { + return new FileInputStream(System.getProperty("TEST_BUNDLE")); + } + + /** Creates a test suite. */ + public static TestSuite suite() throws IOException { + TestSuite suite = new TestSuite(); + InputStream bundle = getBundle(); + try { + List<String> entries = BundleHelper.listEntries(bundle); + for (String entry : entries) { + suite.addTest(new UseCustomDictionaryTestCase(entry)); + } + } finally { + bundle.close(); + } + return suite; + } + + /** Test case with a unique name. */ + static class UseCustomDictionaryTestCase extends TestCase { + final String entryName; + UseCustomDictionaryTestCase(String entryName) { + super("UseCustomDictionaryTest." + entryName); + this.entryName = entryName; + } + + @Override + protected void runTest() throws Throwable { + UseCustomDictionaryTest.run(entryName); + } + } + + private static void run(String entryName) throws Throwable { + InputStream bundle = getBundle(); + byte[] original; + try { + original = BundleHelper.readEntry(bundle, entryName); + } finally { + bundle.close(); + } + + if (original == null) { + throw new RuntimeException("Can't read bundle entry: " + entryName); + } + + ByteBuffer dictionary = BrotliCommon.makeNative(original); + + ByteArrayOutputStream dst = new ByteArrayOutputStream(); + OutputStream encoder = new BrotliOutputStream(dst, + new Encoder.Parameters().setQuality(11).setWindow(23), 1 << 23, dictionary); + try { + encoder.write(original); + } finally { + encoder.close(); + } + + byte[] compressed = dst.toByteArray(); + + // Just copy self from LZ77 dictionary -> ultimate compression ratio. + assertTrue(compressed.length < 80 + original.length / 65536); + + InputStream decoder = new BrotliInputStream(new ByteArrayInputStream(compressed), + 1 << 23, dictionary); + try { + long originalCrc = BundleHelper.fingerprintStream(new ByteArrayInputStream(original)); + long crc = BundleHelper.fingerprintStream(decoder); + assertEquals(originalCrc, crc); + } finally { + decoder.close(); + } + } +} |