ss: Add a package-info file to backend unit tests
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core.tests / src / org / eclipse / tracecompass / statesystem / core / tests / backend / StateHistoryBackendTestBase.java
1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson, EfficiOS Inc. and others
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
10 package org.eclipse.tracecompass.statesystem.core.tests.backend;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend;
23 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
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.Test;
28
29 /**
30 * Abstract class to test implementations of the {@link IStateHistoryBackend} interface.
31 *
32 * @author Patrick Tasse
33 * @author Alexandre Montplaisir
34 */
35 public abstract class StateHistoryBackendTestBase {
36
37 /**
38 * Gets the backend to be used for building.
39 *
40 * @param startTime
41 * The start time of the history
42 *
43 * @return The backend to be used for building.
44 * @throws IOException
45 * if an exception occurs
46 */
47 protected abstract IStateHistoryBackend getBackendForBuilding(long startTime) throws IOException;
48
49 /**
50 * Gets the backend to be used for querying. The default implementation
51 * returns the backend that was used for building.
52 * <p>
53 * Only the returned backend should be used after calling this method. The
54 * one sent in parameter might have been disposed.
55 *
56 * @param backend
57 * The backend that was used for building
58 * @return The backend to be used for querying.
59 * @throws IOException
60 * if an exception occurs
61 */
62 @SuppressWarnings("unused")
63 protected IStateHistoryBackend getBackendForQuerying(IStateHistoryBackend backend) throws IOException {
64 return backend;
65 }
66
67 /**
68 * Prepares a backend to be used in tests. The specified intervals will be
69 * inserted in the backend, and then the backend will be closed.
70 *
71 * @param startTime
72 * The start time of the history
73 * @param endTime
74 * The end time at which to close the history
75 * @param intervals
76 * The intervals to insert in the history backend
77 * @return The backend to be used for querying.
78 */
79 protected final @Nullable IStateHistoryBackend prepareBackend(long startTime, long endTime,
80 List<ITmfStateInterval> intervals) {
81
82 try {
83 IStateHistoryBackend backend = getBackendForBuilding(startTime);
84 insertIntervals(backend, intervals);
85 backend.finishedBuilding(Math.max(endTime, backend.getEndTime()));
86 return getBackendForQuerying(backend);
87 } catch (IOException e) {
88 fail(e.getMessage());
89 return null;
90 }
91 }
92
93 /**
94 * Insert the specified intervals in the provided backend.
95 *
96 * @param backend
97 * The backend to be used
98 * @param intervals
99 * The intervals to insert in the history backend
100 */
101 protected static void insertIntervals(IStateHistoryBackend backend, List<ITmfStateInterval> intervals) {
102 for (ITmfStateInterval interval : intervals) {
103 backend.insertPastState(interval.getStartTime(), interval.getEndTime(), interval.getAttribute(), interval.getStateValue());
104 }
105 }
106
107 /**
108 * Test the integrity of a backend by first building the backend with the
109 * specified intervals, closing it, and then querying at every single
110 * timestamp, making sure that all returned intervals intersect with the
111 * query time. The backend start and end time will be checked.
112 * <p>
113 * If <code>allowNull</code> is false, the specified intervals must cover
114 * the full range for all attributes. The method will make sure that no null
115 * intervals are returned.
116 *
117 * @param startTime
118 * The start time of the history
119 * @param endTime
120 * The end time of the history
121 * @param nbAttr
122 * The number of attributes
123 * @param intervals
124 * The list of intervals to insert
125 * @param allowNull
126 * True if null intervals are allowed, false otherwise
127 * @return The backend to be used for querying.
128 */
129 protected final IStateHistoryBackend buildAndQueryFullRange(long startTime, long endTime, int nbAttr, List<ITmfStateInterval> intervals, boolean allowNull) {
130
131 final IStateHistoryBackend backend = prepareBackend(startTime, endTime, intervals);
132 assertNotNull(backend);
133
134 try {
135 /*
136 * Query at every valid time stamp, making sure only the expected
137 * intervals are returned.
138 */
139 for (long t = backend.getStartTime(); t <= backend.getEndTime(); t++) {
140 List<@Nullable ITmfStateInterval> stateInfo = new ArrayList<>(nbAttr);
141 for (int i = 0; i < nbAttr; i++) {
142 stateInfo.add(null);
143 }
144 backend.doQuery(stateInfo, t);
145 for (int attr = 0; attr < stateInfo.size(); attr++) {
146 ITmfStateInterval interval = stateInfo.get(attr);
147 if (!allowNull) {
148 assertTrue("null interval at t=" + t + " for attr=" + attr, interval != null);
149 }
150 if (interval != null) {
151 assertTrue(interval + " does not intersect t=" + t, interval.intersects(t));
152 }
153 }
154 }
155
156 assertEquals(startTime, backend.getStartTime());
157 assertEquals(endTime, backend.getEndTime());
158 } catch (StateSystemDisposedException e) {
159 fail(e.getMessage());
160 }
161 return backend;
162 }
163
164 /**
165 * Test the full query method by filling a small backend with intervals
166 * placed in a "stair-like" fashion, like this:
167 *
168 * <pre>
169 * |x----x----x---x|
170 * |xx----x----x--x|
171 * |x-x----x----x-x|
172 * |x--x----x----xx|
173 * | ... |
174 * </pre>
175 *
176 * and then querying at every single timestamp, making sure all, and only,
177 * the expected intervals are returned.
178 */
179 @Test
180 public void testCascadingIntervals() {
181 final int nbAttr = 10;
182 final long duration = 10;
183 final long startTime = 0;
184 final long endTime = 1000;
185
186 List<ITmfStateInterval> intervals = new ArrayList<>();
187 for (long t = startTime + 1; t <= endTime + duration; t++) {
188 intervals.add(new TmfStateInterval(
189 Math.max(startTime, t - duration),
190 Math.min(endTime, t - 1),
191 (int) t % nbAttr,
192 TmfStateValue.newValueLong(t)));
193 }
194
195 buildAndQueryFullRange(startTime, endTime, nbAttr, intervals, false);
196 }
197
198 /**
199 * Test the full query method by filling a small backend with intervals that
200 * take the full time range, like this:
201 *
202 * <pre>
203 * |x-------------x|
204 * |x-------------x|
205 * |x-------------x|
206 * |x-------------x|
207 * | ... |
208 * </pre>
209 *
210 * and then querying at every single timestamp, making sure all, and only,
211 * the expected intervals are returned.
212 */
213 @Test
214 public void testFullIntervals() {
215 final int nbAttr = 1000;
216 final long startTime = 0;
217 final long endTime = 1000;
218
219 List<ITmfStateInterval> intervals = new ArrayList<>();
220 for (int attr = 0; attr < nbAttr; attr++) {
221 intervals.add(new TmfStateInterval(
222 startTime,
223 endTime,
224 attr,
225 TmfStateValue.newValueLong(attr)));
226 }
227
228 buildAndQueryFullRange(startTime, endTime, nbAttr, intervals, false);
229 }
230 }
This page took 0.037586 seconds and 6 git commands to generate.