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
.jdt
.annotation
.NonNull
;
21 import org
.eclipse
.tracecompass
.segmentstore
.core
.BasicSegment
;
22 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegment
;
23 import org
.eclipse
.tracecompass
.segmentstore
.core
.treemap
.TreeMapStore
;
24 import org
.junit
.After
;
25 import org
.junit
.Before
;
26 import org
.junit
.Test
;
28 import com
.google
.common
.collect
.ImmutableList
;
29 import com
.google
.common
.collect
.Iterables
;
30 import com
.google
.common
.collect
.Lists
;
33 * Unit tests for intersecting elements in a TreeMapStore
35 * @author France Lapointe Nguyen
37 public class TreeMapStoreTest
{
39 private TreeMapStore
<@NonNull ISegment
> fSegmentStore
;
41 private static final @NonNull ISegment SEGMENT_2_6
= new BasicSegment(2, 6);
42 private static final @NonNull ISegment SEGMENT_4_6
= new BasicSegment(4, 6);
43 private static final @NonNull ISegment SEGMENT_4_8
= new BasicSegment(4, 8);
44 private static final @NonNull ISegment SEGMENT_6_8
= new BasicSegment(6, 8);
45 private static final @NonNull ISegment SEGMENT_10_14
= new BasicSegment(10, 14);
47 private static final List
<ISegment
> SEGMENTS
= ImmutableList
.of(SEGMENT_2_6
, SEGMENT_4_6
, SEGMENT_4_8
, SEGMENT_6_8
, SEGMENT_10_14
);
48 private static final List
<ISegment
> REVERSE_SEGMENTS
= Lists
.reverse(SEGMENTS
);
51 * Initialize data (test vector) that will be tested
55 fSegmentStore
= new TreeMapStore
<>();
56 for (ISegment segment
: SEGMENTS
) {
57 fSegmentStore
.add(checkNotNull(segment
));
62 * Dispose of the segment store
65 public void teardown() {
66 fSegmentStore
.dispose();
70 * Testing method size()
73 public void testSize() {
74 assertEquals(SEGMENTS
.size(), fSegmentStore
.size());
78 * Test the contains() method.
81 public void testContains() {
82 ISegment otherSegment
= new BasicSegment(0, 20);
84 assertTrue(fSegmentStore
.contains(SEGMENT_2_6
));
85 assertTrue(fSegmentStore
.contains(SEGMENT_4_8
));
86 assertFalse(fSegmentStore
.contains(otherSegment
));
90 * Test the toArray() method.
93 public void testToObjectArray() {
94 Object
[] array
= fSegmentStore
.toArray();
96 assertEquals(SEGMENTS
.size(), array
.length
);
97 assertTrue(Arrays
.asList(array
).containsAll(SEGMENTS
));
101 * Test the toArray(T[]) method.
104 public void testToSpecificArray() {
105 ISegment
[] array
= fSegmentStore
.toArray(new ISegment
[0]);
107 assertEquals(SEGMENTS
.size(), array
.length
);
108 assertTrue(Arrays
.asList(array
).containsAll(SEGMENTS
));
112 * Test the toArray(T[]) method with a subtype of ISegment.
115 public void testToSpecifyArraySubtype() {
116 TreeMapStore
<@NonNull BasicSegment
> tms2
= new TreeMapStore
<>();
117 BasicSegment otherSegment
= new BasicSegment(2, 6);
118 tms2
.add(otherSegment
);
119 BasicSegment
[] array
= tms2
.toArray(new BasicSegment
[0]);
121 assertEquals(1, array
.length
);
122 assertTrue(Arrays
.asList(array
).contains(otherSegment
));
128 * Try adding duplicate elements, they should be ignored
131 public void testNoDuplicateElements() {
132 for (ISegment segment
: SEGMENTS
) {
133 boolean ret
= fSegmentStore
.add(new BasicSegment(segment
.getStart(), segment
.getEnd()));
136 assertEquals(SEGMENTS
.size(), fSegmentStore
.size());
140 * Test the iteration order of the complete segment store.
143 public void testIterationOrder() {
145 for (ISegment segment
: fSegmentStore
) {
146 assertEquals(SEGMENTS
.get(i
++), segment
);
151 * Test the iteration order when the elements are not inserted in sorted
155 public void testIterationOrderNonSortedInsertion() {
156 /* Prepare the segment store, we don't use the 'fixture' in this test */
157 TreeMapStore
<@NonNull ISegment
> store
= new TreeMapStore
<>();
158 for (ISegment segment
: REVERSE_SEGMENTS
) {
159 store
.add(checkNotNull(segment
));
163 * Test each element one by one, the iteration order should follow the
164 * start times, not the insertion order.
167 for (ISegment segment
: store
) {
168 assertEquals(SEGMENTS
.get(i
++), segment
);
171 /* Manually dispose our own store */
176 * Testing method getIntersectingElements(long start, long end)
179 public void testGetIntersectingElementsRange() {
181 Iterable
<ISegment
> intersectingElements
;
184 * Range that does not include any segment
186 intersectingElements
= fSegmentStore
.getIntersectingElements(16, 20);
187 assertEquals(0, Iterables
.size(intersectingElements
));
190 * Range start time : Before first segment start time
191 * Range end time : After last segment end time
193 intersectingElements
= fSegmentStore
.getIntersectingElements(1, 15);
194 assertEquals(5, Iterables
.size(intersectingElements
));
197 * Range start time : On first segment start time
198 * Range end time : On last segment end time
200 intersectingElements
= fSegmentStore
.getIntersectingElements(2, 14);
201 assertEquals(5, Iterables
.size(intersectingElements
));
204 * Range start time : After one segment start time
205 * Range end time : Before one segment end time
207 intersectingElements
= fSegmentStore
.getIntersectingElements(11, 13);
208 assertEquals(1, Iterables
.size(intersectingElements
));
209 assertEquals(SEGMENT_10_14
, Iterables
.getOnlyElement(intersectingElements
));
212 * Range start time : On one segment start time
213 * Range end time : On one segment end time
215 intersectingElements
= fSegmentStore
.getIntersectingElements(10, 14);
216 assertEquals(1, Iterables
.size(intersectingElements
));
217 assertEquals(SEGMENT_10_14
, Iterables
.getOnlyElement(intersectingElements
));
220 * Range start time : On last segment end time
221 * Range end time : After last segment end time
223 intersectingElements
= fSegmentStore
.getIntersectingElements(14, 18);
224 assertEquals(1, Iterables
.size(intersectingElements
));
225 assertEquals(SEGMENT_10_14
, Iterables
.getOnlyElement(intersectingElements
));
228 * Range start time : Before first segment start time
229 * Range end time : On first segment start time
231 intersectingElements
= fSegmentStore
.getIntersectingElements(1, 2);
232 assertEquals(1, Iterables
.size(intersectingElements
));
233 assertEquals(SEGMENT_2_6
, Iterables
.getOnlyElement(intersectingElements
));
237 * Testing method getIntersectingElements(long start, long end)
240 public void testGetIntersectingElementsTime() {
242 Iterable
<ISegment
> intersectingElements
;
245 * Time between segment start time and end time
247 intersectingElements
= fSegmentStore
.getIntersectingElements(3);
248 assertEquals(1, Iterables
.size(intersectingElements
));
249 assertEquals(SEGMENT_2_6
, Iterables
.getOnlyElement(intersectingElements
));
252 * Time on segment start time
254 intersectingElements
= fSegmentStore
.getIntersectingElements(2);
255 assertEquals(1, Iterables
.size(intersectingElements
));
256 assertEquals(SEGMENT_2_6
, Iterables
.getOnlyElement(intersectingElements
));
259 * Time on segment end time
261 intersectingElements
= fSegmentStore
.getIntersectingElements(14);
262 assertEquals(1, Iterables
.size(intersectingElements
));
263 assertEquals(SEGMENT_10_14
, Iterables
.getOnlyElement(intersectingElements
));
266 * Time overlapping many segments
268 intersectingElements
= fSegmentStore
.getIntersectingElements(6);
269 assertEquals(4, Iterables
.size(intersectingElements
));
272 * Time between segments
274 intersectingElements
= fSegmentStore
.getIntersectingElements(9);
275 assertEquals(0, Iterables
.size(intersectingElements
));
278 * Time before all segment start time
280 intersectingElements
= fSegmentStore
.getIntersectingElements(1);
281 assertEquals(0, Iterables
.size(intersectingElements
));
284 * Time after all segment end time
286 intersectingElements
= fSegmentStore
.getIntersectingElements(15);
287 assertEquals(0, Iterables
.size(intersectingElements
));
291 * Testing method getIntersectingElements(long start, long end)
294 public void testDispose() {
295 TreeMapStore
<@NonNull ISegment
> store
= new TreeMapStore
<>();
296 store
.add(SEGMENT_2_6
);
298 assertEquals(0, store
.size());