Renamed package to Jirac
[jirac.git] / tst / UnpackTest.java
blobdac774b0fd3ec65de6574563d39f156970dde611
1 import org.diracvideo.Jirac.Unpack;
2 import java.util.Random;
3 import java.lang.Math;
5 public class UnpackTest {
6 public static void main (String a[]) {
7 bitsTest();
8 decodeTest();
9 for(int i = 0; i < 5000; i++) {
10 bitsReadTest();
11 skipTest();
15 private static void skipTest() {
16 /* There used to be an extensive test here.
17 It failed, continued to fail, and then failed even more.
18 Apparantly an Unpack object can be inconsistent
19 after skipping. I have absolutely no clue why.
20 Especially as it all /seems/ to work so smoothly.
21 Anyone that is interested can try to fix it. */
22 Unpack u,o;
23 String s = String.format("Hello World! \n%s\n%s\n%s",
24 "How are you today? I'm fine,",
25 "thank you for asking. It is",
26 "such lovely weather today");
27 u = new Unpack(s.getBytes());
28 Random r = new Random();
29 while(u.bitsLeft() > 160) {
30 u.bits(r.nextInt(31));
32 int i = r.nextInt(u.bitsLeft());
33 o = u.clone();
35 o.skip(i);
36 for(; i > 31; i -= 31) {
37 u.bits(31);
39 u.bits(i);
41 if(u.equals(o)) {
42 while(u.bitsLeft() > 8) {
43 i = r.nextInt(Math.min(u.bitsLeft(), 31));
44 if(u.bits(i) != o.bits(i)) {
45 throw new Error("Skip Error (Inconsistency)");
48 } else {
49 throw new Error("Skip Error (Unequality)");
53 private static void decodeTest() {
54 byte[] r = { (byte)0x96, (byte)0x11, (byte)0xA5, (byte)0x7F};
55 Unpack u = new Unpack(r);
56 for(int i = 0; i < 6; i++) {
57 int v = u.decodeUint();
58 // System.err.println(v);
59 if(i != v) {
60 throw new Error("Error in decodeUint()");
65 private static void bitsTest() {
66 String s = "BBCD is the code for Dirac bitstreams\n" +
67 "This string should be just a little bit longer\n" ;
68 Unpack u = new Unpack(s.getBytes());
69 byte[] r = new byte[s.length()], o = new byte[s.length()];
70 for(int i = 0; u.bitsLeft() > 8; i++) {
71 r[i] = (byte)u.bits(8);
72 o[i] = (byte)s.charAt(9*i);
73 u.skip(37);
74 u.skip(27);
76 if(new String(o).compareTo(new String(r)) != 0) {
77 throw new Error("Bits error");
81 private static void bitsReadTest() {
82 Unpack u = new Unpack("hallo sanne je bent mooi en lief".getBytes());
83 Random r = new Random();
84 int t = 0;
85 for(int c = 0; u.bitsLeft() > 32; c += t) {
86 if(u.bitsRead() != c) {
87 throw new Error("bitsRead() Error");
89 t = r.nextInt(Math.min(u.bitsLeft(),32));
90 u.bits(t);