Debian Codegraph @

test/uk/me/parabola/imgfmt/app/net/VarBitWriterTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2012.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13 package uk.me.parabola.imgfmt.app.net;
14
15 import uk.me.parabola.imgfmt.app.BitWriter;
16
17 import org.junit.Test;
18
19 import static org.junit.Assert.*;
20 /**
21 * @author Steve Ratcliffe
22 */
23 public class VarBitWriterTest {
24
25 private final BitWriter bw = new BitWriter();
26
27 @Test
28 public void testPositive() {
29 VarBitWriter vbw = new VarBitWriter(bw, 3);
30
31 // should be able to write numbers up to 7
32 vbw.write(7);
33
34 byte b = bw.getBytes()[0];
35 assertEquals(b, 7);
36 }
37
38 @Test
39 public void testPositiveWithWidth() {
40 VarBitWriter vbw = new VarBitWriter(bw, 3);
41 vbw.bitWidth = 1;
42
43 // should be able to write numbers up to 15
44 vbw.write(15);
45
46 byte b = bw.getBytes()[0];
47 assertEquals(b, 15);
48 }
49
50 @Test(expected = Abandon.class)
51 public void testPositiveWithWidthFail() {
52 VarBitWriter vbw = new VarBitWriter(bw, 3);
53 vbw.bitWidth = 1;
54
55 // should be able to write numbers up to 15
56 vbw.write(16);
57 assertTrue(false);
58 }
59
60 @Test(expected=Abandon.class)
61 public void testPositiveFail() {
62 VarBitWriter vbw = new VarBitWriter(bw, 3);
63
64 // should be able to write numbers up to 7
65 vbw.write(8);
66 assertTrue(false);
67 }
68
69 @Test
70 public void testNegative() {
71 VarBitWriter vbw = new VarBitWriter(bw, 3);
72 vbw.negative = true;
73
74 // write up to -7
75 vbw.write(-7);
76 byte b = bw.getBytes()[0];
77 assertEquals(b, 7);
78 }
79
80 @Test(expected = Abandon.class)
81 public void testNegativeWithPositive() {
82 VarBitWriter vbw = new VarBitWriter(bw, 3);
83 vbw.negative = true;
84
85 // positive numbers are invalid
86 vbw.write(7);
87 assertTrue(false);
88 }
89
90 @Test(expected = Abandon.class)
91 public void testNegativeTooBig() {
92 VarBitWriter vbw = new VarBitWriter(bw, 3);
93 vbw.negative = true;
94
95 // number too large
96 vbw.write(8);
97 assertTrue(false);
98 }
99
100 @Test
101 public void testSignedPositive() {
102 VarBitWriter vbw = new VarBitWriter(bw, 3);
103 vbw.signed = true;
104
105 // up to 7
106 vbw.write(7);
107 byte b = bw.getBytes()[0];
108 assertEquals(b, 7);
109 }
110
111 @Test
112 public void testSignedNegative() {
113 VarBitWriter vbw = new VarBitWriter(bw, 3);
114 vbw.signed = true;
115
116 // up to -8
117 vbw.write(-8);
118 byte b = bw.getBytes()[0];
119 assertEquals(b, 0x8);
120 }
121
122 @Test(expected = Abandon.class)
123 public void testSignedPositiveTooBig() {
124 VarBitWriter vbw = new VarBitWriter(bw, 3);
125 vbw.signed = true;
126
127 // up to 7, 8 too big
128 vbw.write(8);
129 assertTrue(false);
130 }
131
132 @Test(expected = Abandon.class)
133 public void testSignedNegativeTooBig() {
134 VarBitWriter vbw = new VarBitWriter(bw, 3);
135 vbw.signed = true;
136
137 // up to -8, -9 is too big
138 vbw.write(-9);
139 assertTrue(false);
140 }
141 }