ctf: Cleanup BitBufferIntTest
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / io / BitBufferIntTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013 Ericsson
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Alexandre Montplaisir - Extracted from BitBufferTest, cleanup
10 * Matthew Khouzam - Additional tests
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.ctf.core.tests.io;
14
15 import static org.junit.Assert.assertEquals;
16
17 import java.nio.ByteBuffer;
18 import java.nio.ByteOrder;
19
20 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
21 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
22 import org.junit.Before;
23 import org.junit.Test;
24
25 /**
26 * Part of the {@link BitBuffer} tests which test the methods to read/write
27 * integers. These are separated from the main file because the fixture is
28 * different.
29 *
30 * @author Alexandre Montplaisir
31 */
32 public class BitBufferIntTest {
33
34 private BitBuffer fixture;
35
36 /**
37 * Perform pre-test initialization.
38 */
39 @Before
40 public void setUp() {
41 fixture = new BitBuffer(ByteBuffer.allocateDirect(128));
42 fixture.setByteOrder(ByteOrder.BIG_ENDIAN);
43 createBuffer(fixture);
44 }
45
46 private static void createBuffer(BitBuffer fixture) {
47 createBuffer(fixture, 16);
48 }
49
50 private static void createBuffer(BitBuffer fixture, int j) {
51 byte[] bytes = new byte[j];
52 for (int i = 0; i < j; i++) {
53 bytes[i] = (byte) (i % 0xff);
54 }
55 fixture.setByteBuffer(ByteBuffer.wrap(bytes));
56 fixture.position(1);
57 }
58
59 /**
60 * Test {@link BitBuffer#getInt} with a basic value
61 *
62 * @throws CTFReaderException
63 * Not expected
64 */
65 @Test
66 public void testGetInt_base() throws CTFReaderException {
67 int result = fixture.getInt();
68 assertEquals(0x020406, result);
69 }
70
71 /**
72 * Test {@link BitBuffer#getInt} with explicit seek at pos 0.
73 *
74 * @throws CTFReaderException
75 * Not expected
76 */
77 @Test
78 public void testGetInt_pos0() throws CTFReaderException {
79 fixture.position(0);
80 int result = fixture.getInt();
81 assertEquals(0x010203, result);
82 }
83
84 /**
85 * Test {@link BitBuffer#get} with seek at pos 1.
86 *
87 * @throws CTFReaderException
88 * Not expected
89 */
90 @Test
91 public void testGetInt_pos1() throws CTFReaderException {
92 fixture.position(1);
93
94 long result = fixture.get(1, true);
95 assertEquals(0, result);
96 }
97
98 /**
99 * Test {@link BitBuffer#get} with seek at pos 2.
100 *
101 * @throws CTFReaderException
102 * Not expected
103 */
104 @Test
105 public void testGetInt_pos2() throws CTFReaderException {
106 fixture.position(2);
107
108 long result = fixture.get(0, true);
109 assertEquals(0, result);
110 }
111
112
113 /**
114 * Test {@link BitBuffer#get} with explicit little-endian reading.
115 *
116 * @throws CTFReaderException
117 * Not expected
118 */
119 @Test
120 public void testGetInt_le2() throws CTFReaderException {
121 BitBuffer leFixture = new BitBuffer(ByteBuffer.allocateDirect(128));
122 leFixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
123 createBuffer(leFixture);
124 leFixture.position(0);
125
126 long result = leFixture.get(24, false);
127 assertEquals(0x020100, result);
128 }
129
130 /**
131 * Test {@link BitBuffer#get} with explicit little-endian reading, with an
132 * offset.
133 *
134 * @throws CTFReaderException
135 * Not expected
136 */
137 @Test
138 public void testGetInt_le1() throws CTFReaderException {
139 BitBuffer leFixture = new BitBuffer(ByteBuffer.allocateDirect(128));
140 leFixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
141 createBuffer(leFixture);
142 leFixture.position(1);
143
144 long result = leFixture.get(24, false);
145 assertEquals(0x810080, result); /* 0x020100 down-shifted */
146 }
147
148
149 /**
150 * Test {@link BitBuffer#get} with a 32-bit out-of-bounds read. Should throw
151 * an exception.
152 *
153 * @throws CTFReaderException
154 * Expected
155 */
156 @Test(expected = CTFReaderException.class)
157 public void testGetInt_invalid() throws CTFReaderException {
158 BitBuffer smallFixture = new BitBuffer(ByteBuffer.allocateDirect(128));
159 smallFixture.setByteOrder(ByteOrder.BIG_ENDIAN);
160 createBuffer(smallFixture, 2);
161 smallFixture.position(10);
162
163 /* This will attempt to read past the buffer's end. */
164 smallFixture.get(32, true);
165 }
166
167 /**
168 * Test {@link BitBuffer#get} with a 64-bit out-of-bounds read. Should throw
169 * an exception.
170 *
171 * @throws CTFReaderException
172 * Expected
173 */
174 @Test(expected = CTFReaderException.class)
175 public void testGetInt_invalid2() throws CTFReaderException {
176 BitBuffer smallFixture = new BitBuffer(ByteBuffer.allocateDirect(128));
177 smallFixture.setByteOrder(ByteOrder.BIG_ENDIAN);
178 createBuffer(smallFixture, 2);
179 smallFixture.position(1);
180
181 /* This will attempt to read past the buffer's end. */
182 smallFixture.get(64, true);
183 }
184
185 /**
186 * Test {@link BitBuffer#getLong}.
187 *
188 * @throws CTFReaderException
189 * error
190 */
191 @Test
192 public void testGetLong_pos0() throws CTFReaderException {
193 fixture.position(0);
194 long result = fixture.getLong();
195 assertEquals(0x01020304050607L, result);
196 }
197
198 /**
199 * Test {@link BitBuffer#getLong} with an offset of 7.
200 *
201 * @throws CTFReaderException
202 * error
203 */
204 @Test
205 public void testGetLong_pos7() throws CTFReaderException {
206 fixture.position(7);
207 long result = fixture.getLong();
208 assertEquals(0x81018202830384L, result);
209 }
210
211 /**
212 * Test {@link BitBuffer#getLong} with an offset of 8.
213 *
214 * @throws CTFReaderException
215 * error
216 */
217 @Test
218 public void testGetLong_pos8() throws CTFReaderException {
219 fixture.position(8);
220 long result = fixture.getLong();
221 assertEquals(0x0102030405060708L, result);
222 }
223
224 /**
225 * Test {@link BitBuffer#getLong} with a little-endian buffer.
226 *
227 * @throws CTFReaderException
228 * error
229 */
230 @Test
231 public void testGetLong_pos0LE() throws CTFReaderException {
232 fixture.position(0);
233 fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
234 long result = fixture.getLong();
235 assertEquals(0x0706050403020100L, result);
236 }
237
238 /**
239 * Test {@link BitBuffer#getLong} with a little-endian buffer at pos 7.
240 *
241 * @throws CTFReaderException
242 * error
243 */
244 @Test
245 public void testGetLong_pos7LE() throws CTFReaderException {
246 fixture.position(7);
247 fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
248 long result = fixture.getLong();
249 assertEquals(0x100e0c0a08060402L, result);
250 }
251
252 /**
253 * Test {@link BitBuffer#getLong} with a little-endian buffer at pos 8.
254 *
255 * @throws CTFReaderException
256 * error
257 */
258 @Test
259 public void testGetLong_pos8LE() throws CTFReaderException {
260 fixture.position(8);
261 fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
262 long result = fixture.getLong();
263 assertEquals(0x0807060504030201L, result);
264 }
265
266 /**
267 * Test {@link BitBuffer#get} for >32 bits in length.
268 *
269 * @throws CTFReaderException
270 * error
271 */
272 @Test
273 public void testGet35_pos0BE() throws CTFReaderException {
274 fixture.position(0);
275 long result = fixture.get(35, false);
276 assertEquals(0x081018L, result);
277 }
278
279 /**
280 * Test {@link BitBuffer#get} for >32 bits in length at an offset position.
281 *
282 * @throws CTFReaderException
283 * error
284 */
285 @Test
286 public void testGet35_pos8BE() throws CTFReaderException {
287 fixture.position(8);
288 long result = fixture.get(35, false);
289 assertEquals(0x08101820L, result);
290 }
291
292 /**
293 * Test {@link BitBuffer#get} for >32 bits in length in little-endian.
294 *
295 * @throws CTFReaderException
296 * error
297 */
298 @Test
299 public void testGet35_pos0LE() throws CTFReaderException {
300 fixture.position(0);
301 fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
302 long result = fixture.get(35, false);
303 assertEquals(0x0403020100L, result);
304 }
305
306 /**
307 * Test {@link BitBuffer#get} for >32 bits in length, in little-endian, at
308 * position 7.
309 *
310 * @throws CTFReaderException
311 * error
312 */
313 @Test
314 public void testGetLong35_pos7LE() throws CTFReaderException {
315 fixture.position(7);
316 fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
317 long result = fixture.get(35, false);
318 assertEquals(0x0208060402L, result);
319 }
320
321 /**
322 * Test {@link BitBuffer#get} for >32 bits in length, in little-endian, at
323 * position 8.
324 *
325 * @throws CTFReaderException
326 * error
327 */
328 @Test
329 public void testGetLong35_pos8LE() throws CTFReaderException {
330 fixture.position(8);
331 fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
332 long result = fixture.get(35, false);
333 assertEquals(0x0504030201L, result);
334 }
335
336 /**
337 * Test {@link BitBuffer#get} for >32 bits in length, in little-endian, for
338 * a signed value.
339 *
340 * @throws CTFReaderException
341 * error
342 */
343 @Test
344 public void testGetLong35s_pos0LE() throws CTFReaderException {
345 fixture.position(0);
346 fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
347 long result = fixture.get(35, true);
348 assertEquals(0xfffffffc03020100L, result);
349 }
350
351 /**
352 * Test {@link BitBuffer#get} for >32 bits in length, in little-endian, for
353 * a signed value, at position 7.
354 *
355 * @throws CTFReaderException
356 * error
357 */
358 @Test
359 public void testGetLong35s_pos7LE() throws CTFReaderException {
360 fixture.position(7);
361 fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
362 long result = fixture.get(35, true);
363 assertEquals(0x0208060402L, result);
364 }
365
366 /**
367 * Test {@link BitBuffer#get} for >32 bits in length, in little-endian, for
368 * a signed value, at position 8.
369 *
370 * @throws CTFReaderException
371 * error
372 */
373 @Test
374 public void testGetLong35s_pos8LE() throws CTFReaderException {
375 fixture.position(8);
376 fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
377 long result = fixture.get(35, true);
378 assertEquals(0xfffffffd04030201L, result);
379 }
380
381 /**
382 * Test reading negative values as signed values.
383 *
384 * @throws CTFReaderException
385 * error
386 */
387 @Test
388 public void testGetSigned() throws CTFReaderException {
389 fixture.position(0);
390 fixture.putInt(-1);
391 fixture.putInt(-1);
392 fixture.position(0);
393 fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
394
395 long result = fixture.get(32, true);
396 assertEquals(-1L, result);
397 }
398
399 /**
400 * Test reading negative values as unsigned values.
401 *
402 * @throws CTFReaderException
403 * error
404 */
405 @Test
406 public void testGetUnsigned() throws CTFReaderException {
407 fixture.position(0);
408 fixture.putInt(-1);
409 fixture.putInt(-1);
410 fixture.position(0);
411 fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
412
413 long result = fixture.get(32, false);
414 assertEquals(0xFFFFFFFFL, result);
415 }
416
417
418 /**
419 * Test reading 24 bits of a 32-bit negative value as a signed value.
420 *
421 * @throws CTFReaderException
422 * error
423 */
424 @Test
425 public void testGet24Signed() throws CTFReaderException {
426 fixture.position(0);
427 fixture.putInt(-1);
428 fixture.putInt(-1);
429 fixture.position(0);
430 fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
431
432 long result = fixture.get(24, true);
433 assertEquals(-1L, result);
434 }
435
436 /**
437 * Test reading 24 bits of a 32-bit negative value as an unsigned value.
438 *
439 * @throws CTFReaderException
440 * error
441 */
442 @Test
443 public void testGet24Unsigned() throws CTFReaderException {
444 fixture.position(0);
445 fixture.putInt(-1);
446 fixture.putInt(-1);
447 fixture.position(0);
448 fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
449
450 long result = fixture.get(24, false);
451 assertEquals(0xFFFFFFL, result);
452 }
453
454 /**
455 * Test {@link BitBuffer#putInt(int)}
456 *
457 * @throws CTFReaderException
458 * Not expected
459 */
460 @Test
461 public void testPutInt() throws CTFReaderException {
462 fixture.position(1);
463 fixture.putInt(1);
464 }
465
466 /**
467 * Test {@link BitBuffer#putInt(int, int)}
468 *
469 * @throws CTFReaderException
470 * Not expected
471 */
472 @Test
473 public void testPutInt_length1() throws CTFReaderException {
474 fixture.position(1);
475 fixture.putInt(1, 1);
476 }
477
478 /**
479 * Test {@link BitBuffer#putInt(int, int)} with length = 0.
480 *
481 * @throws CTFReaderException
482 * Not expected
483 */
484 @Test
485 public void testPutInt_length0() throws CTFReaderException {
486 fixture.position(1);
487 fixture.putInt(0, 1);
488 }
489
490 /**
491 * Test writing and reading a value defined in hex format.
492 *
493 * @throws CTFReaderException
494 * Not expected
495 */
496 @Test
497 public void testPutInt_hex() throws CTFReaderException {
498 final int value = 0x010203;
499
500 for (int i = 0; i <= 32; i++) {
501 fixture.position(i);
502 fixture.putInt(value);
503
504 fixture.position(i);
505 int read = fixture.getInt();
506
507 assertEquals(value, read);
508 }
509 }
510
511 /**
512 * Test {@link BitBuffer#putInt} with an out-of-bounds length. An exception
513 * should be thrown.
514 *
515 * @throws CTFReaderException
516 * Expected
517 */
518 @Test(expected = CTFReaderException.class)
519 public void testPutInt_invalid() throws CTFReaderException {
520 BitBuffer fixture2;
521 fixture2 = new BitBuffer(ByteBuffer.allocateDirect(128));
522 fixture2.setByteOrder(ByteOrder.BIG_ENDIAN);
523 createBuffer(fixture2, 4);
524 fixture2.position(1);
525
526 /* This will try writing past the buffer's end */
527 fixture2.putInt(32, 1);
528 }
529 }
This page took 0.043724 seconds and 5 git commands to generate.