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