Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
9ee9135e | 2 | * Copyright (c) 2009, 2010, 2012 Ericsson |
d18dd09b ASL |
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 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
9ee9135e | 11 | * Francois Chouinard - Adjusted for new Event Model |
d18dd09b ASL |
12 | *******************************************************************************/ |
13 | ||
6c13869b | 14 | package org.eclipse.linuxtools.tmf.core.tests.event; |
d18dd09b | 15 | |
bbc1c411 | 16 | import junit.framework.TestCase; |
17 | ||
18 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; | |
6c13869b FC |
19 | import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; |
20 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; | |
d18dd09b | 21 | |
d18dd09b ASL |
22 | /** |
23 | * <b><u>TmfTimeRangeTest</u></b> | |
24 | * <p> | |
cbd4ad82 | 25 | * Test suite for the TmfTimeRange class. |
d18dd09b | 26 | */ |
3b38ea61 | 27 | @SuppressWarnings("nls") |
d18dd09b ASL |
28 | public class TmfTimeRangeTest extends TestCase { |
29 | ||
cbd4ad82 | 30 | // ------------------------------------------------------------------------ |
d18dd09b | 31 | // Housekeeping |
cbd4ad82 | 32 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
33 | |
34 | public TmfTimeRangeTest(String name) { | |
35 | super(name); | |
36 | } | |
37 | ||
38 | @Override | |
39 | protected void setUp() throws Exception { | |
40 | super.setUp(); | |
41 | } | |
42 | ||
43 | @Override | |
44 | protected void tearDown() throws Exception { | |
45 | super.tearDown(); | |
46 | } | |
47 | ||
cbd4ad82 | 48 | // ------------------------------------------------------------------------ |
d18dd09b | 49 | // Constructors |
cbd4ad82 | 50 | // ------------------------------------------------------------------------ |
d18dd09b | 51 | |
34480e89 FC |
52 | public void testConstructor() throws Exception { |
53 | ITmfTimestamp ts1 = new TmfTimestamp(12345); | |
54 | ITmfTimestamp ts2 = new TmfTimestamp(12350); | |
55 | TmfTimeRange range = new TmfTimeRange(ts1, ts2); | |
56 | ||
57 | assertEquals("startTime", ts1, range.getStartTime()); | |
58 | assertEquals("endTime", ts2, range.getEndTime()); | |
59 | } | |
60 | ||
61 | public void testBadConstructor() throws Exception { | |
62 | try { | |
63 | new TmfTimeRange(TmfTimestamp.BigBang, null); | |
75d42a16 | 64 | fail("TmfTimeRange: bad end time"); |
34480e89 FC |
65 | } catch (IllegalArgumentException e) { |
66 | // Success | |
67 | } | |
68 | ||
69 | try { | |
70 | new TmfTimeRange(null, TmfTimestamp.BigCrunch); | |
75d42a16 | 71 | fail("TmfTimeRange: bad start time"); |
34480e89 FC |
72 | } catch (IllegalArgumentException e) { |
73 | // Success | |
74 | } | |
75 | } | |
76 | ||
77 | public void testOpenRange1() throws Exception { | |
78 | ITmfTimestamp ts2 = new TmfTimestamp(12350); | |
79 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, ts2); | |
80 | ||
81 | assertEquals("startTime", TmfTimestamp.BigBang, range.getStartTime()); | |
82 | assertEquals("endTime", ts2, range.getEndTime()); | |
83 | } | |
84 | ||
85 | public void testOpenRange2() throws Exception { | |
86 | ITmfTimestamp ts1 = new TmfTimestamp(12345); | |
87 | TmfTimeRange range = new TmfTimeRange(ts1, TmfTimestamp.BigCrunch); | |
88 | ||
89 | assertEquals("startTime", ts1, range.getStartTime()); | |
90 | assertEquals("endTime", TmfTimestamp.BigCrunch, range.getEndTime()); | |
91 | } | |
92 | ||
93 | public void testOpenRange3() throws Exception { | |
94 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); | |
95 | ||
96 | assertEquals("startTime", TmfTimestamp.BigBang, range.getStartTime()); | |
97 | assertEquals("endTime", TmfTimestamp.BigCrunch, range.getEndTime()); | |
98 | } | |
99 | ||
100 | public void testCopyConstructor() throws Exception { | |
101 | ITmfTimestamp ts1 = new TmfTimestamp(12345); | |
102 | ITmfTimestamp ts2 = new TmfTimestamp(12350); | |
103 | TmfTimeRange range0 = new TmfTimeRange(ts1, ts2); | |
104 | TmfTimeRange range1 = new TmfTimeRange(range0); | |
105 | ||
106 | assertEquals("startTime", ts1, range1.getStartTime()); | |
107 | assertEquals("endTime", ts2, range1.getEndTime()); | |
108 | ||
109 | TmfTimeRange range2 = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); | |
110 | TmfTimeRange range3 = new TmfTimeRange(range2); | |
111 | ||
112 | assertEquals("startTime", TmfTimestamp.BigBang, range3.getStartTime()); | |
113 | assertEquals("endTime", TmfTimestamp.BigCrunch, range3.getEndTime()); | |
114 | } | |
115 | ||
116 | public void testCopyConstructor2() throws Exception { | |
117 | try { | |
118 | new TmfTimeRange(null); | |
75d42a16 | 119 | fail("TmfTimeRange: null argument"); |
34480e89 FC |
120 | } catch (IllegalArgumentException e) { |
121 | // Success | |
122 | } | |
123 | } | |
cbd4ad82 | 124 | |
9ee9135e FC |
125 | // ------------------------------------------------------------------------ |
126 | // clone | |
127 | // ------------------------------------------------------------------------ | |
128 | ||
129 | public void testClone() throws Exception { | |
130 | ITmfTimestamp ts1 = new TmfTimestamp(12345); | |
131 | ITmfTimestamp ts2 = new TmfTimestamp(12350); | |
132 | ||
133 | TmfTimeRange range = new TmfTimeRange(ts1, ts2); | |
134 | TmfTimeRange clone = range.clone(); | |
135 | ||
136 | assertEquals("clone", range, clone); | |
137 | assertEquals("clone", ts1, clone.getStartTime()); | |
138 | assertEquals("clone", ts2, clone.getEndTime()); | |
139 | } | |
140 | ||
141 | // ------------------------------------------------------------------------ | |
142 | // hashCode | |
143 | // ------------------------------------------------------------------------ | |
144 | ||
145 | public void testHashCode() throws Exception { | |
146 | ITmfTimestamp ts1 = new TmfTimestamp(12345); | |
147 | ITmfTimestamp ts2 = new TmfTimestamp(12350); | |
148 | TmfTimeRange range1 = new TmfTimeRange(ts1, ts2); | |
149 | TmfTimeRange range1b = new TmfTimeRange(range1); | |
150 | TmfTimeRange range2 = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); | |
151 | TmfTimeRange range2b = new TmfTimeRange(range2); | |
152 | ||
153 | assertTrue("hashCode", range1.hashCode() == range1b.hashCode()); | |
154 | assertTrue("hashCode", range2.hashCode() == range2b.hashCode()); | |
155 | ||
156 | assertTrue("hashCode", range1.hashCode() != range2.hashCode()); | |
157 | } | |
158 | ||
cbd4ad82 FC |
159 | // ------------------------------------------------------------------------ |
160 | // equals | |
161 | // ------------------------------------------------------------------------ | |
162 | ||
163 | public void testEqualsReflexivity() throws Exception { | |
bbc1c411 | 164 | ITmfTimestamp ts1 = new TmfTimestamp(12345); |
165 | ITmfTimestamp ts2 = new TmfTimestamp(12350); | |
cbd4ad82 FC |
166 | TmfTimeRange range1 = new TmfTimeRange(ts1, ts2); |
167 | TmfTimeRange range2 = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); | |
d18dd09b | 168 | |
cbd4ad82 FC |
169 | assertTrue("equals", range1.equals(range1)); |
170 | assertTrue("equals", range2.equals(range2)); | |
d18dd09b | 171 | |
cbd4ad82 FC |
172 | assertTrue("equals", !range1.equals(range2)); |
173 | assertTrue("equals", !range2.equals(range1)); | |
174 | } | |
175 | ||
176 | public void testEqualsSymmetry() throws Exception { | |
bbc1c411 | 177 | ITmfTimestamp ts1 = new TmfTimestamp(12345); |
178 | ITmfTimestamp ts2 = new TmfTimestamp(12350); | |
cbd4ad82 FC |
179 | TmfTimeRange range1a = new TmfTimeRange(ts1, ts2); |
180 | TmfTimeRange range1b = new TmfTimeRange(ts1, ts2); | |
181 | ||
182 | TmfTimeRange range2a = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); | |
183 | TmfTimeRange range2b = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); | |
184 | ||
185 | assertTrue("equals", range1a.equals(range1b)); | |
186 | assertTrue("equals", range1b.equals(range1a)); | |
187 | ||
188 | assertTrue("equals", range2a.equals(range2b)); | |
189 | assertTrue("equals", range2b.equals(range2a)); | |
190 | } | |
191 | ||
192 | public void testEqualsTransivity() throws Exception { | |
bbc1c411 | 193 | ITmfTimestamp ts1 = new TmfTimestamp(12345); |
194 | ITmfTimestamp ts2 = new TmfTimestamp(12350); | |
cbd4ad82 FC |
195 | TmfTimeRange range1a = new TmfTimeRange(ts1, ts2); |
196 | TmfTimeRange range1b = new TmfTimeRange(ts1, ts2); | |
197 | TmfTimeRange range1c = new TmfTimeRange(ts1, ts2); | |
198 | ||
199 | assertTrue("equals", range1a.equals(range1b)); | |
200 | assertTrue("equals", range1b.equals(range1c)); | |
201 | assertTrue("equals", range1a.equals(range1c)); | |
d18dd09b | 202 | } |
cbd4ad82 | 203 | |
cbd4ad82 | 204 | public void testEqualsNull() throws Exception { |
bbc1c411 | 205 | ITmfTimestamp ts1 = new TmfTimestamp(12345); |
206 | ITmfTimestamp ts2 = new TmfTimestamp(12350); | |
cbd4ad82 FC |
207 | TmfTimeRange range1 = new TmfTimeRange(ts1, ts2); |
208 | ||
209 | assertTrue("equals", !range1.equals(null)); | |
210 | } | |
211 | ||
34480e89 FC |
212 | public void testEqualsBadType() throws Exception { |
213 | ITmfTimestamp ts1 = new TmfTimestamp(12345); | |
214 | ITmfTimestamp ts2 = new TmfTimestamp(12350); | |
215 | TmfTimeRange range1 = new TmfTimeRange(ts1, ts2); | |
216 | ||
217 | assertTrue("equals", !range1.equals(ts1)); | |
218 | } | |
219 | ||
220 | public void testEqualStartTime() throws Exception { | |
221 | ITmfTimestamp ts1 = new TmfTimestamp(12345); | |
222 | ITmfTimestamp ts2 = new TmfTimestamp(12350); | |
223 | ITmfTimestamp ts3 = new TmfTimestamp(12355); | |
224 | ||
225 | TmfTimeRange range1 = new TmfTimeRange(ts1, ts3); | |
226 | TmfTimeRange range2 = new TmfTimeRange(ts2, ts3); | |
227 | TmfTimeRange range3 = new TmfTimeRange(ts1, ts2); | |
228 | ||
229 | assertTrue("equals", !range1.equals(range2)); | |
230 | assertTrue("equals", !range1.equals(range3)); | |
231 | } | |
232 | ||
233 | public void testEqualsEndTime() throws Exception { | |
234 | ITmfTimestamp ts1 = new TmfTimestamp(12345); | |
235 | ITmfTimestamp ts2 = new TmfTimestamp(12350); | |
236 | ITmfTimestamp ts3 = new TmfTimestamp(12355); | |
237 | ||
238 | TmfTimeRange range1 = new TmfTimeRange(ts1, ts2); | |
239 | TmfTimeRange range2 = new TmfTimeRange(ts1, ts3); | |
240 | TmfTimeRange range3 = new TmfTimeRange(ts2, ts3); | |
241 | ||
242 | assertTrue("equals", !range1.equals(range2)); | |
243 | assertTrue("equals", !range1.equals(range3)); | |
244 | } | |
245 | ||
34480e89 FC |
246 | // ------------------------------------------------------------------------ |
247 | // toString | |
248 | // ------------------------------------------------------------------------ | |
249 | ||
250 | public void testToString() throws Exception { | |
251 | ITmfTimestamp ts1 = new TmfTimestamp(12345); | |
252 | ITmfTimestamp ts2 = new TmfTimestamp(12350); | |
253 | TmfTimeRange range = new TmfTimeRange(ts1, ts2); | |
254 | ||
255 | String expected = "TmfTimeRange [fStartTime=" + ts1 + ", fEndTime=" + ts2 + "]"; | |
256 | assertEquals("toString", expected, range.toString()); | |
257 | } | |
258 | ||
cbd4ad82 FC |
259 | // ------------------------------------------------------------------------ |
260 | // contains | |
261 | // ------------------------------------------------------------------------ | |
262 | ||
34480e89 | 263 | public void testContainsTimestamp() throws Exception { |
bbc1c411 | 264 | ITmfTimestamp ts1 = new TmfTimestamp(12345); |
265 | ITmfTimestamp ts2 = new TmfTimestamp(12350); | |
cbd4ad82 FC |
266 | TmfTimeRange range = new TmfTimeRange(ts1, ts2); |
267 | ||
268 | assertTrue("contains (lower bound)", range.contains(new TmfTimestamp(12345))); | |
269 | assertTrue("contains (higher bound)", range.contains(new TmfTimestamp(12350))); | |
270 | assertTrue("contains (within bounds)", range.contains(new TmfTimestamp(12346))); | |
271 | ||
272 | assertFalse("contains (low value)", range.contains(new TmfTimestamp(12340))); | |
273 | assertFalse("contains (high value)", range.contains(new TmfTimestamp(12351))); | |
34480e89 FC |
274 | |
275 | assertTrue("contains (zero)", range.contains(TmfTimestamp.Zero)); | |
cbd4ad82 FC |
276 | } |
277 | ||
34480e89 FC |
278 | public void testContainsRange() throws Exception { |
279 | ITmfTimestamp ts1 = new TmfTimestamp(10); | |
280 | ITmfTimestamp ts2 = new TmfTimestamp(20); | |
281 | ITmfTimestamp ts3 = new TmfTimestamp(30); | |
282 | ITmfTimestamp ts4 = new TmfTimestamp(40); | |
283 | ITmfTimestamp ts5 = new TmfTimestamp(50); | |
284 | ITmfTimestamp ts6 = new TmfTimestamp(60); | |
285 | ITmfTimestamp ts7 = new TmfTimestamp(70); | |
286 | ITmfTimestamp ts8 = new TmfTimestamp(80); | |
287 | ||
288 | // Reference range | |
289 | TmfTimeRange range0 = new TmfTimeRange(ts3, ts6); | |
290 | ||
291 | // Start time below range | |
292 | TmfTimeRange range1 = new TmfTimeRange(ts1, ts2); | |
293 | TmfTimeRange range2 = new TmfTimeRange(ts2, ts3); | |
294 | TmfTimeRange range3 = new TmfTimeRange(ts2, ts4); | |
295 | TmfTimeRange range4 = new TmfTimeRange(ts2, ts6); | |
296 | TmfTimeRange range5 = new TmfTimeRange(ts2, ts7); | |
297 | ||
298 | assertFalse("contains", range0.contains(range1)); | |
299 | assertFalse("contains", range0.contains(range2)); | |
300 | assertFalse("contains", range0.contains(range3)); | |
301 | assertFalse("contains", range0.contains(range4)); | |
302 | assertFalse("contains", range0.contains(range5)); | |
303 | ||
304 | // End time above range | |
305 | TmfTimeRange range6 = new TmfTimeRange(ts3, ts7); | |
306 | TmfTimeRange range7 = new TmfTimeRange(ts4, ts7); | |
307 | TmfTimeRange range8 = new TmfTimeRange(ts6, ts7); | |
308 | TmfTimeRange range9 = new TmfTimeRange(ts7, ts8); | |
309 | ||
310 | assertFalse("contains", range0.contains(range6)); | |
311 | assertFalse("contains", range0.contains(range7)); | |
312 | assertFalse("contains", range0.contains(range8)); | |
313 | assertFalse("contains", range0.contains(range9)); | |
314 | ||
315 | // Within range | |
316 | TmfTimeRange range10 = new TmfTimeRange(ts3, ts4); | |
317 | TmfTimeRange range11 = new TmfTimeRange(ts3, ts6); | |
318 | TmfTimeRange range12 = new TmfTimeRange(ts4, ts5); | |
319 | TmfTimeRange range13 = new TmfTimeRange(ts4, ts6); | |
320 | ||
321 | assertTrue("contains", range0.contains(range10)); | |
322 | assertTrue("contains", range0.contains(range11)); | |
323 | assertTrue("contains", range0.contains(range12)); | |
324 | assertTrue("contains", range0.contains(range13)); | |
325 | } | |
326 | ||
023761c4 FC |
327 | // ------------------------------------------------------------------------ |
328 | // getIntersection | |
329 | // ------------------------------------------------------------------------ | |
330 | ||
331 | public void testGetIntersection() throws Exception { | |
332 | ||
bbc1c411 | 333 | ITmfTimestamp ts1a = new TmfTimestamp(1000); |
334 | ITmfTimestamp ts1b = new TmfTimestamp(2000); | |
023761c4 FC |
335 | TmfTimeRange range1 = new TmfTimeRange(ts1a, ts1b); |
336 | ||
bbc1c411 | 337 | ITmfTimestamp ts2a = new TmfTimestamp(2000); |
338 | ITmfTimestamp ts2b = new TmfTimestamp(3000); | |
023761c4 FC |
339 | TmfTimeRange range2 = new TmfTimeRange(ts2a, ts2b); |
340 | ||
bbc1c411 | 341 | ITmfTimestamp ts3a = new TmfTimestamp(3000); |
342 | ITmfTimestamp ts3b = new TmfTimestamp(4000); | |
023761c4 FC |
343 | TmfTimeRange range3 = new TmfTimeRange(ts3a, ts3b); |
344 | ||
bbc1c411 | 345 | ITmfTimestamp ts4a = new TmfTimestamp(1500); |
346 | ITmfTimestamp ts4b = new TmfTimestamp(2500); | |
023761c4 FC |
347 | TmfTimeRange range4 = new TmfTimeRange(ts4a, ts4b); |
348 | ||
bbc1c411 | 349 | ITmfTimestamp ts5a = new TmfTimestamp(1500); |
350 | ITmfTimestamp ts5b = new TmfTimestamp(2000); | |
023761c4 FC |
351 | TmfTimeRange range5 = new TmfTimeRange(ts5a, ts5b); |
352 | ||
bbc1c411 | 353 | ITmfTimestamp ts6a = new TmfTimestamp(2000); |
354 | ITmfTimestamp ts6b = new TmfTimestamp(2500); | |
023761c4 FC |
355 | TmfTimeRange range6 = new TmfTimeRange(ts6a, ts6b); |
356 | ||
bbc1c411 | 357 | ITmfTimestamp ts7a = new TmfTimestamp(1500); |
358 | ITmfTimestamp ts7b = new TmfTimestamp(3500); | |
023761c4 FC |
359 | TmfTimeRange range7 = new TmfTimeRange(ts7a, ts7b); |
360 | ||
bbc1c411 | 361 | ITmfTimestamp ts8a = new TmfTimestamp(2250); |
362 | ITmfTimestamp ts8b = new TmfTimestamp(2750); | |
023761c4 FC |
363 | TmfTimeRange range8 = new TmfTimeRange(ts8a, ts8b); |
364 | ||
365 | assertEquals("getIntersection (below - not contiguous)", null, range1.getIntersection(range3)); | |
366 | assertEquals("getIntersection (above - not contiguous)", null, range3.getIntersection(range1)); | |
367 | ||
368 | assertEquals("getIntersection (below - contiguous)", new TmfTimeRange(ts1b, ts1b), range1.getIntersection(range2)); | |
369 | assertEquals("getIntersection (above - contiguous)", new TmfTimeRange(ts3a, ts3a), range3.getIntersection(range2)); | |
370 | ||
371 | assertEquals("getIntersection (below - overlap)", new TmfTimeRange(ts2a, ts4b), range2.getIntersection(range4)); | |
372 | assertEquals("getIntersection (above - overlap)", new TmfTimeRange(ts2a, ts4b), range4.getIntersection(range2)); | |
373 | ||
374 | assertEquals("getIntersection (within - overlap1)", range6, range2.getIntersection(range6)); | |
375 | assertEquals("getIntersection (within - overlap2)", range6, range6.getIntersection(range2)); | |
376 | ||
377 | assertEquals("getIntersection (within - overlap3)", range5, range1.getIntersection(range5)); | |
378 | assertEquals("getIntersection (within - overlap4)", range5, range5.getIntersection(range1)); | |
379 | ||
380 | assertEquals("getIntersection (within - overlap5)", range8, range2.getIntersection(range8)); | |
381 | assertEquals("getIntersection (within - overlap6)", range8, range8.getIntersection(range2)); | |
382 | ||
383 | assertEquals("getIntersection (accross1)", range2, range2.getIntersection(range7)); | |
384 | assertEquals("getIntersection (accross2)", range2, range7.getIntersection(range2)); | |
385 | } | |
386 | ||
d18dd09b | 387 | } |