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
.tracecompass
.statesystem
.core
.backend
.InMemoryBackend
;
21 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
25 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.TmfStateInterval
;
26 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
27 import org
.junit
.BeforeClass
;
28 import org
.junit
.Test
;
31 * Test cases for the in-memory backend
33 * @author Matthew Khouzam
35 public class InMemoryBackendTest
{
37 private static final int NUMBER_OF_ATTRIBUTES
= 10;
38 private static InMemoryBackend fixture
;
41 * Test setup. make a state system that is moderately large
44 public static void init() {
45 fixture
= new InMemoryBackend("test-ss", 0);
46 for (int attribute
= 0; attribute
< NUMBER_OF_ATTRIBUTES
; attribute
++) {
47 for (int timeStart
= 0; timeStart
< 1000; timeStart
++) {
49 final int stateEndTime
= (timeStart
* 100) + 90 + attribute
;
50 final int stateStartTime
= timeStart
* 100 + attribute
;
51 fixture
.insertPastState(stateStartTime
, stateEndTime
, attribute
, TmfStateValue
.newValueInt(timeStart
% 100));
52 if (timeStart
!= 999) {
53 fixture
.insertPastState(stateEndTime
+ 1, stateEndTime
+ 9, attribute
, TmfStateValue
.nullValue());
55 } catch (TimeRangeException e
) {
56 /* Should not happen here */
57 throw new IllegalStateException();
63 private static void testInterval(ITmfStateInterval interval
, int startTime
,
64 int endTime
, int value
) {
65 assertNotNull(interval
);
66 assertEquals(startTime
, interval
.getStartTime());
67 assertEquals(endTime
, interval
.getEndTime());
69 assertEquals(value
, interval
.getStateValue().unboxInt());
70 } catch (StateValueTypeException e
) {
80 public void testStartTime() {
81 assertEquals(0, fixture
.getStartTime());
88 public void testEndTime() {
89 assertEquals(99999, fixture
.getEndTime());
93 * Query the state system
96 public void testDoQuery() {
97 List
<ITmfStateInterval
> interval
= new ArrayList
<>(NUMBER_OF_ATTRIBUTES
);
98 for (int i
= 0; i
< NUMBER_OF_ATTRIBUTES
; i
++) {
102 fixture
.doQuery(interval
, 950);
103 } catch (TimeRangeException e
) {
104 fail(e
.getMessage());
107 assertEquals(NUMBER_OF_ATTRIBUTES
, interval
.size());
108 testInterval(interval
.get(0), 900, 990, 9);
109 testInterval(interval
.get(1), 901, 991, 9);
110 testInterval(interval
.get(2), 902, 992, 9);
111 testInterval(interval
.get(3), 903, 993, 9);
112 testInterval(interval
.get(4), 904, 994, 9);
113 testInterval(interval
.get(5), 905, 995, 9);
114 testInterval(interval
.get(6), 906, 996, 9);
115 testInterval(interval
.get(7), 907, 997, 9);
116 testInterval(interval
.get(8), 908, 998, 9);
117 testInterval(interval
.get(9), 909, 999, 9);
122 * Test single attribute then compare it to a full query
125 public void testQueryAttribute() {
127 ITmfStateInterval interval
[] = new TmfStateInterval
[10];
128 for (int i
= 0; i
< 10; i
++) {
129 interval
[i
] = fixture
.doSingularQuery(950, i
);
132 testInterval(interval
[0], 900, 990, 9);
133 testInterval(interval
[1], 901, 991, 9);
134 testInterval(interval
[2], 902, 992, 9);
135 testInterval(interval
[3], 903, 993, 9);
136 testInterval(interval
[4], 904, 994, 9);
137 testInterval(interval
[5], 905, 995, 9);
138 testInterval(interval
[6], 906, 996, 9);
139 testInterval(interval
[7], 907, 997, 9);
140 testInterval(interval
[8], 908, 998, 9);
141 testInterval(interval
[9], 909, 999, 9);
143 List
<ITmfStateInterval
> intervalQuery
= new ArrayList
<>(NUMBER_OF_ATTRIBUTES
);
144 for (int i
= 0; i
< NUMBER_OF_ATTRIBUTES
; i
++) {
145 intervalQuery
.add(null);
148 fixture
.doQuery(intervalQuery
, 950);
149 ITmfStateInterval ref
[] = intervalQuery
.toArray(new ITmfStateInterval
[0]);
150 assertArrayEquals(ref
, interval
);
152 } catch (TimeRangeException e
) {
153 fail(e
.getMessage());
154 } catch (AttributeNotFoundException e
) {
155 fail(e
.getMessage());
160 * Test single attribute that should not exist
163 public void testQueryAttributeEmpty() {
165 ITmfStateInterval interval
= fixture
.doSingularQuery(999, 0);
166 assertEquals(TmfStateValue
.nullValue(), interval
.getStateValue());
168 } catch (TimeRangeException e
) {
169 fail(e
.getMessage());
170 } catch (AttributeNotFoundException e
) {
171 fail(e
.getMessage());
176 * Test first element in ss
179 public void testBegin() {
181 ITmfStateInterval interval
= fixture
.doSingularQuery(0, 0);
182 assertEquals(0, interval
.getStartTime());
183 assertEquals(90, interval
.getEndTime());
184 assertEquals(0, interval
.getStateValue().unboxInt());
186 } catch (TimeRangeException e
) {
187 fail(e
.getMessage());
188 } catch (AttributeNotFoundException e
) {
189 fail(e
.getMessage());
190 } catch (StateValueTypeException e
) {
191 fail(e
.getMessage());
196 * Test last element in ss
199 public void testEnd() {
201 ITmfStateInterval interval
= fixture
.doSingularQuery(99998, 9);
202 testInterval(interval
, 99909, 99999, 99);
204 } catch (TimeRangeException e
) {
205 fail(e
.getMessage());
206 } catch (AttributeNotFoundException e
) {
207 fail(e
.getMessage());
212 * Test out of range query
214 * @throws TimeRangeException
217 @Test(expected
= TimeRangeException
.class)
218 public void testOutOfRange_1() throws TimeRangeException
{
220 ITmfStateInterval interval
= fixture
.doSingularQuery(-1, 0);
221 assertNull(interval
);
223 } catch (AttributeNotFoundException e
) {
224 fail(e
.getMessage());
229 * Test out of range query
231 * @throws TimeRangeException
234 @Test(expected
= TimeRangeException
.class)
235 public void testOutOfRange_2() throws TimeRangeException
{
237 ITmfStateInterval interval
= fixture
.doSingularQuery(100000, 0);
238 assertNull(interval
);
240 } catch (AttributeNotFoundException e
) {
241 fail(e
.getMessage());