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
9 * Alexandre Montplaisir - Extracted from BitBufferTest, cleanup
10 * Matthew Khouzam - Additional tests
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.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
.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
;
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.
41 fixture
= new BitBuffer(ByteBuffer
.allocateDirect(128));
42 fixture
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
43 createBuffer(fixture
);
46 private static void createBuffer(BitBuffer fixture
) {
47 createBuffer(fixture
, 16);
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);
55 fixture
.setByteBuffer(ByteBuffer
.wrap(bytes
));
60 * Test {@link BitBuffer#getInt} with a basic value
62 * @throws CTFReaderException
66 public void testGetInt_base() throws CTFReaderException
{
67 int result
= fixture
.getInt();
68 assertEquals(0x020406, result
);
72 * Test {@link BitBuffer#getInt} with explicit seek at pos 0.
74 * @throws CTFReaderException
78 public void testGetInt_pos0() throws CTFReaderException
{
80 int result
= fixture
.getInt();
81 assertEquals(0x010203, result
);
85 * Test {@link BitBuffer#get} with seek at pos 1.
87 * @throws CTFReaderException
91 public void testGetInt_pos1() throws CTFReaderException
{
94 long result
= fixture
.get(1, true);
95 assertEquals(0, result
);
99 * Test {@link BitBuffer#get} with seek at pos 2.
101 * @throws CTFReaderException
105 public void testGetInt_pos2() throws CTFReaderException
{
108 long result
= fixture
.get(0, true);
109 assertEquals(0, result
);
114 * Test {@link BitBuffer#get} with explicit little-endian reading.
116 * @throws CTFReaderException
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);
126 long result
= leFixture
.get(24, false);
127 assertEquals(0x020100, result
);
131 * Test {@link BitBuffer#get} with explicit little-endian reading, with an
134 * @throws CTFReaderException
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);
144 long result
= leFixture
.get(24, false);
145 assertEquals(0x810080, result
); /* 0x020100 down-shifted */
150 * Test {@link BitBuffer#get} with a 32-bit out-of-bounds read. Should throw
153 * @throws CTFReaderException
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);
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 CTFReaderException
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);
181 /* This will attempt to read past the buffer's end. */
182 smallFixture
.get(64, true);
186 * Test {@link BitBuffer#getLong}.
188 * @throws CTFReaderException
192 public void testGetLong_pos0() throws CTFReaderException
{
194 long result
= fixture
.getLong();
195 assertEquals(0x01020304050607L
, result
);
199 * Test {@link BitBuffer#getLong} with an offset of 7.
201 * @throws CTFReaderException
205 public void testGetLong_pos7() throws CTFReaderException
{
207 long result
= fixture
.getLong();
208 assertEquals(0x81018202830384L
, result
);
212 * Test {@link BitBuffer#getLong} with an offset of 8.
214 * @throws CTFReaderException
218 public void testGetLong_pos8() throws CTFReaderException
{
220 long result
= fixture
.getLong();
221 assertEquals(0x0102030405060708L
, result
);
225 * Test {@link BitBuffer#getLong} with a little-endian buffer.
227 * @throws CTFReaderException
231 public void testGetLong_pos0LE() throws CTFReaderException
{
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 CTFReaderException
245 public void testGetLong_pos7LE() throws CTFReaderException
{
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 CTFReaderException
259 public void testGetLong_pos8LE() throws CTFReaderException
{
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 CTFReaderException
273 public void testGet35_pos0BE() throws CTFReaderException
{
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 CTFReaderException
286 public void testGet35_pos8BE() throws CTFReaderException
{
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 CTFReaderException
299 public void testGet35_pos0LE() throws CTFReaderException
{
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 CTFReaderException
314 public void testGetLong35_pos7LE() throws CTFReaderException
{
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 CTFReaderException
329 public void testGetLong35_pos8LE() throws CTFReaderException
{
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 CTFReaderException
344 public void testGetLong35s_pos0LE() throws CTFReaderException
{
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 CTFReaderException
359 public void testGetLong35s_pos7LE() throws CTFReaderException
{
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 CTFReaderException
374 public void testGetLong35s_pos8LE() throws CTFReaderException
{
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 CTFReaderException
388 public void testGetSigned() throws CTFReaderException
{
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 CTFReaderException
406 public void testGetUnsigned() throws CTFReaderException
{
411 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
413 long result
= fixture
.get(32, false);
414 assertEquals(0xFFFFFFFFL
, result
);
419 * Test reading 24 bits of a 32-bit negative value as a signed value.
421 * @throws CTFReaderException
425 public void testGet24Signed() throws CTFReaderException
{
430 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
432 long result
= fixture
.get(24, true);
433 assertEquals(-1L, result
);
437 * Test reading 24 bits of a 32-bit negative value as an unsigned value.
439 * @throws CTFReaderException
443 public void testGet24Unsigned() throws CTFReaderException
{
448 fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
450 long result
= fixture
.get(24, false);
451 assertEquals(0xFFFFFFL
, result
);
455 * Test {@link BitBuffer#putInt(int)}
457 * @throws CTFReaderException
461 public void testPutInt() throws CTFReaderException
{
467 * Test {@link BitBuffer#putInt(int, int)}
469 * @throws CTFReaderException
473 public void testPutInt_length1() throws CTFReaderException
{
475 fixture
.putInt(1, 1);
479 * Test {@link BitBuffer#putInt(int, int)} with length = 0.
481 * @throws CTFReaderException
485 public void testPutInt_length0() throws CTFReaderException
{
487 fixture
.putInt(0, 1);
491 * Test writing and reading a value defined in hex format.
493 * @throws CTFReaderException
497 public void testPutInt_hex() throws CTFReaderException
{
498 final int value
= 0x010203;
500 for (int i
= 0; i
<= 32; i
++) {
502 fixture
.putInt(value
);
505 int read
= fixture
.getInt();
507 assertEquals(value
, read
);
512 * Test {@link BitBuffer#putInt} with an out-of-bounds length. An exception
515 * @throws CTFReaderException
518 @Test(expected
= CTFReaderException
.class)
519 public void testPutInt_invalid() throws CTFReaderException
{
521 fixture2
= new BitBuffer(ByteBuffer
.allocateDirect(128));
522 fixture2
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
523 createBuffer(fixture2
, 4);
524 fixture2
.position(1);
526 /* This will try writing past the buffer's end */
527 fixture2
.putInt(32, 1);
This page took 0.043724 seconds and 5 git commands to generate.