1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.segmentstore
.core
.tests
.treemap
;
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
;
17 import java
.util
.Arrays
;
18 import java
.util
.List
;
20 import org
.eclipse
.tracecompass
.segmentstore
.core
.BasicSegment
;
21 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegment
;
22 import org
.eclipse
.tracecompass
.segmentstore
.core
.treemap
.TreeMapStore
;
23 import org
.junit
.After
;
24 import org
.junit
.Before
;
25 import org
.junit
.Test
;
27 import com
.google
.common
.collect
.ImmutableList
;
28 import com
.google
.common
.collect
.Iterables
;
29 import com
.google
.common
.collect
.Lists
;
32 * Unit tests for intersecting elements in a TreeMapStore
34 * @author France Lapointe Nguyen
36 public class TreeMapStoreTest
{
38 private TreeMapStore
<ISegment
> fSegmentStore
;
40 private static final ISegment SEGMENT_2_6
= new BasicSegment(2, 6);
41 private static final ISegment SEGMENT_4_6
= new BasicSegment(4, 6);
42 private static final ISegment SEGMENT_4_8
= new BasicSegment(4, 8);
43 private static final ISegment SEGMENT_6_8
= new BasicSegment(6, 8);
44 private static final ISegment SEGMENT_10_14
= new BasicSegment(10, 14);
46 private static final List
<ISegment
> SEGMENTS
= ImmutableList
.of(SEGMENT_2_6
, SEGMENT_4_6
, SEGMENT_4_8
, SEGMENT_6_8
, SEGMENT_10_14
);
47 private static final List
<ISegment
> REVERSE_SEGMENTS
= Lists
.reverse(SEGMENTS
);
50 * Initialize data (test vector) that will be tested
54 fSegmentStore
= new TreeMapStore
<>();
55 for (ISegment segment
: SEGMENTS
) {
56 fSegmentStore
.add(checkNotNull(segment
));
61 * Dispose of the segment store
64 public void teardown() {
65 fSegmentStore
.dispose();
69 * Testing method size()
72 public void testSize() {
73 assertEquals(SEGMENTS
.size(), fSegmentStore
.size());
77 * Test the contains() method.
80 public void testContains() {
81 ISegment otherSegment
= new BasicSegment(0, 20);
83 assertTrue(fSegmentStore
.contains(SEGMENT_2_6
));
84 assertTrue(fSegmentStore
.contains(SEGMENT_4_8
));
85 assertFalse(fSegmentStore
.contains(otherSegment
));
89 * Test the toArray() method.
92 public void testToObjectArray() {
93 Object
[] array
= fSegmentStore
.toArray();
95 assertEquals(SEGMENTS
.size(), array
.length
);
96 assertTrue(Arrays
.asList(array
).containsAll(SEGMENTS
));
100 * Test the toArray(T[]) method.
103 public void testToSpecificArray() {
104 ISegment
[] array
= fSegmentStore
.toArray(new ISegment
[0]);
106 assertEquals(SEGMENTS
.size(), array
.length
);
107 assertTrue(Arrays
.asList(array
).containsAll(SEGMENTS
));
111 * Test the toArray(T[]) method with a subtype of ISegment.
114 public void testToSpecifyArraySubtype() {
115 TreeMapStore
<BasicSegment
> tms2
= new TreeMapStore
<>();
116 BasicSegment otherSegment
= new BasicSegment(2, 6);
117 tms2
.add(otherSegment
);
118 BasicSegment
[] array
= tms2
.toArray(new BasicSegment
[0]);
120 assertEquals(1, array
.length
);
121 assertTrue(Arrays
.asList(array
).contains(otherSegment
));
127 * Try adding duplicate elements, they should be ignored
130 public void testNoDuplicateElements() {
131 for (ISegment segment
: SEGMENTS
) {
132 boolean ret
= fSegmentStore
.add(new BasicSegment(segment
.getStart(), segment
.getEnd()));
135 assertEquals(SEGMENTS
.size(), fSegmentStore
.size());
139 * Test the iteration order of the complete segment store.
142 public void testIterationOrder() {
144 for (ISegment segment
: fSegmentStore
) {
145 assertEquals(SEGMENTS
.get(i
++), segment
);
150 * Test the iteration order when the elements are not inserted in sorted
154 public void testIterationOrderNonSortedInsertion() {
155 /* Prepare the segment store, we don't use the 'fixture' in this test */
156 TreeMapStore
<ISegment
> store
= new TreeMapStore
<>();
157 for (ISegment segment
: REVERSE_SEGMENTS
) {
158 store
.add(checkNotNull(segment
));
162 * Test each element one by one, the iteration order should follow the
163 * start times, not the insertion order.
166 for (ISegment segment
: store
) {
167 assertEquals(SEGMENTS
.get(i
++), segment
);
170 /* Manually dispose our own store */
175 * Testing method getIntersectingElements(long start, long end)
178 public void testGetIntersectingElementsRange() {
180 Iterable
<ISegment
> intersectingElements
;
183 * Range that does not include any segment
185 intersectingElements
= fSegmentStore
.getIntersectingElements(16, 20);
186 assertEquals(0, Iterables
.size(intersectingElements
));
189 * Range start time : Before first segment start time
190 * Range end time : After last segment end time
192 intersectingElements
= fSegmentStore
.getIntersectingElements(1, 15);
193 assertEquals(5, Iterables
.size(intersectingElements
));
196 * Range start time : On first segment start time
197 * Range end time : On last segment end time
199 intersectingElements
= fSegmentStore
.getIntersectingElements(2, 14);
200 assertEquals(5, Iterables
.size(intersectingElements
));
203 * Range start time : After one segment start time
204 * Range end time : Before one segment end time
206 intersectingElements
= fSegmentStore
.getIntersectingElements(11, 13);
207 assertEquals(1, Iterables
.size(intersectingElements
));
208 assertEquals(SEGMENT_10_14
, Iterables
.getOnlyElement(intersectingElements
));
211 * Range start time : On one segment start time
212 * Range end time : On one segment end time
214 intersectingElements
= fSegmentStore
.getIntersectingElements(10, 14);
215 assertEquals(1, Iterables
.size(intersectingElements
));
216 assertEquals(SEGMENT_10_14
, Iterables
.getOnlyElement(intersectingElements
));
219 * Range start time : On last segment end time
220 * Range end time : After last segment end time
222 intersectingElements
= fSegmentStore
.getIntersectingElements(14, 18);
223 assertEquals(1, Iterables
.size(intersectingElements
));
224 assertEquals(SEGMENT_10_14
, Iterables
.getOnlyElement(intersectingElements
));
227 * Range start time : Before first segment start time
228 * Range end time : On first segment start time
230 intersectingElements
= fSegmentStore
.getIntersectingElements(1, 2);
231 assertEquals(1, Iterables
.size(intersectingElements
));
232 assertEquals(SEGMENT_2_6
, Iterables
.getOnlyElement(intersectingElements
));
236 * Testing method getIntersectingElements(long start, long end)
239 public void testGetIntersectingElementsTime() {
241 Iterable
<ISegment
> intersectingElements
;
244 * Time between segment start time and end time
246 intersectingElements
= fSegmentStore
.getIntersectingElements(3);
247 assertEquals(1, Iterables
.size(intersectingElements
));
248 assertEquals(SEGMENT_2_6
, Iterables
.getOnlyElement(intersectingElements
));
251 * Time on segment start time
253 intersectingElements
= fSegmentStore
.getIntersectingElements(2);
254 assertEquals(1, Iterables
.size(intersectingElements
));
255 assertEquals(SEGMENT_2_6
, Iterables
.getOnlyElement(intersectingElements
));
258 * Time on segment end time
260 intersectingElements
= fSegmentStore
.getIntersectingElements(14);
261 assertEquals(1, Iterables
.size(intersectingElements
));
262 assertEquals(SEGMENT_10_14
, Iterables
.getOnlyElement(intersectingElements
));
265 * Time overlapping many segments
267 intersectingElements
= fSegmentStore
.getIntersectingElements(6);
268 assertEquals(4, Iterables
.size(intersectingElements
));
271 * Time between segments
273 intersectingElements
= fSegmentStore
.getIntersectingElements(9);
274 assertEquals(0, Iterables
.size(intersectingElements
));
277 * Time before all segment start time
279 intersectingElements
= fSegmentStore
.getIntersectingElements(1);
280 assertEquals(0, Iterables
.size(intersectingElements
));
283 * Time after all segment end time
285 intersectingElements
= fSegmentStore
.getIntersectingElements(15);
286 assertEquals(0, Iterables
.size(intersectingElements
));
290 * Testing method getIntersectingElements(long start, long end)
293 public void testDispose() {
294 TreeMapStore
<ISegment
> store
= new TreeMapStore
<>();
295 store
.add(SEGMENT_2_6
);
297 assertEquals(0, store
.size());