Commit | Line | Data |
---|---|---|
aaa6f547 MK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 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 | ||
10 | package org.eclipse.tracecompass.analysis.timing.core.tests.store; | |
11 | ||
12 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; | |
13 | import static org.junit.Assert.assertEquals; | |
14 | import static org.junit.Assert.assertFalse; | |
15 | import static org.junit.Assert.assertTrue; | |
16 | ||
581e0f63 | 17 | import java.util.ArrayList; |
aaa6f547 | 18 | import java.util.Arrays; |
581e0f63 MK |
19 | import java.util.Collection; |
20 | import java.util.Iterator; | |
aaa6f547 MK |
21 | import java.util.List; |
22 | ||
23 | import org.eclipse.jdt.annotation.NonNull; | |
24 | import org.eclipse.tracecompass.segmentstore.core.BasicSegment; | |
25 | import org.eclipse.tracecompass.segmentstore.core.ISegment; | |
26 | import org.eclipse.tracecompass.segmentstore.core.ISegmentStore; | |
27 | import org.junit.After; | |
28 | import org.junit.Before; | |
29 | import org.junit.Test; | |
30 | ||
31 | import com.google.common.collect.ImmutableList; | |
32 | import com.google.common.collect.Iterables; | |
33 | import com.google.common.collect.Lists; | |
34 | ||
35 | /** | |
36 | * Unit tests for intersecting elements in an SegmentStore | |
37 | * | |
38 | * Originally the TreeMapStoreTest, copied for this internal implementation. The | |
39 | * test was barely changed as it tests the interface and not the internals. | |
40 | * | |
41 | * @author Matthew Khouzam | |
42 | */ | |
43 | public abstract class AbstractTestSegmentStore { | |
44 | ||
45 | private ISegmentStore<@NonNull ISegment> fSegmentStore; | |
46 | ||
47 | /** | |
48 | * Get the segment store to test | |
49 | * | |
50 | * @return the segment store | |
51 | */ | |
52 | protected abstract ISegmentStore<@NonNull ISegment> getSegmentStore(); | |
53 | ||
54 | private static final @NonNull ISegment SEGMENT_2_6 = new BasicSegment(2, 6); | |
55 | private static final @NonNull ISegment SEGMENT_4_6 = new BasicSegment(4, 6); | |
56 | private static final @NonNull ISegment SEGMENT_4_8 = new BasicSegment(4, 8); | |
57 | private static final @NonNull ISegment SEGMENT_6_8 = new BasicSegment(6, 8); | |
58 | private static final @NonNull ISegment SEGMENT_10_14 = new BasicSegment(10, 14); | |
59 | private static final @NonNull List<@NonNull ISegment> SEGMENTS = ImmutableList.of(SEGMENT_2_6, SEGMENT_4_6, SEGMENT_4_8, SEGMENT_6_8, SEGMENT_10_14); | |
60 | private static final List<@NonNull ISegment> REVERSE_SEGMENTS = Lists.reverse(SEGMENTS); | |
61 | ||
62 | /** | |
63 | * Constructor | |
64 | */ | |
65 | public AbstractTestSegmentStore() { | |
66 | super(); | |
67 | } | |
68 | ||
69 | /** | |
70 | * Initialize data (test vector) that will be tested | |
71 | */ | |
72 | @Before | |
73 | public void setup() { | |
74 | fSegmentStore = getSegmentStore(); | |
75 | for (ISegment segment : SEGMENTS) { | |
76 | fSegmentStore.add(segment); | |
77 | } | |
78 | } | |
79 | ||
80 | /** | |
81 | * Dispose of the segment store | |
82 | */ | |
83 | @After | |
84 | public void teardown() { | |
85 | fSegmentStore.dispose(); | |
86 | } | |
87 | ||
88 | /** | |
89 | * Testing method size() | |
90 | */ | |
91 | @Test | |
92 | public void testSize() { | |
93 | assertEquals(SEGMENTS.size(), fSegmentStore.size()); | |
94 | } | |
95 | ||
96 | /** | |
97 | * Test the contains() method. | |
98 | */ | |
99 | @Test | |
100 | public void testContains() { | |
101 | ISegment otherSegment = new BasicSegment(0, 20); | |
102 | ||
103 | assertTrue(fSegmentStore.contains(SEGMENT_2_6)); | |
104 | assertTrue(fSegmentStore.contains(SEGMENT_4_8)); | |
105 | assertFalse(fSegmentStore.contains(otherSegment)); | |
106 | } | |
107 | ||
108 | /** | |
109 | * Test the toArray() method. | |
110 | */ | |
111 | @Test | |
112 | public void testToObjectArray() { | |
113 | Object[] array = fSegmentStore.toArray(); | |
114 | ||
115 | assertEquals(SEGMENTS.size(), array.length); | |
116 | assertTrue(Arrays.asList(array).containsAll(SEGMENTS)); | |
117 | } | |
118 | ||
119 | /** | |
120 | * Test the toArray(T[]) method. | |
121 | */ | |
122 | @Test | |
123 | public void testToSpecificArray() { | |
124 | ISegment[] array = fSegmentStore.toArray(new ISegment[0]); | |
125 | ||
126 | assertEquals(SEGMENTS.size(), array.length); | |
127 | assertTrue(Arrays.asList(array).containsAll(SEGMENTS)); | |
128 | } | |
129 | ||
130 | /** | |
131 | * Test the toArray(T[]) method with a subtype of ISegment. | |
132 | */ | |
133 | @Test | |
134 | public void testToSpecifyArraySubtype() { | |
135 | ISegmentStore<@NonNull ISegment> tms2 = getSegmentStore(); | |
136 | BasicSegment otherSegment = new BasicSegment(2, 6); | |
137 | tms2.add(otherSegment); | |
138 | BasicSegment[] array = tms2.toArray(new BasicSegment[0]); | |
139 | ||
140 | assertEquals(1, array.length); | |
141 | assertTrue(Arrays.asList(array).contains(otherSegment)); | |
142 | ||
143 | tms2.dispose(); | |
144 | } | |
145 | ||
146 | /** | |
147 | * Test the iteration order of the complete segment store. | |
148 | */ | |
149 | @Test | |
150 | public void testIterationOrder() { | |
151 | int i = 0; | |
152 | for (ISegment segment : fSegmentStore) { | |
153 | assertEquals(SEGMENTS.get(i++), segment); | |
154 | } | |
155 | } | |
156 | ||
157 | /** | |
158 | * Test the iteration order when the elements are not inserted in sorted | |
159 | * order. | |
160 | */ | |
161 | @Test | |
162 | public void testIterationOrderNonSortedInsertion() { | |
163 | /* Prepare the segment store, we don't use the 'fixture' in this test */ | |
164 | ISegmentStore<@NonNull ISegment> store = getSegmentStore(); | |
165 | for (ISegment segment : REVERSE_SEGMENTS) { | |
166 | store.add(checkNotNull(segment)); | |
167 | } | |
168 | ||
169 | /* | |
170 | * Test each element one by one, the iteration order should follow the | |
171 | * start times, not the insertion order. | |
172 | */ | |
173 | int i = 0; | |
174 | for (ISegment segment : store) { | |
175 | assertEquals(SEGMENTS.get(i++), segment); | |
176 | } | |
177 | ||
178 | /* Manually dispose our own store */ | |
179 | store.dispose(); | |
180 | } | |
181 | ||
182 | /** | |
183 | * Testing method | |
184 | * {@link ISegmentStore#getIntersectingElements(long start, long end)} | |
185 | */ | |
186 | @Test | |
187 | public void testGetIntersectingElementsRange() { | |
188 | ||
189 | Iterable<ISegment> intersectingElements; | |
190 | ||
191 | /* | |
192 | * Range that does not include any segment | |
193 | */ | |
194 | intersectingElements = fSegmentStore.getIntersectingElements(16, 20); | |
195 | assertEquals(0, Iterables.size(intersectingElements)); | |
196 | ||
197 | /* | |
198 | * Range start time : Before first segment start time Range end time : | |
199 | * After last segment end time | |
200 | */ | |
201 | intersectingElements = fSegmentStore.getIntersectingElements(1, 15); | |
202 | assertEquals(5, Iterables.size(intersectingElements)); | |
203 | ||
204 | /* | |
205 | * Range start time : On first segment start time Range end time : On | |
206 | * last segment end time | |
207 | */ | |
208 | intersectingElements = fSegmentStore.getIntersectingElements(2, 14); | |
209 | assertEquals(5, Iterables.size(intersectingElements)); | |
210 | ||
211 | /* | |
212 | * Range start time : After one segment start time Range end time : | |
213 | * Before one segment end time | |
214 | */ | |
215 | intersectingElements = fSegmentStore.getIntersectingElements(11, 13); | |
216 | assertEquals(1, Iterables.size(intersectingElements)); | |
217 | assertEquals(SEGMENT_10_14, Iterables.getOnlyElement(intersectingElements)); | |
218 | ||
219 | /* | |
220 | * Range start time : On one segment start time Range end time : On one | |
221 | * segment end time | |
222 | */ | |
223 | intersectingElements = fSegmentStore.getIntersectingElements(10, 14); | |
224 | assertEquals(1, Iterables.size(intersectingElements)); | |
225 | assertEquals(SEGMENT_10_14, Iterables.getOnlyElement(intersectingElements)); | |
226 | ||
227 | /* | |
228 | * Range start time : On last segment end time Range end time : After | |
229 | * last segment end time | |
230 | */ | |
231 | intersectingElements = fSegmentStore.getIntersectingElements(14, 18); | |
232 | assertEquals(1, Iterables.size(intersectingElements)); | |
233 | assertEquals(SEGMENT_10_14, Iterables.getOnlyElement(intersectingElements)); | |
234 | ||
235 | /* | |
236 | * Range start time : Before first segment start time Range end time : | |
237 | * On first segment start time | |
238 | */ | |
239 | intersectingElements = fSegmentStore.getIntersectingElements(1, 2); | |
240 | assertEquals(1, Iterables.size(intersectingElements)); | |
241 | assertEquals(SEGMENT_2_6, Iterables.getOnlyElement(intersectingElements)); | |
242 | } | |
243 | ||
244 | /** | |
245 | * Testing method {@link ISegmentStore#getIntersectingElements(long time)} | |
246 | */ | |
247 | @Test | |
248 | public void testGetIntersectingElementsTime() { | |
249 | ||
250 | Iterable<ISegment> intersectingElements; | |
251 | ||
252 | /* | |
253 | * Time between segment start time and end time | |
254 | */ | |
255 | intersectingElements = fSegmentStore.getIntersectingElements(3); | |
256 | assertEquals(1, Iterables.size(intersectingElements)); | |
257 | assertEquals(SEGMENT_2_6, Iterables.getOnlyElement(intersectingElements)); | |
258 | ||
259 | /* | |
260 | * Time on segment start time | |
261 | */ | |
262 | intersectingElements = fSegmentStore.getIntersectingElements(2); | |
263 | assertEquals(1, Iterables.size(intersectingElements)); | |
264 | assertEquals(SEGMENT_2_6, Iterables.getOnlyElement(intersectingElements)); | |
265 | ||
266 | /* | |
267 | * Time on segment end time | |
268 | */ | |
269 | intersectingElements = fSegmentStore.getIntersectingElements(14); | |
270 | assertEquals(1, Iterables.size(intersectingElements)); | |
271 | assertEquals(SEGMENT_10_14, Iterables.getOnlyElement(intersectingElements)); | |
272 | ||
273 | /* | |
274 | * Time overlapping many segments | |
275 | */ | |
276 | intersectingElements = fSegmentStore.getIntersectingElements(6); | |
277 | assertEquals(4, Iterables.size(intersectingElements)); | |
278 | ||
279 | /* | |
280 | * Time between segments | |
281 | */ | |
282 | intersectingElements = fSegmentStore.getIntersectingElements(9); | |
283 | assertEquals(0, Iterables.size(intersectingElements)); | |
284 | ||
285 | /* | |
286 | * Time before all segment start time | |
287 | */ | |
288 | intersectingElements = fSegmentStore.getIntersectingElements(1); | |
289 | assertEquals(0, Iterables.size(intersectingElements)); | |
290 | ||
291 | /* | |
292 | * Time after all segment end time | |
293 | */ | |
294 | intersectingElements = fSegmentStore.getIntersectingElements(15); | |
295 | assertEquals(0, Iterables.size(intersectingElements)); | |
296 | } | |
297 | ||
298 | /** | |
299 | * Testing method {@link ISegmentStore#dispose()} | |
300 | */ | |
301 | @Test | |
302 | public void testDispose() { | |
303 | ISegmentStore<@NonNull ISegment> store = getSegmentStore(); | |
304 | store.add(SEGMENT_2_6); | |
305 | store.dispose(); | |
306 | assertEquals(0, store.size()); | |
307 | } | |
308 | ||
581e0f63 MK |
309 | /** |
310 | * Test iterating over a store being built. | |
311 | * | |
312 | * bug 500607 | |
313 | */ | |
314 | @Test | |
315 | public void testIterator() { | |
316 | Collection<@NonNull ISegment> beforeExpected = ImmutableList.of(SEGMENT_2_6); | |
317 | Collection<@NonNull ISegment> afterExpected = ImmutableList.of(SEGMENT_2_6, SEGMENT_4_8); | |
318 | Collection<@NonNull ISegment> lastExpected = ImmutableList.of(SEGMENT_2_6, SEGMENT_4_8, SEGMENT_6_8); | |
319 | Collection<@NonNull ISegment> fixture = new ArrayList<>(); | |
320 | ISegmentStore<@NonNull ISegment> store = getSegmentStore(); | |
321 | ||
322 | // Add one segment to the segment store and iterate | |
323 | store.add(SEGMENT_2_6); | |
324 | for (ISegment item : store) { | |
325 | fixture.add(item); | |
326 | } | |
327 | assertEquals(beforeExpected, fixture); | |
328 | ||
329 | // Add a second segment to the store and iterate | |
330 | fixture.clear(); | |
331 | store.add(SEGMENT_4_8); | |
332 | for (ISegment item : store) { | |
333 | fixture.add(item); | |
334 | } | |
335 | assertEquals(afterExpected, fixture); | |
336 | ||
337 | fixture.clear(); | |
338 | // Take an iterator | |
339 | Iterator<@NonNull ISegment> iter = store.iterator(); | |
340 | ||
341 | // Add a third segment to the store and iterate | |
342 | store.add(SEGMENT_6_8); | |
343 | Iterator<@NonNull ISegment> iter2 = store.iterator(); | |
344 | fixture.clear(); | |
345 | ||
346 | // Make sure the first iterator take has only 2 elements and the second | |
347 | // has 3 elements | |
348 | while (iter.hasNext()) { | |
349 | fixture.add(iter.next()); | |
350 | } | |
351 | assertEquals(afterExpected, fixture); | |
352 | fixture.clear(); | |
353 | while (iter2.hasNext()) { | |
354 | fixture.add(iter2.next()); | |
355 | } | |
356 | assertEquals(lastExpected, fixture); | |
357 | } | |
358 | ||
aaa6f547 | 359 | } |