Commit | Line | Data |
---|---|---|
a4e71249 | 1 | /******************************************************************************* |
b2f62cb5 | 2 | * Copyright (c) 2012, 2015 Ericsson |
a4e71249 AM |
3 | * |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Alexandre Montplaisir - Initial API and implementation | |
6e1886bc | 11 | * Alexandre Montplaisir - Port to JUnit4 |
a4e71249 AM |
12 | ******************************************************************************/ |
13 | ||
e894a508 | 14 | package org.eclipse.tracecompass.statesystem.core.tests; |
a4e71249 | 15 | |
6e1886bc | 16 | import static org.junit.Assert.assertEquals; |
1dd75589 | 17 | import static org.junit.Assert.assertNotNull; |
6e1886bc AM |
18 | import static org.junit.Assert.assertTrue; |
19 | import static org.junit.Assert.fail; | |
20 | ||
a4e71249 AM |
21 | import java.io.File; |
22 | import java.io.IOException; | |
23 | import java.util.List; | |
24 | ||
e894a508 AM |
25 | import org.eclipse.tracecompass.internal.statesystem.core.StateSystem; |
26 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder; | |
1dd75589 | 27 | import org.eclipse.tracecompass.statesystem.core.StateSystemUtils; |
e894a508 AM |
28 | import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend; |
29 | import org.eclipse.tracecompass.statesystem.core.backend.historytree.HistoryTreeBackend; | |
30 | import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException; | |
31 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException; | |
32 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; | |
33 | import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException; | |
34 | import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval; | |
35 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue; | |
36 | import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue; | |
6e1886bc AM |
37 | import org.junit.After; |
38 | import org.junit.Before; | |
39 | import org.junit.Test; | |
a4e71249 AM |
40 | |
41 | /** | |
42 | * Unit tests for stack-attributes in the Generic State System (using | |
43 | * pushAttribute() and popAttribute()) | |
44 | * | |
45 | * @author Alexandre Montplaisir | |
46 | */ | |
6e1886bc | 47 | public class StateSystemPushPopTest { |
a4e71249 | 48 | |
f1f86dfb | 49 | private ITmfStateSystemBuilder ss; |
a4e71249 AM |
50 | private int attribute; |
51 | ||
947504fa | 52 | private File testHtFile; |
a4e71249 | 53 | |
cad06250 | 54 | private final static String errMsg = "Caught exception: "; |
a4e71249 AM |
55 | |
56 | /* State values that will be used */ | |
57 | //private final static ITmfStateValue nullValue = TmfStateValue.nullValue(); | |
cad06250 | 58 | private final static ITmfStateValue value1 = TmfStateValue.newValueString("A"); |
a4e71249 AM |
59 | private final static ITmfStateValue value2 = TmfStateValue.newValueInt(10); |
60 | private final static ITmfStateValue value3 = TmfStateValue.nullValue(); | |
cad06250 | 61 | private final static ITmfStateValue value4 = TmfStateValue.newValueString("D"); |
1cbf1a19 | 62 | private final static ITmfStateValue value5 = TmfStateValue.newValueLong(Long.MAX_VALUE); |
a4e71249 | 63 | |
a4e71249 AM |
64 | /** |
65 | * Initialization. We run the checks for the return values of | |
66 | * .popAttribute() in here, since this is only available when we are | |
67 | * building the state history. | |
68 | * | |
69 | * @throws IOException | |
70 | * If we can write the file to the temporary directory. | |
71 | * @throws TimeRangeException | |
72 | * Fails the test | |
73 | * @throws AttributeNotFoundException | |
74 | * Fails the test | |
75 | * @throws StateValueTypeException | |
76 | * Fails the test | |
77 | */ | |
6e1886bc | 78 | @Before |
a4e71249 AM |
79 | public void setUp() throws IOException, TimeRangeException, |
80 | AttributeNotFoundException, StateValueTypeException { | |
81 | ITmfStateValue value; | |
947504fa | 82 | testHtFile = File.createTempFile("test", ".ht"); |
a4e71249 | 83 | |
b2f62cb5 AM |
84 | IStateHistoryBackend backend = new HistoryTreeBackend("push-pop-test", testHtFile, 0, 0L); |
85 | ss = new StateSystem(backend, true); | |
a4e71249 AM |
86 | |
87 | /* Build the thing */ | |
cad06250 | 88 | final int attrib = ss.getQuarkAbsoluteAndAdd("Test", "stack"); |
a4e71249 AM |
89 | |
90 | ss.pushAttribute( 2, value1, attrib); | |
91 | ss.pushAttribute( 4, value2, attrib); | |
92 | ss.pushAttribute( 6, value3, attrib); | |
1cbf1a19 FR |
93 | ss.pushAttribute( 8, value4, attrib); |
94 | ss.pushAttribute(10, value5, attrib); | |
95 | ||
96 | value = ss.popAttribute(11, attrib); | |
97 | assertEquals(value5, value); | |
a4e71249 AM |
98 | |
99 | value = ss.popAttribute(12, attrib); | |
100 | assertEquals(value4, value); | |
101 | ||
102 | value = ss.popAttribute(14, attrib); | |
103 | assertEquals(value3, value); | |
104 | ||
105 | value = ss.popAttribute(16, attrib); | |
106 | assertEquals(value2, value); | |
107 | ||
108 | value = ss.popAttribute(17, attrib); | |
109 | assertEquals(value1, value); | |
110 | ||
111 | value = ss.popAttribute(20, attrib); | |
112 | assertEquals(null, value); // Stack should already be empty here. | |
113 | ||
114 | ss.pushAttribute(21, value1, attrib); | |
115 | //ss.pushAttribute(22, value1, attrib); //FIXME pushing twice the same value bugs out atm | |
116 | ss.pushAttribute(22, value2, attrib); | |
117 | ||
118 | value = ss.popAttribute(24, attrib); | |
119 | //assertEquals(value1, value); | |
120 | assertEquals(value2, value); | |
121 | ||
122 | value = ss.popAttribute(26, attrib); | |
123 | assertEquals(value1, value); | |
124 | ||
125 | value = ss.popAttribute(28, attrib); | |
126 | assertEquals(null, value); // Stack should already be empty here. | |
127 | ||
128 | ss.closeHistory(30); | |
cad06250 | 129 | attribute = ss.getQuarkAbsolute("Test", "stack"); |
a4e71249 AM |
130 | } |
131 | ||
132 | /** | |
133 | * Clean-up after running a test. Delete the .ht file we created. | |
134 | */ | |
6e1886bc | 135 | @After |
a4e71249 AM |
136 | public void tearDown() { |
137 | testHtFile.delete(); | |
138 | } | |
139 | ||
140 | /** | |
141 | * Test that the value of the stack-attribute at the start and end of the | |
142 | * history are correct. | |
143 | */ | |
6e1886bc | 144 | @Test |
a4e71249 AM |
145 | public void testBeginEnd() { |
146 | try { | |
1dd75589 | 147 | ITmfStateInterval interval = ss.querySingleState(0, attribute); |
a4e71249 AM |
148 | assertEquals(0, interval.getStartTime()); |
149 | assertEquals(1, interval.getEndTime()); | |
150 | assertTrue(interval.getStateValue().isNull()); | |
151 | ||
152 | interval = ss.querySingleState(29, attribute); | |
153 | assertEquals(26, interval.getStartTime()); | |
154 | assertEquals(30, interval.getEndTime()); | |
155 | assertTrue(interval.getStateValue().isNull()); | |
156 | ||
bcec0116 | 157 | } catch (AttributeNotFoundException | TimeRangeException | StateSystemDisposedException e) { |
96345c5a | 158 | fail(errMsg + e.toString()); |
a4e71249 AM |
159 | } |
160 | } | |
161 | ||
162 | /** | |
163 | * Run single queries on the attribute stacks (with .querySingleState()). | |
164 | */ | |
6e1886bc | 165 | @Test |
a4e71249 AM |
166 | public void testSingleQueries() { |
167 | try { | |
cad06250 AM |
168 | final int subAttribute1 = ss.getQuarkRelative(attribute, "1"); |
169 | final int subAttribute2 = ss.getQuarkRelative(attribute, "2"); | |
a4e71249 AM |
170 | |
171 | /* Test the stack attributes themselves */ | |
1dd75589 | 172 | ITmfStateInterval interval = ss.querySingleState(11, attribute); |
a4e71249 AM |
173 | assertEquals(4, interval.getStateValue().unboxInt()); |
174 | ||
175 | interval = ss.querySingleState(24, attribute); | |
176 | assertEquals(1, interval.getStateValue().unboxInt()); | |
177 | ||
178 | /* Go retrieve the user values manually */ | |
179 | interval = ss.querySingleState(10, subAttribute1); | |
180 | assertEquals(value1, interval.getStateValue()); // | |
181 | ||
182 | interval = ss.querySingleState(22, subAttribute2); | |
183 | assertEquals(value2, interval.getStateValue()); | |
184 | ||
185 | interval = ss.querySingleState(25, subAttribute2); | |
186 | assertTrue(interval.getStateValue().isNull()); // Stack depth is 1 at that point. | |
187 | ||
1dd75589 | 188 | } catch (AttributeNotFoundException | TimeRangeException | StateSystemDisposedException e) { |
96345c5a | 189 | fail(errMsg + e.toString()); |
a4e71249 AM |
190 | } |
191 | } | |
192 | ||
193 | /** | |
194 | * Test the .querySingletStackTop() convenience method. | |
195 | */ | |
6e1886bc | 196 | @Test |
a4e71249 | 197 | public void testStackTop() { |
1dd75589 AM |
198 | final ITmfStateSystemBuilder ss2 = ss; |
199 | assertNotNull(ss2); | |
200 | ||
a4e71249 | 201 | try { |
1dd75589 AM |
202 | ITmfStateInterval interval = StateSystemUtils.querySingleStackTop(ss2, 10, attribute); |
203 | assertNotNull(interval); | |
1cbf1a19 FR |
204 | assertEquals(value5, interval.getStateValue()); |
205 | ||
1dd75589 AM |
206 | interval = StateSystemUtils.querySingleStackTop(ss2, 9, attribute); |
207 | assertNotNull(interval); | |
a4e71249 AM |
208 | assertEquals(value4, interval.getStateValue()); |
209 | ||
1dd75589 AM |
210 | interval = StateSystemUtils.querySingleStackTop(ss2, 13, attribute); |
211 | assertNotNull(interval); | |
a4e71249 AM |
212 | assertEquals(value3, interval.getStateValue()); |
213 | ||
1dd75589 AM |
214 | interval = StateSystemUtils.querySingleStackTop(ss2, 16, attribute); |
215 | assertNotNull(interval); | |
a4e71249 AM |
216 | assertEquals(value1, interval.getStateValue()); |
217 | ||
1dd75589 AM |
218 | interval = StateSystemUtils.querySingleStackTop(ss2, 25, attribute); |
219 | assertNotNull(interval); | |
a4e71249 AM |
220 | assertEquals(value1, interval.getStateValue()); |
221 | ||
1dd75589 | 222 | } catch (AttributeNotFoundException | TimeRangeException | StateSystemDisposedException e) { |
96345c5a | 223 | fail(errMsg + e.toString()); |
a4e71249 AM |
224 | } |
225 | } | |
226 | ||
227 | /** | |
228 | * Test the places where the stack is empty. | |
229 | */ | |
6e1886bc | 230 | @Test |
a4e71249 | 231 | public void testEmptyStack() { |
1dd75589 AM |
232 | final ITmfStateSystemBuilder ss2 = ss; |
233 | assertNotNull(ss2); | |
234 | ||
a4e71249 AM |
235 | try { |
236 | /* At the start */ | |
1dd75589 | 237 | ITmfStateInterval interval = ss.querySingleState(1, attribute); |
a4e71249 | 238 | assertTrue(interval.getStateValue().isNull()); |
1dd75589 | 239 | interval = StateSystemUtils.querySingleStackTop(ss2, 1, attribute); |
a4e71249 AM |
240 | assertEquals(null, interval); |
241 | ||
242 | /* Between the two "stacks" in the state history */ | |
243 | interval = ss.querySingleState(19, attribute); | |
244 | assertTrue(interval.getStateValue().isNull()); | |
1dd75589 | 245 | interval = StateSystemUtils.querySingleStackTop(ss2, 19, attribute); |
a4e71249 AM |
246 | assertEquals(null, interval); |
247 | ||
248 | /* At the end */ | |
249 | interval = ss.querySingleState(27, attribute); | |
250 | assertTrue(interval.getStateValue().isNull()); | |
1dd75589 | 251 | interval = StateSystemUtils.querySingleStackTop(ss2, 27, attribute); |
a4e71249 AM |
252 | assertEquals(null, interval); |
253 | ||
1dd75589 | 254 | } catch (AttributeNotFoundException | TimeRangeException | StateSystemDisposedException e) { |
96345c5a | 255 | fail(errMsg + e.toString()); |
a4e71249 AM |
256 | } |
257 | } | |
258 | ||
259 | /** | |
260 | * Test full-queries (.queryFullState()) on the attribute stacks. | |
261 | */ | |
6e1886bc | 262 | @Test |
a4e71249 AM |
263 | public void testFullQueries() { |
264 | List<ITmfStateInterval> state; | |
265 | try { | |
cad06250 AM |
266 | final int subAttrib1 = ss.getQuarkRelative(attribute, "1"); |
267 | final int subAttrib2 = ss.getQuarkRelative(attribute, "2"); | |
268 | final int subAttrib3 = ss.getQuarkRelative(attribute, "3"); | |
269 | final int subAttrib4 = ss.getQuarkRelative(attribute, "4"); | |
a4e71249 | 270 | |
1cbf1a19 | 271 | /* Stack depth = 5 */ |
a4e71249 | 272 | state = ss.queryFullState(10); |
1cbf1a19 | 273 | assertEquals(5, state.get(attribute).getStateValue().unboxInt()); |
a4e71249 AM |
274 | assertEquals(value1, state.get(subAttrib1).getStateValue()); |
275 | assertEquals(value2, state.get(subAttrib2).getStateValue()); | |
276 | assertEquals(value3, state.get(subAttrib3).getStateValue()); | |
277 | assertEquals(value4, state.get(subAttrib4).getStateValue()); | |
278 | ||
279 | /* Stack is empty */ | |
280 | state = ss.queryFullState(18); | |
281 | assertTrue(state.get(attribute).getStateValue().isNull()); | |
282 | assertTrue(state.get(subAttrib1).getStateValue().isNull()); | |
283 | assertTrue(state.get(subAttrib2).getStateValue().isNull()); | |
284 | assertTrue(state.get(subAttrib3).getStateValue().isNull()); | |
285 | assertTrue(state.get(subAttrib4).getStateValue().isNull()); | |
286 | ||
287 | /* Stack depth = 1 */ | |
288 | state = ss.queryFullState(21); | |
289 | assertEquals(1, state.get(attribute).getStateValue().unboxInt()); | |
290 | assertEquals(value1, state.get(subAttrib1).getStateValue()); | |
291 | assertTrue(state.get(subAttrib2).getStateValue().isNull()); | |
292 | assertTrue(state.get(subAttrib3).getStateValue().isNull()); | |
293 | assertTrue(state.get(subAttrib4).getStateValue().isNull()); | |
294 | ||
1dd75589 | 295 | } catch (AttributeNotFoundException | TimeRangeException | StateSystemDisposedException e) { |
96345c5a | 296 | fail(errMsg + e.toString()); |
a4e71249 AM |
297 | } |
298 | } | |
299 | } |