1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Alexandre Montplaisir - Initial API and implementation
11 * Alexandre Montplaisir - Port to JUnit4
12 ******************************************************************************/
14 package org
.eclipse
.tracecompass
.statesystem
.core
.tests
;
16 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
17 import static org
.junit
.Assert
.assertEquals
;
18 import static org
.junit
.Assert
.assertNotNull
;
19 import static org
.junit
.Assert
.assertTrue
;
20 import static org
.junit
.Assert
.fail
;
23 import java
.io
.IOException
;
24 import java
.util
.List
;
26 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.StateSystem
;
27 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystemBuilder
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemUtils
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.IStateHistoryBackend
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.StateHistoryBackendFactory
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
33 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
34 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
35 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
36 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
37 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
38 import org
.junit
.After
;
39 import org
.junit
.Before
;
40 import org
.junit
.Test
;
43 * Unit tests for stack-attributes in the Generic State System (using
44 * pushAttribute() and popAttribute())
46 * @author Alexandre Montplaisir
48 public class StateSystemPushPopTest
{
50 private ITmfStateSystemBuilder ss
;
51 private int attribute
;
53 private File testHtFile
;
55 private final static String errMsg
= "Caught exception: ";
57 /* State values that will be used */
58 //private final static ITmfStateValue nullValue = TmfStateValue.nullValue();
59 private final static ITmfStateValue value1
= TmfStateValue
.newValueString("A");
60 private final static ITmfStateValue value2
= TmfStateValue
.newValueInt(10);
61 private final static ITmfStateValue value3
= TmfStateValue
.nullValue();
62 private final static ITmfStateValue value4
= TmfStateValue
.newValueString("D");
63 private final static ITmfStateValue value5
= TmfStateValue
.newValueLong(Long
.MAX_VALUE
);
66 * Initialization. We run the checks for the return values of
67 * .popAttribute() in here, since this is only available when we are
68 * building the state history.
71 * If we can write the file to the temporary directory.
72 * @throws TimeRangeException
74 * @throws AttributeNotFoundException
76 * @throws StateValueTypeException
80 public void setUp() throws IOException
, TimeRangeException
,
81 AttributeNotFoundException
, StateValueTypeException
{
83 testHtFile
= File
.createTempFile("test", ".ht");
85 IStateHistoryBackend backend
= StateHistoryBackendFactory
.createHistoryTreeBackendNewFile(
86 "push-pop-test", checkNotNull(testHtFile
), 0, 0, 0);
87 ss
= new StateSystem(backend
, true);
90 final int attrib
= ss
.getQuarkAbsoluteAndAdd("Test", "stack");
92 ss
.pushAttribute( 2, value1
, attrib
);
93 ss
.pushAttribute( 4, value2
, attrib
);
94 ss
.pushAttribute( 6, value3
, attrib
);
95 ss
.pushAttribute( 8, value4
, attrib
);
96 ss
.pushAttribute(10, value5
, attrib
);
98 value
= ss
.popAttribute(11, attrib
);
99 assertEquals(value5
, value
);
101 value
= ss
.popAttribute(12, attrib
);
102 assertEquals(value4
, value
);
104 value
= ss
.popAttribute(14, attrib
);
105 assertEquals(value3
, value
);
107 value
= ss
.popAttribute(16, attrib
);
108 assertEquals(value2
, value
);
110 value
= ss
.popAttribute(17, attrib
);
111 assertEquals(value1
, value
);
113 value
= ss
.popAttribute(20, attrib
);
114 assertEquals(null, value
); // Stack should already be empty here.
116 ss
.pushAttribute(21, value1
, attrib
);
117 //ss.pushAttribute(22, value1, attrib); //FIXME pushing twice the same value bugs out atm
118 ss
.pushAttribute(22, value2
, attrib
);
120 value
= ss
.popAttribute(24, attrib
);
121 //assertEquals(value1, value);
122 assertEquals(value2
, value
);
124 value
= ss
.popAttribute(26, attrib
);
125 assertEquals(value1
, value
);
127 value
= ss
.popAttribute(28, attrib
);
128 assertEquals(null, value
); // Stack should already be empty here.
131 attribute
= ss
.getQuarkAbsolute("Test", "stack");
135 * Clean-up after running a test. Delete the .ht file we created.
138 public void tearDown() {
143 * Test that the value of the stack-attribute at the start and end of the
144 * history are correct.
147 public void testBeginEnd() {
149 ITmfStateInterval interval
= ss
.querySingleState(0, attribute
);
150 assertEquals(0, interval
.getStartTime());
151 assertEquals(1, interval
.getEndTime());
152 assertTrue(interval
.getStateValue().isNull());
154 interval
= ss
.querySingleState(29, attribute
);
155 assertEquals(26, interval
.getStartTime());
156 assertEquals(30, interval
.getEndTime());
157 assertTrue(interval
.getStateValue().isNull());
159 } catch (AttributeNotFoundException
| TimeRangeException
| StateSystemDisposedException e
) {
160 fail(errMsg
+ e
.toString());
165 * Run single queries on the attribute stacks (with .querySingleState()).
168 public void testSingleQueries() {
170 final int subAttribute1
= ss
.getQuarkRelative(attribute
, "1");
171 final int subAttribute2
= ss
.getQuarkRelative(attribute
, "2");
173 /* Test the stack attributes themselves */
174 ITmfStateInterval interval
= ss
.querySingleState(11, attribute
);
175 assertEquals(4, interval
.getStateValue().unboxInt());
177 interval
= ss
.querySingleState(24, attribute
);
178 assertEquals(1, interval
.getStateValue().unboxInt());
180 /* Go retrieve the user values manually */
181 interval
= ss
.querySingleState(10, subAttribute1
);
182 assertEquals(value1
, interval
.getStateValue()); //
184 interval
= ss
.querySingleState(22, subAttribute2
);
185 assertEquals(value2
, interval
.getStateValue());
187 interval
= ss
.querySingleState(25, subAttribute2
);
188 assertTrue(interval
.getStateValue().isNull()); // Stack depth is 1 at that point.
190 } catch (AttributeNotFoundException
| TimeRangeException
| StateSystemDisposedException e
) {
191 fail(errMsg
+ e
.toString());
196 * Test the .querySingletStackTop() convenience method.
199 public void testStackTop() {
200 final ITmfStateSystemBuilder ss2
= ss
;
204 ITmfStateInterval interval
= StateSystemUtils
.querySingleStackTop(ss2
, 10, attribute
);
205 assertNotNull(interval
);
206 assertEquals(value5
, interval
.getStateValue());
208 interval
= StateSystemUtils
.querySingleStackTop(ss2
, 9, attribute
);
209 assertNotNull(interval
);
210 assertEquals(value4
, interval
.getStateValue());
212 interval
= StateSystemUtils
.querySingleStackTop(ss2
, 13, attribute
);
213 assertNotNull(interval
);
214 assertEquals(value3
, interval
.getStateValue());
216 interval
= StateSystemUtils
.querySingleStackTop(ss2
, 16, attribute
);
217 assertNotNull(interval
);
218 assertEquals(value1
, interval
.getStateValue());
220 interval
= StateSystemUtils
.querySingleStackTop(ss2
, 25, attribute
);
221 assertNotNull(interval
);
222 assertEquals(value1
, interval
.getStateValue());
224 } catch (AttributeNotFoundException
| TimeRangeException
| StateSystemDisposedException e
) {
225 fail(errMsg
+ e
.toString());
230 * Test the places where the stack is empty.
233 public void testEmptyStack() {
234 final ITmfStateSystemBuilder ss2
= ss
;
239 ITmfStateInterval interval
= ss
.querySingleState(1, attribute
);
240 assertTrue(interval
.getStateValue().isNull());
241 interval
= StateSystemUtils
.querySingleStackTop(ss2
, 1, attribute
);
242 assertEquals(null, interval
);
244 /* Between the two "stacks" in the state history */
245 interval
= ss
.querySingleState(19, attribute
);
246 assertTrue(interval
.getStateValue().isNull());
247 interval
= StateSystemUtils
.querySingleStackTop(ss2
, 19, attribute
);
248 assertEquals(null, interval
);
251 interval
= ss
.querySingleState(27, attribute
);
252 assertTrue(interval
.getStateValue().isNull());
253 interval
= StateSystemUtils
.querySingleStackTop(ss2
, 27, attribute
);
254 assertEquals(null, interval
);
256 } catch (AttributeNotFoundException
| TimeRangeException
| StateSystemDisposedException e
) {
257 fail(errMsg
+ e
.toString());
262 * Test full-queries (.queryFullState()) on the attribute stacks.
265 public void testFullQueries() {
266 List
<ITmfStateInterval
> state
;
268 final int subAttrib1
= ss
.getQuarkRelative(attribute
, "1");
269 final int subAttrib2
= ss
.getQuarkRelative(attribute
, "2");
270 final int subAttrib3
= ss
.getQuarkRelative(attribute
, "3");
271 final int subAttrib4
= ss
.getQuarkRelative(attribute
, "4");
273 /* Stack depth = 5 */
274 state
= ss
.queryFullState(10);
275 assertEquals(5, state
.get(attribute
).getStateValue().unboxInt());
276 assertEquals(value1
, state
.get(subAttrib1
).getStateValue());
277 assertEquals(value2
, state
.get(subAttrib2
).getStateValue());
278 assertEquals(value3
, state
.get(subAttrib3
).getStateValue());
279 assertEquals(value4
, state
.get(subAttrib4
).getStateValue());
282 state
= ss
.queryFullState(18);
283 assertTrue(state
.get(attribute
).getStateValue().isNull());
284 assertTrue(state
.get(subAttrib1
).getStateValue().isNull());
285 assertTrue(state
.get(subAttrib2
).getStateValue().isNull());
286 assertTrue(state
.get(subAttrib3
).getStateValue().isNull());
287 assertTrue(state
.get(subAttrib4
).getStateValue().isNull());
289 /* Stack depth = 1 */
290 state
= ss
.queryFullState(21);
291 assertEquals(1, state
.get(attribute
).getStateValue().unboxInt());
292 assertEquals(value1
, state
.get(subAttrib1
).getStateValue());
293 assertTrue(state
.get(subAttrib2
).getStateValue().isNull());
294 assertTrue(state
.get(subAttrib3
).getStateValue().isNull());
295 assertTrue(state
.get(subAttrib4
).getStateValue().isNull());
297 } catch (AttributeNotFoundException
| TimeRangeException
| StateSystemDisposedException e
) {
298 fail(errMsg
+ e
.toString());