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