common: Annotate some methods in ByteBuffer
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / io / BitBufferIntTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 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.tracecompass.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.tracecompass.ctf.core.CTFException;
21 import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
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 * @throws CTFException
40 * Out of bounds, won't happen
41 */
42 @Before
43 public void setUp() throws CTFException {
44 ByteBuffer allocateDirect = ByteBuffer.allocateDirect(128);
45 fixture = new BitBuffer(allocateDirect);
46 fixture.setByteOrder(ByteOrder.BIG_ENDIAN);
47 fixture = createBuffer();
48 }
49
50 private static BitBuffer createBuffer() throws CTFException {
51 return createBuffer(16);
52 }
53
54 private static BitBuffer createBuffer(int j) throws CTFException {
55 final byte[] bytes = new byte[j];
56 for (int i = 0; i < j; i++) {
57 bytes[i] = (byte) (i % 0xff);
58 }
59 ByteBuffer wrap = ByteBuffer.wrap(bytes);
60 BitBuffer fixture = new BitBuffer(wrap);
61 fixture.position(1);
62 return fixture;
63 }
64
65 /**
66 * Test {@link BitBuffer#getInt} with a basic value
67 *
68 * @throws CTFException
69 * Not expected
70 */
71 @Test
72 public void testGetInt_base() throws CTFException {
73 int result = fixture.getInt();
74 assertEquals(0x020406, result);
75 }
76
77 /**
78 * Test {@link BitBuffer#getInt} with explicit seek at pos 0.
79 *
80 * @throws CTFException
81 * Not expected
82 */
83 @Test
84 public void testGetInt_pos0() throws CTFException {
85 fixture.position(0);
86 int result = fixture.getInt();
87 assertEquals(0x010203, result);
88 }
89
90 /**
91 * Test {@link BitBuffer#get} with seek at pos 1.
92 *
93 * @throws CTFException
94 * Not expected
95 */
96 @Test
97 public void testGetInt_pos1() throws CTFException {
98 fixture.position(1);
99
100 long result = fixture.get(1, true);
101 assertEquals(0, result);
102 }
103
104 /**
105 * Test {@link BitBuffer#get} with seek at pos 2.
106 *
107 * @throws CTFException
108 * Not expected
109 */
110 @Test
111 public void testGetInt_pos2() throws CTFException {
112 fixture.position(2);
113
114 long result = fixture.get(0, true);
115 assertEquals(0, result);
116 }
117
118 /**
119 * Test {@link BitBuffer#get} with explicit little-endian reading.
120 *
121 * @throws CTFException
122 * Not expected
123 */
124 @Test
125 public void testGetInt_le2() throws CTFException {
126 BitBuffer leFixture = createBuffer(128);
127 leFixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
128 leFixture.position(0);
129 long result = leFixture.get(24, false);
130 assertEquals(0x020100, result);
131 }
132
133 /**
134 * Test {@link BitBuffer#get} with explicit little-endian reading, with an
135 * offset.
136 *
137 * @throws CTFException
138 * Not expected
139 */
140 @Test
141 public void testGetInt_le1() throws CTFException {
142 BitBuffer leFixture = createBuffer(128);
143 leFixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
144 leFixture.position(1);
145 long result = leFixture.get(24, false);
146 assertEquals(0x810080, result); /* 0x020100 down-shifted */
147 }
148
149 /**
150 * Test {@link BitBuffer#get} with a 32-bit out-of-bounds read. Should throw
151 * an exception.
152 *
153 * @throws CTFException
154 * Expected
155 */
156 @Test(expected = CTFException.class)
157 public void testGetInt_invalid() throws CTFException {
158 BitBuffer smallFixture = createBuffer(2);
159 smallFixture.setByteOrder(ByteOrder.BIG_ENDIAN);
160
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 CTFException
172 * Expected
173 */
174 @Test(expected = CTFException.class)
175 public void testGetInt_invalid2() throws CTFException {
176 BitBuffer smallFixture = createBuffer(2);
177 smallFixture.setByteOrder(ByteOrder.BIG_ENDIAN);
178
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 CTFException
189 * error
190 */
191 @Test
192 public void testGetLong_pos0() throws CTFException {
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 CTFException
202 * error
203 */
204 @Test
205 public void testGetLong_pos7() throws CTFException {
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 CTFException
215 * error
216 */
217 @Test
218 public void testGetLong_pos8() throws CTFException {
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 CTFException
228 * error
229 */
230 @Test
231 public void testGetLong_pos0LE() throws CTFException {
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 CTFException
242 * error
243 */
244 @Test
245 public void testGetLong_pos7LE() throws CTFException {
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 CTFException
256 * error
257 */
258 @Test
259 public void testGetLong_pos8LE() throws CTFException {
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 CTFException
270 * error
271 */
272 @Test
273 public void testGet35_pos0BE() throws CTFException {
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 CTFException
283 * error
284 */
285 @Test
286 public void testGet35_pos8BE() throws CTFException {
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 CTFException
296 * error
297 */
298 @Test
299 public void testGet35_pos0LE() throws CTFException {
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 CTFException
311 * error
312 */
313 @Test
314 public void testGetLong35_pos7LE() throws CTFException {
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 CTFException
326 * error
327 */
328 @Test
329 public void testGetLong35_pos8LE() throws CTFException {
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 CTFException
341 * error
342 */
343 @Test
344 public void testGetLong35s_pos0LE() throws CTFException {
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 CTFException
356 * error
357 */
358 @Test
359 public void testGetLong35s_pos7LE() throws CTFException {
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 CTFException
371 * error
372 */
373 @Test
374 public void testGetLong35s_pos8LE() throws CTFException {
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 CTFException
385 * error
386 */
387 @Test
388 public void testGetSigned() throws CTFException {
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 CTFException
403 * error
404 */
405 @Test
406 public void testGetUnsigned() throws CTFException {
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 * Test reading 24 bits of a 32-bit negative value as a signed value.
419 *
420 * @throws CTFException
421 * error
422 */
423 @Test
424 public void testGet24Signed() throws CTFException {
425 fixture.position(0);
426 fixture.putInt(-1);
427 fixture.putInt(-1);
428 fixture.position(0);
429 fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
430
431 long result = fixture.get(24, true);
432 assertEquals(-1L, result);
433 }
434
435 /**
436 * Test reading 24 bits of a 32-bit negative value as an unsigned value.
437 *
438 * @throws CTFException
439 * error
440 */
441 @Test
442 public void testGet24Unsigned() throws CTFException {
443 fixture.position(0);
444 fixture.putInt(-1);
445 fixture.putInt(-1);
446 fixture.position(0);
447 fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
448
449 long result = fixture.get(24, false);
450 assertEquals(0xFFFFFFL, result);
451 }
452
453 /**
454 * Test {@link BitBuffer#putInt(int)}
455 *
456 * @throws CTFException
457 * Not expected
458 */
459 @Test
460 public void testPutInt() throws CTFException {
461 fixture.position(1);
462 fixture.putInt(1);
463 }
464
465 /**
466 * Test {@link BitBuffer#putInt(int, int)}
467 *
468 * @throws CTFException
469 * Not expected
470 */
471 @Test
472 public void testPutInt_length1() throws CTFException {
473 fixture.position(1);
474 fixture.putInt(1, 1);
475 }
476
477 /**
478 * Test {@link BitBuffer#putInt(int, int)} with length = 0.
479 *
480 * @throws CTFException
481 * Not expected
482 */
483 @Test
484 public void testPutInt_length0() throws CTFException {
485 fixture.position(1);
486 fixture.putInt(0, 1);
487 }
488
489 /**
490 * Test {@link BitBuffer#putInt(int)} Little endian
491 *
492 * @throws CTFException
493 * Not expected
494 */
495 @Test
496 public void testPutIntLe() throws CTFException {
497 fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
498 fixture.position(1);
499 fixture.putInt(1);
500 }
501
502 /**
503 * Test {@link BitBuffer#putInt(int, int)} Little endian
504 *
505 * @throws CTFException
506 * Not expected
507 */
508 @Test
509 public void testPutIntLe_length1() throws CTFException {
510 fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
511 fixture.position(1);
512 fixture.putInt(1, 1);
513 }
514
515 /**
516 * Test {@link BitBuffer#putInt(int, int)} with length = 0. Little endian
517 *
518 * @throws CTFException
519 * Not expected
520 */
521 @Test
522 public void testPutIntLe_length0() throws CTFException {
523 fixture.setByteOrder(ByteOrder.LITTLE_ENDIAN);
524 fixture.position(1);
525 fixture.putInt(0, 1);
526 }
527
528 /**
529 * Test writing and reading a value defined in hex format.
530 *
531 * @throws CTFException
532 * Not expected
533 */
534 @Test
535 public void testPutInt_hex() throws CTFException {
536 final int value = 0x010203;
537
538 for (int i = 0; i <= 32; i++) {
539 fixture.position(i);
540 fixture.putInt(value);
541
542 fixture.position(i);
543 int read = fixture.getInt();
544
545 assertEquals(value, read);
546 }
547 }
548
549 /**
550 * Test {@link BitBuffer#putInt} with an out-of-bounds length. An exception
551 * should be thrown.
552 *
553 * @throws CTFException
554 * Expected
555 */
556 @Test(expected = CTFException.class)
557 public void testPutInt_invalid() throws CTFException {
558 BitBuffer fixture2;
559 fixture2 = createBuffer(4);
560 fixture2.setByteOrder(ByteOrder.BIG_ENDIAN);
561 fixture2.position(1);
562
563 /* This will try writing past the buffer's end */
564 fixture2.putInt(32, 1);
565 }
566 }
This page took 0.04451 seconds and 5 git commands to generate.