Commit | Line | Data |
---|---|---|
f606c6fa | 1 | /******************************************************************************* |
b2f62cb5 | 2 | * Copyright (c) 2013, 2015 Ericsson |
f606c6fa MK |
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 | * Matthew Khouzam - Initial API and implementation | |
11 | ******************************************************************************/ | |
12 | ||
e894a508 | 13 | package org.eclipse.tracecompass.statesystem.core.tests.backend; |
f606c6fa MK |
14 | |
15 | import static org.junit.Assert.*; | |
16 | ||
17 | import java.util.ArrayList; | |
18 | import java.util.List; | |
19 | ||
0306a843 AM |
20 | import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend; |
21 | import org.eclipse.tracecompass.statesystem.core.backend.StateHistoryBackendFactory; | |
e894a508 | 22 | import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException; |
0306a843 | 23 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException; |
e894a508 AM |
24 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; |
25 | import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException; | |
26 | import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval; | |
27 | import org.eclipse.tracecompass.statesystem.core.interval.TmfStateInterval; | |
28 | import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue; | |
f606c6fa MK |
29 | import org.junit.BeforeClass; |
30 | import org.junit.Test; | |
31 | ||
32 | /** | |
33 | * Test cases for the in-memory backend | |
34 | * | |
35 | * @author Matthew Khouzam | |
36 | */ | |
37 | public class InMemoryBackendTest { | |
38 | ||
39 | private static final int NUMBER_OF_ATTRIBUTES = 10; | |
0306a843 | 40 | private static IStateHistoryBackend fixture; |
f606c6fa MK |
41 | |
42 | /** | |
43 | * Test setup. make a state system that is moderately large | |
44 | */ | |
45 | @BeforeClass | |
46 | public static void init() { | |
0306a843 | 47 | fixture = StateHistoryBackendFactory.createInMemoryBackend("test-ss", 0); |
f606c6fa MK |
48 | for (int attribute = 0; attribute < NUMBER_OF_ATTRIBUTES; attribute++) { |
49 | for (int timeStart = 0; timeStart < 1000; timeStart++) { | |
50 | try { | |
51 | final int stateEndTime = (timeStart * 100) + 90 + attribute; | |
52 | final int stateStartTime = timeStart * 100 + attribute; | |
53 | fixture.insertPastState(stateStartTime, stateEndTime, attribute, TmfStateValue.newValueInt(timeStart % 100)); | |
54 | if (timeStart != 999) { | |
55 | fixture.insertPastState(stateEndTime + 1, stateEndTime + 9, attribute, TmfStateValue.nullValue()); | |
56 | } | |
57 | } catch (TimeRangeException e) { | |
58 | /* Should not happen here */ | |
59 | throw new IllegalStateException(); | |
60 | } | |
61 | } | |
62 | } | |
63 | } | |
64 | ||
65 | private static void testInterval(ITmfStateInterval interval, int startTime, | |
66 | int endTime, int value) { | |
67 | assertNotNull(interval); | |
68 | assertEquals(startTime, interval.getStartTime()); | |
69 | assertEquals(endTime, interval.getEndTime()); | |
70 | try { | |
71 | assertEquals(value, interval.getStateValue().unboxInt()); | |
72 | } catch (StateValueTypeException e) { | |
73 | fail(e.getMessage()); | |
74 | } | |
75 | } | |
76 | ||
77 | ||
78 | /** | |
79 | * Test at start time | |
80 | */ | |
81 | @Test | |
82 | public void testStartTime() { | |
83 | assertEquals(0, fixture.getStartTime()); | |
84 | } | |
85 | ||
86 | /** | |
87 | * Test at end time | |
88 | */ | |
89 | @Test | |
90 | public void testEndTime() { | |
91 | assertEquals(99999, fixture.getEndTime()); | |
92 | } | |
93 | ||
94 | /** | |
95 | * Query the state system | |
96 | */ | |
97 | @Test | |
98 | public void testDoQuery() { | |
ccf2bbb4 | 99 | List<ITmfStateInterval> interval = new ArrayList<>(NUMBER_OF_ATTRIBUTES); |
f606c6fa MK |
100 | for (int i = 0; i < NUMBER_OF_ATTRIBUTES; i++) { |
101 | interval.add(null); | |
102 | } | |
103 | try { | |
104 | fixture.doQuery(interval, 950); | |
0306a843 | 105 | } catch (TimeRangeException | StateSystemDisposedException e) { |
f606c6fa MK |
106 | fail(e.getMessage()); |
107 | } | |
108 | ||
109 | assertEquals(NUMBER_OF_ATTRIBUTES, interval.size()); | |
110 | testInterval(interval.get(0), 900, 990, 9); | |
111 | testInterval(interval.get(1), 901, 991, 9); | |
112 | testInterval(interval.get(2), 902, 992, 9); | |
113 | testInterval(interval.get(3), 903, 993, 9); | |
114 | testInterval(interval.get(4), 904, 994, 9); | |
115 | testInterval(interval.get(5), 905, 995, 9); | |
116 | testInterval(interval.get(6), 906, 996, 9); | |
117 | testInterval(interval.get(7), 907, 997, 9); | |
118 | testInterval(interval.get(8), 908, 998, 9); | |
119 | testInterval(interval.get(9), 909, 999, 9); | |
120 | } | |
121 | ||
122 | ||
123 | /** | |
124 | * Test single attribute then compare it to a full query | |
125 | */ | |
126 | @Test | |
127 | public void testQueryAttribute() { | |
128 | try { | |
129 | ITmfStateInterval interval[] = new TmfStateInterval[10]; | |
130 | for (int i = 0; i < 10; i++) { | |
131 | interval[i] = fixture.doSingularQuery(950, i); | |
132 | } | |
133 | ||
134 | testInterval(interval[0], 900, 990, 9); | |
135 | testInterval(interval[1], 901, 991, 9); | |
136 | testInterval(interval[2], 902, 992, 9); | |
137 | testInterval(interval[3], 903, 993, 9); | |
138 | testInterval(interval[4], 904, 994, 9); | |
139 | testInterval(interval[5], 905, 995, 9); | |
140 | testInterval(interval[6], 906, 996, 9); | |
141 | testInterval(interval[7], 907, 997, 9); | |
142 | testInterval(interval[8], 908, 998, 9); | |
143 | testInterval(interval[9], 909, 999, 9); | |
144 | ||
ccf2bbb4 | 145 | List<ITmfStateInterval> intervalQuery = new ArrayList<>(NUMBER_OF_ATTRIBUTES); |
f606c6fa MK |
146 | for (int i = 0; i < NUMBER_OF_ATTRIBUTES; i++) { |
147 | intervalQuery.add(null); | |
148 | } | |
149 | ||
150 | fixture.doQuery(intervalQuery, 950); | |
151 | ITmfStateInterval ref[] = intervalQuery.toArray(new ITmfStateInterval[0]); | |
152 | assertArrayEquals(ref, interval); | |
153 | ||
0306a843 | 154 | } catch (TimeRangeException | AttributeNotFoundException | StateSystemDisposedException e) { |
f606c6fa MK |
155 | fail(e.getMessage()); |
156 | } | |
157 | } | |
158 | ||
159 | /** | |
160 | * Test single attribute that should not exist | |
161 | */ | |
162 | @Test | |
163 | public void testQueryAttributeEmpty() { | |
164 | try { | |
165 | ITmfStateInterval interval = fixture.doSingularQuery(999, 0); | |
166 | assertEquals(TmfStateValue.nullValue(), interval.getStateValue()); | |
167 | ||
0306a843 | 168 | } catch (TimeRangeException | AttributeNotFoundException | StateSystemDisposedException e) { |
f606c6fa MK |
169 | fail(e.getMessage()); |
170 | } | |
171 | } | |
172 | ||
173 | /** | |
174 | * Test first element in ss | |
175 | */ | |
176 | @Test | |
177 | public void testBegin() { | |
178 | try { | |
179 | ITmfStateInterval interval = fixture.doSingularQuery(0, 0); | |
180 | assertEquals(0, interval.getStartTime()); | |
181 | assertEquals(90, interval.getEndTime()); | |
182 | assertEquals(0, interval.getStateValue().unboxInt()); | |
183 | ||
0306a843 | 184 | } catch (TimeRangeException | AttributeNotFoundException | StateSystemDisposedException e) { |
f606c6fa MK |
185 | fail(e.getMessage()); |
186 | } | |
187 | } | |
188 | ||
189 | /** | |
190 | * Test last element in ss | |
191 | */ | |
192 | @Test | |
193 | public void testEnd() { | |
194 | try { | |
195 | ITmfStateInterval interval = fixture.doSingularQuery(99998, 9); | |
196 | testInterval(interval, 99909, 99999, 99); | |
197 | ||
0306a843 | 198 | } catch (TimeRangeException | AttributeNotFoundException | StateSystemDisposedException e) { |
f606c6fa MK |
199 | fail(e.getMessage()); |
200 | } | |
201 | } | |
202 | ||
203 | /** | |
204 | * Test out of range query | |
205 | * | |
206 | * @throws TimeRangeException | |
207 | * Expected | |
208 | */ | |
209 | @Test(expected = TimeRangeException.class) | |
210 | public void testOutOfRange_1() throws TimeRangeException { | |
211 | try { | |
212 | ITmfStateInterval interval = fixture.doSingularQuery(-1, 0); | |
213 | assertNull(interval); | |
214 | ||
0306a843 | 215 | } catch (AttributeNotFoundException | StateSystemDisposedException e) { |
f606c6fa MK |
216 | fail(e.getMessage()); |
217 | } | |
218 | } | |
219 | ||
220 | /** | |
221 | * Test out of range query | |
222 | * | |
223 | * @throws TimeRangeException | |
224 | * Expected | |
225 | */ | |
226 | @Test(expected = TimeRangeException.class) | |
227 | public void testOutOfRange_2() throws TimeRangeException { | |
228 | try { | |
229 | ITmfStateInterval interval = fixture.doSingularQuery(100000, 0); | |
230 | assertNull(interval); | |
231 | ||
0306a843 | 232 | } catch (AttributeNotFoundException | StateSystemDisposedException e) { |
f606c6fa MK |
233 | fail(e.getMessage()); |
234 | } | |
235 | } | |
236 | } |