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