tmf: Make TmfTimestamp and TmfTimeRange immutable
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / event / TmfTimestampTest.java
... / ...
CommitLineData
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
14package org.eclipse.linuxtools.tmf.core.tests.event;
15
16import junit.framework.TestCase;
17
18import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
19import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
20import org.eclipse.linuxtools.tmf.core.event.TmfTimestampFormat;
21
22/**
23 * Test suite for the TmfTimestamp class.
24 */
25@SuppressWarnings("nls")
26public 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 // hashCode
157 // ------------------------------------------------------------------------
158
159 /**
160 *
161 */
162 public void testHashCode() {
163 final ITmfTimestamp ts0copy = new TmfTimestamp(ts0);
164 final ITmfTimestamp ts1copy = new TmfTimestamp(ts1);
165 final ITmfTimestamp ts2copy = new TmfTimestamp(ts2);
166
167 assertTrue("hashCode", ts0.hashCode() == ts0copy.hashCode());
168 assertTrue("hashCode", ts1.hashCode() == ts1copy.hashCode());
169 assertTrue("hashCode", ts2.hashCode() == ts2copy.hashCode());
170
171 assertTrue("hashCode", ts0.hashCode() != ts1.hashCode());
172 }
173
174 // ------------------------------------------------------------------------
175 // equals
176 // ------------------------------------------------------------------------
177
178 /**
179 *
180 */
181 public void testEqualsReflexivity() {
182 assertTrue("equals", ts0.equals(ts0));
183 assertTrue("equals", ts1.equals(ts1));
184
185 assertTrue("equals", !ts0.equals(ts1));
186 assertTrue("equals", !ts1.equals(ts0));
187 }
188
189 /**
190 *
191 */
192 public void testEqualsSymmetry() {
193 final ITmfTimestamp ts0copy = new TmfTimestamp(ts0);
194 assertTrue("equals", ts0.equals(ts0copy));
195 assertTrue("equals", ts0copy.equals(ts0));
196
197 final ITmfTimestamp ts1copy = new TmfTimestamp(ts1);
198 assertTrue("equals", ts1.equals(ts1copy));
199 assertTrue("equals", ts1copy.equals(ts1));
200
201 final ITmfTimestamp ts2copy = new TmfTimestamp(ts2);
202 assertTrue("equals", ts2.equals(ts2copy));
203 assertTrue("equals", ts2copy.equals(ts2));
204 }
205
206 /**
207 *
208 */
209 public void testEqualsTransivity() {
210 final ITmfTimestamp ts0copy1 = new TmfTimestamp(ts0);
211 final ITmfTimestamp ts0copy2 = new TmfTimestamp(ts0copy1);
212 assertTrue("equals", ts0.equals(ts0copy1));
213 assertTrue("equals", ts0copy1.equals(ts0copy2));
214 assertTrue("equals", ts0.equals(ts0copy2));
215
216 final ITmfTimestamp ts1copy1 = new TmfTimestamp(ts1);
217 final ITmfTimestamp ts1copy2 = new TmfTimestamp(ts1copy1);
218 assertTrue("equals", ts1.equals(ts1copy1));
219 assertTrue("equals", ts1copy1.equals(ts1copy2));
220 assertTrue("equals", ts1.equals(ts1copy2));
221
222 final ITmfTimestamp ts2copy1 = new TmfTimestamp(ts2);
223 final ITmfTimestamp ts2copy2 = new TmfTimestamp(ts2copy1);
224 assertTrue("equals", ts2.equals(ts2copy1));
225 assertTrue("equals", ts2copy1.equals(ts2copy2));
226 assertTrue("equals", ts2.equals(ts2copy2));
227 }
228
229 /**
230 *
231 */
232 public void testEqualsNull() {
233 assertTrue("equals", !ts0.equals(null));
234 assertTrue("equals", !ts1.equals(null));
235 }
236
237 /**
238 *
239 */
240 public void testEqualsNonTimestamp() {
241 assertFalse("equals", ts0.equals(ts0.toString()));
242 }
243
244 // ------------------------------------------------------------------------
245 // toString
246 // ------------------------------------------------------------------------
247
248 /**
249 *
250 */
251 public void testToStringDefault() {
252 assertEquals("toString", "00:00:00.000 000 000", ts0.toString());
253 assertEquals("toString", "03:25:45.000 000 000", ts1.toString());
254 assertEquals("toString", "00:20:34.500 000 000", ts2.toString());
255 assertEquals("toString", "06:55:00.000 000 000", ts3.toString());
256 }
257
258 /**
259 *
260 */
261 public void testToStringInterval() {
262 assertEquals("toString", "000.000 000 000", ts0.toString(TmfTimestampFormat.getDefaulIntervalFormat()));
263 assertEquals("toString", "12345.000 000 000", ts1.toString(TmfTimestampFormat.getDefaulIntervalFormat()));
264 assertEquals("toString", "1234.500 000 000", ts2.toString(TmfTimestampFormat.getDefaulIntervalFormat()));
265 assertEquals("toString", "1234500.000 000 000", ts3.toString(TmfTimestampFormat.getDefaulIntervalFormat()));
266 }
267
268 // ------------------------------------------------------------------------
269 // normalize
270 // ------------------------------------------------------------------------
271
272 /**
273 *
274 */
275 public void testNormalizeOffset() {
276 ITmfTimestamp ts = ts0.normalize(0, 0);
277 assertEquals("getValue", 0, ts.getValue());
278 assertEquals("getscale", 0, ts.getScale());
279 assertEquals("getPrecision", 0, ts.getPrecision());
280
281 ts = ts0.normalize(12345, 0);
282 assertEquals("getValue", 12345, ts.getValue());
283 assertEquals("getscale", 0, ts.getScale());
284 assertEquals("getPrecision", 0, ts.getPrecision());
285
286 ts = ts0.normalize(10, 0);
287 assertEquals("getValue", 10, ts.getValue());
288 assertEquals("getscale", 0, ts.getScale());
289 assertEquals("getPrecision", 0, ts.getPrecision());
290
291 ts = ts0.normalize(-10, 0);
292 assertEquals("getValue", -10, ts.getValue());
293 assertEquals("getscale", 0, ts.getScale());
294 assertEquals("getPrecision", 0, ts.getPrecision());
295 }
296
297 /**
298 *
299 */
300 public void testNormalizeOffsetLowerLimits() {
301 final ITmfTimestamp ref = new TmfTimestamp(Long.MIN_VALUE + 5, 0);
302
303 ITmfTimestamp ts = ref.normalize(-4, 0);
304 assertEquals("getValue", Long.MIN_VALUE + 1, ts.getValue());
305 assertEquals("getscale", 0, ts.getScale());
306 assertEquals("getPrecision", 0, ts.getPrecision());
307
308 ts = ref.normalize(-5, 0);
309 assertEquals("getValue", Long.MIN_VALUE, ts.getValue());
310 assertEquals("getscale", 0, ts.getScale());
311 assertEquals("getPrecision", 0, ts.getPrecision());
312
313 ts = ref.normalize(-6, 0);
314 assertEquals("getValue", Long.MIN_VALUE, ts.getValue());
315 assertEquals("getscale", 0, ts.getScale());
316 assertEquals("getPrecision", 0, ts.getPrecision());
317 }
318
319 /**
320 *
321 */
322 public void testNormalizeOffsetUpperLimits() {
323 final ITmfTimestamp ref = new TmfTimestamp(Long.MAX_VALUE - 5, 0);
324
325 ITmfTimestamp ts = ref.normalize(4, 0);
326 assertEquals("getValue", Long.MAX_VALUE - 1, ts.getValue());
327 assertEquals("getscale", 0, ts.getScale());
328 assertEquals("getPrecision", 0, ts.getPrecision());
329
330 ts = ref.normalize(5, 0);
331 assertEquals("getValue", Long.MAX_VALUE, ts.getValue());
332 assertEquals("getscale", 0, ts.getScale());
333 assertEquals("getPrecision", 0, ts.getPrecision());
334
335 ts = ref.normalize(6, 0);
336 assertEquals("getValue", Long.MAX_VALUE, ts.getValue());
337 assertEquals("getscale", 0, ts.getScale());
338 assertEquals("getPrecision", 0, ts.getPrecision());
339 }
340
341 /**
342 *
343 */
344 public void testNormalizeScale() {
345 ITmfTimestamp ts = ts0.normalize(0, 10);
346 assertEquals("getValue", 0, ts.getValue());
347 assertEquals("getscale", 10, ts.getScale());
348 assertEquals("getPrecision", 0, ts.getPrecision());
349
350 ts = ts0.normalize(0, -10);
351 assertEquals("getValue", 0, ts.getValue());
352 assertEquals("getscale", -10, ts.getScale());
353 assertEquals("getPrecision", 0, ts.getPrecision());
354 }
355
356 /**
357 *
358 */
359 public void testNormalizedScaleLimits() {
360 final int MAX_SCALE_DIFF = 19;
361
362 // Test below limit
363 try {
364 ts1.normalize(0, +MAX_SCALE_DIFF - 1);
365 ts1.normalize(0, -MAX_SCALE_DIFF + 1);
366 } catch (final ArithmeticException e) {
367 fail("normalize: scale error");
368 }
369
370 // Test at limit
371 try {
372 ts1.normalize(0, +MAX_SCALE_DIFF);
373 fail("normalize: scale error");
374 ts1.normalize(0, -MAX_SCALE_DIFF);
375 fail("normalize: scale error");
376 } catch (final ArithmeticException e) {
377 }
378
379 // Test over limit
380 try {
381 ts1.normalize(0, +MAX_SCALE_DIFF + 1);
382 fail("normalize: scale error");
383 ts1.normalize(0, -MAX_SCALE_DIFF - 1);
384 fail("normalize: scale error");
385 } catch (final ArithmeticException e) {
386 }
387 }
388
389 /**
390 *
391 */
392 public void testNormalizeOffsetAndScaleTrivial() {
393 final ITmfTimestamp ts = ts0.normalize(0, 0);
394 assertEquals("getValue", 0, ts.getValue());
395 assertEquals("getscale", 0, ts.getScale());
396 assertEquals("getPrecision", 0, ts.getPrecision());
397 }
398
399 /**
400 *
401 */
402 public void testNormalizeOffsetAndScale() {
403 final int SCALE = 12;
404
405 ITmfTimestamp ts = ts0.normalize(0, SCALE);
406 assertEquals("getValue", 0, ts.getValue());
407 assertEquals("getscale", SCALE, ts.getScale());
408 assertEquals("getPrecision", 0, ts.getPrecision());
409
410 ts = ts0.normalize(12345, SCALE);
411 assertEquals("getValue", 12345, ts.getValue());
412 assertEquals("getscale", SCALE, ts.getScale());
413 assertEquals("getPrecision", 0, ts.getPrecision());
414
415 ts = ts0.normalize(10, SCALE);
416 assertEquals("getValue", 10, ts.getValue());
417 assertEquals("getscale", SCALE, ts.getScale());
418 assertEquals("getPrecision", 0, ts.getPrecision());
419
420 ts = ts0.normalize(-10, SCALE);
421 assertEquals("getValue", -10, ts.getValue());
422 assertEquals("getscale", SCALE, ts.getScale());
423 assertEquals("getPrecision", 0, ts.getPrecision());
424 }
425
426 /**
427 *
428 */
429 public void testNormalizeOffsetAndScale2() {
430 int SCALE = 2;
431 ITmfTimestamp ts = ts1.normalize(0, SCALE);
432 assertEquals("getValue", 123, ts.getValue());
433 assertEquals("getscale", SCALE, ts.getScale());
434 assertEquals("getPrecision", 0, ts.getPrecision());
435
436 ts = ts1.normalize(12345, SCALE);
437 assertEquals("getValue", 12468, ts.getValue());
438 assertEquals("getscale", SCALE, ts.getScale());
439 assertEquals("getPrecision", 0, ts.getPrecision());
440
441 SCALE = -2;
442 ts = ts1.normalize(0, SCALE);
443 assertEquals("getValue", 1234500, ts.getValue());
444 assertEquals("getscale", SCALE, ts.getScale());
445 assertEquals("getPrecision", 0, ts.getPrecision());
446
447 ts = ts1.normalize(67, SCALE);
448 assertEquals("getValue", 1234567, ts.getValue());
449 assertEquals("getscale", SCALE, ts.getScale());
450 assertEquals("getPrecision", 0, ts.getPrecision());
451 }
452
453 // ------------------------------------------------------------------------
454 // compareTo
455 // ------------------------------------------------------------------------
456
457 /**
458 *
459 */
460 @SuppressWarnings("hiding")
461 public void testBasicCompareTo() {
462 final ITmfTimestamp ts1 = new TmfTimestamp(900, 0, 50);
463 final ITmfTimestamp ts2 = new TmfTimestamp(1000, 0, 50);
464 final ITmfTimestamp ts3 = new TmfTimestamp(1100, 0, 50);
465 final ITmfTimestamp ts4 = new TmfTimestamp(1000, 0, 75);
466
467 assertTrue(ts1.compareTo(ts1) == 0);
468
469 assertTrue("CompareTo", ts1.compareTo(ts2) < 0);
470 assertTrue("CompareTo", ts1.compareTo(ts3) < 0);
471 assertTrue("CompareTo", ts1.compareTo(ts4) < 0);
472
473 assertTrue("CompareTo", ts2.compareTo(ts1) > 0);
474 assertTrue("CompareTo", ts2.compareTo(ts3) < 0);
475 assertTrue("CompareTo", ts2.compareTo(ts4) == 0);
476
477 assertTrue("CompareTo", ts3.compareTo(ts1) > 0);
478 assertTrue("CompareTo", ts3.compareTo(ts2) > 0);
479 assertTrue("CompareTo", ts3.compareTo(ts4) > 0);
480 }
481
482 /**
483 *
484 */
485 public void testCompareToCornerCases1() {
486 final ITmfTimestamp ts0a = new TmfTimestamp(ts0);
487 final ITmfTimestamp ts0b = new TmfTimestamp(ts0.getValue(), ts0.getScale() + 1);
488 final ITmfTimestamp ts0c = new TmfTimestamp(ts0.getValue() + 1, ts0.getScale());
489 final ITmfTimestamp ts0d = new TmfTimestamp(ts0.getValue() + 1, ts0.getScale() + 1);
490
491 assertTrue("compareTo", ts0.compareTo(ts0, false) == 0);
492 assertTrue("compareTo", ts0.compareTo(ts0a, false) == 0);
493 assertTrue("compareTo", ts0.compareTo(ts0b, false) == 0);
494 assertTrue("compareTo", ts0.compareTo(ts0c, false) == -1);
495 assertTrue("compareTo", ts0.compareTo(ts0d, false) == -1);
496 }
497
498 /**
499 *
500 */
501 public void testCompareToCornerCases2() {
502 final ITmfTimestamp ts0a = new TmfTimestamp(Long.MAX_VALUE, Integer.MAX_VALUE - 1);
503 final ITmfTimestamp ts0b = new TmfTimestamp(0, Integer.MAX_VALUE);
504 final ITmfTimestamp ts0c = new TmfTimestamp(Long.MAX_VALUE, Integer.MAX_VALUE);
505
506 assertTrue("compareTo", ts0a.compareTo(ts0b, false) == 1);
507 assertTrue("compareTo", ts0a.compareTo(ts0c, false) == -1);
508
509 assertTrue("compareTo", ts0b.compareTo(ts0a, false) == -1);
510 assertTrue("compareTo", ts0b.compareTo(ts0c, false) == -1);
511
512 assertTrue("compareTo", ts0c.compareTo(ts0a, false) == 1);
513 assertTrue("compareTo", ts0c.compareTo(ts0b, false) == 1);
514 }
515
516 /**
517 *
518 */
519 public void testCompareToCornerCases3() {
520 final ITmfTimestamp ts0a = new TmfTimestamp(Long.MIN_VALUE, Integer.MAX_VALUE - 1);
521 final ITmfTimestamp ts0b = new TmfTimestamp(0, Integer.MAX_VALUE);
522 final ITmfTimestamp ts0c = new TmfTimestamp(Long.MIN_VALUE, Integer.MAX_VALUE);
523
524 assertTrue("compareTo", ts0a.compareTo(ts0b, false) == -1);
525 assertTrue("compareTo", ts0a.compareTo(ts0c, false) == 1);
526
527 assertTrue("compareTo", ts0b.compareTo(ts0a, false) == 1);
528 assertTrue("compareTo", ts0b.compareTo(ts0c, false) == 1);
529
530 assertTrue("compareTo", ts0c.compareTo(ts0a, false) == -1);
531 assertTrue("compareTo", ts0c.compareTo(ts0b, false) == -1);
532 }
533
534 /**
535 *
536 */
537 public void testCompareToCornerCases4() {
538 assertTrue("compareTo", ts0.compareTo(null, false) == 1);
539 assertTrue("compareTo", ts0.compareTo(null, true) == 1);
540 }
541
542 /**
543 *
544 */
545 @SuppressWarnings("hiding")
546 public void testCompareToSameScale() {
547 final ITmfTimestamp ts1 = new TmfTimestamp(900, 0, 50);
548 final ITmfTimestamp ts2 = new TmfTimestamp(1000, 0, 50);
549 final ITmfTimestamp ts3 = new TmfTimestamp(1100, 0, 50);
550 final ITmfTimestamp ts4 = new TmfTimestamp(1000, 0, 75);
551
552 assertTrue(ts1.compareTo(ts1, false) == 0);
553
554 assertTrue("CompareTo", ts1.compareTo(ts2, false) < 0);
555 assertTrue("CompareTo", ts1.compareTo(ts3, false) < 0);
556 assertTrue("CompareTo", ts1.compareTo(ts4, false) < 0);
557
558 assertTrue("CompareTo", ts2.compareTo(ts1, false) > 0);
559 assertTrue("CompareTo", ts2.compareTo(ts3, false) < 0);
560 assertTrue("CompareTo", ts2.compareTo(ts4, false) == 0);
561
562 assertTrue("CompareTo", ts3.compareTo(ts1, false) > 0);
563 assertTrue("CompareTo", ts3.compareTo(ts2, false) > 0);
564 assertTrue("CompareTo", ts3.compareTo(ts4, false) > 0);
565 }
566
567 /**
568 *
569 */
570 @SuppressWarnings("hiding")
571 public void testCompareToDifferentScale() {
572 final ITmfTimestamp ts1 = new TmfTimestamp(9000, -1, 50);
573 final ITmfTimestamp ts2 = new TmfTimestamp(1000, 0, 50);
574 final ITmfTimestamp ts3 = new TmfTimestamp(110, 1, 50);
575 final ITmfTimestamp ts4 = new TmfTimestamp(1, 3, 75);
576
577 assertTrue("CompareTo", ts1.compareTo(ts1, false) == 0);
578
579 assertTrue("CompareTo", ts1.compareTo(ts2, false) < 0);
580 assertTrue("CompareTo", ts1.compareTo(ts3, false) < 0);
581 assertTrue("CompareTo", ts1.compareTo(ts4, false) < 0);
582
583 assertTrue("CompareTo", ts2.compareTo(ts1, false) > 0);
584 assertTrue("CompareTo", ts2.compareTo(ts3, false) < 0);
585 assertTrue("CompareTo", ts2.compareTo(ts4, false) == 0);
586
587 assertTrue("CompareTo", ts3.compareTo(ts1, false) > 0);
588 assertTrue("CompareTo", ts3.compareTo(ts2, false) > 0);
589 assertTrue("CompareTo", ts3.compareTo(ts4, false) > 0);
590 }
591
592 /**
593 *
594 */
595 @SuppressWarnings("hiding")
596 public void testCompareToWithinPrecision() {
597 final ITmfTimestamp ts1 = new TmfTimestamp(900, 0, 50);
598 final ITmfTimestamp ts2 = new TmfTimestamp(1000, 0, 50);
599 final ITmfTimestamp ts3 = new TmfTimestamp(1100, 0, 50);
600 final ITmfTimestamp ts4 = new TmfTimestamp(1000, 0, 75);
601
602 assertTrue("CompareTo", ts1.compareTo(ts1, true) == 0);
603
604 assertTrue("CompareTo", ts1.compareTo(ts2, true) == 0);
605 assertTrue("CompareTo", ts1.compareTo(ts3, true) < 0);
606 assertTrue("CompareTo", ts1.compareTo(ts4, true) == 0);
607
608 assertTrue("CompareTo", ts2.compareTo(ts1, true) == 0);
609 assertTrue("CompareTo", ts2.compareTo(ts3, true) == 0);
610 assertTrue("CompareTo", ts2.compareTo(ts4, true) == 0);
611
612 assertTrue("CompareTo", ts3.compareTo(ts1, true) > 0);
613 assertTrue("CompareTo", ts3.compareTo(ts2, true) == 0);
614 assertTrue("CompareTo", ts3.compareTo(ts4, true) == 0);
615 }
616
617 /**
618 *
619 */
620 @SuppressWarnings("hiding")
621 public void testCompareToLargeScale1() {
622 final ITmfTimestamp ts1 = new TmfTimestamp(-1, 100);
623 final ITmfTimestamp ts2 = new TmfTimestamp(-1000, -100);
624 final ITmfTimestamp ts3 = new TmfTimestamp(1, 100);
625 final ITmfTimestamp ts4 = new TmfTimestamp(1000, -100);
626
627 assertTrue("CompareTo", ts1.compareTo(ts2, false) < 0);
628 assertTrue("CompareTo", ts1.compareTo(ts3, false) < 0);
629 assertTrue("CompareTo", ts1.compareTo(ts4, false) < 0);
630
631 assertTrue("CompareTo", ts2.compareTo(ts1, false) > 0);
632 assertTrue("CompareTo", ts2.compareTo(ts3, false) < 0);
633 assertTrue("CompareTo", ts2.compareTo(ts4, false) < 0);
634
635 assertTrue("CompareTo", ts3.compareTo(ts1, false) > 0);
636 assertTrue("CompareTo", ts3.compareTo(ts2, false) > 0);
637 assertTrue("CompareTo", ts3.compareTo(ts4, false) > 0);
638
639 assertTrue("CompareTo", ts4.compareTo(ts1, false) > 0);
640 assertTrue("CompareTo", ts4.compareTo(ts2, false) > 0);
641 assertTrue("CompareTo", ts4.compareTo(ts3, false) < 0);
642 }
643
644 /**
645 *
646 */
647 public void testCompareToLargeScale2() {
648 final ITmfTimestamp ts0a = new TmfTimestamp(0, Integer.MAX_VALUE);
649 final ITmfTimestamp ts0b = new TmfTimestamp(1, Integer.MAX_VALUE);
650
651 assertTrue("CompareTo", ts0a.compareTo(ts0, false) == 0);
652 assertTrue("CompareTo", ts0.compareTo(ts0a, false) == 0);
653
654 assertTrue("CompareTo", ts0b.compareTo(ts0, false) == 1);
655 assertTrue("CompareTo", ts0.compareTo(ts0b, false) == -1);
656 }
657
658 // ------------------------------------------------------------------------
659 // getDelta
660 // ------------------------------------------------------------------------
661
662 /**
663 *
664 */
665 @SuppressWarnings("hiding")
666 public void testDelta() {
667 // Delta for same scale and precision (delta > 0)
668 ITmfTimestamp ts0 = new TmfTimestamp(10, 9);
669 ITmfTimestamp ts1 = new TmfTimestamp(5, 9);
670 ITmfTimestamp exp = new TmfTimestamp(5, 9);
671
672 ITmfTimestamp delta = ts0.getDelta(ts1);
673 assertEquals("getDelta", 0, delta.compareTo(exp, false));
674
675 // Delta for same scale and precision (delta < 0)
676 ts0 = new TmfTimestamp(5, 9);
677 ts1 = new TmfTimestamp(10, 9);
678 exp = new TmfTimestamp(-5, 9);
679
680 delta = ts0.getDelta(ts1);
681 assertEquals("getDelta", 0, delta.compareTo(exp, false));
682
683 // Delta for different scale and same precision (delta > 0)
684 ts0 = new TmfTimestamp(5, 9);
685 ts1 = new TmfTimestamp(10, 8);
686 exp = new TmfTimestamp(4, 9);
687
688 delta = ts0.getDelta(ts1);
689 assertEquals("getDelta", 0, delta.compareTo(exp, false));
690
691 // Delta for different scale and same precision (delta > 0)
692 ts0 = new TmfTimestamp(5, 9);
693 ts1 = new TmfTimestamp(10, 7);
694 exp = new TmfTimestamp(5, 9);
695
696 delta = ts0.getDelta(ts1);
697 assertEquals("getDelta", 0, delta.compareTo(exp, false));
698
699 // Delta for different scale and same precision
700 ts0 = new TmfTimestamp(10, 9);
701 ts1 = new TmfTimestamp(5, 8);
702 exp = new TmfTimestamp(10, 9);
703
704 delta = ts0.getDelta(ts1);
705 assertEquals("getDelta", 0, delta.compareTo(exp, false));
706
707 // Delta for same scale and different precision
708 ts0 = new TmfTimestamp(10, 9, 1);
709 ts1 = new TmfTimestamp(5, 9, 2);
710 exp = new TmfTimestamp(5, 9, 3);
711
712 delta = ts0.getDelta(ts1);
713 assertEquals("getDelta", 0, delta.compareTo(exp, true));
714 assertEquals("precision", 3, delta.getPrecision());
715
716 // Delta for same scale and different precision
717 ts0 = new TmfTimestamp(5, 9, 2);
718 ts1 = new TmfTimestamp(10, 9, 1);
719 exp = new TmfTimestamp(-5, 9, 3);
720
721 delta = ts0.getDelta(ts1);
722 assertEquals("getDelta", 0, delta.compareTo(exp, true));
723 assertEquals("precision", 3, delta.getPrecision());
724
725 // Delta for different scale and different precision
726 ts0 = new TmfTimestamp(5, 9, 2);
727 ts1 = new TmfTimestamp(10, 8, 1);
728 exp = new TmfTimestamp(4, 9, 3);
729 delta = ts0.getDelta(ts1);
730 assertEquals("getDelta", 0, delta.compareTo(exp, true));
731 assertEquals("precision", 2, delta.getPrecision());
732 }
733
734}
This page took 0.033438 seconds and 5 git commands to generate.