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