1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.event
;
15 import junit
.framework
.TestCase
;
17 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
20 * <b><u>TmfTimestampTest</u></b>
22 * Test suite for the TmfTimestamp class.
24 @SuppressWarnings("nls")
25 public class TmfTimestampTest
extends TestCase
{
27 // ------------------------------------------------------------------------
29 // ------------------------------------------------------------------------
31 private final TmfTimestamp ts0
= new TmfTimestamp();
32 private final TmfTimestamp ts1
= new TmfTimestamp(12345);
33 private final TmfTimestamp ts2
= new TmfTimestamp(12345, (byte) -1);
34 private final TmfTimestamp ts3
= new TmfTimestamp(12345, (byte) 2, 5);
36 private final TmfTimestamp ts0copy
= new TmfTimestamp();
37 private final TmfTimestamp ts1copy
= new TmfTimestamp(12345);
39 private final TmfTimestamp ts0copy2
= new TmfTimestamp();
40 private final TmfTimestamp ts1copy2
= new TmfTimestamp(12345);
42 private final TmfTimestamp bigBang
= new TmfTimestamp(TmfTimestamp
.BigBang
);
43 private final TmfTimestamp bigCrunch
= new TmfTimestamp(TmfTimestamp
.BigCrunch
);
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
50 * @param name the test name
52 public TmfTimestampTest(String name
) {
57 protected void setUp() throws Exception
{
62 protected void tearDown() throws Exception
{
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
70 public void testDefaultConstructor() throws Exception
{
71 assertEquals("getValue", 0, ts0
.getValue());
72 assertEquals("getscale", 0, ts0
.getScale());
73 assertEquals("getPrecision", 0, ts0
.getPrecision());
76 public void testSimpleConstructor() throws Exception
{
77 assertEquals("getValue", 12345, ts1
.getValue());
78 assertEquals("getscale", 0, ts1
.getScale());
79 assertEquals("getPrecision", 0, ts1
.getPrecision());
82 public void testSimpleConstructor2() throws Exception
{
83 assertEquals("getValue", 12345, ts2
.getValue());
84 assertEquals("getscale", -1, ts2
.getScale());
85 assertEquals("getPrecision", 0, ts2
.getPrecision());
88 public void testFullConstructor() throws Exception
{
89 assertEquals("getValue", 12345, ts3
.getValue());
90 assertEquals("getscale", 2, ts3
.getScale());
91 assertEquals("getPrecision", 5, ts3
.getPrecision());
94 public void testCopyConstructor() throws Exception
{
95 TmfTimestamp ts0
= new TmfTimestamp(12345, (byte) 2, 5);
96 TmfTimestamp ts
= new TmfTimestamp(ts0
);
97 assertEquals("getValue", 12345, ts
.getValue());
98 assertEquals("getscale", 2, ts
.getScale());
99 assertEquals("getPrecision", 5, ts
.getPrecision());
102 public void testCopyConstructor2() throws Exception
{
104 @SuppressWarnings("unused")
105 TmfTimestamp timestamp
= new TmfTimestamp(null);
108 catch (IllegalArgumentException e
) {
113 public void testCopyConstructorBigBang() throws Exception
{
114 assertEquals("getValue", TmfTimestamp
.BigBang
.getValue(), bigBang
.getValue());
115 assertEquals("getscale", TmfTimestamp
.BigBang
.getScale(), bigBang
.getScale());
116 assertEquals("getPrecision", TmfTimestamp
.BigBang
.getPrecision(), bigBang
.getPrecision());
119 public void testCopyConstructorBigCrunch() throws Exception
{
120 assertEquals("getValue", TmfTimestamp
.BigCrunch
.getValue(), bigCrunch
.getValue());
121 assertEquals("getscale", TmfTimestamp
.BigCrunch
.getScale(), bigCrunch
.getScale());
122 assertEquals("getPrecision", TmfTimestamp
.BigCrunch
.getPrecision(), bigCrunch
.getPrecision());
125 // ------------------------------------------------------------------------
127 // ------------------------------------------------------------------------
129 public void testEqualsReflexivity() throws Exception
{
130 assertTrue("equals", ts0
.equals(ts0
));
131 assertTrue("equals", ts1
.equals(ts1
));
133 assertTrue("equals", !ts0
.equals(ts1
));
134 assertTrue("equals", !ts1
.equals(ts0
));
137 public void testEqualsSymmetry() throws Exception
{
138 assertTrue("equals", ts0
.equals(ts0copy
));
139 assertTrue("equals", ts0copy
.equals(ts0
));
141 assertTrue("equals", ts1
.equals(ts1copy
));
142 assertTrue("equals", ts1copy
.equals(ts1
));
145 public void testEqualsTransivity() throws Exception
{
146 assertTrue("equals", ts0
.equals(ts0copy
));
147 assertTrue("equals", ts0copy
.equals(ts0copy2
));
148 assertTrue("equals", ts0
.equals(ts0copy2
));
150 assertTrue("equals", ts1
.equals(ts1copy
));
151 assertTrue("equals", ts1copy
.equals(ts1copy2
));
152 assertTrue("equals", ts1
.equals(ts1copy2
));
155 public void testEqualsNull() throws Exception
{
156 assertTrue("equals", !ts0
.equals(null));
157 assertTrue("equals", !ts1
.equals(null));
160 // ------------------------------------------------------------------------
162 // ------------------------------------------------------------------------
164 public void testHashCode() throws Exception
{
165 assertTrue("hashCode", ts0
.hashCode() == ts0copy
.hashCode());
166 assertTrue("hashCode", ts1
.hashCode() == ts1copy
.hashCode());
168 assertTrue("hashCode", ts0
.hashCode() != ts1
.hashCode());
171 // ------------------------------------------------------------------------
173 // ------------------------------------------------------------------------
175 public void testToString() throws Exception
{
176 assertEquals("toString", "[TmfTimestamp(0,0,0)]", ts0
.toString());
177 assertEquals("toString", "[TmfTimestamp(12345,0,0)]", ts1
.toString());
178 assertEquals("toString", "[TmfTimestamp(12345,-1,0)]", ts2
.toString());
179 assertEquals("toString", "[TmfTimestamp(12345,2,5)]", ts3
.toString());
182 // ------------------------------------------------------------------------
184 // ------------------------------------------------------------------------
186 public class MyTimestamp
extends TmfTimestamp
{
188 public boolean equals(Object other
) {
189 return super.equals(other
);
192 public MyTimestamp
clone() {
193 return (MyTimestamp
) super.clone();
197 public void testClone() throws Exception
{
198 TmfTimestamp timestamp
= ts0
.clone();
199 assertEquals("clone", timestamp
, ts0
);
202 public void testClone2() throws Exception
{
203 MyTimestamp timestamp
= new MyTimestamp();
204 MyTimestamp clone
= timestamp
.clone();
205 assertEquals("clone", clone
, timestamp
);
208 // ------------------------------------------------------------------------
210 // ------------------------------------------------------------------------
212 public void testSynchronizeOffset() throws Exception
{
214 TmfTimestamp ts
= ts0
.synchronize(0, (byte) 0);
215 assertEquals("getValue", 0, ts
.getValue());
216 assertEquals("getscale", 0, ts
.getScale());
217 assertEquals("getPrecision", 0, ts
.getPrecision());
219 ts
= ts0
.synchronize(12345, (byte) 0);
220 assertEquals("getValue", 12345, ts
.getValue());
221 assertEquals("getscale", 0, ts
.getScale());
222 assertEquals("getPrecision", 0, ts
.getPrecision());
224 ts
= ts0
.synchronize(10, (byte) 0);
225 assertEquals("getValue", 10, ts
.getValue());
226 assertEquals("getscale", 0, ts
.getScale());
227 assertEquals("getPrecision", 0, ts
.getPrecision());
229 ts
= ts0
.synchronize(-10, (byte) 0);
230 assertEquals("getValue", -10, ts
.getValue());
231 assertEquals("getscale", 0, ts
.getScale());
232 assertEquals("getPrecision", 0, ts
.getPrecision());
235 public void testSynchronizeScale() throws Exception
{
237 TmfTimestamp ts
= ts0
.synchronize(0, (byte) 10);
238 assertEquals("getValue", 0, ts
.getValue());
239 assertEquals("getscale", 10, ts
.getScale());
240 assertEquals("getPrecision", 0, ts
.getPrecision());
242 ts
= ts0
.synchronize(0, (byte) -10);
243 assertEquals("getValue", 0, ts
.getValue());
244 assertEquals("getscale", -10, ts
.getScale());
245 assertEquals("getPrecision", 0, ts
.getPrecision());
248 public void testSynchronizeOffsetAndScale() throws Exception
{
251 TmfTimestamp ts
= ts0
.synchronize(0, SCALE
);
252 assertEquals("getValue", 0, ts
.getValue());
253 assertEquals("getscale", SCALE
, ts
.getScale());
254 assertEquals("getPrecision", 0, ts
.getPrecision());
256 ts
= ts0
.synchronize(12345, SCALE
);
257 assertEquals("getValue", 12345, ts
.getValue());
258 assertEquals("getscale", SCALE
, ts
.getScale());
259 assertEquals("getPrecision", 0, ts
.getPrecision());
261 ts
= ts0
.synchronize(10, SCALE
);
262 assertEquals("getValue", 10, ts
.getValue());
263 assertEquals("getscale", SCALE
, ts
.getScale());
264 assertEquals("getPrecision", 0, ts
.getPrecision());
266 ts
= ts0
.synchronize(-10, SCALE
);
267 assertEquals("getValue", -10, ts
.getValue());
268 assertEquals("getscale", SCALE
, ts
.getScale());
269 assertEquals("getPrecision", 0, ts
.getPrecision());
272 // ------------------------------------------------------------------------
274 // ------------------------------------------------------------------------
276 public void testGetAdjustmentSameScale() throws Exception
{
277 long delta
= ts0
.getAdjustment(ts0
, ts0
.getScale());
278 assertEquals("delta", 0, delta
);
280 delta
= ts1
.getAdjustment(ts1
, ts1
.getScale());
281 assertEquals("delta", 0, delta
);
283 delta
= ts0
.getAdjustment(ts1
, ts1
.getScale());
284 assertEquals("delta", 12345, delta
);
286 delta
= ts1
.getAdjustment(ts0
, ts0
.getScale());
287 assertEquals("delta", -12345, delta
);
290 public void testGetAdjustmentDifferentScales() throws Exception
{
291 long delta
= ts0
.getAdjustment(ts2
, ts2
.getScale());
292 assertEquals("delta", 12345, delta
);
294 delta
= ts2
.getAdjustment(ts0
, ts0
.getScale());
295 assertEquals("delta", -1234, delta
);
298 // ------------------------------------------------------------------------
300 // ------------------------------------------------------------------------
302 public void testCompareToSameScale() throws Exception
{
303 TmfTimestamp ts1
= new TmfTimestamp(900, (byte) 0, 50);
304 TmfTimestamp ts2
= new TmfTimestamp(1000, (byte) 0, 50);
305 TmfTimestamp ts3
= new TmfTimestamp(1100, (byte) 0, 50);
306 TmfTimestamp ts4
= new TmfTimestamp(1000, (byte) 0, 75);
308 assertTrue(ts1
.compareTo(ts1
, false) == 0);
310 assertTrue("CompareTo", ts1
.compareTo(ts2
, false) < 0);
311 assertTrue("CompareTo", ts1
.compareTo(ts3
, false) < 0);
312 assertTrue("CompareTo", ts1
.compareTo(ts4
, false) < 0);
314 assertTrue("CompareTo", ts2
.compareTo(ts1
, false) > 0);
315 assertTrue("CompareTo", ts2
.compareTo(ts3
, false) < 0);
316 assertTrue("CompareTo", ts2
.compareTo(ts4
, false) == 0);
318 assertTrue("CompareTo", ts3
.compareTo(ts1
, false) > 0);
319 assertTrue("CompareTo", ts3
.compareTo(ts2
, false) > 0);
320 assertTrue("CompareTo", ts3
.compareTo(ts4
, false) > 0);
323 public void testCompareToDifferentScale() throws Exception
{
324 TmfTimestamp ts1
= new TmfTimestamp(9000, (byte) -1, 50);
325 TmfTimestamp ts2
= new TmfTimestamp(1000, (byte) 0, 50);
326 TmfTimestamp ts3
= new TmfTimestamp(110, (byte) 1, 50);
327 TmfTimestamp ts4
= new TmfTimestamp(1, (byte) 3, 75);
329 assertTrue("CompareTo", ts1
.compareTo(ts1
, false) == 0);
331 assertTrue("CompareTo", ts1
.compareTo(ts2
, false) < 0);
332 assertTrue("CompareTo", ts1
.compareTo(ts3
, false) < 0);
333 assertTrue("CompareTo", ts1
.compareTo(ts4
, false) < 0);
335 assertTrue("CompareTo", ts2
.compareTo(ts1
, false) > 0);
336 assertTrue("CompareTo", ts2
.compareTo(ts3
, false) < 0);
337 assertTrue("CompareTo", ts2
.compareTo(ts4
, false) == 0);
339 assertTrue("CompareTo", ts3
.compareTo(ts1
, false) > 0);
340 assertTrue("CompareTo", ts3
.compareTo(ts2
, false) > 0);
341 assertTrue("CompareTo", ts3
.compareTo(ts4
, false) > 0);
344 public void testCompareToWithinPrecision() throws Exception
{
345 TmfTimestamp ts1
= new TmfTimestamp(900, (byte) 0, 50);
346 TmfTimestamp ts2
= new TmfTimestamp(1000, (byte) 0, 50);
347 TmfTimestamp ts3
= new TmfTimestamp(1100, (byte) 0, 50);
348 TmfTimestamp ts4
= new TmfTimestamp(1000, (byte) 0, 75);
350 assertTrue("CompareTo", ts1
.compareTo(ts1
, true) == 0);
352 assertTrue("CompareTo", ts1
.compareTo(ts2
, true) == 0);
353 assertTrue("CompareTo", ts1
.compareTo(ts3
, true) < 0);
354 assertTrue("CompareTo", ts1
.compareTo(ts4
, true) == 0);
356 assertTrue("CompareTo", ts2
.compareTo(ts1
, true) == 0);
357 assertTrue("CompareTo", ts2
.compareTo(ts3
, true) == 0);
358 assertTrue("CompareTo", ts2
.compareTo(ts4
, true) == 0);
360 assertTrue("CompareTo", ts3
.compareTo(ts1
, true) > 0);
361 assertTrue("CompareTo", ts3
.compareTo(ts2
, true) == 0);
362 assertTrue("CompareTo", ts3
.compareTo(ts4
, true) == 0);
365 public void testCompareToLargeScale() throws Exception
{
366 TmfTimestamp ts1
= new TmfTimestamp(-1, (byte) 100);
367 TmfTimestamp ts2
= new TmfTimestamp(-1000, (byte) -100);
368 TmfTimestamp ts3
= new TmfTimestamp(1, (byte) 100);
369 TmfTimestamp ts4
= new TmfTimestamp(1000, (byte) -100);
371 assertTrue("CompareTo", ts1
.compareTo(ts2
, false) < 0);
372 assertTrue("CompareTo", ts1
.compareTo(ts3
, false) < 0);
373 assertTrue("CompareTo", ts1
.compareTo(ts4
, false) < 0);
375 assertTrue("CompareTo", ts2
.compareTo(ts1
, false) > 0);
376 assertTrue("CompareTo", ts2
.compareTo(ts3
, false) < 0);
377 assertTrue("CompareTo", ts2
.compareTo(ts4
, false) < 0);
379 assertTrue("CompareTo", ts3
.compareTo(ts1
, false) > 0);
380 assertTrue("CompareTo", ts3
.compareTo(ts2
, false) > 0);
381 assertTrue("CompareTo", ts3
.compareTo(ts4
, false) > 0);
383 assertTrue("CompareTo", ts4
.compareTo(ts1
, false) > 0);
384 assertTrue("CompareTo", ts4
.compareTo(ts2
, false) > 0);
385 assertTrue("CompareTo", ts4
.compareTo(ts3
, false) < 0);
388 public void testCompareToBigRanges() throws Exception
{
389 TmfTimestamp ts0a
= new TmfTimestamp( 0, Byte
.MAX_VALUE
);
390 TmfTimestamp ts0b
= new TmfTimestamp( 0, Byte
.MIN_VALUE
);
391 TmfTimestamp ts1
= new TmfTimestamp(-1, Byte
.MAX_VALUE
);
392 TmfTimestamp ts2
= new TmfTimestamp(-1, Byte
.MIN_VALUE
);
393 TmfTimestamp ts3
= new TmfTimestamp( 1, Byte
.MAX_VALUE
);
394 TmfTimestamp ts4
= new TmfTimestamp( 1, Byte
.MIN_VALUE
);
396 assertEquals("CompareTo", 1, ts0a
.compareTo(TmfTimestamp
.BigBang
, false));
397 assertEquals("CompareTo", -1, ts0a
.compareTo(TmfTimestamp
.BigCrunch
, false));
399 assertEquals("CompareTo", 1, ts0b
.compareTo(TmfTimestamp
.BigBang
, false));
400 assertEquals("CompareTo", -1, ts0b
.compareTo(TmfTimestamp
.BigCrunch
, false));
402 assertEquals("CompareTo", 0, ts0a
.compareTo(ts0b
, false));
403 assertEquals("CompareTo", 0, ts0b
.compareTo(ts0a
, false));
405 assertEquals("CompareTo", 1, ts0a
.compareTo(TmfTimestamp
.BigBang
, false));
406 assertEquals("CompareTo", -1, ts0a
.compareTo(TmfTimestamp
.BigCrunch
, false));
408 assertEquals("CompareTo", 1, ts1
.compareTo(TmfTimestamp
.BigBang
, false));
409 assertEquals("CompareTo", -1, ts1
.compareTo(TmfTimestamp
.BigCrunch
, false));
411 assertEquals("CompareTo", 1, ts2
.compareTo(TmfTimestamp
.BigBang
, false));
412 assertEquals("CompareTo", -1, ts2
.compareTo(TmfTimestamp
.BigCrunch
, false));
414 assertEquals("CompareTo", 1, ts3
.compareTo(TmfTimestamp
.BigBang
, false));
415 assertEquals("CompareTo", -1, ts3
.compareTo(TmfTimestamp
.BigCrunch
, false));
417 assertEquals("CompareTo", 1, ts4
.compareTo(TmfTimestamp
.BigBang
, false));
418 assertEquals("CompareTo", -1, ts4
.compareTo(TmfTimestamp
.BigCrunch
, false));
421 // ------------------------------------------------------------------------
423 // ------------------------------------------------------------------------
425 public void testDelta() throws Exception
{
426 // Delta for same scale and precision (delta > 0)
427 TmfTimestamp ts0
= new TmfTimestamp(10, (byte)9);
428 TmfTimestamp ts1
= new TmfTimestamp(5, (byte)9);
429 TmfTimestamp exp
= new TmfTimestamp(5, (byte)9);
431 TmfTimestamp delta
= ts0
.getDelta(ts1
);
432 assertEquals("getDelta", 0, delta
.compareTo(exp
, false));
434 // Delta for same scale and precision (delta < 0)
435 ts0
= new TmfTimestamp(5, (byte)9);
436 ts1
= new TmfTimestamp(10, (byte)9);
437 exp
= new TmfTimestamp(-5, (byte)9);
439 delta
= ts0
.getDelta(ts1
);
440 assertEquals("getDelta", 0, delta
.compareTo(exp
, false));
442 // Delta for different scale and same precision (delta > 0)
443 ts0
= new TmfTimestamp(5, (byte)9);
444 ts1
= new TmfTimestamp(10, (byte)8);
445 exp
= new TmfTimestamp(4, (byte)9);
447 delta
= ts0
.getDelta(ts1
);
448 assertEquals("getDelta", 0, delta
.compareTo(exp
, false));
450 // Delta for different scale and same precision (delta > 0)
451 ts0
= new TmfTimestamp(5, (byte)9);
452 ts1
= new TmfTimestamp(10, (byte)7);
453 exp
= new TmfTimestamp(5, (byte)9);
455 delta
= ts0
.getDelta(ts1
);
456 assertEquals("getDelta", 0, delta
.compareTo(exp
, false));
458 // Delta for different scale and same precision
459 ts0
= new TmfTimestamp(10, (byte)9);
460 ts1
= new TmfTimestamp(5, (byte)8);
461 exp
= new TmfTimestamp(10, (byte)9);
463 delta
= ts0
.getDelta(ts1
);
464 assertEquals("getDelta", 0, delta
.compareTo(exp
, false));
466 // Delta for same scale and different precision
467 ts0
= new TmfTimestamp(10, (byte)9,1);
468 ts1
= new TmfTimestamp(5, (byte)9, 2);
469 exp
= new TmfTimestamp(5, (byte)9, 2);
471 delta
= ts0
.getDelta(ts1
);
472 assertEquals("getDelta", 0, delta
.compareTo(exp
, true));
473 assertEquals("precision", 2, delta
.getPrecision());
475 // Delta for same scale and different precision
476 ts0
= new TmfTimestamp(5, (byte)9, 2);
477 ts1
= new TmfTimestamp(10, (byte)9, 1);
478 exp
= new TmfTimestamp(-5, (byte)9, 2);
480 delta
= ts0
.getDelta(ts1
);
481 assertEquals("getDelta", 0, delta
.compareTo(exp
, true));
482 assertEquals("precision", 2, delta
.getPrecision());
484 // Delta for different scale and different precision
485 ts0
= new TmfTimestamp(5, (byte)9, 2);
486 ts1
= new TmfTimestamp(10, (byte)8, 1);
487 exp
= new TmfTimestamp(4, (byte)9, 2);
488 assertEquals("precision", 2, delta
.getPrecision());
489 assertEquals("precision", 2, delta
.getPrecision());
491 delta
= ts0
.getDelta(ts1
);
492 assertEquals("getDelta", 0, delta
.compareTo(exp
, true));
493 assertEquals("precision", 2, delta
.getPrecision());