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