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
9 * Alexandre Montplaisir - Extracted from BitBufferTest, cleanup
10 * Matthew Khouzam - Additional tests
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.ctf
.core
.tests
.io
;
15 import static org
.junit
.Assert
.assertEquals
;
17 import java
.nio
.ByteBuffer
;
18 import java
.nio
.ByteOrder
;
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
;
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
30 * @author Alexandre Montplaisir
32 public class BitBufferIntTest
{
34 private BitBuffer fixture
;
37 * Perform pre-test initialization.
39 * @throws CTFException
40 * Out of bounds, won't happen
43 public void setUp() throws CTFException
{
44 ByteBuffer allocateDirect
= ByteBuffer
.allocateDirect(128);
45 if (allocateDirect
== null) {
46 throw new IllegalStateException("Failed to allocate memory");
48 fixture
= new BitBuffer(allocateDirect
);
49 fixture
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
50 fixture
= createBuffer();
53 private static BitBuffer
createBuffer() throws CTFException
{
54 return createBuffer(16);
57 private static BitBuffer
createBuffer(int j
) throws CTFException
{
58 final byte[] bytes
= new byte[j
];
59 for (int i
= 0; i
< j
; i
++) {
60 bytes
[i
] = (byte) (i
% 0xff);
62 ByteBuffer wrap
= ByteBuffer
.wrap(bytes
);
64 throw new IllegalStateException("Failed to allocate memory");
66 BitBuffer fixture
= new BitBuffer(wrap
);
72 * Test {@link BitBuffer#getInt} with a basic value
74 * @throws CTFException
78 public void testGetInt_base() throws CTFException
{
79 int result
= fixture
.getInt();
80 assertEquals(0x020406, result
);
84 * Test {@link BitBuffer#getInt} with explicit seek at pos 0.
86 * @throws CTFException
90 public void testGetInt_pos0() throws CTFException
{
92 int result
= fixture
.getInt();
93 assertEquals(0x010203, result
);
97 * Test {@link BitBuffer#get} with seek at pos 1.
99 * @throws CTFException
103 public void testGetInt_pos1() throws CTFException
{
106 long result
= fixture
.get(1, true);
107 assertEquals(0, result
);
111 * Test {@link BitBuffer#get} with seek at pos 2.
113 * @throws CTFException
117 public void testGetInt_pos2() throws CTFException
{
120 long result
= fixture
.get(0, true);
121 assertEquals(0, result
);
125 * Test {@link BitBuffer#get} with explicit little-endian reading.
127 * @throws CTFException
131 public void testGetInt_le2() throws CTFException
{
132 BitBuffer leFixture
= createBuffer(128);
133 leFixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
134 leFixture
.position(0);
135 long result
= leFixture
.get(24, false);
136 assertEquals(0x020100, result
);
140 * Test {@link BitBuffer#get} with explicit little-endian reading, with an
143 * @throws CTFException
147 public void testGetInt_le1() throws CTFException
{
148 BitBuffer leFixture
= createBuffer(128);
149 leFixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
150 leFixture
.position(1);
151 long result
= leFixture
.get(24, false);
152 assertEquals(0x810080, result
); /* 0x020100 down-shifted */
156 * Test {@link BitBuffer#get} with a 32-bit out-of-bounds read. Should throw
159 * @throws CTFException
162 @Test(expected
= CTFException
.class)
163 public void testGetInt_invalid() throws CTFException
{
164 BitBuffer smallFixture
= createBuffer(2);
165 smallFixture
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
167 smallFixture
.position(10);
169 /* This will attempt to read past the buffer's end. */
170 smallFixture
.get(32, true);
174 * Test {@link BitBuffer#get} with a 64-bit out-of-bounds read. Should throw
177 * @throws CTFException
180 @Test(expected
= CTFException
.class)
181 public void testGetInt_invalid2() throws CTFException
{
182 BitBuffer smallFixture
= createBuffer(2);
183 smallFixture
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
185 smallFixture
.position(1);
187 /* This will attempt to read past the buffer's end. */
188 smallFixture
.get(64, true);
192 * Test {@link BitBuffer#getLong}.
194 * @throws CTFException
198 public void testGetLong_pos0() throws CTFException
{
200 long result
= fixture
.getLong();
201 assertEquals(0x01020304050607L
, result
);
205 * Test {@link BitBuffer#getLong} with an offset of 7.
207 * @throws CTFException
211 public void testGetLong_pos7() throws CTFException
{
213 long result
= fixture
.getLong();
214 assertEquals(0x81018202830384L
, result
);
218 * Test {@link BitBuffer#getLong} with an offset of 8.
220 * @throws CTFException
224 public void testGetLong_pos8() throws CTFException
{
226 long result
= fixture
.getLong();
227 assertEquals(0x0102030405060708L
, result
);
231 * Test {@link BitBuffer#getLong} with a little-endian buffer.
233 * @throws CTFException
237 public void testGetLong_pos0LE() throws CTFException
{
239 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
240 long result
= fixture
.getLong();
241 assertEquals(0x0706050403020100L
, result
);
245 * Test {@link BitBuffer#getLong} with a little-endian buffer at pos 7.
247 * @throws CTFException
251 public void testGetLong_pos7LE() throws CTFException
{
253 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
254 long result
= fixture
.getLong();
255 assertEquals(0x100e0c0a08060402L
, result
);
259 * Test {@link BitBuffer#getLong} with a little-endian buffer at pos 8.
261 * @throws CTFException
265 public void testGetLong_pos8LE() throws CTFException
{
267 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
268 long result
= fixture
.getLong();
269 assertEquals(0x0807060504030201L
, result
);
273 * Test {@link BitBuffer#get} for >32 bits in length.
275 * @throws CTFException
279 public void testGet35_pos0BE() throws CTFException
{
281 long result
= fixture
.get(35, false);
282 assertEquals(0x081018L
, result
);
286 * Test {@link BitBuffer#get} for >32 bits in length at an offset position.
288 * @throws CTFException
292 public void testGet35_pos8BE() throws CTFException
{
294 long result
= fixture
.get(35, false);
295 assertEquals(0x08101820L
, result
);
299 * Test {@link BitBuffer#get} for >32 bits in length in little-endian.
301 * @throws CTFException
305 public void testGet35_pos0LE() throws CTFException
{
307 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
308 long result
= fixture
.get(35, false);
309 assertEquals(0x0403020100L
, result
);
313 * Test {@link BitBuffer#get} for >32 bits in length, in little-endian, at
316 * @throws CTFException
320 public void testGetLong35_pos7LE() throws CTFException
{
322 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
323 long result
= fixture
.get(35, false);
324 assertEquals(0x0208060402L
, result
);
328 * Test {@link BitBuffer#get} for >32 bits in length, in little-endian, at
331 * @throws CTFException
335 public void testGetLong35_pos8LE() throws CTFException
{
337 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
338 long result
= fixture
.get(35, false);
339 assertEquals(0x0504030201L
, result
);
343 * Test {@link BitBuffer#get} for >32 bits in length, in little-endian, for
346 * @throws CTFException
350 public void testGetLong35s_pos0LE() throws CTFException
{
352 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
353 long result
= fixture
.get(35, true);
354 assertEquals(0xfffffffc03020100L
, result
);
358 * Test {@link BitBuffer#get} for >32 bits in length, in little-endian, for
359 * a signed value, at position 7.
361 * @throws CTFException
365 public void testGetLong35s_pos7LE() throws CTFException
{
367 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
368 long result
= fixture
.get(35, true);
369 assertEquals(0x0208060402L
, result
);
373 * Test {@link BitBuffer#get} for >32 bits in length, in little-endian, for
374 * a signed value, at position 8.
376 * @throws CTFException
380 public void testGetLong35s_pos8LE() throws CTFException
{
382 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
383 long result
= fixture
.get(35, true);
384 assertEquals(0xfffffffd04030201L
, result
);
388 * Test reading negative values as signed values.
390 * @throws CTFException
394 public void testGetSigned() throws CTFException
{
399 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
401 long result
= fixture
.get(32, true);
402 assertEquals(-1L, result
);
406 * Test reading negative values as unsigned values.
408 * @throws CTFException
412 public void testGetUnsigned() throws CTFException
{
417 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
419 long result
= fixture
.get(32, false);
420 assertEquals(0xFFFFFFFFL
, result
);
424 * Test reading 24 bits of a 32-bit negative value as a signed value.
426 * @throws CTFException
430 public void testGet24Signed() throws CTFException
{
435 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
437 long result
= fixture
.get(24, true);
438 assertEquals(-1L, result
);
442 * Test reading 24 bits of a 32-bit negative value as an unsigned value.
444 * @throws CTFException
448 public void testGet24Unsigned() throws CTFException
{
453 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
455 long result
= fixture
.get(24, false);
456 assertEquals(0xFFFFFFL
, result
);
460 * Test {@link BitBuffer#putInt(int)}
462 * @throws CTFException
466 public void testPutInt() throws CTFException
{
472 * Test {@link BitBuffer#putInt(int, int)}
474 * @throws CTFException
478 public void testPutInt_length1() throws CTFException
{
480 fixture
.putInt(1, 1);
484 * Test {@link BitBuffer#putInt(int, int)} with length = 0.
486 * @throws CTFException
490 public void testPutInt_length0() throws CTFException
{
492 fixture
.putInt(0, 1);
496 * Test {@link BitBuffer#putInt(int)} Little endian
498 * @throws CTFException
502 public void testPutIntLe() throws CTFException
{
503 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
509 * Test {@link BitBuffer#putInt(int, int)} Little endian
511 * @throws CTFException
515 public void testPutIntLe_length1() throws CTFException
{
516 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
518 fixture
.putInt(1, 1);
522 * Test {@link BitBuffer#putInt(int, int)} with length = 0. Little endian
524 * @throws CTFException
528 public void testPutIntLe_length0() throws CTFException
{
529 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
531 fixture
.putInt(0, 1);
535 * Test writing and reading a value defined in hex format.
537 * @throws CTFException
541 public void testPutInt_hex() throws CTFException
{
542 final int value
= 0x010203;
544 for (int i
= 0; i
<= 32; i
++) {
546 fixture
.putInt(value
);
549 int read
= fixture
.getInt();
551 assertEquals(value
, read
);
556 * Test {@link BitBuffer#putInt} with an out-of-bounds length. An exception
559 * @throws CTFException
562 @Test(expected
= CTFException
.class)
563 public void testPutInt_invalid() throws CTFException
{
565 fixture2
= createBuffer(4);
566 fixture2
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
567 fixture2
.position(1);
569 /* This will try writing past the buffer's end */
570 fixture2
.putInt(32, 1);