1 /*******************************************************************************
2 * Copyright (c) 2013, 2016 Ericsson
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
10 * Matthew Khouzam - Initial API and implementation
11 ******************************************************************************/
13 package org
.eclipse
.tracecompass
.statesystem
.core
.tests
.backend
;
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
;
21 import java
.util
.ArrayList
;
22 import java
.util
.List
;
24 import org
.eclipse
.jdt
.annotation
.Nullable
;
25 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.IStateHistoryBackend
;
26 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.StateHistoryBackendFactory
;
27 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.TmfStateInterval
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
33 import org
.junit
.BeforeClass
;
34 import org
.junit
.Test
;
37 * Test cases for the in-memory backend
39 * @author Matthew Khouzam
41 public class InMemoryBackendTest
extends StateHistoryBackendTestBase
{
43 private static final String SSID
= "test-ss";
44 private static final int NUMBER_OF_ATTRIBUTES
= 10;
46 private static @Nullable IStateHistoryBackend fixture
;
49 * Test setup. make a state system that is moderately large
52 public static void init() {
53 IStateHistoryBackend backend
= StateHistoryBackendFactory
.createInMemoryBackend(SSID
, 0);
54 for (int attribute
= 0; attribute
< NUMBER_OF_ATTRIBUTES
; attribute
++) {
55 for (int timeStart
= 0; timeStart
< 1000; timeStart
++) {
57 final int stateEndTime
= (timeStart
* 100) + 90 + attribute
;
58 final int stateStartTime
= timeStart
* 100 + attribute
;
59 backend
.insertPastState(stateStartTime
, stateEndTime
, attribute
, TmfStateValue
.newValueInt(timeStart
% 100));
60 if (timeStart
!= 999) {
61 backend
.insertPastState(stateEndTime
+ 1, stateEndTime
+ 9, attribute
, TmfStateValue
.nullValue());
63 } catch (TimeRangeException e
) {
64 /* Should not happen here */
65 throw new IllegalStateException();
72 private static void testInterval(@Nullable ITmfStateInterval interval
, int startTime
,
73 int endTime
, int value
) {
74 assertNotNull(interval
);
75 assertEquals(startTime
, interval
.getStartTime());
76 assertEquals(endTime
, interval
.getEndTime());
78 assertEquals(value
, interval
.getStateValue().unboxInt());
79 } catch (StateValueTypeException e
) {
85 protected IStateHistoryBackend
getBackendForBuilding(long startTime
) {
86 return StateHistoryBackendFactory
.createInMemoryBackend(SSID
, startTime
);
93 public void testStartTime() {
94 IStateHistoryBackend backend
= fixture
;
95 assertNotNull(backend
);
96 assertEquals(0, backend
.getStartTime());
103 public void testEndTime() {
104 IStateHistoryBackend backend
= fixture
;
105 assertNotNull(backend
);
106 assertEquals(99999, backend
.getEndTime());
110 * Query the state system
113 public void testDoQuery() {
114 List
<@Nullable ITmfStateInterval
> interval
= new ArrayList
<>(NUMBER_OF_ATTRIBUTES
);
115 for (int i
= 0; i
< NUMBER_OF_ATTRIBUTES
; i
++) {
118 IStateHistoryBackend backend
= fixture
;
119 assertNotNull(backend
);
121 backend
.doQuery(interval
, 950);
122 } catch (TimeRangeException
| StateSystemDisposedException e
) {
123 fail(e
.getMessage());
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);
141 * Test single attribute then compare it to a full query
144 public void testQueryAttribute() {
146 IStateHistoryBackend backend
= fixture
;
147 assertNotNull(backend
);
148 ITmfStateInterval interval
[] = new TmfStateInterval
[10];
149 for (int i
= 0; i
< 10; i
++) {
150 interval
[i
] = backend
.doSingularQuery(950, i
);
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);
164 List
<@Nullable ITmfStateInterval
> intervalQuery
= new ArrayList
<>(NUMBER_OF_ATTRIBUTES
);
165 for (int i
= 0; i
< NUMBER_OF_ATTRIBUTES
; i
++) {
166 intervalQuery
.add(null);
169 backend
.doQuery(intervalQuery
, 950);
170 ITmfStateInterval ref
[] = intervalQuery
.toArray(new ITmfStateInterval
[0]);
171 assertArrayEquals(ref
, interval
);
173 } catch (TimeRangeException
| StateSystemDisposedException e
) {
174 fail(e
.getMessage());
179 * Test single attribute that should not exist
182 public void testQueryAttributeEmpty() {
184 IStateHistoryBackend backend
= fixture
;
185 assertNotNull(backend
);
186 ITmfStateInterval interval
= backend
.doSingularQuery(999, 0);
187 assertEquals(TmfStateValue
.nullValue(), interval
.getStateValue());
189 } catch (TimeRangeException
| StateSystemDisposedException e
) {
190 fail(e
.getMessage());
195 * Test first element in ss
198 public void testBegin() {
200 IStateHistoryBackend backend
= fixture
;
201 assertNotNull(backend
);
202 ITmfStateInterval interval
= backend
.doSingularQuery(0, 0);
203 assertEquals(0, interval
.getStartTime());
204 assertEquals(90, interval
.getEndTime());
205 assertEquals(0, interval
.getStateValue().unboxInt());
207 } catch (TimeRangeException
| StateSystemDisposedException e
) {
208 fail(e
.getMessage());
213 * Test last element in ss
216 public void testEnd() {
218 IStateHistoryBackend backend
= fixture
;
219 assertNotNull(backend
);
220 ITmfStateInterval interval
= backend
.doSingularQuery(99998, 9);
221 testInterval(interval
, 99909, 99999, 99);
223 } catch (TimeRangeException
| StateSystemDisposedException e
) {
224 fail(e
.getMessage());
229 * Test out of range query
231 * @throws TimeRangeException
234 @Test(expected
= TimeRangeException
.class)
235 public void testOutOfRange_1() throws TimeRangeException
{
237 IStateHistoryBackend backend
= fixture
;
238 assertNotNull(backend
);
239 ITmfStateInterval interval
= backend
.doSingularQuery(-1, 0);
240 assertNull(interval
);
242 } catch (StateSystemDisposedException e
) {
243 fail(e
.getMessage());
248 * Test out of range query
250 * @throws TimeRangeException
253 @Test(expected
= TimeRangeException
.class)
254 public void testOutOfRange_2() throws TimeRangeException
{
256 IStateHistoryBackend backend
= fixture
;
257 assertNotNull(backend
);
258 ITmfStateInterval interval
= backend
.doSingularQuery(100000, 0);
259 assertNull(interval
);
261 } catch (StateSystemDisposedException e
) {
262 fail(e
.getMessage());