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