Permalink
Browse files

Added random test.

  • Loading branch information...
esstrifork committed Jun 23, 2011
1 parent 6b4cc39 commit f755a978e6367199f826c1e406219aa6e1948c4b
Showing with 40 additions and 0 deletions.
  1. +40 −0 src/test/java/com/trifork/deltazip/DeltaZipTest.java
@@ -4,6 +4,7 @@
import java.io.IOException;
import com.trifork.deltazip.DeltaZip.AppendSpecification;
import com.trifork.deltazip.DZUtil.ByteArrayAccess;
+import java.util.Random;
import org.junit.Test;
import static junit.framework.Assert.*;
@@ -109,6 +110,39 @@ public void test_add_get_with(byte[] file0, ByteBuffer rev1, ByteBuffer rev2) th
} catch (Exception e) {}
}
+ @Test
+ public void totally_random_test() throws IOException {
+ final Random rnd = new Random();
+ byte[] file = new byte[0];
+
+ // Create versions:
+ ByteBuffer[] versions = new ByteBuffer[100];
+ for (int i=0; i<versions.length; i++)
+ versions[i] = createRandomBinary(1000, rnd);
+
+ // Add versions:
+ for (int i=0; i<100; i++) {
+ ByteArrayAccess access = new ByteArrayAccess(file);
+ DeltaZip dz = new DeltaZip(access);
+ AppendSpecification app_spec =
+ dz.add(versions[i]);
+ file = access.applyAppendSpec(app_spec);
+ }
+
+ {// Verify contents:
+ ByteArrayAccess access = new ByteArrayAccess(file);
+ DeltaZip dz = new DeltaZip(access);
+ for (int i=versions.length-1; i>=0; i--) {
+ assertEquals(dz.get(), versions[i]);
+ if (i>0) {
+ assert(dz.hasPrevious());
+ dz.previous();
+ }
+ }
+ assert(! dz.hasPrevious());
+ }
+ }
+
//======================================================================
public static String toString(ByteBuffer buf) {
@@ -129,4 +163,10 @@ public static void dump(String s, byte[] buf) {
}
System.err.println(">>");
}
+
+ public static ByteBuffer createRandomBinary(int length, Random rnd) {
+ byte[] buf = new byte[length];
+ rnd.nextBytes(buf);
+ return ByteBuffer.wrap(buf).asReadOnlyBuffer();
+ }
}

0 comments on commit f755a97

Please sign in to comment.