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