Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Better test programs... getting the hang of junit.

RoundTripTest now has a pretty useful main function.  No longer
reading seeds from a text file, just add one line to test code.
  • Loading branch information...
commit fcd756f3e8a2e39b7bc9f36ff2ec1cec07751aea 1 parent 10c59af
@league authored
View
18 net/contrapunctus/lzma/ConcurrentBufferInputTest.java
@@ -27,15 +27,7 @@
Collection<Object[]> args = new ArrayList<Object[]>();
args.add(new Object[] { System.currentTimeMillis(), true });
args.add(new Object[] { System.currentTimeMillis(), false });
- // get the rest of the seeds from text file
- FileInputStream fis = new FileInputStream("tests/input-seeds.txt");
- BufferedReader br = new BufferedReader(new InputStreamReader(fis));
- String s = br.readLine();
- while(s != null)
- {
- args.add(new Object[] { Long.parseLong(s), false });
- s = br.readLine();
- }
+ args.add(new Object[] { 1251234417455L, false });
return args;
}
@@ -54,9 +46,15 @@ public ConcurrentBufferInputTest(long seed, boolean boundary)
this.boundary = boundary;
}
+ public String toString()
+ {
+ return (boundary? "boundary" : "random") +
+ " seed " + seed + 'L';
+ }
+
@Test(timeout=5000) public void run() throws InterruptedException
{
- System.out.printf("boundary %s seed %dL\n", boundary, seed);
+ System.out.printf("%s: ", this);
Random rng = new Random(seed);
if(boundary)
{
View
58 net/contrapunctus/lzma/ConcurrentBufferOutputTest.java
@@ -2,11 +2,16 @@
import java.io.IOException;
import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collection;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.zip.CRC32;
-import org.junit.Test;
import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized.Parameters;
+import org.junit.runners.Parameterized;
import static net.contrapunctus.lzma.ConcurrentBufferOutputStream.*;
/**
@@ -14,13 +19,44 @@
* is randomized, but the seed is printed, so that results should be
* reproducible if needed.
*/
+@RunWith(Parameterized.class)
public class ConcurrentBufferOutputTest
{
- @Test public void withRandomSeed() throws Exception
+ @Parameters public static Collection<Object[]> parameters()
+ {
+ Collection<Object[]> args = new ArrayList<Object[]>();
+ args.add(new Object[] { 0L });
+ args.add(new Object[] { System.currentTimeMillis() });
+ args.add(new Object[] { 1251302491693L });
+ return args;
+ }
+
+ public static void main(String[] args)
+ throws InterruptedException
{
- long seed = System.currentTimeMillis();
- System.out.println("seed " + seed);
- withSeed(seed);
+ new ConcurrentBufferOutputTest(Long.parseLong(args[0]));
+ }
+
+ private long seed;
+ private Writer wr;
+
+ public ConcurrentBufferOutputTest(long seed)
+ {
+ this.seed = seed;
+ this.wr = (0L == seed)? new BoundaryWriter() :
+ new RandomWriter(new Random(seed));
+ }
+
+ public String toString()
+ {
+ return "seed " + seed + "L";
+ }
+
+ @Test(timeout=5000) public void run()
+ throws InterruptedException
+ {
+ System.out.printf("%s:", this);
+ testReadWrite(wr);
}
static final int MAX_BUFFER = BUFSIZE * 2;
@@ -182,17 +218,7 @@ private void testReadWrite(Writer wr) throws InterruptedException
wr.join();
Assert.assertNull(wr.exn);
Assert.assertNull(rd.exn);
- System.out.printf("sums %x -> %x\n", wr.getSum(), rd.getSum());
+ System.out.printf(" sums %x -> %x\n", wr.getSum(), rd.getSum());
Assert.assertEquals(wr.getSum(), rd.getSum());
}
-
- private void withSeed(long seed) throws InterruptedException
- {
- testReadWrite(new RandomWriter(new Random(seed)));
- }
-
- @Test public void boundaryTest() throws InterruptedException
- {
- testReadWrite(new BoundaryWriter());
- }
}
View
101 net/contrapunctus/lzma/RoundTripTest.java
@@ -32,31 +32,76 @@
Collection<Object[]> args = new ArrayList<Object[]>();
for(File f : fs)
{
- args.add(new Object[] { f });
+ args.add(new Object[] { f, false });
+ args.add(new Object[] { f, true });
}
return args;
}
+ String name;
byte[] original;
+ boolean header;
- public RoundTripTest(File f0) throws IOException
+ private static final boolean DEBUG;
+ private static final String sample;
+ static {
+ String ds = null;
+ try { ds = System.getProperty("DEBUG_RoundTrip"); }
+ catch(SecurityException e) { }
+ DEBUG = ds != null;
+
+ String s = null;
+ try {
+ s = System.getProperty("RoundTripText");
+ }
+ catch(SecurityException e) { }
+ if(s != null) sample = s;
+ else sample = "Yes yes yes test test test.";
+ }
+
+ public RoundTripTest(File file, boolean header) throws IOException
{
- RandomAccessFile f = new RandomAccessFile(f0, "r");
- long len = f.length();
- assert len < Integer.MAX_VALUE;
- original = new byte[(int)len];
- f.readFully(original);
+ this.header = header;
+ if(file != null)
+ {
+ this.name = file.getName();
+ RandomAccessFile f = new RandomAccessFile(file, "r");
+ long len = f.length();
+ assert len < Integer.MAX_VALUE; // huge files will fail, because
+ original = new byte[(int)len]; // we read whole thing into mem
+ f.readFully(original);
+ }
+ else
+ {
+ this.name = "-";
+ this.original = sample.getBytes();
+ }
+ }
+
+ public String toString()
+ {
+ return name + '[' + (header? '+':'-') + ']';
}
@Test public void run() throws IOException
{
+ System.out.printf("%s:", this);
+ LzmaOutputStream.LZMA_HEADER = header;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
LzmaOutputStream los = new LzmaOutputStream( baos );
los.write(original);
los.close();
byte[] compressed = baos.toByteArray();
- System.out.printf("original %d, compressed %d\n",
+ System.out.printf(" original %d, compressed %d\n",
original.length, compressed.length);
+ if(DEBUG)
+ {
+ for(int i = 0; i < compressed.length; i++)
+ {
+ System.out.printf("%02x ", compressed[i]);
+ }
+ System.out.println();
+ }
// and back again
ByteArrayInputStream bais = new ByteArrayInputStream(compressed);
LzmaInputStream lis = new LzmaInputStream(bais);
@@ -66,36 +111,20 @@ public RoundTripTest(File f0) throws IOException
Assert.assertTrue(Arrays.equals(original, expanded));
}
- public static void doit( ) throws IOException
+ public static void main( String[] args ) throws IOException
{
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- LzmaOutputStream lo = new LzmaOutputStream( baos );
- PrintStream ps = new PrintStream( lo );
- String k = "Yes yes yes test test test.";
- ps.print( k );
- ps.close( );
- byte[] buf = baos.toByteArray();
-
- for(int i = 0; i < buf.length; i++)
+ if(0 == args.length)
{
- System.out.printf("%02x ", buf[i]);
+ new RoundTripTest(null, false).run();
+ new RoundTripTest(null, true).run();
+ }
+ else
+ {
+ for(String s : args)
+ {
+ new RoundTripTest(new File(s), false).run();
+ new RoundTripTest(new File(s), true).run();
+ }
}
- System.out.println();
- // and back again
- ByteArrayInputStream bais = new ByteArrayInputStream( buf );
- LzmaInputStream li = new LzmaInputStream( bais );
- BufferedReader br = new BufferedReader(new InputStreamReader(li));
- String s = br.readLine();
- System.out.println( s );
- System.out.println( k );
- assert s.equals( k );
- }
-
- public static void main( String[] args ) throws IOException
- {
- LzmaOutputStream.LZMA_HEADER = true;
- doit();
- LzmaOutputStream.LZMA_HEADER = false;
- doit();
}
}
View
1  tests/input-seeds.txt
@@ -1 +0,0 @@
-1251234417455
Please sign in to comment.
Something went wrong with that request. Please try again.