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