Gunzip: Add tests to main()

This commit is contained in:
zzz
2015-05-27 20:49:51 +00:00
parent 042b03d6b8
commit 1d8842cfc8

View File

@@ -375,40 +375,51 @@ public class ResettableGZIPInputStream extends InflaterInputStream {
/****** /******
public static void main(String args[]) { public static void main(String args[]) {
for (int i = 129; i < 64*1024; i++) {
if (!test(i)) return; java.util.Random r = new java.util.Random();
for (int i = 129; i < 64*1024; i+= 17) {
byte[] b = new byte[i];
r.nextBytes(b);
if (!test(b)) return;
} }
byte orig[] = "ho ho ho, merry christmas".getBytes();
try { try {
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(64);
java.util.zip.GZIPOutputStream o = new java.util.zip.GZIPOutputStream(baos);
o.write(orig);
o.finish();
o.flush();
o.close();
byte compressed[] = baos.toByteArray();
ResettableGZIPInputStream i = new ResettableGZIPInputStream(); ResettableGZIPInputStream i = new ResettableGZIPInputStream();
i.initialize(new ByteArrayInputStream(compressed)); for (int k = 1; k < 1599; k++) {
byte readBuf[] = new byte[128]; byte orig[] = new byte[k];
int read = i.read(readBuf); r.nextBytes(orig);
if (read != orig.length) java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(k+100);
throw new RuntimeException("read=" + read); java.util.zip.GZIPOutputStream o = new java.util.zip.GZIPOutputStream(baos);
for (int j = 0; j < read; j++) o.write(orig);
if (readBuf[j] != orig[j]) o.finish();
throw new RuntimeException("wtf, j=" + j + " readBuf=" + readBuf[j] + " orig=" + orig[j]); o.flush();
boolean ok = (-1 == i.read()); o.close();
if (!ok) throw new RuntimeException("wtf, not EOF after the data?"); byte compressed[] = baos.toByteArray();
i.initialize(new java.io.ByteArrayInputStream(compressed));
byte readBuf[] = new byte[k];
int read = DataHelper.read(i, readBuf);
if (read != orig.length)
throw new RuntimeException("read=" + read + " expected " + orig.length);
for (int j = 0; j < read; j++) {
if (readBuf[j] != orig[j])
throw new RuntimeException("wtf, j=" + j + " readBuf=" + readBuf[j] + " orig=" + orig[j]);
}
boolean ok = (-1 == i.read());
if (!ok) throw new RuntimeException("wtf, not EOF after the data?");
//System.out.println("Match ok");
// try both closing and not
if ((k % 2) != 0)
i.close();
}
System.out.println("Match ok"); System.out.println("Match ok");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private static boolean test(int size) { private static boolean test(byte[] b) {
byte b[] = new byte[size]; int size = b.length;
new java.util.Random().nextBytes(b);
try { try {
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(size); java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(size);
java.util.zip.GZIPOutputStream o = new java.util.zip.GZIPOutputStream(baos); java.util.zip.GZIPOutputStream o = new java.util.zip.GZIPOutputStream(baos);
@@ -417,7 +428,7 @@ public class ResettableGZIPInputStream extends InflaterInputStream {
o.flush(); o.flush();
byte compressed[] = baos.toByteArray(); byte compressed[] = baos.toByteArray();
ResettableGZIPInputStream in = new ResettableGZIPInputStream(new ByteArrayInputStream(compressed)); ResettableGZIPInputStream in = new ResettableGZIPInputStream(new java.io.ByteArrayInputStream(compressed));
java.io.ByteArrayOutputStream baos2 = new java.io.ByteArrayOutputStream(size); java.io.ByteArrayOutputStream baos2 = new java.io.ByteArrayOutputStream(size);
byte rbuf[] = new byte[512]; byte rbuf[] = new byte[512];
while (true) { while (true) {
@@ -433,7 +444,7 @@ public class ResettableGZIPInputStream extends InflaterInputStream {
if (!net.i2p.data.DataHelper.eq(rv, 0, b, 0, b.length)) { if (!net.i2p.data.DataHelper.eq(rv, 0, b, 0, b.length)) {
throw new RuntimeException("foo, read=" + rv.length); throw new RuntimeException("foo, read=" + rv.length);
} else { } else {
System.out.println("match, w00t @ " + size); //System.out.println("match, w00t @ " + size);
return true; return true;
} }
} catch (Exception e) { } catch (Exception e) {