Test case clean-up + minor fixes.

This commit is contained in:
Harald Kuhr 2011-11-01 13:31:29 +01:00
parent 38b197f6c1
commit 8dcfb46bdb
3 changed files with 22 additions and 39 deletions

View File

@ -28,9 +28,9 @@
package com.twelvemonkeys.io.enc; package com.twelvemonkeys.io.enc;
import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.EOFException;
/** /**
* Decoder implementation for Apple PackBits run-length encoding. * Decoder implementation for Apple PackBits run-length encoding.
@ -63,6 +63,8 @@ import java.io.EOFException;
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/io/enc/PackBitsDecoder.java#1 $ * @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/io/enc/PackBitsDecoder.java#1 $
*/ */
public final class PackBitsDecoder implements Decoder { public final class PackBitsDecoder implements Decoder {
// TODO: Look at ICNSImageReader#unpackbits... What is this weirdness?
private final boolean disableNoop; private final boolean disableNoop;
private int leftOfRun; private int leftOfRun;
@ -78,10 +80,8 @@ public final class PackBitsDecoder implements Decoder {
* Creates a {@code PackBitsDecoder}, with optional compatibility mode. * Creates a {@code PackBitsDecoder}, with optional compatibility mode.
* <p/> * <p/>
* As some implementations of PackBits-like encoders treat {@code -128} as length of * As some implementations of PackBits-like encoders treat {@code -128} as length of
* a compressed run, instead of a no-op, it's possible to disable no-ops * a compressed run, instead of a no-op, it's possible to disable no-ops for compatibility.
* for compatibility. * Should be used with caution, even though, most known encoders never write no-ops in the compressed streams.
* Should be used with caution, even though, most known encoders never write
* no-ops in the compressed streams.
* *
* @param pDisableNoop {@code true} if {@code -128} should be treated as a compressed run, and not a no-op * @param pDisableNoop {@code true} if {@code -128} should be treated as a compressed run, and not a no-op
*/ */
@ -93,11 +93,10 @@ public final class PackBitsDecoder implements Decoder {
* Decodes bytes from the given input stream, to the given buffer. * Decodes bytes from the given input stream, to the given buffer.
* *
* @param pStream the stream to decode from * @param pStream the stream to decode from
* @param pBuffer a byte array, minimum 128 (or 129 if no-op is disabled) * @param pBuffer a byte array, minimum 128 (or 129 if no-op is disabled) bytes long
* bytes long
* @return The number of bytes decoded * @return The number of bytes decoded
* *
* @throws IOException * @throws java.io.IOException
*/ */
public int decode(final InputStream pStream, final byte[] pBuffer) throws IOException { public int decode(final InputStream pStream, final byte[] pBuffer) throws IOException {
if (reachedEOF) { if (reachedEOF) {
@ -164,7 +163,7 @@ public final class PackBitsDecoder implements Decoder {
return read; return read;
} }
private static byte readByte(final InputStream pStream) throws IOException { static byte readByte(final InputStream pStream) throws IOException {
int read = pStream.read(); int read = pStream.read();
if (read < 0) { if (read < 0) {
@ -174,21 +173,21 @@ public final class PackBitsDecoder implements Decoder {
return (byte) read; return (byte) read;
} }
private static void readFully(final InputStream pStream, final byte[] pBuffer, final int pOffset, final int pLength) throws IOException { static void readFully(final InputStream pStream, final byte[] pBuffer, final int pOffset, final int pLength) throws IOException {
if (pLength < 0) { if (pLength < 0) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException(String.format("Negative length: %d", pLength));
} }
int read = 0; int total = 0;
while (read < pLength) { while (total < pLength) {
int count = pStream.read(pBuffer, pOffset + read, pLength - read); int count = pStream.read(pBuffer, pOffset + total, pLength - total);
if (count < 0) { if (count < 0) {
throw new EOFException("Unexpected end of PackBits stream"); throw new EOFException("Unexpected end of PackBits stream");
} }
read += count; total += count;
} }
} }
} }

View File

@ -5,7 +5,6 @@ import com.twelvemonkeys.lang.ObjectAbstractTestCase;
import org.junit.Test; import org.junit.Test;
import java.io.*; import java.io.*;
import java.util.Arrays;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -25,17 +24,13 @@ public abstract class DecoderAbstractTestCase extends ObjectAbstractTestCase {
return createDecoder(); return createDecoder();
} }
@Test @Test(expected = NullPointerException.class)
public final void testNullDecode() throws IOException { public final void testNullDecode() throws IOException {
Decoder decoder = createDecoder(); Decoder decoder = createDecoder();
ByteArrayInputStream bytes = new ByteArrayInputStream(new byte[20]); ByteArrayInputStream bytes = new ByteArrayInputStream(new byte[20]);
try { decoder.decode(bytes, null);
decoder.decode(bytes, null); fail("null should throw NullPointerException");
fail("null should throw NullPointerException");
}
catch (NullPointerException e) {
}
} }
@Test @Test
@ -44,7 +39,7 @@ public abstract class DecoderAbstractTestCase extends ObjectAbstractTestCase {
ByteArrayInputStream bytes = new ByteArrayInputStream(new byte[0]); ByteArrayInputStream bytes = new ByteArrayInputStream(new byte[0]);
try { try {
int count = decoder.decode(bytes, new byte[2]); int count = decoder.decode(bytes, new byte[128]);
assertEquals("Should not be able to read any bytes", 0, count); assertEquals("Should not be able to read any bytes", 0, count);
} }
catch (EOFException allowed) { catch (EOFException allowed) {
@ -68,27 +63,21 @@ public abstract class DecoderAbstractTestCase extends ObjectAbstractTestCase {
byte[] encoded = outBytes.toByteArray(); byte[] encoded = outBytes.toByteArray();
byte[] decoded = FileUtil.read(new DecoderStream(new ByteArrayInputStream(encoded), createDecoder())); byte[] decoded = FileUtil.read(new DecoderStream(new ByteArrayInputStream(encoded), createDecoder()));
assertTrue(Arrays.equals(data, decoded)); assertArrayEquals(String.format("Data %d", pLength), data, decoded);
InputStream in = new DecoderStream(new ByteArrayInputStream(encoded), createDecoder()); InputStream in = new DecoderStream(new ByteArrayInputStream(encoded), createDecoder());
outBytes = new ByteArrayOutputStream(); outBytes = new ByteArrayOutputStream();
/*
byte[] buffer = new byte[3];
for (int n = in.read(buffer); n > 0; n = in.read(buffer)) {
outBytes.write(buffer, 0, n);
}
*/
FileUtil.copy(in, outBytes); FileUtil.copy(in, outBytes);
outBytes.close(); outBytes.close();
in.close(); in.close();
decoded = outBytes.toByteArray(); decoded = outBytes.toByteArray();
assertTrue(Arrays.equals(data, decoded)); assertArrayEquals(String.format("Data %d", pLength), data, decoded);
} }
@Test @Test
public final void testStreams() throws Exception { public final void testStreams() throws Exception {
for (int i = 0; i < 100; i++) { for (int i = 1; i < 100; i++) {
try { try {
runStreamTest(i); runStreamTest(i);
} }

View File

@ -1,10 +1,5 @@
package com.twelvemonkeys.io.enc; package com.twelvemonkeys.io.enc;
import com.twelvemonkeys.io.enc.Decoder;
import com.twelvemonkeys.io.enc.Encoder;
import com.twelvemonkeys.io.enc.PackBitsDecoder;
import com.twelvemonkeys.io.enc.PackBitsEncoder;
/** /**
* PackBitsDecoderTest * PackBitsDecoderTest
* <p/> * <p/>