tmf: Decouple tmf.core unit tests from TmfEvent
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / statesystem / StateSystemPushPopTest.java
CommitLineData
a4e71249
AM
1/*******************************************************************************
2 * Copyright (c) 2012 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 ******************************************************************************/
12
13package org.eclipse.linuxtools.tmf.core.tests.statesystem;
14
15import java.io.File;
16import java.io.IOException;
17import java.util.List;
18
19import junit.framework.TestCase;
20
a4e71249 21import org.eclipse.linuxtools.internal.tmf.core.statesystem.StateSystem;
f9a76cac
AM
22import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.IStateHistoryBackend;
23import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.historytree.HistoryTreeBackend;
a4e71249 24import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException;
96345c5a 25import org.eclipse.linuxtools.tmf.core.exceptions.StateSystemDisposedException;
a4e71249
AM
26import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;
27import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
28import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval;
f1f86dfb 29import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystemBuilder;
a4e71249
AM
30import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue;
31import org.eclipse.linuxtools.tmf.core.statevalue.TmfStateValue;
32
33/**
34 * Unit tests for stack-attributes in the Generic State System (using
35 * pushAttribute() and popAttribute())
36 *
37 * @author Alexandre Montplaisir
38 */
96345c5a 39public class StateSystemPushPopTest extends TestCase {
a4e71249 40
f1f86dfb 41 private ITmfStateSystemBuilder ss;
a4e71249
AM
42
43 private ITmfStateInterval interval;
44 private int attribute;
45
46 private final File testHtFile;
47
48 private final static String errMsg = "Caught exception: "; //$NON-NLS-1$
49
50 /* State values that will be used */
51 //private final static ITmfStateValue nullValue = TmfStateValue.nullValue();
52 private final static ITmfStateValue value1 = TmfStateValue.newValueString("A"); //$NON-NLS-1$
53 private final static ITmfStateValue value2 = TmfStateValue.newValueInt(10);
54 private final static ITmfStateValue value3 = TmfStateValue.nullValue();
55 private final static ITmfStateValue value4 = TmfStateValue.newValueString("D"); //$NON-NLS-1$
56
57 /**
58 * Test case constructor
59 *
60 * @param name
61 * The test name
62 * @throws IOException
63 * If we couldn't create the state history test file
64 */
65 public StateSystemPushPopTest(final String name) throws IOException {
66 super(name);
67 testHtFile = File.createTempFile("test", ".ht"); //$NON-NLS-1$ //$NON-NLS-2$
68 testHtFile.deleteOnExit();
69 }
70
71 /**
72 * Initialization. We run the checks for the return values of
73 * .popAttribute() in here, since this is only available when we are
74 * building the state history.
75 *
76 * @throws IOException
77 * If we can write the file to the temporary directory.
78 * @throws TimeRangeException
79 * Fails the test
80 * @throws AttributeNotFoundException
81 * Fails the test
82 * @throws StateValueTypeException
83 * Fails the test
84 */
85 @Override
86 public void setUp() throws IOException, TimeRangeException,
87 AttributeNotFoundException, StateValueTypeException {
88 ITmfStateValue value;
89
90 IStateHistoryBackend backend = new HistoryTreeBackend(testHtFile, 0);
91 ss = new StateSystem(backend, true);
92
93 /* Build the thing */
94 final int attrib = ss.getQuarkAbsoluteAndAdd("Test", "stack"); //$NON-NLS-1$ //$NON-NLS-2$
95
96 ss.pushAttribute( 2, value1, attrib);
97 ss.pushAttribute( 4, value2, attrib);
98 ss.pushAttribute( 6, value3, attrib);
99 ss.pushAttribute(10, value4, attrib);
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"); //$NON-NLS-1$ //$NON-NLS-2$
132 }
133
134 /**
135 * Clean-up after running a test. Delete the .ht file we created.
136 */
137 @Override
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 public void testBeginEnd() {
147 try {
148 interval = ss.querySingleState(0, attribute);
149 assertEquals(0, interval.getStartTime());
150 assertEquals(1, interval.getEndTime());
151 assertTrue(interval.getStateValue().isNull());
152
153 interval = ss.querySingleState(29, attribute);
154 assertEquals(26, interval.getStartTime());
155 assertEquals(30, interval.getEndTime());
156 assertTrue(interval.getStateValue().isNull());
157
158 } catch (AttributeNotFoundException e) {
159 fail(errMsg + e.toString());
160 } catch (TimeRangeException e) {
161 fail(errMsg + e.toString());
96345c5a
AM
162 } catch (StateSystemDisposedException e) {
163 fail(errMsg + e.toString());
a4e71249
AM
164 }
165 }
166
167 /**
168 * Run single queries on the attribute stacks (with .querySingleState()).
169 */
170 public void testSingleQueries() {
171 try {
172 final int subAttribute1 = ss.getQuarkRelative(attribute, "1"); //$NON-NLS-1$
173 final int subAttribute2 = ss.getQuarkRelative(attribute, "2"); //$NON-NLS-1$
174
175 /* Test the stack attributes themselves */
176 interval = ss.querySingleState(11, attribute);
177 assertEquals(4, interval.getStateValue().unboxInt());
178
179 interval = ss.querySingleState(24, attribute);
180 assertEquals(1, interval.getStateValue().unboxInt());
181
182 /* Go retrieve the user values manually */
183 interval = ss.querySingleState(10, subAttribute1);
184 assertEquals(value1, interval.getStateValue()); //
185
186 interval = ss.querySingleState(22, subAttribute2);
187 assertEquals(value2, interval.getStateValue());
188
189 interval = ss.querySingleState(25, subAttribute2);
190 assertTrue(interval.getStateValue().isNull()); // Stack depth is 1 at that point.
191
192 } catch (AttributeNotFoundException e) {
193 fail(errMsg + e.toString());
194 } catch (StateValueTypeException e) {
195 fail(errMsg + e.toString());
196 } catch (TimeRangeException e) {
197 fail(errMsg + e.toString());
96345c5a
AM
198 } catch (StateSystemDisposedException e) {
199 fail(errMsg + e.toString());
a4e71249
AM
200 }
201 }
202
203 /**
204 * Test the .querySingletStackTop() convenience method.
205 */
206 public void testStackTop() {
207 try {
208 interval = ss.querySingleStackTop(10, attribute);
209 assertEquals(value4, interval.getStateValue());
210
211 interval = ss.querySingleStackTop(13, attribute);
212 assertEquals(value3, interval.getStateValue());
213
214 interval = ss.querySingleStackTop(16, attribute);
215 assertEquals(value1, interval.getStateValue());
216
217 interval = ss.querySingleStackTop(25, attribute);
218 assertEquals(value1, interval.getStateValue());
219
220 } catch (AttributeNotFoundException e) {
221 fail(errMsg + e.toString());
222 } catch (StateValueTypeException e) {
223 fail(errMsg + e.toString());
224 } catch (TimeRangeException e) {
225 fail(errMsg + e.toString());
96345c5a
AM
226 } catch (StateSystemDisposedException e) {
227 fail(errMsg + e.toString());
a4e71249
AM
228 }
229 }
230
231 /**
232 * Test the places where the stack is empty.
233 */
234 public void testEmptyStack() {
235 try {
236 /* At the start */
237 interval = ss.querySingleState(1, attribute);
238 assertTrue(interval.getStateValue().isNull());
239 interval = ss.querySingleStackTop(1, attribute);
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());
245 interval = ss.querySingleStackTop(19, attribute);
246 assertEquals(null, interval);
247
248 /* At the end */
249 interval = ss.querySingleState(27, attribute);
250 assertTrue(interval.getStateValue().isNull());
251 interval = ss.querySingleStackTop(27, attribute);
252 assertEquals(null, interval);
253
254 } catch (AttributeNotFoundException e) {
255 fail(errMsg + e.toString());
256 } catch (StateValueTypeException e) {
257 fail(errMsg + e.toString());
258 } catch (TimeRangeException e) {
259 fail(errMsg + e.toString());
96345c5a
AM
260 } catch (StateSystemDisposedException e) {
261 fail(errMsg + e.toString());
a4e71249
AM
262 }
263 }
264
265 /**
266 * Test full-queries (.queryFullState()) on the attribute stacks.
267 */
268 public void testFullQueries() {
269 List<ITmfStateInterval> state;
270 try {
271 final int subAttrib1 = ss.getQuarkRelative(attribute, "1"); //$NON-NLS-1$
272 final int subAttrib2 = ss.getQuarkRelative(attribute, "2"); //$NON-NLS-1$
273 final int subAttrib3 = ss.getQuarkRelative(attribute, "3"); //$NON-NLS-1$
274 final int subAttrib4 = ss.getQuarkRelative(attribute, "4"); //$NON-NLS-1$
275
276 /* Stack depth = 4 */
277 state = ss.queryFullState(10);
278 assertEquals(4, state.get(attribute).getStateValue().unboxInt());
279 assertEquals(value1, state.get(subAttrib1).getStateValue());
280 assertEquals(value2, state.get(subAttrib2).getStateValue());
281 assertEquals(value3, state.get(subAttrib3).getStateValue());
282 assertEquals(value4, state.get(subAttrib4).getStateValue());
283
284 /* Stack is empty */
285 state = ss.queryFullState(18);
286 assertTrue(state.get(attribute).getStateValue().isNull());
287 assertTrue(state.get(subAttrib1).getStateValue().isNull());
288 assertTrue(state.get(subAttrib2).getStateValue().isNull());
289 assertTrue(state.get(subAttrib3).getStateValue().isNull());
290 assertTrue(state.get(subAttrib4).getStateValue().isNull());
291
292 /* Stack depth = 1 */
293 state = ss.queryFullState(21);
294 assertEquals(1, state.get(attribute).getStateValue().unboxInt());
295 assertEquals(value1, state.get(subAttrib1).getStateValue());
296 assertTrue(state.get(subAttrib2).getStateValue().isNull());
297 assertTrue(state.get(subAttrib3).getStateValue().isNull());
298 assertTrue(state.get(subAttrib4).getStateValue().isNull());
299
300 } catch (AttributeNotFoundException e) {
301 fail(errMsg + e.toString());
302 } catch (StateValueTypeException e) {
303 fail(errMsg + e.toString());
304 } catch (TimeRangeException e) {
305 fail(errMsg + e.toString());
96345c5a
AM
306 } catch (StateSystemDisposedException e) {
307 fail(errMsg + e.toString());
a4e71249
AM
308 }
309 }
310}
This page took 0.037977 seconds and 5 git commands to generate.