ctf: Add test for 8-byte integer sign extension bug
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core.tests / src / org / eclipse / tracecompass / tmf / ctf / core / tests / event / CtfTmfEventFieldSignExtensionTest.java
1 /*******************************************************************************
2 * Copyright (c) 2016 EfficiOS Inc., Alexandre Montplaisir
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9
10 package org.eclipse.tracecompass.tmf.ctf.core.tests.event;
11
12 import static org.junit.Assert.assertArrayEquals;
13 import static org.junit.Assert.assertEquals;
14
15 import java.util.stream.Collectors;
16 import java.util.stream.LongStream;
17
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.tracecompass.internal.tmf.ctf.core.trace.iterator.CtfIterator;
20 import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
21 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
22 import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent;
23 import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTraceUtils;
24 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28
29 /**
30 * Tests making sure sign extension sign extension of field values works
31 * correctly.
32 *
33 * See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=491382
34 *
35 * @author Alexandre Montplaisir
36 */
37 public class CtfTmfEventFieldSignExtensionTest {
38
39 private static final @NonNull CtfTestTrace DEBUG_INFO_TRACE = CtfTestTrace.DEBUG_INFO3;
40
41 private CtfTmfTrace trace;
42
43 /**
44 * Test setup
45 */
46 @Before
47 public void setUp() {
48 trace = CtfTmfTestTraceUtils.getTrace(DEBUG_INFO_TRACE);
49 }
50
51 /**
52 * Test teardown
53 */
54 @After
55 public void teardown() {
56 if (trace != null) {
57 trace.dispose();
58 }
59 }
60
61 /**
62 * Test that signed 8-byte integers are printed correctly.
63 */
64 @Test
65 public void testUnsignedByte() {
66 /*
67 * Third event of the trace is printed like this by Babeltrace:
68 *
69 * [16:25:03.003427176] (+0.000001578) sonoshee lttng_ust_statedump:build_id:
70 * { cpu_id = 0 }, { ip = 0x7F3BBEDDDE1E, vpid = 3520 },
71 * { baddr = 0x400000, _build_id_length = 20, build_id = [ [0] = 0x1, [1] = 0xC6, [2] = 0x5, [3] = 0xBC, [4] = 0xF3, [5] = 0x8D, [6] = 0x6, [7] = 0x8D, [8] = 0x77, [9] = 0xA6, [10] = 0xE0, [11] = 0xA0, [12] = 0x2C, [13] = 0xED, [14] = 0xE6, [15] = 0xA5, [16] = 0xC, [17] = 0x57, [18] = 0x50, [19] = 0xB5 ] }
72 */
73 long[] expectedValues = new long[] {
74 0x1,
75 0xC6,
76 0x5,
77 0xBC,
78 0xF3,
79 0x8D,
80 0x6,
81 0x8D,
82 0x77,
83 0xA6,
84 0xE0,
85 0xA0,
86 0x2C,
87 0xED,
88 0xE6,
89 0xA5,
90 0xC,
91 0x57,
92 0x50,
93 0xB5
94 };
95
96 String expectedToString = LongStream.of(expectedValues)
97 .mapToObj(i -> "0x" + Long.toHexString(i))
98 .collect(Collectors.joining(", ", "build_id=[", "]"));
99
100 try (CtfIterator iter = (CtfIterator) trace.createIterator();) {
101 /* Go to third event */
102 iter.advance();
103 iter.advance();
104
105 /* Retrieve the event's field called "build_id" */
106 CtfTmfEvent event = iter.getCurrentEvent();
107 ITmfEventField field = event.getContent().getField("build_id");
108 long[] values = (long[]) field.getValue();
109
110 assertArrayEquals(expectedValues, values);
111 assertEquals(expectedToString, field.toString());
112 }
113 }
114 }
This page took 0.042925 seconds and 5 git commands to generate.