Update test coverage for TmfEventType
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / event / TmfTimestampTest.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2012 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Adjusted for new Event Model
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.tests.event;
15
16 import junit.framework.TestCase;
17
18 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
19 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
20
21 /**
22 * <b><u>TmfTimestampTest</u></b>
23 * <p>
24 * Test suite for the TmfTimestamp class.
25 */
26 @SuppressWarnings("nls")
27 public class TmfTimestampTest extends TestCase {
28
29 // ------------------------------------------------------------------------
30 // Variables
31 // ------------------------------------------------------------------------
32
33 private final ITmfTimestamp ts0 = new TmfTimestamp();
34 private final ITmfTimestamp ts1 = new TmfTimestamp(12345);
35 private final ITmfTimestamp ts2 = new TmfTimestamp(12345, -1);
36 private final ITmfTimestamp ts3 = new TmfTimestamp(12345, 2, 5);
37
38 // ------------------------------------------------------------------------
39 // Housekeping
40 // ------------------------------------------------------------------------
41
42 /**
43 * @param name the test name
44 */
45 public TmfTimestampTest(String name) {
46 super(name);
47 }
48
49 @Override
50 protected void setUp() throws Exception {
51 super.setUp();
52 }
53
54 @Override
55 protected void tearDown() throws Exception {
56 super.tearDown();
57 }
58
59 // ------------------------------------------------------------------------
60 // Constructors
61 // ------------------------------------------------------------------------
62
63 public void testDefaultConstructor() throws Exception {
64 assertEquals("getValue", 0, ts0.getValue());
65 assertEquals("getscale", 0, ts0.getScale());
66 assertEquals("getPrecision", 0, ts0.getPrecision());
67 }
68
69 public void testValueConstructor() throws Exception {
70 assertEquals("getValue", 12345, ts1.getValue());
71 assertEquals("getscale", 0, ts1.getScale());
72 assertEquals("getPrecision", 0, ts1.getPrecision());
73 }
74
75 public void testValueScaleConstructor() throws Exception {
76 assertEquals("getValue", 12345, ts2.getValue());
77 assertEquals("getscale", -1, ts2.getScale());
78 assertEquals("getPrecision", 0, ts2.getPrecision());
79 }
80
81 public void testFullConstructor() throws Exception {
82 assertEquals("getValue", 12345, ts3.getValue());
83 assertEquals("getscale", 2, ts3.getScale());
84 assertEquals("getPrecision", 5, ts3.getPrecision());
85 }
86
87 public void testCopyConstructor() throws Exception {
88 ITmfTimestamp ts = new TmfTimestamp(12345, 2, 5);
89 ITmfTimestamp copy = new TmfTimestamp(ts);
90
91 assertEquals("getValue", ts.getValue(), copy.getValue());
92 assertEquals("getscale", ts.getScale(), copy.getScale());
93 assertEquals("getPrecision", ts.getPrecision(), copy.getPrecision());
94
95 assertEquals("getValue", 12345, copy.getValue());
96 assertEquals("getscale", 2, copy.getScale());
97 assertEquals("getPrecision", 5, copy.getPrecision());
98 }
99
100 public void testCopyNullConstructor() throws Exception {
101 try {
102 @SuppressWarnings("unused")
103 ITmfTimestamp timestamp = new TmfTimestamp(null);
104 fail("null copy");
105 } catch (IllegalArgumentException e) {
106 }
107 }
108
109 public void testCopyConstructorBigBang() throws Exception {
110 ITmfTimestamp ts = new TmfTimestamp(TmfTimestamp.BigBang);
111 assertEquals("getValue", TmfTimestamp.BigBang.getValue(), ts.getValue());
112 assertEquals("getscale", TmfTimestamp.BigBang.getScale(), ts.getScale());
113 assertEquals("getPrecision", TmfTimestamp.BigBang.getPrecision(), ts.getPrecision());
114 }
115
116 public void testCopyConstructorBigCrunch() throws Exception {
117 ITmfTimestamp ts = new TmfTimestamp(TmfTimestamp.BigCrunch);
118 assertEquals("getValue", TmfTimestamp.BigCrunch.getValue(), ts.getValue());
119 assertEquals("getscale", TmfTimestamp.BigCrunch.getScale(), ts.getScale());
120 assertEquals("getPrecision", TmfTimestamp.BigCrunch.getPrecision(), ts.getPrecision());
121 }
122
123 public void testCopyConstructorZero() throws Exception {
124 ITmfTimestamp ts = new TmfTimestamp(TmfTimestamp.Zero);
125 assertEquals("getValue", TmfTimestamp.Zero.getValue(), ts.getValue());
126 assertEquals("getscale", TmfTimestamp.Zero.getScale(), ts.getScale());
127 assertEquals("getPrecision", TmfTimestamp.Zero.getPrecision(), ts.getPrecision());
128 }
129
130 // ------------------------------------------------------------------------
131 // clone
132 // ------------------------------------------------------------------------
133
134 public class MyTimestamp extends TmfTimestamp {
135
136 @Override
137 public boolean equals(Object other) {
138 return super.equals(other);
139 }
140
141 @Override
142 public MyTimestamp clone() {
143 return (MyTimestamp) super.clone();
144 }
145 }
146
147 public void testClone() throws Exception {
148 ITmfTimestamp clone = ts0.clone();
149 assertEquals("clone", clone, ts0);
150 }
151
152 public void testClone2() throws Exception {
153 MyTimestamp timestamp = new MyTimestamp();
154 MyTimestamp clone = timestamp.clone();
155 assertEquals("clone", clone, timestamp);
156 }
157
158 // ------------------------------------------------------------------------
159 // hashCode
160 // ------------------------------------------------------------------------
161
162 public void testHashCode() throws Exception {
163 ITmfTimestamp ts0copy = new TmfTimestamp(ts0);
164 ITmfTimestamp ts1copy = new TmfTimestamp(ts1);
165 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 public void testEqualsReflexivity() throws Exception {
179 assertTrue("equals", ts0.equals(ts0));
180 assertTrue("equals", ts1.equals(ts1));
181
182 assertTrue("equals", !ts0.equals(ts1));
183 assertTrue("equals", !ts1.equals(ts0));
184 }
185
186 public void testEqualsSymmetry() throws Exception {
187 ITmfTimestamp ts0copy = new TmfTimestamp(ts0);
188 assertTrue("equals", ts0.equals(ts0copy));
189 assertTrue("equals", ts0copy.equals(ts0));
190
191 ITmfTimestamp ts1copy = new TmfTimestamp(ts1);
192 assertTrue("equals", ts1.equals(ts1copy));
193 assertTrue("equals", ts1copy.equals(ts1));
194
195 ITmfTimestamp ts2copy = new TmfTimestamp(ts2);
196 assertTrue("equals", ts2.equals(ts2copy));
197 assertTrue("equals", ts2copy.equals(ts2));
198 }
199
200 public void testEqualsTransivity() throws Exception {
201 ITmfTimestamp ts0copy1 = new TmfTimestamp(ts0);
202 ITmfTimestamp ts0copy2 = new TmfTimestamp(ts0copy1);
203 assertTrue("equals", ts0.equals(ts0copy1));
204 assertTrue("equals", ts0copy1.equals(ts0copy2));
205 assertTrue("equals", ts0.equals(ts0copy2));
206
207 ITmfTimestamp ts1copy1 = new TmfTimestamp(ts1);
208 ITmfTimestamp ts1copy2 = new TmfTimestamp(ts1copy1);
209 assertTrue("equals", ts1.equals(ts1copy1));
210 assertTrue("equals", ts1copy1.equals(ts1copy2));
211 assertTrue("equals", ts1.equals(ts1copy2));
212
213 ITmfTimestamp ts2copy1 = new TmfTimestamp(ts2);
214 ITmfTimestamp ts2copy2 = new TmfTimestamp(ts2copy1);
215 assertTrue("equals", ts2.equals(ts2copy1));
216 assertTrue("equals", ts2copy1.equals(ts2copy2));
217 assertTrue("equals", ts2.equals(ts2copy2));
218 }
219
220 public void testEqualsNull() throws Exception {
221 assertTrue("equals", !ts0.equals(null));
222 assertTrue("equals", !ts1.equals(null));
223 }
224
225 public void testEqualsNonTimestamp() throws Exception {
226 assertFalse("equals", ts0.equals(ts0.toString()));
227 }
228
229 // ------------------------------------------------------------------------
230 // toString
231 // ------------------------------------------------------------------------
232
233 public void testToString() throws Exception {
234 assertEquals("toString", "TmfTimestamp [fValue=0, fScale=0, fPrecision=0]", ts0.toString());
235 assertEquals("toString", "TmfTimestamp [fValue=12345, fScale=0, fPrecision=0]", ts1.toString());
236 assertEquals("toString", "TmfTimestamp [fValue=12345, fScale=-1, fPrecision=0]", ts2.toString());
237 assertEquals("toString", "TmfTimestamp [fValue=12345, fScale=2, fPrecision=5]", ts3.toString());
238 }
239
240 // ------------------------------------------------------------------------
241 // normalize
242 // ------------------------------------------------------------------------
243
244 public void testNormalizeOffset() throws Exception {
245 ITmfTimestamp ts = ts0.normalize(0, 0);
246 assertEquals("getValue", 0, ts.getValue());
247 assertEquals("getscale", 0, ts.getScale());
248 assertEquals("getPrecision", 0, ts.getPrecision());
249
250 ts = ts0.normalize(12345, 0);
251 assertEquals("getValue", 12345, ts.getValue());
252 assertEquals("getscale", 0, ts.getScale());
253 assertEquals("getPrecision", 0, ts.getPrecision());
254
255 ts = ts0.normalize(10, 0);
256 assertEquals("getValue", 10, ts.getValue());
257 assertEquals("getscale", 0, ts.getScale());
258 assertEquals("getPrecision", 0, ts.getPrecision());
259
260 ts = ts0.normalize(-10, 0);
261 assertEquals("getValue", -10, ts.getValue());
262 assertEquals("getscale", 0, ts.getScale());
263 assertEquals("getPrecision", 0, ts.getPrecision());
264 }
265
266 public void testNormalizeOffsetLowerLimits() throws Exception {
267 ITmfTimestamp ref = new TmfTimestamp(Long.MIN_VALUE + 5);
268
269 ITmfTimestamp ts = ref.normalize(-4, 0);
270 assertEquals("getValue", Long.MIN_VALUE + 1, ts.getValue());
271 assertEquals("getscale", 0, ts.getScale());
272 assertEquals("getPrecision", 0, ts.getPrecision());
273
274 ts = ref.normalize(-5, 0);
275 assertEquals("getValue", Long.MIN_VALUE, ts.getValue());
276 assertEquals("getscale", 0, ts.getScale());
277 assertEquals("getPrecision", 0, ts.getPrecision());
278
279 ts = ref.normalize(-6, 0);
280 assertEquals("getValue", Long.MIN_VALUE, ts.getValue());
281 assertEquals("getscale", 0, ts.getScale());
282 assertEquals("getPrecision", 0, ts.getPrecision());
283 }
284
285 public void testNormalizeOffsetUpperLimits() throws Exception {
286 ITmfTimestamp ref = new TmfTimestamp(Long.MAX_VALUE - 5);
287
288 ITmfTimestamp ts = ref.normalize(4, 0);
289 assertEquals("getValue", Long.MAX_VALUE - 1, ts.getValue());
290 assertEquals("getscale", 0, ts.getScale());
291 assertEquals("getPrecision", 0, ts.getPrecision());
292
293 ts = ref.normalize(5, 0);
294 assertEquals("getValue", Long.MAX_VALUE, ts.getValue());
295 assertEquals("getscale", 0, ts.getScale());
296 assertEquals("getPrecision", 0, ts.getPrecision());
297
298 ts = ref.normalize(6, 0);
299 assertEquals("getValue", Long.MAX_VALUE, ts.getValue());
300 assertEquals("getscale", 0, ts.getScale());
301 assertEquals("getPrecision", 0, ts.getPrecision());
302 }
303
304 public void testNormalizeScale() throws Exception {
305 ITmfTimestamp ts = ts0.normalize(0, 10);
306 assertEquals("getValue", 0, ts.getValue());
307 assertEquals("getscale", 10, ts.getScale());
308 assertEquals("getPrecision", 0, ts.getPrecision());
309
310 ts = ts0.normalize(0, -10);
311 assertEquals("getValue", 0, ts.getValue());
312 assertEquals("getscale", -10, ts.getScale());
313 assertEquals("getPrecision", 0, ts.getPrecision());
314 }
315
316 public void testNormalizedScaleLimits() throws Exception {
317 int MAX_SCALE_DIFF = 19;
318
319 // Test below limit
320 try {
321 ts1.normalize(0, +MAX_SCALE_DIFF - 1);
322 ts1.normalize(0, -MAX_SCALE_DIFF + 1);
323 } catch (ArithmeticException e) {
324 fail();
325 }
326
327 // Test at limit
328 try {
329 ts1.normalize(0, +MAX_SCALE_DIFF);
330 fail();
331 ts1.normalize(0, -MAX_SCALE_DIFF);
332 fail();
333 } catch (ArithmeticException e) {
334 }
335
336 // Test over limit
337 try {
338 ts1.normalize(0, +MAX_SCALE_DIFF + 1);
339 fail();
340 ts1.normalize(0, -MAX_SCALE_DIFF - 1);
341 fail();
342 } catch (ArithmeticException e) {
343 }
344 }
345
346 public void testNormalizeOffsetAndScaleTrivial() throws Exception {
347 ITmfTimestamp ts = ts0.normalize(0, 0);
348 assertEquals("getValue", 0, ts.getValue());
349 assertEquals("getscale", 0, ts.getScale());
350 assertEquals("getPrecision", 0, ts.getPrecision());
351 }
352
353 public void testNormalizeOffsetAndScale() throws Exception {
354 int SCALE = 12;
355
356 ITmfTimestamp ts = ts0.normalize(0, SCALE);
357 assertEquals("getValue", 0, ts.getValue());
358 assertEquals("getscale", SCALE, ts.getScale());
359 assertEquals("getPrecision", 0, ts.getPrecision());
360
361 ts = ts0.normalize(12345, SCALE);
362 assertEquals("getValue", 12345, ts.getValue());
363 assertEquals("getscale", SCALE, ts.getScale());
364 assertEquals("getPrecision", 0, ts.getPrecision());
365
366 ts = ts0.normalize(10, SCALE);
367 assertEquals("getValue", 10, ts.getValue());
368 assertEquals("getscale", SCALE, ts.getScale());
369 assertEquals("getPrecision", 0, ts.getPrecision());
370
371 ts = ts0.normalize(-10, SCALE);
372 assertEquals("getValue", -10, ts.getValue());
373 assertEquals("getscale", SCALE, ts.getScale());
374 assertEquals("getPrecision", 0, ts.getPrecision());
375 }
376
377 public void testNormalizeOffsetAndScale2() throws Exception {
378 int SCALE = 2;
379 ITmfTimestamp ts = ts1.normalize(0, SCALE);
380 assertEquals("getValue", 123, ts.getValue());
381 assertEquals("getscale", SCALE, ts.getScale());
382 assertEquals("getPrecision", 0, ts.getPrecision());
383
384 ts = ts1.normalize(12345, SCALE);
385 assertEquals("getValue", 12468, ts.getValue());
386 assertEquals("getscale", SCALE, ts.getScale());
387 assertEquals("getPrecision", 0, ts.getPrecision());
388
389 SCALE = -2;
390 ts = ts1.normalize(0, SCALE);
391 assertEquals("getValue", 1234500, ts.getValue());
392 assertEquals("getscale", SCALE, ts.getScale());
393 assertEquals("getPrecision", 0, ts.getPrecision());
394
395 ts = ts1.normalize(67, SCALE);
396 assertEquals("getValue", 1234567, ts.getValue());
397 assertEquals("getscale", SCALE, ts.getScale());
398 assertEquals("getPrecision", 0, ts.getPrecision());
399 }
400
401 // ------------------------------------------------------------------------
402 // compareTo
403 // ------------------------------------------------------------------------
404
405 public void testBasicCompareTo() throws Exception {
406 ITmfTimestamp ts1 = new TmfTimestamp(900, 0, 50);
407 ITmfTimestamp ts2 = new TmfTimestamp(1000, 0, 50);
408 ITmfTimestamp ts3 = new TmfTimestamp(1100, 0, 50);
409 ITmfTimestamp ts4 = new TmfTimestamp(1000, 0, 75);
410
411 assertTrue(ts1.compareTo(ts1) == 0);
412
413 assertTrue("CompareTo", ts1.compareTo(ts2) < 0);
414 assertTrue("CompareTo", ts1.compareTo(ts3) < 0);
415 assertTrue("CompareTo", ts1.compareTo(ts4) < 0);
416
417 assertTrue("CompareTo", ts2.compareTo(ts1) > 0);
418 assertTrue("CompareTo", ts2.compareTo(ts3) < 0);
419 assertTrue("CompareTo", ts2.compareTo(ts4) == 0);
420
421 assertTrue("CompareTo", ts3.compareTo(ts1) > 0);
422 assertTrue("CompareTo", ts3.compareTo(ts2) > 0);
423 assertTrue("CompareTo", ts3.compareTo(ts4) > 0);
424 }
425
426 public void testCompareToCornerCases1() throws Exception {
427 ITmfTimestamp ts0a = new TmfTimestamp(ts0);
428 ITmfTimestamp ts0b = new TmfTimestamp(ts0.getValue(), ts0.getScale() + 1);
429 ITmfTimestamp ts0c = new TmfTimestamp(ts0.getValue() + 1, ts0.getScale());
430 ITmfTimestamp ts0d = new TmfTimestamp(ts0.getValue() + 1, ts0.getScale() + 1);
431
432 assertTrue("compareTo", ts0.compareTo(ts0, false) == 0);
433 assertTrue("compareTo", ts0.compareTo(ts0a, false) == 0);
434 assertTrue("compareTo", ts0.compareTo(ts0b, false) == 0);
435 assertTrue("compareTo", ts0.compareTo(ts0c, false)== -1);
436 assertTrue("compareTo", ts0.compareTo(ts0d, false) == -1);
437 }
438
439 public void testCompareToCornerCases2() throws Exception {
440 ITmfTimestamp ts0a = new TmfTimestamp(Long.MAX_VALUE, Integer.MAX_VALUE - 1);
441 ITmfTimestamp ts0b = new TmfTimestamp(0, Integer.MAX_VALUE);
442 ITmfTimestamp ts0c = new TmfTimestamp(Long.MAX_VALUE, Integer.MAX_VALUE);
443
444 assertTrue("compareTo", ts0a.compareTo(ts0b, false)== 1);
445 assertTrue("compareTo", ts0a.compareTo(ts0c, false)== -1);
446
447 assertTrue("compareTo", ts0b.compareTo(ts0a, false)== -1);
448 assertTrue("compareTo", ts0b.compareTo(ts0c, false)== -1);
449
450 assertTrue("compareTo", ts0c.compareTo(ts0a, false)== 1);
451 assertTrue("compareTo", ts0c.compareTo(ts0b, false)== 1);
452 }
453
454 public void testCompareToCornerCases3() throws Exception {
455 ITmfTimestamp ts0a = new TmfTimestamp(Long.MIN_VALUE, Integer.MAX_VALUE - 1);
456 ITmfTimestamp ts0b = new TmfTimestamp(0, Integer.MAX_VALUE);
457 ITmfTimestamp ts0c = new TmfTimestamp(Long.MIN_VALUE, Integer.MAX_VALUE);
458
459 assertTrue("compareTo", ts0a.compareTo(ts0b, false)== -1);
460 assertTrue("compareTo", ts0a.compareTo(ts0c, false)== 1);
461
462 assertTrue("compareTo", ts0b.compareTo(ts0a, false)== 1);
463 assertTrue("compareTo", ts0b.compareTo(ts0c, false)== 1);
464
465 assertTrue("compareTo", ts0c.compareTo(ts0a, false)== -1);
466 assertTrue("compareTo", ts0c.compareTo(ts0b, false)== -1);
467 }
468
469 public void testCompareToSameScale() throws Exception {
470 ITmfTimestamp ts1 = new TmfTimestamp(900, 0, 50);
471 ITmfTimestamp ts2 = new TmfTimestamp(1000, 0, 50);
472 ITmfTimestamp ts3 = new TmfTimestamp(1100, 0, 50);
473 ITmfTimestamp ts4 = new TmfTimestamp(1000, 0, 75);
474
475 assertTrue(ts1.compareTo(ts1, false) == 0);
476
477 assertTrue("CompareTo", ts1.compareTo(ts2, false) < 0);
478 assertTrue("CompareTo", ts1.compareTo(ts3, false) < 0);
479 assertTrue("CompareTo", ts1.compareTo(ts4, false) < 0);
480
481 assertTrue("CompareTo", ts2.compareTo(ts1, false) > 0);
482 assertTrue("CompareTo", ts2.compareTo(ts3, false) < 0);
483 assertTrue("CompareTo", ts2.compareTo(ts4, false) == 0);
484
485 assertTrue("CompareTo", ts3.compareTo(ts1, false) > 0);
486 assertTrue("CompareTo", ts3.compareTo(ts2, false) > 0);
487 assertTrue("CompareTo", ts3.compareTo(ts4, false) > 0);
488 }
489
490 public void testCompareToDifferentScale() throws Exception {
491 ITmfTimestamp ts1 = new TmfTimestamp(9000, -1, 50);
492 ITmfTimestamp ts2 = new TmfTimestamp(1000, 0, 50);
493 ITmfTimestamp ts3 = new TmfTimestamp(110, 1, 50);
494 ITmfTimestamp ts4 = new TmfTimestamp(1, 3, 75);
495
496 assertTrue("CompareTo", ts1.compareTo(ts1, false) == 0);
497
498 assertTrue("CompareTo", ts1.compareTo(ts2, false) < 0);
499 assertTrue("CompareTo", ts1.compareTo(ts3, false) < 0);
500 assertTrue("CompareTo", ts1.compareTo(ts4, false) < 0);
501
502 assertTrue("CompareTo", ts2.compareTo(ts1, false) > 0);
503 assertTrue("CompareTo", ts2.compareTo(ts3, false) < 0);
504 assertTrue("CompareTo", ts2.compareTo(ts4, false) == 0);
505
506 assertTrue("CompareTo", ts3.compareTo(ts1, false) > 0);
507 assertTrue("CompareTo", ts3.compareTo(ts2, false) > 0);
508 assertTrue("CompareTo", ts3.compareTo(ts4, false) > 0);
509 }
510
511 public void testCompareToWithinPrecision() throws Exception {
512 ITmfTimestamp ts1 = new TmfTimestamp(900, 0, 50);
513 ITmfTimestamp ts2 = new TmfTimestamp(1000, 0, 50);
514 ITmfTimestamp ts3 = new TmfTimestamp(1100, 0, 50);
515 ITmfTimestamp ts4 = new TmfTimestamp(1000, 0, 75);
516
517 assertTrue("CompareTo", ts1.compareTo(ts1, true) == 0);
518
519 assertTrue("CompareTo", ts1.compareTo(ts2, true) == 0);
520 assertTrue("CompareTo", ts1.compareTo(ts3, true) < 0);
521 assertTrue("CompareTo", ts1.compareTo(ts4, true) == 0);
522
523 assertTrue("CompareTo", ts2.compareTo(ts1, true) == 0);
524 assertTrue("CompareTo", ts2.compareTo(ts3, true) == 0);
525 assertTrue("CompareTo", ts2.compareTo(ts4, true) == 0);
526
527 assertTrue("CompareTo", ts3.compareTo(ts1, true) > 0);
528 assertTrue("CompareTo", ts3.compareTo(ts2, true) == 0);
529 assertTrue("CompareTo", ts3.compareTo(ts4, true) == 0);
530 }
531
532 public void testCompareToLargeScale1() throws Exception {
533 ITmfTimestamp ts1 = new TmfTimestamp(-1, 100);
534 ITmfTimestamp ts2 = new TmfTimestamp(-1000, -100);
535 ITmfTimestamp ts3 = new TmfTimestamp(1, 100);
536 ITmfTimestamp ts4 = new TmfTimestamp(1000, -100);
537
538 assertTrue("CompareTo", ts1.compareTo(ts2, false) < 0);
539 assertTrue("CompareTo", ts1.compareTo(ts3, false) < 0);
540 assertTrue("CompareTo", ts1.compareTo(ts4, false) < 0);
541
542 assertTrue("CompareTo", ts2.compareTo(ts1, false) > 0);
543 assertTrue("CompareTo", ts2.compareTo(ts3, false) < 0);
544 assertTrue("CompareTo", ts2.compareTo(ts4, false) < 0);
545
546 assertTrue("CompareTo", ts3.compareTo(ts1, false) > 0);
547 assertTrue("CompareTo", ts3.compareTo(ts2, false) > 0);
548 assertTrue("CompareTo", ts3.compareTo(ts4, false) > 0);
549
550 assertTrue("CompareTo", ts4.compareTo(ts1, false) > 0);
551 assertTrue("CompareTo", ts4.compareTo(ts2, false) > 0);
552 assertTrue("CompareTo", ts4.compareTo(ts3, false) < 0);
553 }
554
555 public void testCompareToLargeScale2() throws Exception {
556 ITmfTimestamp ts0a = new TmfTimestamp(0, Integer.MAX_VALUE);
557 ITmfTimestamp ts0b = new TmfTimestamp(1, Integer.MAX_VALUE);
558
559 assertTrue("CompareTo", ts0a.compareTo(ts0, false) == 0);
560 assertTrue("CompareTo", ts0.compareTo(ts0a, false) == 0);
561
562 assertTrue("CompareTo", ts0b.compareTo(ts0, false) == 1);
563 assertTrue("CompareTo", ts0.compareTo(ts0b, false) == -1);
564 }
565
566 // ------------------------------------------------------------------------
567 // getDelta
568 // ------------------------------------------------------------------------
569
570 public void testDelta() throws Exception {
571 // Delta for same scale and precision (delta > 0)
572 ITmfTimestamp ts0 = new TmfTimestamp(10, 9);
573 ITmfTimestamp ts1 = new TmfTimestamp(5, 9);
574 ITmfTimestamp exp = new TmfTimestamp(5, 9);
575
576 ITmfTimestamp delta = ts0.getDelta(ts1);
577 assertEquals("getDelta", 0, delta.compareTo(exp, false));
578
579 // Delta for same scale and precision (delta < 0)
580 ts0 = new TmfTimestamp(5, 9);
581 ts1 = new TmfTimestamp(10, 9);
582 exp = new TmfTimestamp(-5, 9);
583
584 delta = ts0.getDelta(ts1);
585 assertEquals("getDelta", 0, delta.compareTo(exp, false));
586
587 // Delta for different scale and same precision (delta > 0)
588 ts0 = new TmfTimestamp(5, 9);
589 ts1 = new TmfTimestamp(10, 8);
590 exp = new TmfTimestamp(4, 9);
591
592 delta = ts0.getDelta(ts1);
593 assertEquals("getDelta", 0, delta.compareTo(exp, false));
594
595 // Delta for different scale and same precision (delta > 0)
596 ts0 = new TmfTimestamp(5, 9);
597 ts1 = new TmfTimestamp(10, 7);
598 exp = new TmfTimestamp(5, 9);
599
600 delta = ts0.getDelta(ts1);
601 assertEquals("getDelta", 0, delta.compareTo(exp, false));
602
603 // Delta for different scale and same precision
604 ts0 = new TmfTimestamp(10, 9);
605 ts1 = new TmfTimestamp(5, 8);
606 exp = new TmfTimestamp(10, 9);
607
608 delta = ts0.getDelta(ts1);
609 assertEquals("getDelta", 0, delta.compareTo(exp, false));
610
611 // Delta for same scale and different precision
612 ts0 = new TmfTimestamp(10, 9, 1);
613 ts1 = new TmfTimestamp(5, 9, 2);
614 exp = new TmfTimestamp(5, 9, 3);
615
616 delta = ts0.getDelta(ts1);
617 assertEquals("getDelta", 0, delta.compareTo(exp, true));
618 assertEquals("precision", 3, delta.getPrecision());
619
620 // Delta for same scale and different precision
621 ts0 = new TmfTimestamp(5, 9, 2);
622 ts1 = new TmfTimestamp(10, 9, 1);
623 exp = new TmfTimestamp(-5, 9, 3);
624
625 delta = ts0.getDelta(ts1);
626 assertEquals("getDelta", 0, delta.compareTo(exp, true));
627 assertEquals("precision", 3, delta.getPrecision());
628
629 // Delta for different scale and different precision
630 ts0 = new TmfTimestamp(5, 9, 2);
631 ts1 = new TmfTimestamp(10, 8, 1);
632 exp = new TmfTimestamp(4, 9, 3);
633 delta = ts0.getDelta(ts1);
634 assertEquals("getDelta", 0, delta.compareTo(exp, true));
635 assertEquals("precision", 2, delta.getPrecision());
636 }
637
638 }
This page took 0.047377 seconds and 6 git commands to generate.