Fix warnings from FindBugs
[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 new TmfTimestamp(null);
103 fail("TmfTimestamp: null argument");
104 } catch (IllegalArgumentException e) {
105 }
106 }
107
108 public void testCopyConstructorBigBang() throws Exception {
109 ITmfTimestamp ts = new TmfTimestamp(TmfTimestamp.BigBang);
110 assertEquals("getValue", TmfTimestamp.BigBang.getValue(), ts.getValue());
111 assertEquals("getscale", TmfTimestamp.BigBang.getScale(), ts.getScale());
112 assertEquals("getPrecision", TmfTimestamp.BigBang.getPrecision(), ts.getPrecision());
113 }
114
115 public void testCopyConstructorBigCrunch() throws Exception {
116 ITmfTimestamp ts = new TmfTimestamp(TmfTimestamp.BigCrunch);
117 assertEquals("getValue", TmfTimestamp.BigCrunch.getValue(), ts.getValue());
118 assertEquals("getscale", TmfTimestamp.BigCrunch.getScale(), ts.getScale());
119 assertEquals("getPrecision", TmfTimestamp.BigCrunch.getPrecision(), ts.getPrecision());
120 }
121
122 public void testCopyConstructorZero() throws Exception {
123 ITmfTimestamp ts = new TmfTimestamp(TmfTimestamp.Zero);
124 assertEquals("getValue", TmfTimestamp.Zero.getValue(), ts.getValue());
125 assertEquals("getscale", TmfTimestamp.Zero.getScale(), ts.getScale());
126 assertEquals("getPrecision", TmfTimestamp.Zero.getPrecision(), ts.getPrecision());
127 }
128
129 // ------------------------------------------------------------------------
130 // clone
131 // ------------------------------------------------------------------------
132
133 public static class MyTimestamp extends TmfTimestamp {
134
135 @Override
136 public boolean equals(Object other) {
137 return super.equals(other);
138 }
139
140 @Override
141 public MyTimestamp clone() {
142 return (MyTimestamp) super.clone();
143 }
144 }
145
146 public void testClone() throws Exception {
147 ITmfTimestamp clone = ts0.clone();
148 assertEquals("clone", clone, ts0);
149 }
150
151 public void testClone2() throws Exception {
152 MyTimestamp timestamp = new MyTimestamp();
153 MyTimestamp clone = timestamp.clone();
154 assertEquals("clone", clone, timestamp);
155 }
156
157 // ------------------------------------------------------------------------
158 // hashCode
159 // ------------------------------------------------------------------------
160
161 public void testHashCode() throws Exception {
162 ITmfTimestamp ts0copy = new TmfTimestamp(ts0);
163 ITmfTimestamp ts1copy = new TmfTimestamp(ts1);
164 ITmfTimestamp ts2copy = new TmfTimestamp(ts2);
165
166 assertTrue("hashCode", ts0.hashCode() == ts0copy.hashCode());
167 assertTrue("hashCode", ts1.hashCode() == ts1copy.hashCode());
168 assertTrue("hashCode", ts2.hashCode() == ts2copy.hashCode());
169
170 assertTrue("hashCode", ts0.hashCode() != ts1.hashCode());
171 }
172
173 // ------------------------------------------------------------------------
174 // equals
175 // ------------------------------------------------------------------------
176
177 public void testEqualsReflexivity() throws Exception {
178 assertTrue("equals", ts0.equals(ts0));
179 assertTrue("equals", ts1.equals(ts1));
180
181 assertTrue("equals", !ts0.equals(ts1));
182 assertTrue("equals", !ts1.equals(ts0));
183 }
184
185 public void testEqualsSymmetry() throws Exception {
186 ITmfTimestamp ts0copy = new TmfTimestamp(ts0);
187 assertTrue("equals", ts0.equals(ts0copy));
188 assertTrue("equals", ts0copy.equals(ts0));
189
190 ITmfTimestamp ts1copy = new TmfTimestamp(ts1);
191 assertTrue("equals", ts1.equals(ts1copy));
192 assertTrue("equals", ts1copy.equals(ts1));
193
194 ITmfTimestamp ts2copy = new TmfTimestamp(ts2);
195 assertTrue("equals", ts2.equals(ts2copy));
196 assertTrue("equals", ts2copy.equals(ts2));
197 }
198
199 public void testEqualsTransivity() throws Exception {
200 ITmfTimestamp ts0copy1 = new TmfTimestamp(ts0);
201 ITmfTimestamp ts0copy2 = new TmfTimestamp(ts0copy1);
202 assertTrue("equals", ts0.equals(ts0copy1));
203 assertTrue("equals", ts0copy1.equals(ts0copy2));
204 assertTrue("equals", ts0.equals(ts0copy2));
205
206 ITmfTimestamp ts1copy1 = new TmfTimestamp(ts1);
207 ITmfTimestamp ts1copy2 = new TmfTimestamp(ts1copy1);
208 assertTrue("equals", ts1.equals(ts1copy1));
209 assertTrue("equals", ts1copy1.equals(ts1copy2));
210 assertTrue("equals", ts1.equals(ts1copy2));
211
212 ITmfTimestamp ts2copy1 = new TmfTimestamp(ts2);
213 ITmfTimestamp ts2copy2 = new TmfTimestamp(ts2copy1);
214 assertTrue("equals", ts2.equals(ts2copy1));
215 assertTrue("equals", ts2copy1.equals(ts2copy2));
216 assertTrue("equals", ts2.equals(ts2copy2));
217 }
218
219 public void testEqualsNull() throws Exception {
220 assertTrue("equals", !ts0.equals(null));
221 assertTrue("equals", !ts1.equals(null));
222 }
223
224 public void testEqualsNonTimestamp() throws Exception {
225 assertFalse("equals", ts0.equals(ts0.toString()));
226 }
227
228 // ------------------------------------------------------------------------
229 // toString
230 // ------------------------------------------------------------------------
231
232 public void testToString() throws Exception {
233 assertEquals("toString", "TmfTimestamp [fValue=0, fScale=0, fPrecision=0]", ts0.toString());
234 assertEquals("toString", "TmfTimestamp [fValue=12345, fScale=0, fPrecision=0]", ts1.toString());
235 assertEquals("toString", "TmfTimestamp [fValue=12345, fScale=-1, fPrecision=0]", ts2.toString());
236 assertEquals("toString", "TmfTimestamp [fValue=12345, fScale=2, fPrecision=5]", ts3.toString());
237 }
238
239 // ------------------------------------------------------------------------
240 // normalize
241 // ------------------------------------------------------------------------
242
243 public void testNormalizeOffset() throws Exception {
244 ITmfTimestamp ts = ts0.normalize(0, 0);
245 assertEquals("getValue", 0, ts.getValue());
246 assertEquals("getscale", 0, ts.getScale());
247 assertEquals("getPrecision", 0, ts.getPrecision());
248
249 ts = ts0.normalize(12345, 0);
250 assertEquals("getValue", 12345, ts.getValue());
251 assertEquals("getscale", 0, ts.getScale());
252 assertEquals("getPrecision", 0, ts.getPrecision());
253
254 ts = ts0.normalize(10, 0);
255 assertEquals("getValue", 10, ts.getValue());
256 assertEquals("getscale", 0, ts.getScale());
257 assertEquals("getPrecision", 0, ts.getPrecision());
258
259 ts = ts0.normalize(-10, 0);
260 assertEquals("getValue", -10, ts.getValue());
261 assertEquals("getscale", 0, ts.getScale());
262 assertEquals("getPrecision", 0, ts.getPrecision());
263 }
264
265 public void testNormalizeOffsetLowerLimits() throws Exception {
266 ITmfTimestamp ref = new TmfTimestamp(Long.MIN_VALUE + 5);
267
268 ITmfTimestamp ts = ref.normalize(-4, 0);
269 assertEquals("getValue", Long.MIN_VALUE + 1, ts.getValue());
270 assertEquals("getscale", 0, ts.getScale());
271 assertEquals("getPrecision", 0, ts.getPrecision());
272
273 ts = ref.normalize(-5, 0);
274 assertEquals("getValue", Long.MIN_VALUE, ts.getValue());
275 assertEquals("getscale", 0, ts.getScale());
276 assertEquals("getPrecision", 0, ts.getPrecision());
277
278 ts = ref.normalize(-6, 0);
279 assertEquals("getValue", Long.MIN_VALUE, ts.getValue());
280 assertEquals("getscale", 0, ts.getScale());
281 assertEquals("getPrecision", 0, ts.getPrecision());
282 }
283
284 public void testNormalizeOffsetUpperLimits() throws Exception {
285 ITmfTimestamp ref = new TmfTimestamp(Long.MAX_VALUE - 5);
286
287 ITmfTimestamp ts = ref.normalize(4, 0);
288 assertEquals("getValue", Long.MAX_VALUE - 1, ts.getValue());
289 assertEquals("getscale", 0, ts.getScale());
290 assertEquals("getPrecision", 0, ts.getPrecision());
291
292 ts = ref.normalize(5, 0);
293 assertEquals("getValue", Long.MAX_VALUE, ts.getValue());
294 assertEquals("getscale", 0, ts.getScale());
295 assertEquals("getPrecision", 0, ts.getPrecision());
296
297 ts = ref.normalize(6, 0);
298 assertEquals("getValue", Long.MAX_VALUE, ts.getValue());
299 assertEquals("getscale", 0, ts.getScale());
300 assertEquals("getPrecision", 0, ts.getPrecision());
301 }
302
303 public void testNormalizeScale() throws Exception {
304 ITmfTimestamp ts = ts0.normalize(0, 10);
305 assertEquals("getValue", 0, ts.getValue());
306 assertEquals("getscale", 10, ts.getScale());
307 assertEquals("getPrecision", 0, ts.getPrecision());
308
309 ts = ts0.normalize(0, -10);
310 assertEquals("getValue", 0, ts.getValue());
311 assertEquals("getscale", -10, ts.getScale());
312 assertEquals("getPrecision", 0, ts.getPrecision());
313 }
314
315 public void testNormalizedScaleLimits() throws Exception {
316 int MAX_SCALE_DIFF = 19;
317
318 // Test below limit
319 try {
320 ts1.normalize(0, +MAX_SCALE_DIFF - 1);
321 ts1.normalize(0, -MAX_SCALE_DIFF + 1);
322 } catch (ArithmeticException e) {
323 fail("normalize: scale error");
324 }
325
326 // Test at limit
327 try {
328 ts1.normalize(0, +MAX_SCALE_DIFF);
329 fail("normalize: scale error");
330 ts1.normalize(0, -MAX_SCALE_DIFF);
331 fail("normalize: scale error");
332 } catch (ArithmeticException e) {
333 }
334
335 // Test over limit
336 try {
337 ts1.normalize(0, +MAX_SCALE_DIFF + 1);
338 fail("normalize: scale error");
339 ts1.normalize(0, -MAX_SCALE_DIFF - 1);
340 fail("normalize: scale error");
341 } catch (ArithmeticException e) {
342 }
343 }
344
345 public void testNormalizeOffsetAndScaleTrivial() throws Exception {
346 ITmfTimestamp ts = ts0.normalize(0, 0);
347 assertEquals("getValue", 0, ts.getValue());
348 assertEquals("getscale", 0, ts.getScale());
349 assertEquals("getPrecision", 0, ts.getPrecision());
350 }
351
352 public void testNormalizeOffsetAndScale() throws Exception {
353 int SCALE = 12;
354
355 ITmfTimestamp ts = ts0.normalize(0, SCALE);
356 assertEquals("getValue", 0, ts.getValue());
357 assertEquals("getscale", SCALE, ts.getScale());
358 assertEquals("getPrecision", 0, ts.getPrecision());
359
360 ts = ts0.normalize(12345, SCALE);
361 assertEquals("getValue", 12345, 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 ts = ts0.normalize(-10, SCALE);
371 assertEquals("getValue", -10, ts.getValue());
372 assertEquals("getscale", SCALE, ts.getScale());
373 assertEquals("getPrecision", 0, ts.getPrecision());
374 }
375
376 public void testNormalizeOffsetAndScale2() throws Exception {
377 int SCALE = 2;
378 ITmfTimestamp ts = ts1.normalize(0, SCALE);
379 assertEquals("getValue", 123, ts.getValue());
380 assertEquals("getscale", SCALE, ts.getScale());
381 assertEquals("getPrecision", 0, ts.getPrecision());
382
383 ts = ts1.normalize(12345, SCALE);
384 assertEquals("getValue", 12468, ts.getValue());
385 assertEquals("getscale", SCALE, ts.getScale());
386 assertEquals("getPrecision", 0, ts.getPrecision());
387
388 SCALE = -2;
389 ts = ts1.normalize(0, SCALE);
390 assertEquals("getValue", 1234500, ts.getValue());
391 assertEquals("getscale", SCALE, ts.getScale());
392 assertEquals("getPrecision", 0, ts.getPrecision());
393
394 ts = ts1.normalize(67, SCALE);
395 assertEquals("getValue", 1234567, ts.getValue());
396 assertEquals("getscale", SCALE, ts.getScale());
397 assertEquals("getPrecision", 0, ts.getPrecision());
398 }
399
400 // ------------------------------------------------------------------------
401 // compareTo
402 // ------------------------------------------------------------------------
403
404 public void testBasicCompareTo() throws Exception {
405 ITmfTimestamp ts1 = new TmfTimestamp(900, 0, 50);
406 ITmfTimestamp ts2 = new TmfTimestamp(1000, 0, 50);
407 ITmfTimestamp ts3 = new TmfTimestamp(1100, 0, 50);
408 ITmfTimestamp ts4 = new TmfTimestamp(1000, 0, 75);
409
410 assertTrue(ts1.compareTo(ts1) == 0);
411
412 assertTrue("CompareTo", ts1.compareTo(ts2) < 0);
413 assertTrue("CompareTo", ts1.compareTo(ts3) < 0);
414 assertTrue("CompareTo", ts1.compareTo(ts4) < 0);
415
416 assertTrue("CompareTo", ts2.compareTo(ts1) > 0);
417 assertTrue("CompareTo", ts2.compareTo(ts3) < 0);
418 assertTrue("CompareTo", ts2.compareTo(ts4) == 0);
419
420 assertTrue("CompareTo", ts3.compareTo(ts1) > 0);
421 assertTrue("CompareTo", ts3.compareTo(ts2) > 0);
422 assertTrue("CompareTo", ts3.compareTo(ts4) > 0);
423 }
424
425 public void testCompareToCornerCases1() throws Exception {
426 ITmfTimestamp ts0a = new TmfTimestamp(ts0);
427 ITmfTimestamp ts0b = new TmfTimestamp(ts0.getValue(), ts0.getScale() + 1);
428 ITmfTimestamp ts0c = new TmfTimestamp(ts0.getValue() + 1, ts0.getScale());
429 ITmfTimestamp ts0d = new TmfTimestamp(ts0.getValue() + 1, ts0.getScale() + 1);
430
431 assertTrue("compareTo", ts0.compareTo(ts0, false) == 0);
432 assertTrue("compareTo", ts0.compareTo(ts0a, false) == 0);
433 assertTrue("compareTo", ts0.compareTo(ts0b, false) == 0);
434 assertTrue("compareTo", ts0.compareTo(ts0c, false)== -1);
435 assertTrue("compareTo", ts0.compareTo(ts0d, false) == -1);
436 }
437
438 public void testCompareToCornerCases2() throws Exception {
439 ITmfTimestamp ts0a = new TmfTimestamp(Long.MAX_VALUE, Integer.MAX_VALUE - 1);
440 ITmfTimestamp ts0b = new TmfTimestamp(0, Integer.MAX_VALUE);
441 ITmfTimestamp ts0c = new TmfTimestamp(Long.MAX_VALUE, Integer.MAX_VALUE);
442
443 assertTrue("compareTo", ts0a.compareTo(ts0b, false)== 1);
444 assertTrue("compareTo", ts0a.compareTo(ts0c, false)== -1);
445
446 assertTrue("compareTo", ts0b.compareTo(ts0a, false)== -1);
447 assertTrue("compareTo", ts0b.compareTo(ts0c, false)== -1);
448
449 assertTrue("compareTo", ts0c.compareTo(ts0a, false)== 1);
450 assertTrue("compareTo", ts0c.compareTo(ts0b, false)== 1);
451 }
452
453 public void testCompareToCornerCases3() throws Exception {
454 ITmfTimestamp ts0a = new TmfTimestamp(Long.MIN_VALUE, Integer.MAX_VALUE - 1);
455 ITmfTimestamp ts0b = new TmfTimestamp(0, Integer.MAX_VALUE);
456 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 public void testCompareToSameScale() throws Exception {
469 ITmfTimestamp ts1 = new TmfTimestamp(900, 0, 50);
470 ITmfTimestamp ts2 = new TmfTimestamp(1000, 0, 50);
471 ITmfTimestamp ts3 = new TmfTimestamp(1100, 0, 50);
472 ITmfTimestamp ts4 = new TmfTimestamp(1000, 0, 75);
473
474 assertTrue(ts1.compareTo(ts1, false) == 0);
475
476 assertTrue("CompareTo", ts1.compareTo(ts2, false) < 0);
477 assertTrue("CompareTo", ts1.compareTo(ts3, false) < 0);
478 assertTrue("CompareTo", ts1.compareTo(ts4, false) < 0);
479
480 assertTrue("CompareTo", ts2.compareTo(ts1, false) > 0);
481 assertTrue("CompareTo", ts2.compareTo(ts3, false) < 0);
482 assertTrue("CompareTo", ts2.compareTo(ts4, false) == 0);
483
484 assertTrue("CompareTo", ts3.compareTo(ts1, false) > 0);
485 assertTrue("CompareTo", ts3.compareTo(ts2, false) > 0);
486 assertTrue("CompareTo", ts3.compareTo(ts4, false) > 0);
487 }
488
489 public void testCompareToDifferentScale() throws Exception {
490 ITmfTimestamp ts1 = new TmfTimestamp(9000, -1, 50);
491 ITmfTimestamp ts2 = new TmfTimestamp(1000, 0, 50);
492 ITmfTimestamp ts3 = new TmfTimestamp(110, 1, 50);
493 ITmfTimestamp ts4 = new TmfTimestamp(1, 3, 75);
494
495 assertTrue("CompareTo", ts1.compareTo(ts1, false) == 0);
496
497 assertTrue("CompareTo", ts1.compareTo(ts2, false) < 0);
498 assertTrue("CompareTo", ts1.compareTo(ts3, false) < 0);
499 assertTrue("CompareTo", ts1.compareTo(ts4, false) < 0);
500
501 assertTrue("CompareTo", ts2.compareTo(ts1, false) > 0);
502 assertTrue("CompareTo", ts2.compareTo(ts3, false) < 0);
503 assertTrue("CompareTo", ts2.compareTo(ts4, false) == 0);
504
505 assertTrue("CompareTo", ts3.compareTo(ts1, false) > 0);
506 assertTrue("CompareTo", ts3.compareTo(ts2, false) > 0);
507 assertTrue("CompareTo", ts3.compareTo(ts4, false) > 0);
508 }
509
510 public void testCompareToWithinPrecision() throws Exception {
511 ITmfTimestamp ts1 = new TmfTimestamp(900, 0, 50);
512 ITmfTimestamp ts2 = new TmfTimestamp(1000, 0, 50);
513 ITmfTimestamp ts3 = new TmfTimestamp(1100, 0, 50);
514 ITmfTimestamp ts4 = new TmfTimestamp(1000, 0, 75);
515
516 assertTrue("CompareTo", ts1.compareTo(ts1, true) == 0);
517
518 assertTrue("CompareTo", ts1.compareTo(ts2, true) == 0);
519 assertTrue("CompareTo", ts1.compareTo(ts3, true) < 0);
520 assertTrue("CompareTo", ts1.compareTo(ts4, true) == 0);
521
522 assertTrue("CompareTo", ts2.compareTo(ts1, true) == 0);
523 assertTrue("CompareTo", ts2.compareTo(ts3, true) == 0);
524 assertTrue("CompareTo", ts2.compareTo(ts4, true) == 0);
525
526 assertTrue("CompareTo", ts3.compareTo(ts1, true) > 0);
527 assertTrue("CompareTo", ts3.compareTo(ts2, true) == 0);
528 assertTrue("CompareTo", ts3.compareTo(ts4, true) == 0);
529 }
530
531 public void testCompareToLargeScale1() throws Exception {
532 ITmfTimestamp ts1 = new TmfTimestamp(-1, 100);
533 ITmfTimestamp ts2 = new TmfTimestamp(-1000, -100);
534 ITmfTimestamp ts3 = new TmfTimestamp(1, 100);
535 ITmfTimestamp ts4 = new TmfTimestamp(1000, -100);
536
537 assertTrue("CompareTo", ts1.compareTo(ts2, false) < 0);
538 assertTrue("CompareTo", ts1.compareTo(ts3, false) < 0);
539 assertTrue("CompareTo", ts1.compareTo(ts4, false) < 0);
540
541 assertTrue("CompareTo", ts2.compareTo(ts1, false) > 0);
542 assertTrue("CompareTo", ts2.compareTo(ts3, false) < 0);
543 assertTrue("CompareTo", ts2.compareTo(ts4, false) < 0);
544
545 assertTrue("CompareTo", ts3.compareTo(ts1, false) > 0);
546 assertTrue("CompareTo", ts3.compareTo(ts2, false) > 0);
547 assertTrue("CompareTo", ts3.compareTo(ts4, false) > 0);
548
549 assertTrue("CompareTo", ts4.compareTo(ts1, false) > 0);
550 assertTrue("CompareTo", ts4.compareTo(ts2, false) > 0);
551 assertTrue("CompareTo", ts4.compareTo(ts3, false) < 0);
552 }
553
554 public void testCompareToLargeScale2() throws Exception {
555 ITmfTimestamp ts0a = new TmfTimestamp(0, Integer.MAX_VALUE);
556 ITmfTimestamp ts0b = new TmfTimestamp(1, Integer.MAX_VALUE);
557
558 assertTrue("CompareTo", ts0a.compareTo(ts0, false) == 0);
559 assertTrue("CompareTo", ts0.compareTo(ts0a, false) == 0);
560
561 assertTrue("CompareTo", ts0b.compareTo(ts0, false) == 1);
562 assertTrue("CompareTo", ts0.compareTo(ts0b, false) == -1);
563 }
564
565 // ------------------------------------------------------------------------
566 // getDelta
567 // ------------------------------------------------------------------------
568
569 public void testDelta() throws Exception {
570 // Delta for same scale and precision (delta > 0)
571 ITmfTimestamp ts0 = new TmfTimestamp(10, 9);
572 ITmfTimestamp ts1 = new TmfTimestamp(5, 9);
573 ITmfTimestamp exp = new TmfTimestamp(5, 9);
574
575 ITmfTimestamp delta = ts0.getDelta(ts1);
576 assertEquals("getDelta", 0, delta.compareTo(exp, false));
577
578 // Delta for same scale and precision (delta < 0)
579 ts0 = new TmfTimestamp(5, 9);
580 ts1 = new TmfTimestamp(10, 9);
581 exp = new TmfTimestamp(-5, 9);
582
583 delta = ts0.getDelta(ts1);
584 assertEquals("getDelta", 0, delta.compareTo(exp, false));
585
586 // Delta for different scale and same precision (delta > 0)
587 ts0 = new TmfTimestamp(5, 9);
588 ts1 = new TmfTimestamp(10, 8);
589 exp = new TmfTimestamp(4, 9);
590
591 delta = ts0.getDelta(ts1);
592 assertEquals("getDelta", 0, delta.compareTo(exp, false));
593
594 // Delta for different scale and same precision (delta > 0)
595 ts0 = new TmfTimestamp(5, 9);
596 ts1 = new TmfTimestamp(10, 7);
597 exp = new TmfTimestamp(5, 9);
598
599 delta = ts0.getDelta(ts1);
600 assertEquals("getDelta", 0, delta.compareTo(exp, false));
601
602 // Delta for different scale and same precision
603 ts0 = new TmfTimestamp(10, 9);
604 ts1 = new TmfTimestamp(5, 8);
605 exp = new TmfTimestamp(10, 9);
606
607 delta = ts0.getDelta(ts1);
608 assertEquals("getDelta", 0, delta.compareTo(exp, false));
609
610 // Delta for same scale and different precision
611 ts0 = new TmfTimestamp(10, 9, 1);
612 ts1 = new TmfTimestamp(5, 9, 2);
613 exp = new TmfTimestamp(5, 9, 3);
614
615 delta = ts0.getDelta(ts1);
616 assertEquals("getDelta", 0, delta.compareTo(exp, true));
617 assertEquals("precision", 3, delta.getPrecision());
618
619 // Delta for same scale and different precision
620 ts0 = new TmfTimestamp(5, 9, 2);
621 ts1 = new TmfTimestamp(10, 9, 1);
622 exp = new TmfTimestamp(-5, 9, 3);
623
624 delta = ts0.getDelta(ts1);
625 assertEquals("getDelta", 0, delta.compareTo(exp, true));
626 assertEquals("precision", 3, delta.getPrecision());
627
628 // Delta for different scale and different precision
629 ts0 = new TmfTimestamp(5, 9, 2);
630 ts1 = new TmfTimestamp(10, 8, 1);
631 exp = new TmfTimestamp(4, 9, 3);
632 delta = ts0.getDelta(ts1);
633 assertEquals("getDelta", 0, delta.compareTo(exp, true));
634 assertEquals("precision", 2, delta.getPrecision());
635 }
636
637 }
This page took 0.054716 seconds and 6 git commands to generate.