Commit | Line | Data |
---|---|---|
9448be7c FLN |
1 | /******************************************************************************* |
2 | * Copyright (c) 2015 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.segmentstore.core.tests.treemap; | |
11 | ||
e5083481 | 12 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
9448be7c | 13 | import static org.junit.Assert.assertEquals; |
1a9cb076 AM |
14 | import static org.junit.Assert.assertFalse; |
15 | import static org.junit.Assert.assertTrue; | |
9448be7c | 16 | |
1a9cb076 | 17 | import java.util.Arrays; |
9448be7c FLN |
18 | import java.util.List; |
19 | ||
4c4e2816 | 20 | import org.eclipse.jdt.annotation.NonNull; |
9448be7c FLN |
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; | |
27 | ||
28 | import com.google.common.collect.ImmutableList; | |
29 | import com.google.common.collect.Iterables; | |
e5083481 | 30 | import com.google.common.collect.Lists; |
9448be7c FLN |
31 | |
32 | /** | |
33 | * Unit tests for intersecting elements in a TreeMapStore | |
34 | * | |
35 | * @author France Lapointe Nguyen | |
36 | */ | |
37 | public class TreeMapStoreTest { | |
38 | ||
4c4e2816 | 39 | private TreeMapStore<@NonNull ISegment> fSegmentStore; |
9448be7c | 40 | |
66838307 MAL |
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); | |
9448be7c | 46 | |
e5083481 PT |
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); | |
9448be7c FLN |
49 | |
50 | /** | |
51 | * Initialize data (test vector) that will be tested | |
52 | */ | |
53 | @Before | |
54 | public void setup() { | |
55 | fSegmentStore = new TreeMapStore<>(); | |
e5083481 | 56 | for (ISegment segment : SEGMENTS) { |
1a9cb076 | 57 | fSegmentStore.add(checkNotNull(segment)); |
9448be7c FLN |
58 | } |
59 | } | |
60 | ||
61 | /** | |
62 | * Dispose of the segment store | |
63 | */ | |
64 | @After | |
65 | public void teardown() { | |
66 | fSegmentStore.dispose(); | |
67 | } | |
68 | ||
69 | /** | |
1a9cb076 | 70 | * Testing method size() |
9448be7c FLN |
71 | */ |
72 | @Test | |
1a9cb076 AM |
73 | public void testSize() { |
74 | assertEquals(SEGMENTS.size(), fSegmentStore.size()); | |
75 | } | |
76 | ||
77 | /** | |
78 | * Test the contains() method. | |
79 | */ | |
80 | @Test | |
81 | public void testContains() { | |
82 | ISegment otherSegment = new BasicSegment(0, 20); | |
83 | ||
84 | assertTrue(fSegmentStore.contains(SEGMENT_2_6)); | |
85 | assertTrue(fSegmentStore.contains(SEGMENT_4_8)); | |
86 | assertFalse(fSegmentStore.contains(otherSegment)); | |
87 | } | |
88 | ||
89 | /** | |
90 | * Test the toArray() method. | |
91 | */ | |
92 | @Test | |
93 | public void testToObjectArray() { | |
94 | Object[] array = fSegmentStore.toArray(); | |
95 | ||
96 | assertEquals(SEGMENTS.size(), array.length); | |
97 | assertTrue(Arrays.asList(array).containsAll(SEGMENTS)); | |
98 | } | |
99 | ||
100 | /** | |
101 | * Test the toArray(T[]) method. | |
102 | */ | |
103 | @Test | |
104 | public void testToSpecificArray() { | |
105 | ISegment[] array = fSegmentStore.toArray(new ISegment[0]); | |
106 | ||
107 | assertEquals(SEGMENTS.size(), array.length); | |
108 | assertTrue(Arrays.asList(array).containsAll(SEGMENTS)); | |
109 | } | |
110 | ||
111 | /** | |
112 | * Test the toArray(T[]) method with a subtype of ISegment. | |
113 | */ | |
114 | @Test | |
115 | public void testToSpecifyArraySubtype() { | |
4c4e2816 | 116 | TreeMapStore<@NonNull BasicSegment> tms2 = new TreeMapStore<>(); |
1a9cb076 AM |
117 | BasicSegment otherSegment = new BasicSegment(2, 6); |
118 | tms2.add(otherSegment); | |
119 | BasicSegment[] array = tms2.toArray(new BasicSegment[0]); | |
120 | ||
121 | assertEquals(1, array.length); | |
122 | assertTrue(Arrays.asList(array).contains(otherSegment)); | |
123 | ||
124 | tms2.dispose(); | |
9448be7c FLN |
125 | } |
126 | ||
127 | /** | |
e5083481 | 128 | * Try adding duplicate elements, they should be ignored |
9448be7c FLN |
129 | */ |
130 | @Test | |
e5083481 PT |
131 | public void testNoDuplicateElements() { |
132 | for (ISegment segment : SEGMENTS) { | |
8b246d45 AM |
133 | boolean ret = fSegmentStore.add(new BasicSegment(segment.getStart(), segment.getEnd())); |
134 | assertFalse(ret); | |
e5083481 | 135 | } |
1a9cb076 | 136 | assertEquals(SEGMENTS.size(), fSegmentStore.size()); |
e5083481 PT |
137 | } |
138 | ||
139 | /** | |
140 | * Test the iteration order of the complete segment store. | |
141 | */ | |
142 | @Test | |
143 | public void testIterationOrder() { | |
144 | int i = 0; | |
145 | for (ISegment segment : fSegmentStore) { | |
146 | assertEquals(SEGMENTS.get(i++), segment); | |
9448be7c FLN |
147 | } |
148 | } | |
149 | ||
e5083481 PT |
150 | /** |
151 | * Test the iteration order when the elements are not inserted in sorted | |
152 | * order. | |
153 | */ | |
154 | @Test | |
155 | public void testIterationOrderNonSortedInsertion() { | |
156 | /* Prepare the segment store, we don't use the 'fixture' in this test */ | |
4c4e2816 | 157 | TreeMapStore<@NonNull ISegment> store = new TreeMapStore<>(); |
e5083481 | 158 | for (ISegment segment : REVERSE_SEGMENTS) { |
1a9cb076 | 159 | store.add(checkNotNull(segment)); |
e5083481 PT |
160 | } |
161 | ||
162 | /* | |
163 | * Test each element one by one, the iteration order should follow the | |
164 | * start times, not the insertion order. | |
165 | */ | |
166 | int i = 0; | |
167 | for (ISegment segment : store) { | |
168 | assertEquals(SEGMENTS.get(i++), segment); | |
169 | } | |
170 | ||
171 | /* Manually dispose our own store */ | |
172 | store.dispose(); | |
173 | } | |
174 | ||
9448be7c FLN |
175 | /** |
176 | * Testing method getIntersectingElements(long start, long end) | |
177 | */ | |
178 | @Test | |
179 | public void testGetIntersectingElementsRange() { | |
180 | ||
181 | Iterable<ISegment> intersectingElements; | |
182 | ||
183 | /* | |
184 | * Range that does not include any segment | |
185 | */ | |
186 | intersectingElements = fSegmentStore.getIntersectingElements(16, 20); | |
187 | assertEquals(0, Iterables.size(intersectingElements)); | |
188 | ||
189 | /* | |
e5083481 PT |
190 | * Range start time : Before first segment start time |
191 | * Range end time : After last segment end time | |
9448be7c FLN |
192 | */ |
193 | intersectingElements = fSegmentStore.getIntersectingElements(1, 15); | |
e5083481 | 194 | assertEquals(5, Iterables.size(intersectingElements)); |
9448be7c FLN |
195 | |
196 | /* | |
e5083481 PT |
197 | * Range start time : On first segment start time |
198 | * Range end time : On last segment end time | |
9448be7c FLN |
199 | */ |
200 | intersectingElements = fSegmentStore.getIntersectingElements(2, 14); | |
e5083481 | 201 | assertEquals(5, Iterables.size(intersectingElements)); |
9448be7c FLN |
202 | |
203 | /* | |
e5083481 PT |
204 | * Range start time : After one segment start time |
205 | * Range end time : Before one segment end time | |
9448be7c FLN |
206 | */ |
207 | intersectingElements = fSegmentStore.getIntersectingElements(11, 13); | |
208 | assertEquals(1, Iterables.size(intersectingElements)); | |
e5083481 | 209 | assertEquals(SEGMENT_10_14, Iterables.getOnlyElement(intersectingElements)); |
9448be7c FLN |
210 | |
211 | /* | |
e5083481 PT |
212 | * Range start time : On one segment start time |
213 | * Range end time : On one segment end time | |
9448be7c | 214 | */ |
e5083481 | 215 | intersectingElements = fSegmentStore.getIntersectingElements(10, 14); |
9448be7c | 216 | assertEquals(1, Iterables.size(intersectingElements)); |
e5083481 | 217 | assertEquals(SEGMENT_10_14, Iterables.getOnlyElement(intersectingElements)); |
9448be7c FLN |
218 | |
219 | /* | |
e5083481 PT |
220 | * Range start time : On last segment end time |
221 | * Range end time : After last segment end time | |
9448be7c FLN |
222 | */ |
223 | intersectingElements = fSegmentStore.getIntersectingElements(14, 18); | |
224 | assertEquals(1, Iterables.size(intersectingElements)); | |
e5083481 | 225 | assertEquals(SEGMENT_10_14, Iterables.getOnlyElement(intersectingElements)); |
9448be7c FLN |
226 | |
227 | /* | |
e5083481 PT |
228 | * Range start time : Before first segment start time |
229 | * Range end time : On first segment start time | |
9448be7c FLN |
230 | */ |
231 | intersectingElements = fSegmentStore.getIntersectingElements(1, 2); | |
232 | assertEquals(1, Iterables.size(intersectingElements)); | |
e5083481 | 233 | assertEquals(SEGMENT_2_6, Iterables.getOnlyElement(intersectingElements)); |
9448be7c FLN |
234 | } |
235 | ||
236 | /** | |
237 | * Testing method getIntersectingElements(long start, long end) | |
238 | */ | |
239 | @Test | |
240 | public void testGetIntersectingElementsTime() { | |
241 | ||
242 | Iterable<ISegment> intersectingElements; | |
243 | ||
244 | /* | |
245 | * Time between segment start time and end time | |
246 | */ | |
247 | intersectingElements = fSegmentStore.getIntersectingElements(3); | |
248 | assertEquals(1, Iterables.size(intersectingElements)); | |
e5083481 | 249 | assertEquals(SEGMENT_2_6, Iterables.getOnlyElement(intersectingElements)); |
9448be7c FLN |
250 | |
251 | /* | |
252 | * Time on segment start time | |
253 | */ | |
254 | intersectingElements = fSegmentStore.getIntersectingElements(2); | |
255 | assertEquals(1, Iterables.size(intersectingElements)); | |
e5083481 | 256 | assertEquals(SEGMENT_2_6, Iterables.getOnlyElement(intersectingElements)); |
9448be7c FLN |
257 | |
258 | /* | |
259 | * Time on segment end time | |
260 | */ | |
e5083481 | 261 | intersectingElements = fSegmentStore.getIntersectingElements(14); |
9448be7c | 262 | assertEquals(1, Iterables.size(intersectingElements)); |
e5083481 PT |
263 | assertEquals(SEGMENT_10_14, Iterables.getOnlyElement(intersectingElements)); |
264 | ||
265 | /* | |
266 | * Time overlapping many segments | |
267 | */ | |
268 | intersectingElements = fSegmentStore.getIntersectingElements(6); | |
269 | assertEquals(4, Iterables.size(intersectingElements)); | |
270 | ||
271 | /* | |
272 | * Time between segments | |
273 | */ | |
274 | intersectingElements = fSegmentStore.getIntersectingElements(9); | |
275 | assertEquals(0, Iterables.size(intersectingElements)); | |
9448be7c FLN |
276 | |
277 | /* | |
278 | * Time before all segment start time | |
279 | */ | |
280 | intersectingElements = fSegmentStore.getIntersectingElements(1); | |
281 | assertEquals(0, Iterables.size(intersectingElements)); | |
282 | ||
283 | /* | |
284 | * Time after all segment end time | |
285 | */ | |
286 | intersectingElements = fSegmentStore.getIntersectingElements(15); | |
287 | assertEquals(0, Iterables.size(intersectingElements)); | |
288 | } | |
289 | ||
290 | /** | |
291 | * Testing method getIntersectingElements(long start, long end) | |
292 | */ | |
293 | @Test | |
294 | public void testDispose() { | |
4c4e2816 | 295 | TreeMapStore<@NonNull ISegment> store = new TreeMapStore<>(); |
1a9cb076 | 296 | store.add(SEGMENT_2_6); |
9448be7c | 297 | store.dispose(); |
1a9cb076 | 298 | assertEquals(0, store.size()); |
9448be7c FLN |
299 | } |
300 | } |