ss: Replace AttributeNotFoundException with IOOBE for quark parameters
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core.tests / src / org / eclipse / tracecompass / statesystem / core / tests / StateSystemPushPopTest.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2016 Ericsson
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
11 * Alexandre Montplaisir - Port to JUnit4
12 ******************************************************************************/
13
14 package org.eclipse.tracecompass.statesystem.core.tests;
15
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;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.List;
25
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;
41
42 /**
43 * Unit tests for stack-attributes in the Generic State System (using
44 * pushAttribute() and popAttribute())
45 *
46 * @author Alexandre Montplaisir
47 */
48 public class StateSystemPushPopTest {
49
50 private ITmfStateSystemBuilder ss;
51 private int attribute;
52
53 private File testHtFile;
54
55 private final static String errMsg = "Caught exception: ";
56
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);
64
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 */
79 @Before
80 public void setUp() throws IOException, TimeRangeException,
81 AttributeNotFoundException, StateValueTypeException {
82 ITmfStateValue value;
83 testHtFile = File.createTempFile("test", ".ht");
84
85 IStateHistoryBackend backend = StateHistoryBackendFactory.createHistoryTreeBackendNewFile(
86 "push-pop-test", checkNotNull(testHtFile), 0, 0, 0);
87 ss = new StateSystem(backend, true);
88
89 /* Build the thing */
90 final int attrib = ss.getQuarkAbsoluteAndAdd("Test", "stack");
91
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);
97
98 value = ss.popAttribute(11, attrib);
99 assertEquals(value5, value);
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);
131 attribute = ss.getQuarkAbsolute("Test", "stack");
132 }
133
134 /**
135 * Clean-up after running a test. Delete the .ht file we created.
136 */
137 @After
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 */
146 @Test
147 public void testBeginEnd() {
148 try {
149 ITmfStateInterval interval = ss.querySingleState(0, attribute);
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
159 } catch (TimeRangeException | StateSystemDisposedException e) {
160 fail(errMsg + e.toString());
161 }
162 }
163
164 /**
165 * Run single queries on the attribute stacks (with .querySingleState()).
166 */
167 @Test
168 public void testSingleQueries() {
169 try {
170 final int subAttribute1 = ss.getQuarkRelative(attribute, "1");
171 final int subAttribute2 = ss.getQuarkRelative(attribute, "2");
172
173 /* Test the stack attributes themselves */
174 ITmfStateInterval interval = ss.querySingleState(11, attribute);
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
190 } catch (AttributeNotFoundException | TimeRangeException | StateSystemDisposedException e) {
191 fail(errMsg + e.toString());
192 }
193 }
194
195 /**
196 * Test the .querySingletStackTop() convenience method.
197 */
198 @Test
199 public void testStackTop() {
200 final ITmfStateSystemBuilder ss2 = ss;
201 assertNotNull(ss2);
202
203 try {
204 ITmfStateInterval interval = StateSystemUtils.querySingleStackTop(ss2, 10, attribute);
205 assertNotNull(interval);
206 assertEquals(value5, interval.getStateValue());
207
208 interval = StateSystemUtils.querySingleStackTop(ss2, 9, attribute);
209 assertNotNull(interval);
210 assertEquals(value4, interval.getStateValue());
211
212 interval = StateSystemUtils.querySingleStackTop(ss2, 13, attribute);
213 assertNotNull(interval);
214 assertEquals(value3, interval.getStateValue());
215
216 interval = StateSystemUtils.querySingleStackTop(ss2, 16, attribute);
217 assertNotNull(interval);
218 assertEquals(value1, interval.getStateValue());
219
220 interval = StateSystemUtils.querySingleStackTop(ss2, 25, attribute);
221 assertNotNull(interval);
222 assertEquals(value1, interval.getStateValue());
223
224 } catch (AttributeNotFoundException | TimeRangeException | StateSystemDisposedException e) {
225 fail(errMsg + e.toString());
226 }
227 }
228
229 /**
230 * Test the places where the stack is empty.
231 */
232 @Test
233 public void testEmptyStack() {
234 final ITmfStateSystemBuilder ss2 = ss;
235 assertNotNull(ss2);
236
237 try {
238 /* At the start */
239 ITmfStateInterval interval = ss.querySingleState(1, attribute);
240 assertTrue(interval.getStateValue().isNull());
241 interval = StateSystemUtils.querySingleStackTop(ss2, 1, attribute);
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());
247 interval = StateSystemUtils.querySingleStackTop(ss2, 19, attribute);
248 assertEquals(null, interval);
249
250 /* At the end */
251 interval = ss.querySingleState(27, attribute);
252 assertTrue(interval.getStateValue().isNull());
253 interval = StateSystemUtils.querySingleStackTop(ss2, 27, attribute);
254 assertEquals(null, interval);
255
256 } catch (AttributeNotFoundException | TimeRangeException | StateSystemDisposedException e) {
257 fail(errMsg + e.toString());
258 }
259 }
260
261 /**
262 * Test full-queries (.queryFullState()) on the attribute stacks.
263 */
264 @Test
265 public void testFullQueries() {
266 List<ITmfStateInterval> state;
267 try {
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");
272
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());
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
297 } catch (AttributeNotFoundException | TimeRangeException | StateSystemDisposedException e) {
298 fail(errMsg + e.toString());
299 }
300 }
301 }
This page took 0.037956 seconds and 5 git commands to generate.