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