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