1 /*******************************************************************************
2 * Copyright (c) 2013, 2015 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
.*;
17 import java
.util
.ArrayList
;
18 import java
.util
.List
;
20 import org
.eclipse
.jdt
.annotation
.Nullable
;
21 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.IStateHistoryBackend
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.StateHistoryBackendFactory
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
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
;
30 import org
.junit
.BeforeClass
;
31 import org
.junit
.Test
;
34 * Test cases for the in-memory backend
36 * @author Matthew Khouzam
38 public class InMemoryBackendTest
{
40 private static final int NUMBER_OF_ATTRIBUTES
= 10;
41 private static IStateHistoryBackend fixture
;
44 * Test setup. make a state system that is moderately large
47 public static void init() {
48 fixture
= StateHistoryBackendFactory
.createInMemoryBackend("test-ss", 0);
49 for (int attribute
= 0; attribute
< NUMBER_OF_ATTRIBUTES
; attribute
++) {
50 for (int timeStart
= 0; timeStart
< 1000; timeStart
++) {
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());
58 } catch (TimeRangeException e
) {
59 /* Should not happen here */
60 throw new IllegalStateException();
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());
72 assertEquals(value
, interval
.getStateValue().unboxInt());
73 } catch (StateValueTypeException e
) {
83 public void testStartTime() {
84 assertEquals(0, fixture
.getStartTime());
91 public void testEndTime() {
92 assertEquals(99999, fixture
.getEndTime());
96 * Query the state system
99 public void testDoQuery() {
100 List
<@Nullable ITmfStateInterval
> interval
= new ArrayList
<>(NUMBER_OF_ATTRIBUTES
);
101 for (int i
= 0; i
< NUMBER_OF_ATTRIBUTES
; i
++) {
105 fixture
.doQuery(interval
, 950);
106 } catch (TimeRangeException
| StateSystemDisposedException e
) {
107 fail(e
.getMessage());
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);
125 * Test single attribute then compare it to a full query
128 public void testQueryAttribute() {
130 ITmfStateInterval interval
[] = new TmfStateInterval
[10];
131 for (int i
= 0; i
< 10; i
++) {
132 interval
[i
] = fixture
.doSingularQuery(950, i
);
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);
146 List
<@Nullable ITmfStateInterval
> intervalQuery
= new ArrayList
<>(NUMBER_OF_ATTRIBUTES
);
147 for (int i
= 0; i
< NUMBER_OF_ATTRIBUTES
; i
++) {
148 intervalQuery
.add(null);
151 fixture
.doQuery(intervalQuery
, 950);
152 ITmfStateInterval ref
[] = intervalQuery
.toArray(new ITmfStateInterval
[0]);
153 assertArrayEquals(ref
, interval
);
155 } catch (TimeRangeException
| AttributeNotFoundException
| StateSystemDisposedException e
) {
156 fail(e
.getMessage());
161 * Test single attribute that should not exist
164 public void testQueryAttributeEmpty() {
166 ITmfStateInterval interval
= fixture
.doSingularQuery(999, 0);
167 assertEquals(TmfStateValue
.nullValue(), interval
.getStateValue());
169 } catch (TimeRangeException
| AttributeNotFoundException
| StateSystemDisposedException e
) {
170 fail(e
.getMessage());
175 * Test first element in ss
178 public void testBegin() {
180 ITmfStateInterval interval
= fixture
.doSingularQuery(0, 0);
181 assertEquals(0, interval
.getStartTime());
182 assertEquals(90, interval
.getEndTime());
183 assertEquals(0, interval
.getStateValue().unboxInt());
185 } catch (TimeRangeException
| AttributeNotFoundException
| StateSystemDisposedException e
) {
186 fail(e
.getMessage());
191 * Test last element in ss
194 public void testEnd() {
196 ITmfStateInterval interval
= fixture
.doSingularQuery(99998, 9);
197 testInterval(interval
, 99909, 99999, 99);
199 } catch (TimeRangeException
| AttributeNotFoundException
| StateSystemDisposedException e
) {
200 fail(e
.getMessage());
205 * Test out of range query
207 * @throws TimeRangeException
210 @Test(expected
= TimeRangeException
.class)
211 public void testOutOfRange_1() throws TimeRangeException
{
213 ITmfStateInterval interval
= fixture
.doSingularQuery(-1, 0);
214 assertNull(interval
);
216 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
217 fail(e
.getMessage());
222 * Test out of range query
224 * @throws TimeRangeException
227 @Test(expected
= TimeRangeException
.class)
228 public void testOutOfRange_2() throws TimeRangeException
{
230 ITmfStateInterval interval
= fixture
.doSingularQuery(100000, 0);
231 assertNull(interval
);
233 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
234 fail(e
.getMessage());