Skip to content

Commit

Permalink
fixes #16 and some codebeat stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Frotty committed Jun 11, 2017
1 parent b32886c commit 63162ac
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 30 deletions.
6 changes: 3 additions & 3 deletions src/main/java/ru/eustas/zopfli/Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

public class Buffer {

byte[] data;
int size;
private byte[] data;
private int size;
private int bp;

Buffer() {
Expand All @@ -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);
Expand Down
16 changes: 2 additions & 14 deletions src/main/java/ru/eustas/zopfli/Cookie.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ru/eustas/zopfli/LongestMatchCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ru/eustas/zopfli/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
30 changes: 21 additions & 9 deletions src/test/java/systems/crigges/jmpq3test/MpqTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -47,48 +47,48 @@ 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);
Assert.assertEquals(ht.getFileBlockIndex(fp1, defaultLocale), 0);
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();
}
Expand Down Expand Up @@ -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);
Expand All @@ -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));

Expand Down
43 changes: 43 additions & 0 deletions src/test/java/systems/crigges/jmpq3test/TestHelper.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 63162ac

Please sign in to comment.