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