tmf: Fix in-memory backend to use a TreeSet insead of a list
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / statesystem / backends / InMemoryBackendTest.java
CommitLineData
f606c6fa
MK
1/*******************************************************************************
2 * Copyright (c) 2013 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.linuxtools.tmf.core.tests.statesystem.backends;
14
15import static org.junit.Assert.*;
16
17import java.util.ArrayList;
18import java.util.List;
19
20import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.InMemoryBackend;
21import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException;
22import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;
23import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
24import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval;
25import org.eclipse.linuxtools.tmf.core.interval.TmfStateInterval;
26import org.eclipse.linuxtools.tmf.core.statevalue.TmfStateValue;
27import org.junit.BeforeClass;
28import org.junit.Test;
29
30/**
31 * Test cases for the in-memory backend
32 *
33 * @author Matthew Khouzam
34 */
35public class InMemoryBackendTest {
36
37 private static final int NUMBER_OF_ATTRIBUTES = 10;
38 private static InMemoryBackend fixture;
39
40 /**
41 * Test setup. make a state system that is moderately large
42 */
43 @BeforeClass
44 public static void init() {
45 fixture = new InMemoryBackend(0);
46 for (int attribute = 0; attribute < NUMBER_OF_ATTRIBUTES; attribute++) {
47 for (int timeStart = 0; timeStart < 1000; timeStart++) {
48 try {
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());
54 }
55 } catch (TimeRangeException e) {
56 /* Should not happen here */
57 throw new IllegalStateException();
58 }
59 }
60 }
61 }
62
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());
68 try {
69 assertEquals(value, interval.getStateValue().unboxInt());
70 } catch (StateValueTypeException e) {
71 fail(e.getMessage());
72 }
73 }
74
75
76 /**
77 * Test at start time
78 */
79 @Test
80 public void testStartTime() {
81 assertEquals(0, fixture.getStartTime());
82 }
83
84 /**
85 * Test at end time
86 */
87 @Test
88 public void testEndTime() {
89 assertEquals(99999, fixture.getEndTime());
90 }
91
92 /**
93 * Query the state system
94 */
95 @Test
96 public void testDoQuery() {
97 List<ITmfStateInterval> interval = new ArrayList<ITmfStateInterval>(NUMBER_OF_ATTRIBUTES);
98 for (int i = 0; i < NUMBER_OF_ATTRIBUTES; i++) {
99 interval.add(null);
100 }
101 try {
102 fixture.doQuery(interval, 950);
103 } catch (TimeRangeException e) {
104 fail(e.getMessage());
105 }
106
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);
118 }
119
120
121 /**
122 * Test single attribute then compare it to a full query
123 */
124 @Test
125 public void testQueryAttribute() {
126 try {
127 ITmfStateInterval interval[] = new TmfStateInterval[10];
128 for (int i = 0; i < 10; i++) {
129 interval[i] = fixture.doSingularQuery(950, i);
130 }
131
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);
142
143 List<ITmfStateInterval> intervalQuery = new ArrayList<ITmfStateInterval>(NUMBER_OF_ATTRIBUTES);
144 for (int i = 0; i < NUMBER_OF_ATTRIBUTES; i++) {
145 intervalQuery.add(null);
146 }
147
148 fixture.doQuery(intervalQuery, 950);
149 ITmfStateInterval ref[] = intervalQuery.toArray(new ITmfStateInterval[0]);
150 assertArrayEquals(ref, interval);
151
152 } catch (TimeRangeException e) {
153 fail(e.getMessage());
154 } catch (AttributeNotFoundException 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 e) {
169 fail(e.getMessage());
170 } catch (AttributeNotFoundException e) {
171 fail(e.getMessage());
172 }
173 }
174
175 /**
176 * Test first element in ss
177 */
178 @Test
179 public void testBegin() {
180 try {
181 ITmfStateInterval interval = fixture.doSingularQuery(0, 0);
182 assertEquals(0, interval.getStartTime());
183 assertEquals(90, interval.getEndTime());
184 assertEquals(0, interval.getStateValue().unboxInt());
185
186 } catch (TimeRangeException e) {
187 fail(e.getMessage());
188 } catch (AttributeNotFoundException e) {
189 fail(e.getMessage());
190 } catch (StateValueTypeException e) {
191 fail(e.getMessage());
192 }
193 }
194
195 /**
196 * Test last element in ss
197 */
198 @Test
199 public void testEnd() {
200 try {
201 ITmfStateInterval interval = fixture.doSingularQuery(99998, 9);
202 testInterval(interval, 99909, 99999, 99);
203
204 } catch (TimeRangeException e) {
205 fail(e.getMessage());
206 } catch (AttributeNotFoundException e) {
207 fail(e.getMessage());
208 }
209 }
210
211 /**
212 * Test out of range query
213 *
214 * @throws TimeRangeException
215 * Expected
216 */
217 @Test(expected = TimeRangeException.class)
218 public void testOutOfRange_1() throws TimeRangeException {
219 try {
220 ITmfStateInterval interval = fixture.doSingularQuery(-1, 0);
221 assertNull(interval);
222
223 } catch (AttributeNotFoundException e) {
224 fail(e.getMessage());
225 }
226 }
227
228 /**
229 * Test out of range query
230 *
231 * @throws TimeRangeException
232 * Expected
233 */
234 @Test(expected = TimeRangeException.class)
235 public void testOutOfRange_2() throws TimeRangeException {
236 try {
237 ITmfStateInterval interval = fixture.doSingularQuery(100000, 0);
238 assertNull(interval);
239
240 } catch (AttributeNotFoundException e) {
241 fail(e.getMessage());
242 }
243 }
244}
This page took 0.034693 seconds and 5 git commands to generate.