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