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