aedef1995968f650d49ff3fba63334f70731b923
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / io / BitBufferIntTest.java
1 package org.eclipse.linuxtools.ctf.core.tests.io;
2
3 import static org.junit.Assert.assertEquals;
4
5 import java.nio.ByteBuffer;
6 import java.nio.ByteOrder;
7
8 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
9 import org.junit.After;
10 import org.junit.Before;
11 import org.junit.Test;
12
13 /**
14 * Part of the BitBuffet tests with test the methods to read/write integers.
15 * These are separated from the main file because the fixture is different.
16 *
17 * @author alexmont
18 *
19 */
20 public class BitBufferIntTest {
21
22 private BitBuffer fixture;
23
24 /**
25 * Launch the test.
26 *
27 * @param args
28 * the command line arguments
29 */
30 public static void main(String[] args) {
31 new org.junit.runner.JUnitCore().run(BitBufferTest.class);
32 }
33
34 /**
35 * Perform pre-test initialization.
36 */
37 @Before
38 public void setUp() {
39 fixture = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128));
40 fixture.setByteOrder(ByteOrder.BIG_ENDIAN);
41 createBuffer(fixture);
42 }
43
44 /**
45 * Perform post-test clean-up.
46 */
47 @After
48 public void tearDown() {
49 // Add additional tear down code here
50 }
51
52 private static void createBuffer(BitBuffer fixture) {
53 createBuffer(fixture, 16);
54 }
55
56 private static void createBuffer(BitBuffer fixture, int j) {
57 byte[] bytes = new byte[j];
58 for (int i = 0; i < j; i++) {
59 bytes[i] = (byte) (i % 0xff);
60 }
61 fixture.setByteBuffer(ByteBuffer.wrap(bytes));
62 fixture.position(1);
63 }
64
65 /**
66 * Run the int getInt() method test.
67 */
68 @Test
69 public void testGetInt_base() {
70 int result = fixture.getInt();
71 assertEquals(0x020406, result);
72 }
73
74 /**
75 * Run the int getInt(int) method test.
76 */
77 @Test
78 public void testGetInt_pos0() {
79 fixture.position(0);
80 int result = fixture.getInt();
81 assertEquals(0x010203, result);
82 }
83
84 /**
85 * Run the int getInt(int,boolean) method test.
86 */
87 @Test
88 public void testGetInt_pos1() {
89 fixture.position(1);
90 int length = 1;
91 boolean signed = true;
92
93 int result = fixture.getInt(length, signed);
94 assertEquals(0, result);
95 }
96
97 /**
98 * Run the int getInt(int,boolean) method test.
99 */
100 @Test
101 public void testGetInt_pos2() {
102 fixture.position(2);
103 int length = 0;
104 boolean signed = true;
105
106 int result = fixture.getInt(length, signed);
107 assertEquals(0, result);
108 }
109
110 /**
111 * Run the int getInt(int,int,boolean) method test.
112 */
113 @Test
114 public void testGetInt_signed() {
115 fixture.position(1);
116 int index = 1;
117 int length = 0;
118 boolean signed = true;
119
120 int result = fixture.getInt(index, length, signed);
121 assertEquals(0, result);
122 }
123
124 /**
125 * Run the int getInt(int,int,boolean) method test.
126 */
127 @Test
128 public void testGetInt_signed_length1() {
129 fixture.position(1);
130 int index = 1;
131 int length = 1;
132 boolean signed = true;
133
134 int result = fixture.getInt(index, length, signed);
135 assertEquals(0, result);
136 }
137
138 /**
139 * Run the int getInt(int,int,boolean) method test with a little-endian
140 * BitBuffer.
141 */
142 @Test
143 public void testGetInt_le1() {
144 BitBuffer le_fixture = new BitBuffer(
145 java.nio.ByteBuffer.allocateDirect(128));
146 le_fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
147 createBuffer(le_fixture);
148 le_fixture.position(1);
149 int index = 1;
150 int length = 24;
151 int result = le_fixture.getInt(index, length, false);
152
153 /* 0x020100 downshifted */
154 assertEquals(0x810080, result);
155 }
156
157 /**
158 * Run the int getInt(int,int,boolean) method test with a little-endian
159 * BitBuffer.
160 */
161 @Test
162 public void testGetInt_le2() {
163 BitBuffer le_fixture = new BitBuffer(
164 java.nio.ByteBuffer.allocateDirect(128));
165 le_fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
166 createBuffer(le_fixture);
167 le_fixture.position(0);
168 int index = 0;
169 int length = 24;
170 int result = le_fixture.getInt(index, length, false);
171 assertEquals(0x020100, result);
172 }
173
174 /**
175 * Run the int getInt(int,boolean) method test and expect an overflow.
176 */
177 @Test(expected = java.nio.BufferOverflowException.class)
178 public void testGetInt_invalid() {
179 BitBuffer small_fixture = new BitBuffer(
180 java.nio.ByteBuffer.allocateDirect(128));
181 small_fixture.setByteOrder(ByteOrder.BIG_ENDIAN);
182 createBuffer(small_fixture, 2);
183 small_fixture.position(10);
184 int length = 32;
185 boolean signed = true;
186
187 int result = small_fixture.getInt(length, signed);
188 assertEquals(0, result);
189 }
190
191 /**
192 * Run the int getInt(int,int,boolean) method test and expect an overflow.
193 */
194 @Test(expected = java.nio.BufferOverflowException.class)
195 public void testGetInt_invalid2() {
196 BitBuffer small_fixture = new BitBuffer(
197 java.nio.ByteBuffer.allocateDirect(128));
198 small_fixture.setByteOrder(ByteOrder.BIG_ENDIAN);
199 createBuffer(small_fixture, 2);
200 small_fixture.position(1);
201 int index = 1;
202 int length = 64;
203 boolean signed = true;
204
205 int result = small_fixture.getInt(index, length, signed);
206 assertEquals(0, result);
207 }
208
209 /**
210 * Run the void putInt(int) method test.
211 */
212 @Test
213 public void testPutInt() {
214 int value = 1;
215 fixture.position(1);
216 fixture.putInt(value);
217 }
218
219 /**
220 * Run the void putInt(int,int,boolean) method test.
221 */
222 @Test
223 public void testPutInt_signed() {
224 int length = 1;
225 int value = 1;
226
227 fixture.position(1);
228 fixture.putInt(length, value);
229 }
230
231 /**
232 * Run the void putInt(int,int,int,boolean) method test.
233 */
234 @Test
235 public void testPutInt_length0() {
236 int index = 1;
237 int length = 0;
238 int value = 1;
239
240 fixture.position(1);
241 fixture.putInt(index, length, value);
242 }
243
244 /**
245 * Run the void putInt(int,int,int,boolean) method test.
246 */
247 @Test
248 public void testPutInt_length1() {
249 int index = 1;
250 int length = 1;
251 int value = 1;
252
253 fixture.position(1);
254 fixture.putInt(index, length, value);
255 }
256
257 /**
258 * Run the void putInt(int) method test.
259 */
260 @Test
261 public void testPutInt_hex() {
262 int value = 0x010203;
263
264 fixture.position(1);
265 fixture.putInt(value);
266 int read = fixture.getInt();
267 assertEquals(value, read);
268 }
269
270 /**
271 * Run the void putInt(int,int,int,boolean) method test.
272 */
273 @Test(expected = java.nio.BufferOverflowException.class)
274 public void testPutInt_invalid() {
275 BitBuffer fixture2;
276 fixture2 = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128));
277 fixture2.setByteOrder(ByteOrder.BIG_ENDIAN);
278 createBuffer(fixture2, 4);
279 fixture2.position(1);
280
281 int index = 16;
282 int length = 32;
283 int value = 1;
284
285 fixture2.putInt(index, length, value);
286
287 int read = fixture2.getInt(1, true);
288 assertEquals(value, read);
289 }
290 }
This page took 0.036538 seconds and 5 git commands to generate.