1 package org
.eclipse
.linuxtools
.ctf
.core
.tests
.io
;
3 import static org
.junit
.Assert
.assertEquals
;
5 import java
.nio
.ByteBuffer
;
6 import java
.nio
.ByteOrder
;
8 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.io
.BitBuffer
;
9 import org
.junit
.After
;
10 import org
.junit
.Before
;
11 import org
.junit
.Test
;
14 * Part of the BitBuffet tests with test the methods to read/write integers.
15 * These are separated from the main file because the fixture is different.
20 public class BitBufferIntTest
{
22 private BitBuffer fixture
;
28 * the command line arguments
30 public static void main(String
[] args
) {
31 new org
.junit
.runner
.JUnitCore().run(BitBufferTest
.class);
35 * Perform pre-test initialization.
39 fixture
= new BitBuffer(java
.nio
.ByteBuffer
.allocateDirect(128));
40 fixture
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
41 createBuffer(fixture
);
45 * Perform post-test clean-up.
48 public void tearDown() {
49 // Add additional tear down code here
52 private static void createBuffer(BitBuffer fixture
) {
53 createBuffer(fixture
, 16);
56 private static void createBuffer(BitBuffer fixture
, int j
) {
57 byte[] bytes
= new byte[j
];
58 for (int i
= 0; i
< j
; i
++) {
59 bytes
[i
] = (byte) (i
% 0xff);
61 fixture
.setByteBuffer(ByteBuffer
.wrap(bytes
));
66 * Run the int getInt() method test.
69 public void testGetInt_base() {
70 int result
= fixture
.getInt();
71 assertEquals(0x020406, result
);
75 * Run the int getInt(int) method test.
78 public void testGetInt_pos0() {
80 int result
= fixture
.getInt();
81 assertEquals(0x010203, result
);
85 * Run the int getInt(int,boolean) method test.
88 public void testGetInt_pos1() {
91 boolean signed
= true;
93 int result
= fixture
.getInt(length
, signed
);
94 assertEquals(0, result
);
98 * Run the int getInt(int,boolean) method test.
101 public void testGetInt_pos2() {
104 boolean signed
= true;
106 int result
= fixture
.getInt(length
, signed
);
107 assertEquals(0, result
);
111 * Run the int getInt(int,int,boolean) method test.
114 public void testGetInt_signed() {
117 boolean signed
= true;
119 int result
= fixture
.getInt(length
, signed
);
120 assertEquals(0, result
);
124 * Run the int getInt(int,int,boolean) method test.
127 public void testGetInt_signed_length1() {
130 boolean signed
= true;
132 int result
= fixture
.getInt(length
, signed
);
133 assertEquals(0, result
);
137 * Run the int getInt(int,int,boolean) method test with a little-endian
141 public void testGetInt_le1() {
142 BitBuffer le_fixture
= new BitBuffer(
143 java
.nio
.ByteBuffer
.allocateDirect(128));
144 le_fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
145 createBuffer(le_fixture
);
146 le_fixture
.position(1);
148 int result
= le_fixture
.getInt(length
, false);
150 /* 0x020100 downshifted */
151 assertEquals(0x810080, result
);
155 * Run the int getInt(int,int,boolean) method test with a little-endian
159 public void testGetInt_le2() {
160 BitBuffer le_fixture
= new BitBuffer(
161 java
.nio
.ByteBuffer
.allocateDirect(128));
162 le_fixture
.setByteOrder(ByteOrder
.LITTLE_ENDIAN
);
163 createBuffer(le_fixture
);
164 le_fixture
.position(0);
166 int result
= le_fixture
.getInt(length
, false);
167 assertEquals(0x020100, result
);
171 * Run the int getInt(int,boolean) method test and expect an overflow.
173 @Test(expected
= java
.nio
.BufferOverflowException
.class)
174 public void testGetInt_invalid() {
175 BitBuffer small_fixture
= new BitBuffer(
176 java
.nio
.ByteBuffer
.allocateDirect(128));
177 small_fixture
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
178 createBuffer(small_fixture
, 2);
179 small_fixture
.position(10);
181 boolean signed
= true;
183 int result
= small_fixture
.getInt(length
, signed
);
184 assertEquals(0, result
);
188 * Run the int getInt(int,int,boolean) method test and expect an overflow.
190 @Test(expected
= java
.nio
.BufferOverflowException
.class)
191 public void testGetInt_invalid2() {
192 BitBuffer small_fixture
= new BitBuffer(
193 java
.nio
.ByteBuffer
.allocateDirect(128));
194 small_fixture
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
195 createBuffer(small_fixture
, 2);
196 small_fixture
.position(1);
198 boolean signed
= true;
200 int result
= small_fixture
.getInt(length
, signed
);
201 assertEquals(0, result
);
205 * Run the void putInt(int) method test.
208 public void testPutInt() {
211 fixture
.putInt(value
);
215 * Run the void putInt(int,int,boolean) method test.
218 public void testPutInt_signed() {
223 fixture
.putInt(length
, value
);
227 * Run the void putInt(int,int,int,boolean) method test.
230 public void testPutInt_length0() {
235 fixture
.putInt(length
, value
);
239 * Run the void putInt(int,int,int,boolean) method test.
242 public void testPutInt_length1() {
247 fixture
.putInt(length
, value
);
251 * Run the void putInt(int) method test.
254 public void testPutInt_hex() {
255 final int value
= 0x010203;
258 for (int i
= 0; i
<= 32; i
++) {
260 fixture
.putInt(value
);
263 read
= fixture
.getInt();
265 assertEquals(value
, read
);
270 * Run the void putInt(int,int,int,boolean) method test.
272 @Test(expected
= java
.nio
.BufferOverflowException
.class)
273 public void testPutInt_invalid() {
275 fixture2
= new BitBuffer(java
.nio
.ByteBuffer
.allocateDirect(128));
276 fixture2
.setByteOrder(ByteOrder
.BIG_ENDIAN
);
277 createBuffer(fixture2
, 4);
278 fixture2
.position(1);
283 fixture2
.putInt(length
, value
);
285 int read
= fixture2
.getInt(1, true);
286 assertEquals(value
, read
);
This page took 0.038095 seconds and 6 git commands to generate.