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
.tests
.event
;
15 import junit
.framework
.TestCase
;
17 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
20 * <b><u>TmfTimestampTest</u></b>
22 * Test suite for the TmfTimestamp class.
24 public class TmfTimestampTest
extends TestCase
{
26 // ------------------------------------------------------------------------
28 // ------------------------------------------------------------------------
30 private final TmfTimestamp ts0
= new TmfTimestamp();
31 private final TmfTimestamp ts1
= new TmfTimestamp(12345);
32 private final TmfTimestamp ts2
= new TmfTimestamp(12345, (byte) -1);
33 private final TmfTimestamp ts3
= new TmfTimestamp(12345, (byte) 2, 5);
35 private final TmfTimestamp ts0copy
= new TmfTimestamp();
36 private final TmfTimestamp ts1copy
= new TmfTimestamp(12345);
38 private final TmfTimestamp ts0copy2
= new TmfTimestamp();
39 private final TmfTimestamp ts1copy2
= new TmfTimestamp(12345);
41 private final TmfTimestamp bigBang
= new TmfTimestamp(TmfTimestamp
.BigBang
);
42 private final TmfTimestamp bigCrunch
= new TmfTimestamp(TmfTimestamp
.BigCrunch
);
44 // ------------------------------------------------------------------------
46 // ------------------------------------------------------------------------
49 * @param name the test name
51 public TmfTimestampTest(String name
) {
56 protected void setUp() throws Exception
{
61 protected void tearDown() throws Exception
{
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
69 public void testDefaultConstructor() throws Exception
{
70 assertEquals("getValue", 0, ts0
.getValue());
71 assertEquals("getscale", 0, ts0
.getScale());
72 assertEquals("getPrecision", 0, ts0
.getPrecision());
75 public void testSimpleConstructor() throws Exception
{
76 assertEquals("getValue", 12345, ts1
.getValue());
77 assertEquals("getscale", 0, ts1
.getScale());
78 assertEquals("getPrecision", 0, ts1
.getPrecision());
81 public void testSimpleConstructor2() throws Exception
{
82 assertEquals("getValue", 12345, ts2
.getValue());
83 assertEquals("getscale", -1, ts2
.getScale());
84 assertEquals("getPrecision", 0, ts2
.getPrecision());
87 public void testFullConstructor() throws Exception
{
88 assertEquals("getValue", 12345, ts3
.getValue());
89 assertEquals("getscale", 2, ts3
.getScale());
90 assertEquals("getPrecision", 5, ts3
.getPrecision());
93 public void testCopyConstructor() throws Exception
{
94 TmfTimestamp ts0
= new TmfTimestamp(12345, (byte) 2, 5);
95 TmfTimestamp ts
= new TmfTimestamp(ts0
);
96 assertEquals("getValue", 12345, ts
.getValue());
97 assertEquals("getscale", 2, ts
.getScale());
98 assertEquals("getPrecision", 5, ts
.getPrecision());
101 public void testCopyConstructor2() throws Exception
{
103 @SuppressWarnings("unused")
104 TmfTimestamp timestamp
= new TmfTimestamp(null);
107 catch (IllegalArgumentException e
) {
112 public void testCopyConstructorBigBang() throws Exception
{
113 assertEquals("getValue", TmfTimestamp
.BigBang
.getValue(), bigBang
.getValue());
114 assertEquals("getscale", TmfTimestamp
.BigBang
.getScale(), bigBang
.getScale());
115 assertEquals("getPrecision", TmfTimestamp
.BigBang
.getPrecision(), bigBang
.getPrecision());
118 public void testCopyConstructorBigCrunch() throws Exception
{
119 assertEquals("getValue", TmfTimestamp
.BigCrunch
.getValue(), bigCrunch
.getValue());
120 assertEquals("getscale", TmfTimestamp
.BigCrunch
.getScale(), bigCrunch
.getScale());
121 assertEquals("getPrecision", TmfTimestamp
.BigCrunch
.getPrecision(), bigCrunch
.getPrecision());
124 // ------------------------------------------------------------------------
126 // ------------------------------------------------------------------------
128 public void testEqualsReflexivity() throws Exception
{
129 assertTrue("equals", ts0
.equals(ts0
));
130 assertTrue("equals", ts1
.equals(ts1
));
132 assertTrue("equals", !ts0
.equals(ts1
));
133 assertTrue("equals", !ts1
.equals(ts0
));
136 public void testEqualsSymmetry() throws Exception
{
137 assertTrue("equals", ts0
.equals(ts0copy
));
138 assertTrue("equals", ts0copy
.equals(ts0
));
140 assertTrue("equals", ts1
.equals(ts1copy
));
141 assertTrue("equals", ts1copy
.equals(ts1
));
144 public void testEqualsTransivity() throws Exception
{
145 assertTrue("equals", ts0
.equals(ts0copy
));
146 assertTrue("equals", ts0copy
.equals(ts0copy2
));
147 assertTrue("equals", ts0
.equals(ts0copy2
));
149 assertTrue("equals", ts1
.equals(ts1copy
));
150 assertTrue("equals", ts1copy
.equals(ts1copy2
));
151 assertTrue("equals", ts1
.equals(ts1copy2
));
154 public void testEqualsNull() throws Exception
{
155 assertTrue("equals", !ts0
.equals(null));
156 assertTrue("equals", !ts1
.equals(null));
159 // ------------------------------------------------------------------------
161 // ------------------------------------------------------------------------
163 public void testHashCode() throws Exception
{
164 assertTrue("hashCode", ts0
.hashCode() == ts0copy
.hashCode());
165 assertTrue("hashCode", ts1
.hashCode() == ts1copy
.hashCode());
167 assertTrue("hashCode", ts0
.hashCode() != ts1
.hashCode());
170 // ------------------------------------------------------------------------
172 // ------------------------------------------------------------------------
174 public void testToString() throws Exception
{
175 assertEquals("toString", "[TmfTimestamp(0,0,0)]", ts0
.toString());
176 assertEquals("toString", "[TmfTimestamp(12345,0,0)]", ts1
.toString());
177 assertEquals("toString", "[TmfTimestamp(12345,-1,0)]", ts2
.toString());
178 assertEquals("toString", "[TmfTimestamp(12345,2,5)]", ts3
.toString());
181 // ------------------------------------------------------------------------
183 // ------------------------------------------------------------------------
185 public class MyTimestamp
extends TmfTimestamp
{
187 public boolean equals(Object other
) {
188 return super.equals(other
);
191 public MyTimestamp
clone() {
192 return (MyTimestamp
) super.clone();
196 public void testClone() throws Exception
{
197 TmfTimestamp timestamp
= ts0
.clone();
198 assertEquals("clone", timestamp
, ts0
);
201 public void testClone2() throws Exception
{
202 MyTimestamp timestamp
= new MyTimestamp();
203 MyTimestamp clone
= timestamp
.clone();
204 assertEquals("clone", clone
, timestamp
);
207 // ------------------------------------------------------------------------
209 // ------------------------------------------------------------------------
211 public void testSynchronizeOffset() throws Exception
{
213 TmfTimestamp ts
= ts0
.synchronize(0, (byte) 0);
214 assertEquals("getValue", 0, ts
.getValue());
215 assertEquals("getscale", 0, ts
.getScale());
216 assertEquals("getPrecision", 0, ts
.getPrecision());
218 ts
= ts0
.synchronize(12345, (byte) 0);
219 assertEquals("getValue", 12345, ts
.getValue());
220 assertEquals("getscale", 0, ts
.getScale());
221 assertEquals("getPrecision", 0, ts
.getPrecision());
223 ts
= ts0
.synchronize(10, (byte) 0);
224 assertEquals("getValue", 10, ts
.getValue());
225 assertEquals("getscale", 0, ts
.getScale());
226 assertEquals("getPrecision", 0, ts
.getPrecision());
228 ts
= ts0
.synchronize(-10, (byte) 0);
229 assertEquals("getValue", -10, ts
.getValue());
230 assertEquals("getscale", 0, ts
.getScale());
231 assertEquals("getPrecision", 0, ts
.getPrecision());
234 public void testSynchronizeScale() throws Exception
{
236 TmfTimestamp ts
= ts0
.synchronize(0, (byte) 10);
237 assertEquals("getValue", 0, ts
.getValue());
238 assertEquals("getscale", 10, ts
.getScale());
239 assertEquals("getPrecision", 0, ts
.getPrecision());
241 ts
= ts0
.synchronize(0, (byte) -10);
242 assertEquals("getValue", 0, ts
.getValue());
243 assertEquals("getscale", -10, ts
.getScale());
244 assertEquals("getPrecision", 0, ts
.getPrecision());
247 public void testSynchronizeOffsetAndScale() throws Exception
{
250 TmfTimestamp ts
= ts0
.synchronize(0, SCALE
);
251 assertEquals("getValue", 0, ts
.getValue());
252 assertEquals("getscale", SCALE
, ts
.getScale());
253 assertEquals("getPrecision", 0, ts
.getPrecision());
255 ts
= ts0
.synchronize(12345, SCALE
);
256 assertEquals("getValue", 12345, ts
.getValue());
257 assertEquals("getscale", SCALE
, ts
.getScale());
258 assertEquals("getPrecision", 0, ts
.getPrecision());
260 ts
= ts0
.synchronize(10, SCALE
);
261 assertEquals("getValue", 10, ts
.getValue());
262 assertEquals("getscale", SCALE
, ts
.getScale());
263 assertEquals("getPrecision", 0, ts
.getPrecision());
265 ts
= ts0
.synchronize(-10, SCALE
);
266 assertEquals("getValue", -10, ts
.getValue());
267 assertEquals("getscale", SCALE
, ts
.getScale());
268 assertEquals("getPrecision", 0, ts
.getPrecision());
271 // ------------------------------------------------------------------------
273 // ------------------------------------------------------------------------
275 public void testGetAdjustmentSameScale() throws Exception
{
276 long delta
= ts0
.getAdjustment(ts0
, ts0
.getScale());
277 assertEquals("delta", 0, delta
);
279 delta
= ts1
.getAdjustment(ts1
, ts1
.getScale());
280 assertEquals("delta", 0, delta
);
282 delta
= ts0
.getAdjustment(ts1
, ts1
.getScale());
283 assertEquals("delta", 12345, delta
);
285 delta
= ts1
.getAdjustment(ts0
, ts0
.getScale());
286 assertEquals("delta", -12345, delta
);
289 public void testGetAdjustmentDifferentScales() throws Exception
{
290 long delta
= ts0
.getAdjustment(ts2
, ts2
.getScale());
291 assertEquals("delta", 12345, delta
);
293 delta
= ts2
.getAdjustment(ts0
, ts0
.getScale());
294 assertEquals("delta", -1234, delta
);
297 // ------------------------------------------------------------------------
299 // ------------------------------------------------------------------------
301 public void testCompareToSameScale() throws Exception
{
302 TmfTimestamp ts1
= new TmfTimestamp(900, (byte) 0, 50);
303 TmfTimestamp ts2
= new TmfTimestamp(1000, (byte) 0, 50);
304 TmfTimestamp ts3
= new TmfTimestamp(1100, (byte) 0, 50);
305 TmfTimestamp ts4
= new TmfTimestamp(1000, (byte) 0, 75);
307 assertTrue(ts1
.compareTo(ts1
, false) == 0);
309 assertTrue("CompareTo", ts1
.compareTo(ts2
, false) < 0);
310 assertTrue("CompareTo", ts1
.compareTo(ts3
, false) < 0);
311 assertTrue("CompareTo", ts1
.compareTo(ts4
, false) < 0);
313 assertTrue("CompareTo", ts2
.compareTo(ts1
, false) > 0);
314 assertTrue("CompareTo", ts2
.compareTo(ts3
, false) < 0);
315 assertTrue("CompareTo", ts2
.compareTo(ts4
, false) == 0);
317 assertTrue("CompareTo", ts3
.compareTo(ts1
, false) > 0);
318 assertTrue("CompareTo", ts3
.compareTo(ts2
, false) > 0);
319 assertTrue("CompareTo", ts3
.compareTo(ts4
, false) > 0);
322 public void testCompareToDifferentScale() throws Exception
{
323 TmfTimestamp ts1
= new TmfTimestamp(9000, (byte) -1, 50);
324 TmfTimestamp ts2
= new TmfTimestamp(1000, (byte) 0, 50);
325 TmfTimestamp ts3
= new TmfTimestamp(110, (byte) 1, 50);
326 TmfTimestamp ts4
= new TmfTimestamp(1, (byte) 3, 75);
328 assertTrue("CompareTo", ts1
.compareTo(ts1
, false) == 0);
330 assertTrue("CompareTo", ts1
.compareTo(ts2
, false) < 0);
331 assertTrue("CompareTo", ts1
.compareTo(ts3
, false) < 0);
332 assertTrue("CompareTo", ts1
.compareTo(ts4
, false) < 0);
334 assertTrue("CompareTo", ts2
.compareTo(ts1
, false) > 0);
335 assertTrue("CompareTo", ts2
.compareTo(ts3
, false) < 0);
336 assertTrue("CompareTo", ts2
.compareTo(ts4
, false) == 0);
338 assertTrue("CompareTo", ts3
.compareTo(ts1
, false) > 0);
339 assertTrue("CompareTo", ts3
.compareTo(ts2
, false) > 0);
340 assertTrue("CompareTo", ts3
.compareTo(ts4
, false) > 0);
343 public void testCompareToWithinPrecision() throws Exception
{
344 TmfTimestamp ts1
= new TmfTimestamp(900, (byte) 0, 50);
345 TmfTimestamp ts2
= new TmfTimestamp(1000, (byte) 0, 50);
346 TmfTimestamp ts3
= new TmfTimestamp(1100, (byte) 0, 50);
347 TmfTimestamp ts4
= new TmfTimestamp(1000, (byte) 0, 75);
349 assertTrue("CompareTo", ts1
.compareTo(ts1
, true) == 0);
351 assertTrue("CompareTo", ts1
.compareTo(ts2
, true) == 0);
352 assertTrue("CompareTo", ts1
.compareTo(ts3
, true) < 0);
353 assertTrue("CompareTo", ts1
.compareTo(ts4
, true) == 0);
355 assertTrue("CompareTo", ts2
.compareTo(ts1
, true) == 0);
356 assertTrue("CompareTo", ts2
.compareTo(ts3
, true) == 0);
357 assertTrue("CompareTo", ts2
.compareTo(ts4
, true) == 0);
359 assertTrue("CompareTo", ts3
.compareTo(ts1
, true) > 0);
360 assertTrue("CompareTo", ts3
.compareTo(ts2
, true) == 0);
361 assertTrue("CompareTo", ts3
.compareTo(ts4
, true) == 0);
364 public void testCompareToLargeScale() throws Exception
{
365 TmfTimestamp ts1
= new TmfTimestamp(-1, (byte) 100);
366 TmfTimestamp ts2
= new TmfTimestamp(-1000, (byte) -100);
367 TmfTimestamp ts3
= new TmfTimestamp(1, (byte) 100);
368 TmfTimestamp ts4
= new TmfTimestamp(1000, (byte) -100);
370 assertTrue("CompareTo", ts1
.compareTo(ts2
, false) < 0);
371 assertTrue("CompareTo", ts1
.compareTo(ts3
, false) < 0);
372 assertTrue("CompareTo", ts1
.compareTo(ts4
, false) < 0);
374 assertTrue("CompareTo", ts2
.compareTo(ts1
, false) > 0);
375 assertTrue("CompareTo", ts2
.compareTo(ts3
, false) < 0);
376 assertTrue("CompareTo", ts2
.compareTo(ts4
, false) < 0);
378 assertTrue("CompareTo", ts3
.compareTo(ts1
, false) > 0);
379 assertTrue("CompareTo", ts3
.compareTo(ts2
, false) > 0);
380 assertTrue("CompareTo", ts3
.compareTo(ts4
, false) > 0);
382 assertTrue("CompareTo", ts4
.compareTo(ts1
, false) > 0);
383 assertTrue("CompareTo", ts4
.compareTo(ts2
, false) > 0);
384 assertTrue("CompareTo", ts4
.compareTo(ts3
, false) < 0);
387 public void testCompareToBigRanges() throws Exception
{
388 TmfTimestamp ts0a
= new TmfTimestamp(0, Byte
.MAX_VALUE
);
389 TmfTimestamp ts0b
= new TmfTimestamp(0, Byte
.MIN_VALUE
);
390 TmfTimestamp ts1
= new TmfTimestamp(-1, Byte
.MAX_VALUE
);
391 TmfTimestamp ts2
= new TmfTimestamp(-1, Byte
.MIN_VALUE
);
392 TmfTimestamp ts3
= new TmfTimestamp(1, Byte
.MAX_VALUE
);
393 TmfTimestamp ts4
= new TmfTimestamp(1, Byte
.MIN_VALUE
);
395 assertTrue("CompareTo", ts0a
.compareTo(TmfTimestamp
.BigBang
, false) > 0);
396 assertTrue("CompareTo", ts0a
.compareTo(TmfTimestamp
.BigCrunch
, false) < 0);
398 assertTrue("CompareTo", ts0b
.compareTo(TmfTimestamp
.BigBang
, false) > 0);
399 assertTrue("CompareTo", ts0b
.compareTo(TmfTimestamp
.BigCrunch
, false) < 0);
401 assertTrue("CompareTo", ts0a
.compareTo(ts0b
, false) == 0);
402 assertTrue("CompareTo", ts0b
.compareTo(ts0a
, false) == 0);
404 assertTrue("CompareTo", ts0a
.compareTo(TmfTimestamp
.BigBang
, false) > 0);
405 assertTrue("CompareTo", ts0a
.compareTo(TmfTimestamp
.BigCrunch
, false) < 0);
407 assertTrue("CompareTo", ts1
.compareTo(TmfTimestamp
.BigBang
, false) > 0);
408 assertTrue("CompareTo", ts1
.compareTo(TmfTimestamp
.BigCrunch
, false) < 0);
410 assertTrue("CompareTo", ts2
.compareTo(TmfTimestamp
.BigBang
, false) > 0);
411 assertTrue("CompareTo", ts2
.compareTo(TmfTimestamp
.BigCrunch
, false) < 0);
413 assertTrue("CompareTo", ts3
.compareTo(TmfTimestamp
.BigBang
, false) > 0);
414 assertTrue("CompareTo", ts3
.compareTo(TmfTimestamp
.BigCrunch
, false) < 0);
416 assertTrue("CompareTo", ts4
.compareTo(TmfTimestamp
.BigBang
, false) > 0);
417 assertTrue("CompareTo", ts4
.compareTo(TmfTimestamp
.BigCrunch
, false) < 0);