From 63162ac0f59e4b98c56e3e21e3820d32a366534a Mon Sep 17 00:00:00 2001 From: Frotty Date: Mon, 12 Jun 2017 00:03:02 +0200 Subject: [PATCH] fixes #16 and some codebeat stuff --- src/main/java/ru/eustas/zopfli/Buffer.java | 6 +-- src/main/java/ru/eustas/zopfli/Cookie.java | 16 +------ .../ru/eustas/zopfli/LongestMatchCache.java | 2 +- src/main/java/ru/eustas/zopfli/Options.java | 6 +-- .../systems/crigges/jmpq3test/MpqTests.java | 30 +++++++++---- .../systems/crigges/jmpq3test/TestHelper.java | 43 +++++++++++++++++++ 6 files changed, 73 insertions(+), 30 deletions(-) create mode 100644 src/test/java/systems/crigges/jmpq3test/TestHelper.java diff --git a/src/main/java/ru/eustas/zopfli/Buffer.java b/src/main/java/ru/eustas/zopfli/Buffer.java index 3e6be63..fa15f00 100644 --- a/src/main/java/ru/eustas/zopfli/Buffer.java +++ b/src/main/java/ru/eustas/zopfli/Buffer.java @@ -20,8 +20,8 @@ public class Buffer { - byte[] data; - int size; + private byte[] data; + private int size; private int bp; Buffer() { @@ -36,7 +36,7 @@ public int getSize() { return size; } - void append(byte value) { + private void append(byte value) { if (size == data.length) { byte[] copy = new byte[size * 2]; System.arraycopy(data, 0, copy, 0, size); diff --git a/src/main/java/ru/eustas/zopfli/Cookie.java b/src/main/java/ru/eustas/zopfli/Cookie.java index 58e569d..568706c 100644 --- a/src/main/java/ru/eustas/zopfli/Cookie.java +++ b/src/main/java/ru/eustas/zopfli/Cookie.java @@ -34,7 +34,7 @@ static class Node { final static byte[] byteZeroes = new byte[65536]; final static int[] intMOnes = new int[65536]; final static char[] charOnes = new char[65536]; - final static long[] costMax = new long[65536]; + private final static long[] costMax = new long[65536]; static { for (int i = 0; i < 64; ++i) { @@ -47,7 +47,7 @@ static class Node { expand(costMax); } - static void expand(Object array) { + private static void expand(Object array) { for (int i = 64; i < 65536; i = i + i) { System.arraycopy(array, 0, array, i, i); } @@ -128,18 +128,6 @@ final void resetPool() { nextNode = 0; } - static void fill0(int[] array, int length) { - int i = 0; - while (i < length) { - int j = i + 65536; - if (j > length) { - j = length; - } - System.arraycopy(intZeroes, 0, array, i, j - i); - i = j; - } - } - static void fill0(char[] array, int length) { int i = 0; while (i < length) { diff --git a/src/main/java/ru/eustas/zopfli/LongestMatchCache.java b/src/main/java/ru/eustas/zopfli/LongestMatchCache.java index aff5bbb..7a2d2d2 100644 --- a/src/main/java/ru/eustas/zopfli/LongestMatchCache.java +++ b/src/main/java/ru/eustas/zopfli/LongestMatchCache.java @@ -23,7 +23,7 @@ class LongestMatchCache { private final static int CACHE_LENGTH = 8; public final char[] length; - public final char[] dist; + final char[] dist; private final char[] subLenPos; private final byte[] subLenLen; diff --git a/src/main/java/ru/eustas/zopfli/Options.java b/src/main/java/ru/eustas/zopfli/Options.java index 02dbcd6..7c70d7f 100644 --- a/src/main/java/ru/eustas/zopfli/Options.java +++ b/src/main/java/ru/eustas/zopfli/Options.java @@ -29,9 +29,9 @@ public static enum OutputFormat { DEFLATE } - public final int numIterations; - public final BlockSplitting blockSplitting; - public final OutputFormat outputType; + final int numIterations; + final BlockSplitting blockSplitting; + final OutputFormat outputType; public Options(OutputFormat outputType, BlockSplitting blockSplitting, int numIterations) { diff --git a/src/test/java/systems/crigges/jmpq3test/MpqTests.java b/src/test/java/systems/crigges/jmpq3test/MpqTests.java index bde63c8..a06fc2e 100644 --- a/src/test/java/systems/crigges/jmpq3test/MpqTests.java +++ b/src/test/java/systems/crigges/jmpq3test/MpqTests.java @@ -34,7 +34,7 @@ private static File getFile(String name) { @Test public void cryptoTest() throws IOException { byte[] bytes = "Hello World!".getBytes(); - + final ByteBuffer workBuffer = ByteBuffer.allocate(bytes.length); final MPQEncryption encryptor = new MPQEncryption(-1011927184, false); encryptor.processFinal(ByteBuffer.wrap(bytes), workBuffer); @@ -47,34 +47,34 @@ public void cryptoTest() throws IOException { //Assert.assertTrue(Arrays.equals(new byte[]{2, -106, -97, 38, 5, -82, -88, -91, -6, 63, 114, -31}, b)); Assert.assertTrue(Arrays.equals(bytes, workBuffer.array())); } - + @Test public void hashTableTest() throws IOException { // get real example file paths final InputStream listFileFile = getClass().getClassLoader().getResourceAsStream("DefaultListfile.txt"); final Scanner listFile = new Scanner(listFileFile); - + final String fp1 = listFile.nextLine(); final String fp2 = listFile.nextLine(); - + // small test hash table final HashTable ht = new HashTable(8); final short defaultLocale = HashTable.DEFAULT_LOCALE; final short germanLocale = 0x407; final short frenchLocale = 0x40c; final short russianLocale = 0x419; - + // assignment test ht.setFileBlockIndex(fp1, defaultLocale, 0); ht.setFileBlockIndex(fp2, defaultLocale, 1); Assert.assertEquals(ht.getFileBlockIndex(fp1, defaultLocale), 0); Assert.assertEquals(ht.getFileBlockIndex(fp2, defaultLocale), 1); - + // deletion test ht.removeFile(fp2, defaultLocale); Assert.assertEquals(ht.getFileBlockIndex(fp1, defaultLocale), 0); Assert.assertFalse(ht.hasFile(fp2)); - + // locale test ht.setFileBlockIndex(fp1, germanLocale, 2); ht.setFileBlockIndex(fp1, frenchLocale, 3); @@ -82,13 +82,13 @@ public void hashTableTest() throws IOException { Assert.assertEquals(ht.getFileBlockIndex(fp1, germanLocale), 2); Assert.assertEquals(ht.getFileBlockIndex(fp1, frenchLocale), 3); Assert.assertEquals(ht.getFileBlockIndex(fp1, russianLocale), 0); - + // file path deletion test ht.setFileBlockIndex(fp2, defaultLocale, 1); ht.removeFileAll(fp1); Assert.assertFalse(ht.hasFile(fp1)); Assert.assertEquals(ht.getFileBlockIndex(fp2, defaultLocale), 1); - + // clean up listFile.close(); } @@ -189,9 +189,15 @@ private void insertAndVerify(File mpq, String filename) throws IOException { JMpqEditor mpqEditor = new JMpqEditor(mpq, MPQOpenOption.FORCE_V0); try { File file = getFile(filename); + String hashBefore = TestHelper.md5(mpq); byte[] bytes = Files.readAllBytes(file.toPath()); mpqEditor.insertFile(filename, getFile(filename), false); mpqEditor.close(); + + String hashAfter = TestHelper.md5(mpq); + // If this fails, the mpq is not changed by the insert file command and something went wrong + Assert.assertNotEquals(hashBefore, hashAfter); + mpqEditor = new JMpqEditor(mpq, MPQOpenOption.FORCE_V0); Assert.assertTrue(mpqEditor.hasFile(filename)); byte[] bytes2 = mpqEditor.extractFileAsBytes(filename); @@ -208,10 +214,16 @@ private void insertAndDelete(File mpq, String filename) throws IOException { JMpqEditor mpqEditor = new JMpqEditor(mpq, MPQOpenOption.FORCE_V0); Assert.assertFalse(mpqEditor.hasFile(filename)); try { + String hashBefore = TestHelper.md5(mpq); mpqEditor.insertFile(filename, getFile(filename), true); mpqEditor.deleteFile(filename); mpqEditor.insertFile(filename, getFile(filename), false); mpqEditor.close(); + + String hashAfter = TestHelper.md5(mpq); + // If this fails, the mpq is not changed by the insert file command and something went wrong + Assert.assertNotEquals(hashBefore, hashAfter); + mpqEditor = new JMpqEditor(mpq, MPQOpenOption.FORCE_V0); Assert.assertTrue(mpqEditor.hasFile(filename)); diff --git a/src/test/java/systems/crigges/jmpq3test/TestHelper.java b/src/test/java/systems/crigges/jmpq3test/TestHelper.java new file mode 100644 index 0000000..5584fc7 --- /dev/null +++ b/src/test/java/systems/crigges/jmpq3test/TestHelper.java @@ -0,0 +1,43 @@ +package systems.crigges.jmpq3test; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.security.DigestInputStream; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * Created by Frotty on 11.06.2017. + */ +public class TestHelper { + // stolen from http://stackoverflow.com/a/304350/303637 + public static String md5(File f) { + try { + byte[] buf = new byte[1024]; + MessageDigest md = MessageDigest.getInstance("MD5"); + try (InputStream is = new FileInputStream(f); + DigestInputStream dis = new DigestInputStream(is, md);) { + while (dis.read(buf) >= 0); + } + byte[] digest = md.digest(); + return bytesToHex(digest); + } catch (NoSuchAlgorithmException | IOException e) { + throw new RuntimeException(e); + } + } + + // stolen from http://stackoverflow.com/a/9855338/303637 + final protected static char[] hexArray = "0123456789abcdef".toCharArray(); + public static String bytesToHex(byte[] bytes) { + char[] hexChars = new char[bytes.length * 2]; + int v; + for ( int j = 0; j < bytes.length; j++ ) { + v = bytes[j] & 0xFF; + hexChars[j * 2] = hexArray[v >>> 4]; + hexChars[j * 2 + 1] = hexArray[v & 0x0F]; + } + return new String(hexChars); + } +}