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