Skip to content

Commit

Permalink
More tests and removed some dumb code
Browse files Browse the repository at this point in the history
  • Loading branch information
markrileybot committed Dec 3, 2016
1 parent b406678 commit 5ae7b08
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 82 deletions.
22 changes: 8 additions & 14 deletions src/main/java/heatshrink/HsInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
public class HsInputStream extends FilterInputStream {

private static final double LN2 = Math.log(2);

/**
* State machine states. Don't really need this but it's nice
* to match the c code.
Expand Down Expand Up @@ -232,19 +234,16 @@ private State readBackrefCount() throws IOException {
}

private State readBackref(ReadResult rr) {
int count = rr.end - rr.off;
int count = Math.min(rr.end - rr.off, outputCount);
if(count > 0) {
if(outputCount < count) {
count = outputCount;
}
int mask = (1 << windowSize) - 1;
for(int i = 0; i < count; i++) {
for (int i = 0; i < count; i++) {
byte c = window[(windowPos - outputIndex) & mask];
rr.b[rr.off++] = c;
window[windowPos++ & mask] = c;
}
outputCount -= count;
if(outputCount == 0) {
if (outputCount == 0) {
return State.TAG_BIT;
}
}
Expand Down Expand Up @@ -333,6 +332,7 @@ public void close() throws IOException {
* @see java.io.FilterInputStream#reset()
*/
public void mark(int readlimit) {
throw new UnsupportedOperationException();
}

/**
Expand All @@ -357,6 +357,7 @@ public void mark(int readlimit) {
* @see java.io.FilterInputStream#mark(int)
*/
public void reset() throws IOException {
throw new UnsupportedOperationException();
}

/**
Expand Down Expand Up @@ -392,7 +393,7 @@ private boolean ensureAvailable(int bitsRequired) throws IOException {
int bytesRemaining = inputBufferLen - inputBufferPos;
int bitsAvailable = bytesRemaining * 8;

bitsRequired -= (currentBytePos > 0 ? 8 - (currentBytePos >> 3) : 0);
bitsRequired -= (currentBytePos > 0 ? 8 - ((Math.log(currentBytePos) / LN2) + 1) : 0);
if(bitsRequired > bitsAvailable) {
if(bytesRemaining > 0) {
// lame buffer shift won't happen often
Expand All @@ -419,12 +420,6 @@ private int getBits(int numBits) throws IOException {
}
for(; numBits > 0; numBits--, currentBytePos >>= 1) {
if(currentBytePos == 0) {
if(!ensureAvailable(numBits)) {
if(ret == 0) {
return -1;
}
throw new EOFException();
}
currentByte = inputBuffer[inputBufferPos++];
currentBytePos = 0x80;
}
Expand All @@ -443,7 +438,6 @@ private int getBits(int numBits) throws IOException {
return ret;
}


private static final class ReadResult {
int off;
int len;
Expand Down
93 changes: 93 additions & 0 deletions src/test/java/heatshrink/HsInputStreamFileTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package heatshrink;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* Created by mriley on 12/3/16.
*/
@RunWith(Parameterized.class)
public class HsInputStreamFileTest {

@Parameters(name="{0}")
public static Iterable<Object[]> generateParameters() {
List<Object[]> result = new ArrayList<>();
for(TestFile testFile : TestData.getTestFiles()) {
result.add(new Object[]{testFile});
}
if(result.isEmpty()) {
throw new RuntimeException("No test files found!");
}
return result;
}

private final TestFile testFile;

public HsInputStreamFileTest(TestFile testFile) {
this.testFile = testFile;
}

@Test
public void testReadIntoLargeBuffer() throws IOException {
byte[] compressed = FileUtils.readFileToByteArray(testFile.getCompressed());
byte[] uncompressed = FileUtils.readFileToByteArray(testFile.getUncompressed());
try(HsInputStream hsi = new HsInputStream(new ByteArrayInputStream(compressed), testFile.getWindowSize(), testFile.getLookaheadSize())) {
int btr = (int) testFile.getUncompressed().length();
byte[] uncompressed2 = new byte[btr*2];
int bytesRead = IOUtils.read(hsi, uncompressed2);
Assert.assertEquals(new String(uncompressed2), uncompressed.length, bytesRead);
byte[] uncompressed3 = new byte[btr];
System.arraycopy(uncompressed2, 0, uncompressed3, 0, uncompressed.length);
Assert.assertArrayEquals(new String(uncompressed3), uncompressed3, uncompressed);
}
}

@Test
public void testReadIntoSmallBuffer() throws IOException {
byte[] compressed = FileUtils.readFileToByteArray(testFile.getCompressed());
byte[] uncompressed = FileUtils.readFileToByteArray(testFile.getUncompressed());
try(HsInputStream hsi = new HsInputStream(new ByteArrayInputStream(compressed), testFile.getWindowSize(), testFile.getLookaheadSize())) {
int btr = (int) testFile.getUncompressed().length();
byte[] uncompressed2 = new byte[7];
byte[] uncompressed3 = new byte[btr];
int bytesRead = 0;
int total = 0;
while((bytesRead = IOUtils.read(hsi, uncompressed2)) > 0) {
System.arraycopy(uncompressed2, 0, uncompressed3, total, bytesRead);
total += bytesRead;
}
Assert.assertArrayEquals(new String(uncompressed3), uncompressed3, uncompressed);
}
}

@Test
public void readIndividualBytes() throws IOException {
byte[] compressed = FileUtils.readFileToByteArray(testFile.getCompressed());
byte[] uncompressed = FileUtils.readFileToByteArray(testFile.getUncompressed());
try(HsInputStream hsi = new HsInputStream(new ByteArrayInputStream(compressed), testFile.getWindowSize(), testFile.getLookaheadSize())) {
long i;
for(i = 0; hsi.read() != -1; i++);
Assert.assertEquals(uncompressed.length, i);
}
}

@Test
public void testSkip() throws IOException {
byte[] compressed = FileUtils.readFileToByteArray(testFile.getCompressed());
byte[] uncompressed = FileUtils.readFileToByteArray(testFile.getUncompressed());
try(HsInputStream hsi = new HsInputStream(new ByteArrayInputStream(compressed), testFile.getWindowSize(), testFile.getLookaheadSize())) {
long skipped = hsi.skip(uncompressed.length);
Assert.assertEquals(uncompressed.length, skipped);
}
}
}
85 changes: 17 additions & 68 deletions src/test/java/heatshrink/HsInputStreamTest.java
Original file line number Diff line number Diff line change
@@ -1,95 +1,44 @@
package heatshrink;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* Created by mriley on 12/2/16.
*/
@RunWith(Parameterized.class)
public class HsInputStreamTest {

@Parameters(name="{0}")
public static Iterable<Object[]> generateParameters() {
List<Object[]> result = new ArrayList<>();
for(TestFile testFile : TestData.getTestFiles()) {
result.add(new Object[]{testFile});
}
if(result.isEmpty()) {
throw new RuntimeException("No test files found!");
}
return result;
}

private final TestFile testFile;

public HsInputStreamTest(TestFile testFile) {
this.testFile = testFile;
System.err.println(testFile);
}

@Test
public void testReadIntoLargeBuffer() throws IOException {
byte[] compressed = FileUtils.readFileToByteArray(testFile.getCompressed());
byte[] uncompressed = FileUtils.readFileToByteArray(testFile.getUncompressed());
try(HsInputStream hsi = new HsInputStream(new ByteArrayInputStream(compressed), testFile.getWindowSize(), testFile.getLookaheadSize())) {
int btr = (int) testFile.getUncompressed().length();
byte[] uncompressed2 = new byte[btr*2];
int bytesRead = IOUtils.read(hsi, uncompressed2);
Assert.assertEquals(new String(uncompressed2), uncompressed.length, bytesRead);
byte[] uncompressed3 = new byte[btr];
System.arraycopy(uncompressed2, 0, uncompressed3, 0, uncompressed.length);
Assert.assertArrayEquals(new String(uncompressed3), uncompressed3, uncompressed);
public void testMarkSupported() throws IOException {
try(HsInputStream hsi = new HsInputStream(new ByteArrayInputStream(new byte[] {}))) {
Assert.assertFalse(hsi.markSupported());
}
}

@Test
public void testReadIntoSmallBuffer() throws IOException {
byte[] compressed = FileUtils.readFileToByteArray(testFile.getCompressed());
byte[] uncompressed = FileUtils.readFileToByteArray(testFile.getUncompressed());
try(HsInputStream hsi = new HsInputStream(new ByteArrayInputStream(compressed), testFile.getWindowSize(), testFile.getLookaheadSize())) {
int btr = (int) testFile.getUncompressed().length();
byte[] uncompressed2 = new byte[7];
byte[] uncompressed3 = new byte[btr];
int bytesRead = 0;
int total = 0;
while((bytesRead = IOUtils.read(hsi, uncompressed2)) > 0) {
System.arraycopy(uncompressed2, 0, uncompressed3, total, bytesRead);
total += bytesRead;
}
Assert.assertArrayEquals(new String(uncompressed3), uncompressed3, uncompressed);
@Test(expected = UnsupportedOperationException.class)
public void testMark() throws IOException {
try(HsInputStream hsi = new HsInputStream(new ByteArrayInputStream(new byte[] {}))) {
hsi.mark(123);
}
}

@Test
public void readIndividualBytes() throws IOException {
byte[] compressed = FileUtils.readFileToByteArray(testFile.getCompressed());
byte[] uncompressed = FileUtils.readFileToByteArray(testFile.getUncompressed());
try(HsInputStream hsi = new HsInputStream(new ByteArrayInputStream(compressed), testFile.getWindowSize(), testFile.getLookaheadSize())) {
long i;
for(i = 0; hsi.read() != -1; i++);
Assert.assertEquals(uncompressed.length, i);
@Test(expected = UnsupportedOperationException.class)
public void testReset() throws IOException {
try(HsInputStream hsi = new HsInputStream(new ByteArrayInputStream(new byte[] {}))) {
hsi.reset();
}
}

@Test
public void testSkip() throws IOException {
byte[] compressed = FileUtils.readFileToByteArray(testFile.getCompressed());
byte[] uncompressed = FileUtils.readFileToByteArray(testFile.getUncompressed());
try(HsInputStream hsi = new HsInputStream(new ByteArrayInputStream(compressed), testFile.getWindowSize(), testFile.getLookaheadSize())) {
long skipped = hsi.skip(uncompressed.length);
Assert.assertEquals(uncompressed.length, skipped);
public void testAvailable() throws IOException {
try(HsInputStream hsi = new HsInputStream(new ByteArrayInputStream(new byte[] {}))) {
Assert.assertEquals(0, hsi.available());
}
try(HsInputStream hsi = new HsInputStream(new ByteArrayInputStream(new byte[] {1,2,3}))) {
Assert.assertEquals(3, hsi.available());
}
}
}

0 comments on commit 5ae7b08

Please sign in to comment.