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