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