tmf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / event / TmfTimestampTest.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Adjusted for new Event Model
12 * Alexandre Montplaisir - Port to JUnit4
13 * Patrick Tasse - Updated for negative value formatting
14 *******************************************************************************/
15
16 package org.eclipse.linuxtools.tmf.core.tests.event;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22
23 import java.text.DateFormat;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26
27 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
28 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
29 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestampFormat;
30 import org.junit.Test;
31
32 /**
33 * Test suite for the TmfTimestamp class.
34 */
35 @SuppressWarnings("javadoc")
36 public class TmfTimestampTest {
37
38 // ------------------------------------------------------------------------
39 // Variables
40 // ------------------------------------------------------------------------
41
42 private final ITmfTimestamp ts0 = new TmfTimestamp();
43 private final ITmfTimestamp ts1 = new TmfTimestamp(12345, 0);
44 private final ITmfTimestamp ts2 = new TmfTimestamp(12345, -1);
45 private final ITmfTimestamp ts3 = new TmfTimestamp(12345, 2, 5);
46 private final ITmfTimestamp ts4 = new TmfTimestamp(12345, -3, 0);
47 private final ITmfTimestamp ts5 = new TmfTimestamp(12345, -6, 0);
48 private final ITmfTimestamp ts6 = new TmfTimestamp(12345, -9, 0);
49 private final ITmfTimestamp ts7 = new TmfTimestamp(-12345, -3, 0);
50 private final ITmfTimestamp ts8 = new TmfTimestamp(-12345, -6, 0);
51 private final ITmfTimestamp ts9 = new TmfTimestamp(-12345, -9, 0);
52
53 // ------------------------------------------------------------------------
54 // Constructors
55 // ------------------------------------------------------------------------
56
57 @Test
58 public void testDefaultConstructor() {
59 assertEquals("getValue", 0, ts0.getValue());
60 assertEquals("getscale", 0, ts0.getScale());
61 assertEquals("getPrecision", 0, ts0.getPrecision());
62 }
63
64 @Test
65 public void testValueConstructor() {
66 assertEquals("getValue", 12345, ts1.getValue());
67 assertEquals("getscale", 0, ts1.getScale());
68 assertEquals("getPrecision", 0, ts1.getPrecision());
69 }
70
71 @Test
72 public void testValueScaleConstructor() {
73 assertEquals("getValue", 12345, ts2.getValue());
74 assertEquals("getscale", -1, ts2.getScale());
75 assertEquals("getPrecision", 0, ts2.getPrecision());
76 }
77
78 @Test
79 public void testFullConstructor() {
80 assertEquals("getValue", 12345, ts3.getValue());
81 assertEquals("getscale", 2, ts3.getScale());
82 assertEquals("getPrecision", 5, ts3.getPrecision());
83 }
84
85 @Test
86 public void testCopyConstructor() {
87 final ITmfTimestamp ts = new TmfTimestamp(12345, 2, 5);
88 final ITmfTimestamp copy = new TmfTimestamp(ts);
89
90 assertEquals("getValue", ts.getValue(), copy.getValue());
91 assertEquals("getscale", ts.getScale(), copy.getScale());
92 assertEquals("getPrecision", ts.getPrecision(), copy.getPrecision());
93
94 assertEquals("getValue", 12345, copy.getValue());
95 assertEquals("getscale", 2, copy.getScale());
96 assertEquals("getPrecision", 5, copy.getPrecision());
97 }
98
99 @Test(expected=IllegalArgumentException.class)
100 public void testCopyNullConstructor() {
101 new TmfTimestamp((TmfTimestamp) null);
102 }
103
104 @Test
105 public void testCopyConstructorBigBang() {
106 final ITmfTimestamp ts = new TmfTimestamp(TmfTimestamp.BIG_BANG);
107 assertEquals("getValue", TmfTimestamp.BIG_BANG.getValue(), ts.getValue());
108 assertEquals("getscale", TmfTimestamp.BIG_BANG.getScale(), ts.getScale());
109 assertEquals("getPrecision", TmfTimestamp.BIG_BANG.getPrecision(), ts.getPrecision());
110 }
111
112 @Test
113 public void testCopyConstructorBigCrunch() {
114 final ITmfTimestamp ts = new TmfTimestamp(TmfTimestamp.BIG_CRUNCH);
115 assertEquals("getValue", TmfTimestamp.BIG_CRUNCH.getValue(), ts.getValue());
116 assertEquals("getscale", TmfTimestamp.BIG_CRUNCH.getScale(), ts.getScale());
117 assertEquals("getPrecision", TmfTimestamp.BIG_CRUNCH.getPrecision(), ts.getPrecision());
118 }
119
120 @Test
121 public void testCopyConstructorZero() {
122 final ITmfTimestamp ts = new TmfTimestamp(TmfTimestamp.ZERO);
123 assertEquals("getValue", TmfTimestamp.ZERO.getValue(), ts.getValue());
124 assertEquals("getscale", TmfTimestamp.ZERO.getScale(), ts.getScale());
125 assertEquals("getPrecision", TmfTimestamp.ZERO.getPrecision(), ts.getPrecision());
126 }
127
128 // ------------------------------------------------------------------------
129 // hashCode
130 // ------------------------------------------------------------------------
131
132 @Test
133 public void testHashCode() {
134 final ITmfTimestamp ts0copy = new TmfTimestamp(ts0);
135 final ITmfTimestamp ts1copy = new TmfTimestamp(ts1);
136 final ITmfTimestamp ts2copy = new TmfTimestamp(ts2);
137
138 assertTrue("hashCode", ts0.hashCode() == ts0copy.hashCode());
139 assertTrue("hashCode", ts1.hashCode() == ts1copy.hashCode());
140 assertTrue("hashCode", ts2.hashCode() == ts2copy.hashCode());
141
142 assertTrue("hashCode", ts0.hashCode() != ts1.hashCode());
143 }
144
145 // ------------------------------------------------------------------------
146 // equals
147 // ------------------------------------------------------------------------
148
149 @Test
150 public void testEqualsReflexivity() {
151 assertTrue("equals", ts0.equals(ts0));
152 assertTrue("equals", ts1.equals(ts1));
153
154 assertTrue("equals", !ts0.equals(ts1));
155 assertTrue("equals", !ts1.equals(ts0));
156 }
157
158 @Test
159 public void testEqualsSymmetry() {
160 final ITmfTimestamp ts0copy = new TmfTimestamp(ts0);
161 assertTrue("equals", ts0.equals(ts0copy));
162 assertTrue("equals", ts0copy.equals(ts0));
163
164 final ITmfTimestamp ts1copy = new TmfTimestamp(ts1);
165 assertTrue("equals", ts1.equals(ts1copy));
166 assertTrue("equals", ts1copy.equals(ts1));
167
168 final ITmfTimestamp ts2copy = new TmfTimestamp(ts2);
169 assertTrue("equals", ts2.equals(ts2copy));
170 assertTrue("equals", ts2copy.equals(ts2));
171 }
172
173 @Test
174 public void testEqualsTransivity() {
175 final ITmfTimestamp ts0copy1 = new TmfTimestamp(ts0);
176 final ITmfTimestamp ts0copy2 = new TmfTimestamp(ts0copy1);
177 assertTrue("equals", ts0.equals(ts0copy1));
178 assertTrue("equals", ts0copy1.equals(ts0copy2));
179 assertTrue("equals", ts0.equals(ts0copy2));
180
181 final ITmfTimestamp ts1copy1 = new TmfTimestamp(ts1);
182 final ITmfTimestamp ts1copy2 = new TmfTimestamp(ts1copy1);
183 assertTrue("equals", ts1.equals(ts1copy1));
184 assertTrue("equals", ts1copy1.equals(ts1copy2));
185 assertTrue("equals", ts1.equals(ts1copy2));
186
187 final ITmfTimestamp ts2copy1 = new TmfTimestamp(ts2);
188 final ITmfTimestamp ts2copy2 = new TmfTimestamp(ts2copy1);
189 assertTrue("equals", ts2.equals(ts2copy1));
190 assertTrue("equals", ts2copy1.equals(ts2copy2));
191 assertTrue("equals", ts2.equals(ts2copy2));
192 }
193
194 @Test
195 public void testEqualsNull() {
196 assertTrue("equals", !ts0.equals(null));
197 assertTrue("equals", !ts1.equals(null));
198 }
199
200 @Test
201 public void testEqualsNonTimestamp() {
202 assertFalse("equals", ts0.equals(ts0.toString()));
203 }
204
205 // ------------------------------------------------------------------------
206 // toString
207 // ------------------------------------------------------------------------
208
209 @Test
210 public void testToStringDefault() {
211 DateFormat df = new SimpleDateFormat("HH:mm:ss.SSS");
212 Date d0 = new Date((long) (ts0.getValue() * Math.pow(10, ts0.getScale() + 3)));
213 Date d1 = new Date((long) (ts1.getValue() * Math.pow(10, ts1.getScale() + 3)));
214 Date d2 = new Date((long) (ts2.getValue() * Math.pow(10, ts2.getScale() + 3)));
215 Date d3 = new Date((long) (ts3.getValue() * Math.pow(10, ts3.getScale() + 3)));
216 Date d4 = new Date((long) (ts4.getValue() * Math.pow(10, ts4.getScale() + 3)));
217 Date d5 = new Date((long) (ts5.getValue() * Math.pow(10, ts5.getScale() + 3)));
218 Date d6 = new Date((long) (ts6.getValue() * Math.pow(10, ts6.getScale() + 3)));
219 Date d7 = new Date((long) (ts7.getValue() * Math.pow(10, ts7.getScale() + 3)));
220 Date d8 = new Date((long) (ts8.getValue() * Math.pow(10, ts8.getScale() + 3)) - 1);
221 Date d9 = new Date((long) (ts9.getValue() * Math.pow(10, ts9.getScale() + 3)) - 1);
222 assertEquals("toString", df.format(d0) + " 000 000", ts0.toString());
223 assertEquals("toString", df.format(d1) + " 000 000", ts1.toString());
224 assertEquals("toString", df.format(d2) + " 000 000", ts2.toString());
225 assertEquals("toString", df.format(d3) + " 000 000", ts3.toString());
226 assertEquals("toString", df.format(d4) + " 000 000", ts4.toString());
227 assertEquals("toString", df.format(d5) + " 345 000", ts5.toString());
228 assertEquals("toString", df.format(d6) + " 012 345", ts6.toString());
229 assertEquals("toString", df.format(d7) + " 000 000", ts7.toString());
230 assertEquals("toString", df.format(d8) + " 655 000", ts8.toString());
231 assertEquals("toString", df.format(d9) + " 987 655", ts9.toString());
232 }
233
234 @Test
235 public void testToStringInterval() {
236 assertEquals("toString", "000.000 000 000", ts0.toString(TmfTimestampFormat.getDefaulIntervalFormat()));
237 assertEquals("toString", "12345.000 000 000", ts1.toString(TmfTimestampFormat.getDefaulIntervalFormat()));
238 assertEquals("toString", "1234.500 000 000", ts2.toString(TmfTimestampFormat.getDefaulIntervalFormat()));
239 assertEquals("toString", "1234500.000 000 000", ts3.toString(TmfTimestampFormat.getDefaulIntervalFormat()));
240 assertEquals("toString", "012.345 000 000", ts4.toString(TmfTimestampFormat.getDefaulIntervalFormat()));
241 assertEquals("toString", "000.012 345 000", ts5.toString(TmfTimestampFormat.getDefaulIntervalFormat()));
242 assertEquals("toString", "000.000 012 345", ts6.toString(TmfTimestampFormat.getDefaulIntervalFormat()));
243 assertEquals("toString", "-012.345 000 000", ts7.toString(TmfTimestampFormat.getDefaulIntervalFormat()));
244 assertEquals("toString", "-000.012 345 000", ts8.toString(TmfTimestampFormat.getDefaulIntervalFormat()));
245 assertEquals("toString", "-000.000 012 345", ts9.toString(TmfTimestampFormat.getDefaulIntervalFormat()));
246 }
247
248 // ------------------------------------------------------------------------
249 // normalize
250 // ------------------------------------------------------------------------
251
252 @Test
253 public void testNormalizeOffset() {
254 ITmfTimestamp ts = ts0.normalize(0, 0);
255 assertEquals("getValue", 0, ts.getValue());
256 assertEquals("getscale", 0, ts.getScale());
257 assertEquals("getPrecision", 0, ts.getPrecision());
258
259 ts = ts0.normalize(12345, 0);
260 assertEquals("getValue", 12345, ts.getValue());
261 assertEquals("getscale", 0, ts.getScale());
262 assertEquals("getPrecision", 0, ts.getPrecision());
263
264 ts = ts0.normalize(10, 0);
265 assertEquals("getValue", 10, ts.getValue());
266 assertEquals("getscale", 0, ts.getScale());
267 assertEquals("getPrecision", 0, ts.getPrecision());
268
269 ts = ts0.normalize(-10, 0);
270 assertEquals("getValue", -10, ts.getValue());
271 assertEquals("getscale", 0, ts.getScale());
272 assertEquals("getPrecision", 0, ts.getPrecision());
273 }
274
275 @Test
276 public void testNormalizeOffsetLowerLimits() {
277 final ITmfTimestamp ref = new TmfTimestamp(Long.MIN_VALUE + 5, 0);
278
279 ITmfTimestamp ts = ref.normalize(-4, 0);
280 assertEquals("getValue", Long.MIN_VALUE + 1, ts.getValue());
281 assertEquals("getscale", 0, ts.getScale());
282 assertEquals("getPrecision", 0, ts.getPrecision());
283
284 ts = ref.normalize(-5, 0);
285 assertEquals("getValue", Long.MIN_VALUE, ts.getValue());
286 assertEquals("getscale", 0, ts.getScale());
287 assertEquals("getPrecision", 0, ts.getPrecision());
288
289 ts = ref.normalize(-6, 0);
290 assertEquals("getValue", Long.MIN_VALUE, ts.getValue());
291 assertEquals("getscale", 0, ts.getScale());
292 assertEquals("getPrecision", 0, ts.getPrecision());
293 }
294
295 @Test
296 public void testNormalizeOffsetUpperLimits() {
297 final ITmfTimestamp ref = new TmfTimestamp(Long.MAX_VALUE - 5, 0);
298
299 ITmfTimestamp ts = ref.normalize(4, 0);
300 assertEquals("getValue", Long.MAX_VALUE - 1, ts.getValue());
301 assertEquals("getscale", 0, ts.getScale());
302 assertEquals("getPrecision", 0, ts.getPrecision());
303
304 ts = ref.normalize(5, 0);
305 assertEquals("getValue", Long.MAX_VALUE, ts.getValue());
306 assertEquals("getscale", 0, ts.getScale());
307 assertEquals("getPrecision", 0, ts.getPrecision());
308
309 ts = ref.normalize(6, 0);
310 assertEquals("getValue", Long.MAX_VALUE, ts.getValue());
311 assertEquals("getscale", 0, ts.getScale());
312 assertEquals("getPrecision", 0, ts.getPrecision());
313 }
314
315 @Test
316 public void testNormalizeScale() {
317 ITmfTimestamp ts = ts0.normalize(0, 10);
318 assertEquals("getValue", 0, ts.getValue());
319 assertEquals("getscale", 10, ts.getScale());
320 assertEquals("getPrecision", 0, ts.getPrecision());
321
322 ts = ts0.normalize(0, -10);
323 assertEquals("getValue", 0, ts.getValue());
324 assertEquals("getscale", -10, ts.getScale());
325 assertEquals("getPrecision", 0, ts.getPrecision());
326 }
327
328 @Test
329 public void testNormalizedScaleLimits() {
330 final int MAX_SCALE_DIFF = 19;
331
332 // Test below limit
333 try {
334 ts1.normalize(0, +MAX_SCALE_DIFF - 1);
335 ts1.normalize(0, -MAX_SCALE_DIFF + 1);
336 } catch (final ArithmeticException e) {
337 fail("normalize: scale error");
338 }
339
340 // Test at limit
341 try {
342 ts1.normalize(0, +MAX_SCALE_DIFF);
343 fail("normalize: scale error");
344 ts1.normalize(0, -MAX_SCALE_DIFF);
345 fail("normalize: scale error");
346 } catch (final ArithmeticException e) {
347 }
348
349 // Test over limit
350 try {
351 ts1.normalize(0, +MAX_SCALE_DIFF + 1);
352 fail("normalize: scale error");
353 ts1.normalize(0, -MAX_SCALE_DIFF - 1);
354 fail("normalize: scale error");
355 } catch (final ArithmeticException e) {
356 }
357 }
358
359 @Test
360 public void testNormalizeOffsetAndScaleTrivial() {
361 final ITmfTimestamp ts = ts0.normalize(0, 0);
362 assertEquals("getValue", 0, ts.getValue());
363 assertEquals("getscale", 0, ts.getScale());
364 assertEquals("getPrecision", 0, ts.getPrecision());
365 }
366
367 @Test
368 public void testNormalizeOffsetAndScale() {
369 final int SCALE = 12;
370
371 ITmfTimestamp ts = ts0.normalize(0, SCALE);
372 assertEquals("getValue", 0, ts.getValue());
373 assertEquals("getscale", SCALE, ts.getScale());
374 assertEquals("getPrecision", 0, ts.getPrecision());
375
376 ts = ts0.normalize(12345, SCALE);
377 assertEquals("getValue", 12345, ts.getValue());
378 assertEquals("getscale", SCALE, ts.getScale());
379 assertEquals("getPrecision", 0, ts.getPrecision());
380
381 ts = ts0.normalize(10, SCALE);
382 assertEquals("getValue", 10, ts.getValue());
383 assertEquals("getscale", SCALE, ts.getScale());
384 assertEquals("getPrecision", 0, ts.getPrecision());
385
386 ts = ts0.normalize(-10, SCALE);
387 assertEquals("getValue", -10, ts.getValue());
388 assertEquals("getscale", SCALE, ts.getScale());
389 assertEquals("getPrecision", 0, ts.getPrecision());
390 }
391
392 @Test
393 public void testNormalizeOffsetAndScale2() {
394 int SCALE = 2;
395 ITmfTimestamp ts = ts1.normalize(0, SCALE);
396 assertEquals("getValue", 123, ts.getValue());
397 assertEquals("getscale", SCALE, ts.getScale());
398 assertEquals("getPrecision", 0, ts.getPrecision());
399
400 ts = ts1.normalize(12345, SCALE);
401 assertEquals("getValue", 12468, ts.getValue());
402 assertEquals("getscale", SCALE, ts.getScale());
403 assertEquals("getPrecision", 0, ts.getPrecision());
404
405 SCALE = -2;
406 ts = ts1.normalize(0, SCALE);
407 assertEquals("getValue", 1234500, ts.getValue());
408 assertEquals("getscale", SCALE, ts.getScale());
409 assertEquals("getPrecision", 0, ts.getPrecision());
410
411 ts = ts1.normalize(67, SCALE);
412 assertEquals("getValue", 1234567, ts.getValue());
413 assertEquals("getscale", SCALE, ts.getScale());
414 assertEquals("getPrecision", 0, ts.getPrecision());
415 }
416
417 // ------------------------------------------------------------------------
418 // compareTo
419 // ------------------------------------------------------------------------
420
421 @Test
422 public void testBasicCompareTo() {
423 final ITmfTimestamp t1 = new TmfTimestamp(900, 0, 50);
424 final ITmfTimestamp t2 = new TmfTimestamp(1000, 0, 50);
425 final ITmfTimestamp t3 = new TmfTimestamp(1100, 0, 50);
426 final ITmfTimestamp t4 = new TmfTimestamp(1000, 0, 75);
427
428 assertTrue(t1.compareTo(t1) == 0);
429
430 assertTrue("CompareTo", t1.compareTo(t2) < 0);
431 assertTrue("CompareTo", t1.compareTo(t3) < 0);
432 assertTrue("CompareTo", t1.compareTo(t4) < 0);
433
434 assertTrue("CompareTo", t2.compareTo(t1) > 0);
435 assertTrue("CompareTo", t2.compareTo(t3) < 0);
436 assertTrue("CompareTo", t2.compareTo(t4) == 0);
437
438 assertTrue("CompareTo", t3.compareTo(t1) > 0);
439 assertTrue("CompareTo", t3.compareTo(t2) > 0);
440 assertTrue("CompareTo", t3.compareTo(t4) > 0);
441 }
442
443 @Test
444 public void testCompareToCornerCases1() {
445 final ITmfTimestamp ts0a = new TmfTimestamp(ts0);
446 final ITmfTimestamp ts0b = new TmfTimestamp(ts0.getValue(), ts0.getScale() + 1);
447 final ITmfTimestamp ts0c = new TmfTimestamp(ts0.getValue() + 1, ts0.getScale());
448 final ITmfTimestamp ts0d = new TmfTimestamp(ts0.getValue() + 1, ts0.getScale() + 1);
449
450 assertTrue("compareTo", ts0.compareTo(ts0, false) == 0);
451 assertTrue("compareTo", ts0.compareTo(ts0a, false) == 0);
452 assertTrue("compareTo", ts0.compareTo(ts0b, false) == 0);
453 assertTrue("compareTo", ts0.compareTo(ts0c, false) == -1);
454 assertTrue("compareTo", ts0.compareTo(ts0d, false) == -1);
455 }
456
457 @Test
458 public void testCompareToCornerCases2() {
459 final ITmfTimestamp ts0a = new TmfTimestamp(Long.MAX_VALUE, Integer.MAX_VALUE - 1);
460 final ITmfTimestamp ts0b = new TmfTimestamp(0, Integer.MAX_VALUE);
461 final ITmfTimestamp ts0c = new TmfTimestamp(Long.MAX_VALUE, Integer.MAX_VALUE);
462
463 assertTrue("compareTo", ts0a.compareTo(ts0b, false) == 1);
464 assertTrue("compareTo", ts0a.compareTo(ts0c, false) == -1);
465
466 assertTrue("compareTo", ts0b.compareTo(ts0a, false) == -1);
467 assertTrue("compareTo", ts0b.compareTo(ts0c, false) == -1);
468
469 assertTrue("compareTo", ts0c.compareTo(ts0a, false) == 1);
470 assertTrue("compareTo", ts0c.compareTo(ts0b, false) == 1);
471 }
472
473 @Test
474 public void testCompareToCornerCases3() {
475 final ITmfTimestamp ts0a = new TmfTimestamp(Long.MIN_VALUE, Integer.MAX_VALUE - 1);
476 final ITmfTimestamp ts0b = new TmfTimestamp(0, Integer.MAX_VALUE);
477 final ITmfTimestamp ts0c = new TmfTimestamp(Long.MIN_VALUE, Integer.MAX_VALUE);
478
479 assertTrue("compareTo", ts0a.compareTo(ts0b, false) == -1);
480 assertTrue("compareTo", ts0a.compareTo(ts0c, false) == 1);
481
482 assertTrue("compareTo", ts0b.compareTo(ts0a, false) == 1);
483 assertTrue("compareTo", ts0b.compareTo(ts0c, false) == 1);
484
485 assertTrue("compareTo", ts0c.compareTo(ts0a, false) == -1);
486 assertTrue("compareTo", ts0c.compareTo(ts0b, false) == -1);
487 }
488
489 @Test
490 public void testCompareToCornerCases4() {
491 assertTrue("compareTo", ts0.compareTo(null, false) == 1);
492 assertTrue("compareTo", ts0.compareTo(null, true) == 1);
493 }
494
495 @Test
496 public void testCompareToSameScale() {
497 final ITmfTimestamp t1 = new TmfTimestamp(900, 0, 50);
498 final ITmfTimestamp t2 = new TmfTimestamp(1000, 0, 50);
499 final ITmfTimestamp t3 = new TmfTimestamp(1100, 0, 50);
500 final ITmfTimestamp t4 = new TmfTimestamp(1000, 0, 75);
501
502 assertTrue(t1.compareTo(t1, false) == 0);
503
504 assertTrue("CompareTo", t1.compareTo(t2, false) < 0);
505 assertTrue("CompareTo", t1.compareTo(t3, false) < 0);
506 assertTrue("CompareTo", t1.compareTo(t4, false) < 0);
507
508 assertTrue("CompareTo", t2.compareTo(t1, false) > 0);
509 assertTrue("CompareTo", t2.compareTo(t3, false) < 0);
510 assertTrue("CompareTo", t2.compareTo(t4, false) == 0);
511
512 assertTrue("CompareTo", t3.compareTo(t1, false) > 0);
513 assertTrue("CompareTo", t3.compareTo(t2, false) > 0);
514 assertTrue("CompareTo", t3.compareTo(t4, false) > 0);
515 }
516
517 @Test
518 public void testCompareToDifferentScale() {
519 final ITmfTimestamp t1 = new TmfTimestamp(9000, -1, 50);
520 final ITmfTimestamp t2 = new TmfTimestamp(1000, 0, 50);
521 final ITmfTimestamp t3 = new TmfTimestamp(110, 1, 50);
522 final ITmfTimestamp t4 = new TmfTimestamp(1, 3, 75);
523
524 assertTrue("CompareTo", t1.compareTo(t1, false) == 0);
525
526 assertTrue("CompareTo", t1.compareTo(t2, false) < 0);
527 assertTrue("CompareTo", t1.compareTo(t3, false) < 0);
528 assertTrue("CompareTo", t1.compareTo(t4, false) < 0);
529
530 assertTrue("CompareTo", t2.compareTo(t1, false) > 0);
531 assertTrue("CompareTo", t2.compareTo(t3, false) < 0);
532 assertTrue("CompareTo", t2.compareTo(t4, false) == 0);
533
534 assertTrue("CompareTo", t3.compareTo(t1, false) > 0);
535 assertTrue("CompareTo", t3.compareTo(t2, false) > 0);
536 assertTrue("CompareTo", t3.compareTo(t4, false) > 0);
537 }
538
539 @Test
540 public void testCompareToWithinPrecision() {
541 final ITmfTimestamp t1 = new TmfTimestamp(900, 0, 50);
542 final ITmfTimestamp t2 = new TmfTimestamp(1000, 0, 50);
543 final ITmfTimestamp t3 = new TmfTimestamp(1100, 0, 50);
544 final ITmfTimestamp t4 = new TmfTimestamp(1000, 0, 75);
545
546 assertTrue("CompareTo", t1.compareTo(t1, true) == 0);
547
548 assertTrue("CompareTo", t1.compareTo(t2, true) == 0);
549 assertTrue("CompareTo", t1.compareTo(t3, true) < 0);
550 assertTrue("CompareTo", t1.compareTo(t4, true) == 0);
551
552 assertTrue("CompareTo", t2.compareTo(t1, true) == 0);
553 assertTrue("CompareTo", t2.compareTo(t3, true) == 0);
554 assertTrue("CompareTo", t2.compareTo(t4, true) == 0);
555
556 assertTrue("CompareTo", t3.compareTo(t1, true) > 0);
557 assertTrue("CompareTo", t3.compareTo(t2, true) == 0);
558 assertTrue("CompareTo", t3.compareTo(t4, true) == 0);
559 }
560
561 @Test
562 public void testCompareToLargeScale1() {
563 final ITmfTimestamp t1 = new TmfTimestamp(-1, 100);
564 final ITmfTimestamp t2 = new TmfTimestamp(-1000, -100);
565 final ITmfTimestamp t3 = new TmfTimestamp(1, 100);
566 final ITmfTimestamp t4 = new TmfTimestamp(1000, -100);
567
568 assertTrue("CompareTo", t1.compareTo(t2, false) < 0);
569 assertTrue("CompareTo", t1.compareTo(t3, false) < 0);
570 assertTrue("CompareTo", t1.compareTo(t4, false) < 0);
571
572 assertTrue("CompareTo", t2.compareTo(t1, false) > 0);
573 assertTrue("CompareTo", t2.compareTo(t3, false) < 0);
574 assertTrue("CompareTo", t2.compareTo(t4, false) < 0);
575
576 assertTrue("CompareTo", t3.compareTo(t1, false) > 0);
577 assertTrue("CompareTo", t3.compareTo(t2, false) > 0);
578 assertTrue("CompareTo", t3.compareTo(t4, false) > 0);
579
580 assertTrue("CompareTo", t4.compareTo(t1, false) > 0);
581 assertTrue("CompareTo", t4.compareTo(t2, false) > 0);
582 assertTrue("CompareTo", t4.compareTo(t3, false) < 0);
583 }
584
585 @Test
586 public void testCompareToLargeScale2() {
587 final ITmfTimestamp ts0a = new TmfTimestamp(0, Integer.MAX_VALUE);
588 final ITmfTimestamp ts0b = new TmfTimestamp(1, Integer.MAX_VALUE);
589
590 assertTrue("CompareTo", ts0a.compareTo(ts0, false) == 0);
591 assertTrue("CompareTo", ts0.compareTo(ts0a, false) == 0);
592
593 assertTrue("CompareTo", ts0b.compareTo(ts0, false) == 1);
594 assertTrue("CompareTo", ts0.compareTo(ts0b, false) == -1);
595 }
596
597 // ------------------------------------------------------------------------
598 // getDelta
599 // ------------------------------------------------------------------------
600
601 @Test
602 public void testDelta() {
603 // Delta for same scale and precision (delta > 0)
604 ITmfTimestamp t0 = new TmfTimestamp(10, 9);
605 ITmfTimestamp t1 = new TmfTimestamp(5, 9);
606 ITmfTimestamp exp = new TmfTimestamp(5, 9);
607
608 ITmfTimestamp delta = t0.getDelta(t1);
609 assertEquals("getDelta", 0, delta.compareTo(exp, false));
610
611 // Delta for same scale and precision (delta < 0)
612 t0 = new TmfTimestamp(5, 9);
613 t1 = new TmfTimestamp(10, 9);
614 exp = new TmfTimestamp(-5, 9);
615
616 delta = t0.getDelta(t1);
617 assertEquals("getDelta", 0, delta.compareTo(exp, false));
618
619 // Delta for different scale and same precision (delta > 0)
620 t0 = new TmfTimestamp(5, 9);
621 t1 = new TmfTimestamp(10, 8);
622 exp = new TmfTimestamp(4, 9);
623
624 delta = t0.getDelta(t1);
625 assertEquals("getDelta", 0, delta.compareTo(exp, false));
626
627 // Delta for different scale and same precision (delta > 0)
628 t0 = new TmfTimestamp(5, 9);
629 t1 = new TmfTimestamp(10, 7);
630 exp = new TmfTimestamp(5, 9);
631
632 delta = t0.getDelta(t1);
633 assertEquals("getDelta", 0, delta.compareTo(exp, false));
634
635 // Delta for different scale and same precision
636 t0 = new TmfTimestamp(10, 9);
637 t1 = new TmfTimestamp(5, 8);
638 exp = new TmfTimestamp(10, 9);
639
640 delta = t0.getDelta(t1);
641 assertEquals("getDelta", 0, delta.compareTo(exp, false));
642
643 // Delta for same scale and different precision
644 t0 = new TmfTimestamp(10, 9, 1);
645 t1 = new TmfTimestamp(5, 9, 2);
646 exp = new TmfTimestamp(5, 9, 3);
647
648 delta = t0.getDelta(t1);
649 assertEquals("getDelta", 0, delta.compareTo(exp, true));
650 assertEquals("precision", 3, delta.getPrecision());
651
652 // Delta for same scale and different precision
653 t0 = new TmfTimestamp(5, 9, 2);
654 t1 = new TmfTimestamp(10, 9, 1);
655 exp = new TmfTimestamp(-5, 9, 3);
656
657 delta = t0.getDelta(t1);
658 assertEquals("getDelta", 0, delta.compareTo(exp, true));
659 assertEquals("precision", 3, delta.getPrecision());
660
661 // Delta for different scale and different precision
662 t0 = new TmfTimestamp(5, 9, 2);
663 t1 = new TmfTimestamp(10, 8, 1);
664 exp = new TmfTimestamp(4, 9, 3);
665 delta = t0.getDelta(t1);
666 assertEquals("getDelta", 0, delta.compareTo(exp, true));
667 assertEquals("precision", 2, delta.getPrecision());
668 }
669
670 }
This page took 0.061557 seconds and 5 git commands to generate.