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 fixture
= new BitBuffer(allocateDirect
);
46 fixture
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
47 fixture
= createBuffer();
50 private static BitBuffer
createBuffer() throws CTFException
{
51 return createBuffer(16);
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);
59 ByteBuffer wrap
= ByteBuffer
.wrap(bytes
);
60 BitBuffer fixture
= new BitBuffer(wrap
);
66 * Test {@link BitBuffer#getInt} with a basic value
68 * @throws CTFException
72 public void testGetInt_base() throws CTFException
{
73 int result
= fixture
.getInt();
74 assertEquals(0x020406, result
);
78 * Test {@link BitBuffer#getInt} with explicit seek at pos 0.
80 * @throws CTFException
84 public void testGetInt_pos0() throws CTFException
{
86 int result
= fixture
.getInt();
87 assertEquals(0x010203, result
);
91 * Test {@link BitBuffer#get} with seek at pos 1.
93 * @throws CTFException
97 public void testGetInt_pos1() throws CTFException
{
100 long result
= fixture
.get(1, true);
101 assertEquals(0, result
);
105 * Test {@link BitBuffer#get} with seek at pos 2.
107 * @throws CTFException
111 public void testGetInt_pos2() throws CTFException
{
114 long result
= fixture
.get(0, true);
115 assertEquals(0, result
);
119 * Test {@link BitBuffer#get} with explicit little-endian reading.
121 * @throws CTFException
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
);
134 * Test {@link BitBuffer#get} with explicit little-endian reading, with an
137 * @throws CTFException
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 */
150 * Test {@link BitBuffer#get} with a 32-bit out-of-bounds read. Should throw
153 * @throws CTFException
156 @Test(expected
= CTFException
.class)
157 public void testGetInt_invalid() throws CTFException
{
158 BitBuffer smallFixture
= createBuffer(2);
159 smallFixture
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
161 smallFixture
.position(10);
163 /* This will attempt to read past the buffer's end. */
164 smallFixture
.get(32, true);
168 * Test {@link BitBuffer#get} with a 64-bit out-of-bounds read. Should throw
171 * @throws CTFException
174 @Test(expected
= CTFException
.class)
175 public void testGetInt_invalid2() throws CTFException
{
176 BitBuffer smallFixture
= createBuffer(2);
177 smallFixture
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
179 smallFixture
.position(1);
181 /* This will attempt to read past the buffer's end. */
182 smallFixture
.get(64, true);
186 * Test {@link BitBuffer#getLong}.
188 * @throws CTFException
192 public void testGetLong_pos0() throws CTFException
{
194 long result
= fixture
.getLong();
195 assertEquals(0x01020304050607L
, result
);
199 * Test {@link BitBuffer#getLong} with an offset of 7.
201 * @throws CTFException
205 public void testGetLong_pos7() throws CTFException
{
207 long result
= fixture
.getLong();
208 assertEquals(0x81018202830384L
, result
);
212 * Test {@link BitBuffer#getLong} with an offset of 8.
214 * @throws CTFException
218 public void testGetLong_pos8() throws CTFException
{
220 long result
= fixture
.getLong();
221 assertEquals(0x0102030405060708L
, result
);
225 * Test {@link BitBuffer#getLong} with a little-endian buffer.
227 * @throws CTFException
231 public void testGetLong_pos0LE() throws CTFException
{
233 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
234 long result
= fixture
.getLong();
235 assertEquals(0x0706050403020100L
, result
);
239 * Test {@link BitBuffer#getLong} with a little-endian buffer at pos 7.
241 * @throws CTFException
245 public void testGetLong_pos7LE() throws CTFException
{
247 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
248 long result
= fixture
.getLong();
249 assertEquals(0x100e0c0a08060402L
, result
);
253 * Test {@link BitBuffer#getLong} with a little-endian buffer at pos 8.
255 * @throws CTFException
259 public void testGetLong_pos8LE() throws CTFException
{
261 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
262 long result
= fixture
.getLong();
263 assertEquals(0x0807060504030201L
, result
);
267 * Test {@link BitBuffer#get} for >32 bits in length.
269 * @throws CTFException
273 public void testGet35_pos0BE() throws CTFException
{
275 long result
= fixture
.get(35, false);
276 assertEquals(0x081018L
, result
);
280 * Test {@link BitBuffer#get} for >32 bits in length at an offset position.
282 * @throws CTFException
286 public void testGet35_pos8BE() throws CTFException
{
288 long result
= fixture
.get(35, false);
289 assertEquals(0x08101820L
, result
);
293 * Test {@link BitBuffer#get} for >32 bits in length in little-endian.
295 * @throws CTFException
299 public void testGet35_pos0LE() throws CTFException
{
301 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
302 long result
= fixture
.get(35, false);
303 assertEquals(0x0403020100L
, result
);
307 * Test {@link BitBuffer#get} for >32 bits in length, in little-endian, at
310 * @throws CTFException
314 public void testGetLong35_pos7LE() throws CTFException
{
316 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
317 long result
= fixture
.get(35, false);
318 assertEquals(0x0208060402L
, result
);
322 * Test {@link BitBuffer#get} for >32 bits in length, in little-endian, at
325 * @throws CTFException
329 public void testGetLong35_pos8LE() throws CTFException
{
331 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
332 long result
= fixture
.get(35, false);
333 assertEquals(0x0504030201L
, result
);
337 * Test {@link BitBuffer#get} for >32 bits in length, in little-endian, for
340 * @throws CTFException
344 public void testGetLong35s_pos0LE() throws CTFException
{
346 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
347 long result
= fixture
.get(35, true);
348 assertEquals(0xfffffffc03020100L
, result
);
352 * Test {@link BitBuffer#get} for >32 bits in length, in little-endian, for
353 * a signed value, at position 7.
355 * @throws CTFException
359 public void testGetLong35s_pos7LE() throws CTFException
{
361 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
362 long result
= fixture
.get(35, true);
363 assertEquals(0x0208060402L
, result
);
367 * Test {@link BitBuffer#get} for >32 bits in length, in little-endian, for
368 * a signed value, at position 8.
370 * @throws CTFException
374 public void testGetLong35s_pos8LE() throws CTFException
{
376 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
377 long result
= fixture
.get(35, true);
378 assertEquals(0xfffffffd04030201L
, result
);
382 * Test reading negative values as signed values.
384 * @throws CTFException
388 public void testGetSigned() throws CTFException
{
393 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
395 long result
= fixture
.get(32, true);
396 assertEquals(-1L, result
);
400 * Test reading negative values as unsigned values.
402 * @throws CTFException
406 public void testGetUnsigned() throws CTFException
{
411 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
413 long result
= fixture
.get(32, false);
414 assertEquals(0xFFFFFFFFL
, result
);
418 * Test reading 24 bits of a 32-bit negative value as a signed value.
420 * @throws CTFException
424 public void testGet24Signed() throws CTFException
{
429 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
431 long result
= fixture
.get(24, true);
432 assertEquals(-1L, result
);
436 * Test reading 24 bits of a 32-bit negative value as an unsigned value.
438 * @throws CTFException
442 public void testGet24Unsigned() throws CTFException
{
447 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
449 long result
= fixture
.get(24, false);
450 assertEquals(0xFFFFFFL
, result
);
454 * Test {@link BitBuffer#putInt(int)}
456 * @throws CTFException
460 public void testPutInt() throws CTFException
{
466 * Test {@link BitBuffer#putInt(int, int)}
468 * @throws CTFException
472 public void testPutInt_length1() throws CTFException
{
474 fixture
.putInt(1, 1);
478 * Test {@link BitBuffer#putInt(int, int)} with length = 0.
480 * @throws CTFException
484 public void testPutInt_length0() throws CTFException
{
486 fixture
.putInt(0, 1);
490 * Test {@link BitBuffer#putInt(int)} Little endian
492 * @throws CTFException
496 public void testPutIntLe() throws CTFException
{
497 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
503 * Test {@link BitBuffer#putInt(int, int)} Little endian
505 * @throws CTFException
509 public void testPutIntLe_length1() throws CTFException
{
510 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
512 fixture
.putInt(1, 1);
516 * Test {@link BitBuffer#putInt(int, int)} with length = 0. Little endian
518 * @throws CTFException
522 public void testPutIntLe_length0() throws CTFException
{
523 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
525 fixture
.putInt(0, 1);
529 * Test writing and reading a value defined in hex format.
531 * @throws CTFException
535 public void testPutInt_hex() throws CTFException
{
536 final int value
= 0x010203;
538 for (int i
= 0; i
<= 32; i
++) {
540 fixture
.putInt(value
);
543 int read
= fixture
.getInt();
545 assertEquals(value
, read
);
550 * Test {@link BitBuffer#putInt} with an out-of-bounds length. An exception
553 * @throws CTFException
556 @Test(expected
= CTFException
.class)
557 public void testPutInt_invalid() throws CTFException
{
559 fixture2
= createBuffer(4);
560 fixture2
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
561 fixture2
.position(1);
563 /* This will try writing past the buffer's end */
564 fixture2
.putInt(32, 1);