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
1/*******************************************************************************
2 * Copyright (c) 2013, 2015 Ericsson
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
13package org.eclipse.tracecompass.statesystem.core.tests.backend;
14
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;
20
21import java.util.ArrayList;
22import java.util.List;
23
24import org.eclipse.jdt.annotation.Nullable;
25import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend;
26import org.eclipse.tracecompass.statesystem.core.backend.StateHistoryBackendFactory;
27import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
28import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
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;
34import org.junit.BeforeClass;
35import org.junit.Test;
36
37/**
38 * Test cases for the in-memory backend
39 *
40 * @author Matthew Khouzam
41 */
42public class InMemoryBackendTest extends StateHistoryBackendTestBase {
43
44 private static final String SSID = "test-ss";
45 private static final int NUMBER_OF_ATTRIBUTES = 10;
46
47 private static @Nullable IStateHistoryBackend fixture;
48
49 /**
50 * Test setup. make a state system that is moderately large
51 */
52 @BeforeClass
53 public static void init() {
54 IStateHistoryBackend backend = StateHistoryBackendFactory.createInMemoryBackend(SSID, 0);
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 backend.insertPastState(stateStartTime, stateEndTime, attribute, TmfStateValue.newValueInt(timeStart % 100));
61 if (timeStart != 999) {
62 backend.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 fixture = backend;
71 }
72
73 private static void testInterval(@Nullable ITmfStateInterval interval, int startTime,
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
85 @Override
86 protected IStateHistoryBackend getBackendForBuilding(long startTime) {
87 return StateHistoryBackendFactory.createInMemoryBackend(SSID, startTime);
88 }
89
90 /**
91 * Test at start time
92 */
93 @Test
94 public void testStartTime() {
95 IStateHistoryBackend backend = fixture;
96 assertNotNull(backend);
97 assertEquals(0, backend.getStartTime());
98 }
99
100 /**
101 * Test at end time
102 */
103 @Test
104 public void testEndTime() {
105 IStateHistoryBackend backend = fixture;
106 assertNotNull(backend);
107 assertEquals(99999, backend.getEndTime());
108 }
109
110 /**
111 * Query the state system
112 */
113 @Test
114 public void testDoQuery() {
115 List<@Nullable ITmfStateInterval> interval = new ArrayList<>(NUMBER_OF_ATTRIBUTES);
116 for (int i = 0; i < NUMBER_OF_ATTRIBUTES; i++) {
117 interval.add(null);
118 }
119 IStateHistoryBackend backend = fixture;
120 assertNotNull(backend);
121 try {
122 backend.doQuery(interval, 950);
123 } catch (TimeRangeException | StateSystemDisposedException e) {
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 {
147 IStateHistoryBackend backend = fixture;
148 assertNotNull(backend);
149 ITmfStateInterval interval[] = new TmfStateInterval[10];
150 for (int i = 0; i < 10; i++) {
151 interval[i] = backend.doSingularQuery(950, i);
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
165 List<@Nullable ITmfStateInterval> intervalQuery = new ArrayList<>(NUMBER_OF_ATTRIBUTES);
166 for (int i = 0; i < NUMBER_OF_ATTRIBUTES; i++) {
167 intervalQuery.add(null);
168 }
169
170 backend.doQuery(intervalQuery, 950);
171 ITmfStateInterval ref[] = intervalQuery.toArray(new ITmfStateInterval[0]);
172 assertArrayEquals(ref, interval);
173
174 } catch (TimeRangeException | AttributeNotFoundException | StateSystemDisposedException e) {
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 {
185 IStateHistoryBackend backend = fixture;
186 assertNotNull(backend);
187 ITmfStateInterval interval = backend.doSingularQuery(999, 0);
188 assertEquals(TmfStateValue.nullValue(), interval.getStateValue());
189
190 } catch (TimeRangeException | AttributeNotFoundException | StateSystemDisposedException e) {
191 fail(e.getMessage());
192 }
193 }
194
195 /**
196 * Test first element in ss
197 */
198 @Test
199 public void testBegin() {
200 try {
201 IStateHistoryBackend backend = fixture;
202 assertNotNull(backend);
203 ITmfStateInterval interval = backend.doSingularQuery(0, 0);
204 assertEquals(0, interval.getStartTime());
205 assertEquals(90, interval.getEndTime());
206 assertEquals(0, interval.getStateValue().unboxInt());
207
208 } catch (TimeRangeException | AttributeNotFoundException | StateSystemDisposedException e) {
209 fail(e.getMessage());
210 }
211 }
212
213 /**
214 * Test last element in ss
215 */
216 @Test
217 public void testEnd() {
218 try {
219 IStateHistoryBackend backend = fixture;
220 assertNotNull(backend);
221 ITmfStateInterval interval = backend.doSingularQuery(99998, 9);
222 testInterval(interval, 99909, 99999, 99);
223
224 } catch (TimeRangeException | AttributeNotFoundException | StateSystemDisposedException e) {
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 {
238 IStateHistoryBackend backend = fixture;
239 assertNotNull(backend);
240 ITmfStateInterval interval = backend.doSingularQuery(-1, 0);
241 assertNull(interval);
242
243 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
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 {
257 IStateHistoryBackend backend = fixture;
258 assertNotNull(backend);
259 ITmfStateInterval interval = backend.doSingularQuery(100000, 0);
260 assertNull(interval);
261
262 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
263 fail(e.getMessage());
264 }
265 }
266}
This page took 0.02455 seconds and 5 git commands to generate.