"Fixed" the failing test...
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.tests / src / org / eclipse / linuxtools / tmf / tests / event / TmfTimestampTest.java
CommitLineData
d18dd09b
ASL
1/*******************************************************************************
2 * Copyright (c) 2009 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 *******************************************************************************/
12
13package org.eclipse.linuxtools.tmf.tests.event;
14
75828b1a
FC
15import junit.framework.TestCase;
16
cbd4ad82
FC
17import org.eclipse.linuxtools.tmf.event.TmfTimestamp;
18
d18dd09b
ASL
19/**
20 * <b><u>TmfTimestampTest</u></b>
21 * <p>
cbd4ad82 22 * Test suite for the TmfTimestamp class.
d18dd09b
ASL
23 */
24public class TmfTimestampTest extends TestCase {
25
cbd4ad82
FC
26 // ------------------------------------------------------------------------
27 // Variables
28 // ------------------------------------------------------------------------
29
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);
34
35 private final TmfTimestamp ts0copy = new TmfTimestamp();
36 private final TmfTimestamp ts1copy = new TmfTimestamp(12345);
37
38 private final TmfTimestamp ts0copy2 = new TmfTimestamp();
39 private final TmfTimestamp ts1copy2 = new TmfTimestamp(12345);
40
41 private final TmfTimestamp bigBang = new TmfTimestamp(TmfTimestamp.BigBang);
42 private final TmfTimestamp bigCrunch = new TmfTimestamp(TmfTimestamp.BigCrunch);
d18dd09b 43
cbd4ad82
FC
44 // ------------------------------------------------------------------------
45 // Housekeping
46 // ------------------------------------------------------------------------
47
48 /**
49 * @param name the test name
50 */
d18dd09b
ASL
51 public TmfTimestampTest(String name) {
52 super(name);
53 }
54
55 @Override
56 protected void setUp() throws Exception {
57 super.setUp();
58 }
59
60 @Override
61 protected void tearDown() throws Exception {
62 super.tearDown();
63 }
64
cbd4ad82 65 // ------------------------------------------------------------------------
d18dd09b 66 // Constructors
cbd4ad82 67 // ------------------------------------------------------------------------
d18dd09b
ASL
68
69 public void testDefaultConstructor() throws Exception {
cbd4ad82
FC
70 assertEquals("getValue", 0, ts0.getValue());
71 assertEquals("getscale", 0, ts0.getScale());
72 assertEquals("getPrecision", 0, ts0.getPrecision());
d18dd09b
ASL
73 }
74
75 public void testSimpleConstructor() throws Exception {
cbd4ad82
FC
76 assertEquals("getValue", 12345, ts1.getValue());
77 assertEquals("getscale", 0, ts1.getScale());
78 assertEquals("getPrecision", 0, ts1.getPrecision());
d18dd09b
ASL
79 }
80
81 public void testSimpleConstructor2() throws Exception {
cbd4ad82
FC
82 assertEquals("getValue", 12345, ts2.getValue());
83 assertEquals("getscale", -1, ts2.getScale());
84 assertEquals("getPrecision", 0, ts2.getPrecision());
d18dd09b
ASL
85 }
86
87 public void testFullConstructor() throws Exception {
cbd4ad82
FC
88 assertEquals("getValue", 12345, ts3.getValue());
89 assertEquals("getscale", 2, ts3.getScale());
90 assertEquals("getPrecision", 5, ts3.getPrecision());
d18dd09b
ASL
91 }
92
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());
99 }
100
cbd4ad82
FC
101 public void testCopyConstructor2() throws Exception {
102 try {
103 @SuppressWarnings("unused")
104 TmfTimestamp timestamp = new TmfTimestamp(null);
105 fail("null copy");
106 }
107 catch (IllegalArgumentException e) {
108 // Success
109 }
110 }
d18dd09b
ASL
111
112 public void testCopyConstructorBigBang() throws Exception {
cbd4ad82
FC
113 assertEquals("getValue", TmfTimestamp.BigBang.getValue(), bigBang.getValue());
114 assertEquals("getscale", TmfTimestamp.BigBang.getScale(), bigBang.getScale());
115 assertEquals("getPrecision", TmfTimestamp.BigBang.getPrecision(), bigBang.getPrecision());
d18dd09b
ASL
116 }
117
118 public void testCopyConstructorBigCrunch() throws Exception {
cbd4ad82
FC
119 assertEquals("getValue", TmfTimestamp.BigCrunch.getValue(), bigCrunch.getValue());
120 assertEquals("getscale", TmfTimestamp.BigCrunch.getScale(), bigCrunch.getScale());
121 assertEquals("getPrecision", TmfTimestamp.BigCrunch.getPrecision(), bigCrunch.getPrecision());
122 }
123
124 // ------------------------------------------------------------------------
125 // equals
126 // ------------------------------------------------------------------------
127
128 public void testEqualsReflexivity() throws Exception {
129 assertTrue("equals", ts0.equals(ts0));
130 assertTrue("equals", ts1.equals(ts1));
131
132 assertTrue("equals", !ts0.equals(ts1));
133 assertTrue("equals", !ts1.equals(ts0));
d18dd09b 134 }
cbd4ad82
FC
135
136 public void testEqualsSymmetry() throws Exception {
137 assertTrue("equals", ts0.equals(ts0copy));
138 assertTrue("equals", ts0copy.equals(ts0));
d18dd09b 139
cbd4ad82
FC
140 assertTrue("equals", ts1.equals(ts1copy));
141 assertTrue("equals", ts1copy.equals(ts1));
142 }
143
144 public void testEqualsTransivity() throws Exception {
145 assertTrue("equals", ts0.equals(ts0copy));
146 assertTrue("equals", ts0copy.equals(ts0copy2));
147 assertTrue("equals", ts0.equals(ts0copy2));
148
149 assertTrue("equals", ts1.equals(ts1copy));
150 assertTrue("equals", ts1copy.equals(ts1copy2));
151 assertTrue("equals", ts1.equals(ts1copy2));
152 }
153
cbd4ad82
FC
154 public void testEqualsNull() throws Exception {
155 assertTrue("equals", !ts0.equals(null));
156 assertTrue("equals", !ts1.equals(null));
157 }
158
2fb2eb37
FC
159 // ------------------------------------------------------------------------
160 // hashCode
161 // ------------------------------------------------------------------------
162
163 public void testHashCode() throws Exception {
164 assertTrue("hashCode", ts0.hashCode() == ts0copy.hashCode());
165 assertTrue("hashCode", ts1.hashCode() == ts1copy.hashCode());
166
167 assertTrue("hashCode", ts0.hashCode() != ts1.hashCode());
168 }
169
cbd4ad82 170 // ------------------------------------------------------------------------
d18dd09b 171 // toString
cbd4ad82 172 // ------------------------------------------------------------------------
d18dd09b
ASL
173
174 public void testToString() throws Exception {
cbd4ad82
FC
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());
d18dd09b 179 }
cbd4ad82 180
ff4ed569
FC
181 // ------------------------------------------------------------------------
182 // clone
183 // ------------------------------------------------------------------------
184
185 public class MyTimestamp extends TmfTimestamp {
186 @Override
187 public boolean equals(Object other) {
188 return super.equals(other);
189 }
190 @Override
191 public MyTimestamp clone() {
192 return (MyTimestamp) super.clone();
193 }
194 }
195
196 public void testClone() throws Exception {
197 TmfTimestamp timestamp = ts0.clone();
198 assertEquals("clone", timestamp, ts0);
199 }
200
201 public void testClone2() throws Exception {
202 MyTimestamp timestamp = new MyTimestamp();
203 MyTimestamp clone = timestamp.clone();
204 assertEquals("clone", clone, timestamp);
205 }
206
cbd4ad82 207 // ------------------------------------------------------------------------
d18dd09b 208 // synchronize
cbd4ad82 209 // ------------------------------------------------------------------------
d18dd09b
ASL
210
211 public void testSynchronizeOffset() throws Exception {
212
cbd4ad82
FC
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());
217
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());
d18dd09b
ASL
222
223 ts = ts0.synchronize(10, (byte) 0);
cbd4ad82
FC
224 assertEquals("getValue", 10, ts.getValue());
225 assertEquals("getscale", 0, ts.getScale());
226 assertEquals("getPrecision", 0, ts.getPrecision());
d18dd09b
ASL
227
228 ts = ts0.synchronize(-10, (byte) 0);
cbd4ad82
FC
229 assertEquals("getValue", -10, ts.getValue());
230 assertEquals("getscale", 0, ts.getScale());
231 assertEquals("getPrecision", 0, ts.getPrecision());
d18dd09b
ASL
232 }
233
234 public void testSynchronizeScale() throws Exception {
235
cbd4ad82
FC
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());
d18dd09b 240
cbd4ad82
FC
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());
245 }
d18dd09b 246
cbd4ad82
FC
247 public void testSynchronizeOffsetAndScale() throws Exception {
248 byte SCALE = 12;
249
250 TmfTimestamp ts = ts0.synchronize(0, SCALE);
251 assertEquals("getValue", 0, ts.getValue());
252 assertEquals("getscale", SCALE, ts.getScale());
d18dd09b
ASL
253 assertEquals("getPrecision", 0, ts.getPrecision());
254
cbd4ad82
FC
255 ts = ts0.synchronize(12345, SCALE);
256 assertEquals("getValue", 12345, ts.getValue());
257 assertEquals("getscale", SCALE, ts.getScale());
d18dd09b
ASL
258 assertEquals("getPrecision", 0, ts.getPrecision());
259
cbd4ad82
FC
260 ts = ts0.synchronize(10, SCALE);
261 assertEquals("getValue", 10, ts.getValue());
262 assertEquals("getscale", SCALE, ts.getScale());
263 assertEquals("getPrecision", 0, ts.getPrecision());
d18dd09b 264
cbd4ad82
FC
265 ts = ts0.synchronize(-10, SCALE);
266 assertEquals("getValue", -10, ts.getValue());
267 assertEquals("getscale", SCALE, ts.getScale());
268 assertEquals("getPrecision", 0, ts.getPrecision());
d18dd09b
ASL
269 }
270
cbd4ad82 271 // ------------------------------------------------------------------------
d18dd09b 272 // getAdjustment
cbd4ad82 273 // ------------------------------------------------------------------------
d18dd09b
ASL
274
275 public void testGetAdjustmentSameScale() throws Exception {
cbd4ad82
FC
276 long delta = ts0.getAdjustment(ts0 , ts0.getScale());
277 assertEquals("delta", 0, delta);
d18dd09b 278
cbd4ad82
FC
279 delta = ts1.getAdjustment(ts1, ts1.getScale());
280 assertEquals("delta", 0, delta);
d18dd09b 281
cbd4ad82
FC
282 delta = ts0.getAdjustment(ts1, ts1.getScale());
283 assertEquals("delta", 12345, delta);
75828b1a 284
cbd4ad82
FC
285 delta = ts1.getAdjustment(ts0, ts0.getScale());
286 assertEquals("delta", -12345, delta);
287 }
d18dd09b 288
cbd4ad82
FC
289 public void testGetAdjustmentDifferentScales() throws Exception {
290 long delta = ts0.getAdjustment(ts2, ts2.getScale());
291 assertEquals("delta", 12345, delta);
292
293 delta = ts2.getAdjustment(ts0, ts0.getScale());
294 assertEquals("delta", -1234, delta);
d18dd09b
ASL
295 }
296
cbd4ad82 297 // ------------------------------------------------------------------------
d18dd09b 298 // CompareTo
cbd4ad82 299 // ------------------------------------------------------------------------
d18dd09b
ASL
300
301 public void testCompareToSameScale() throws Exception {
cbd4ad82 302 TmfTimestamp ts1 = new TmfTimestamp(900, (byte) 0, 50);
d18dd09b
ASL
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);
306
307 assertTrue(ts1.compareTo(ts1, false) == 0);
308
cbd4ad82
FC
309 assertTrue("CompareTo", ts1.compareTo(ts2, false) < 0);
310 assertTrue("CompareTo", ts1.compareTo(ts3, false) < 0);
311 assertTrue("CompareTo", ts1.compareTo(ts4, false) < 0);
d18dd09b 312
cbd4ad82
FC
313 assertTrue("CompareTo", ts2.compareTo(ts1, false) > 0);
314 assertTrue("CompareTo", ts2.compareTo(ts3, false) < 0);
315 assertTrue("CompareTo", ts2.compareTo(ts4, false) == 0);
d18dd09b 316
cbd4ad82
FC
317 assertTrue("CompareTo", ts3.compareTo(ts1, false) > 0);
318 assertTrue("CompareTo", ts3.compareTo(ts2, false) > 0);
319 assertTrue("CompareTo", ts3.compareTo(ts4, false) > 0);
d18dd09b
ASL
320 }
321
322 public void testCompareToDifferentScale() throws Exception {
323 TmfTimestamp ts1 = new TmfTimestamp(9000, (byte) -1, 50);
324 TmfTimestamp ts2 = new TmfTimestamp(1000, (byte) 0, 50);
cbd4ad82
FC
325 TmfTimestamp ts3 = new TmfTimestamp(110, (byte) 1, 50);
326 TmfTimestamp ts4 = new TmfTimestamp(1, (byte) 3, 75);
d18dd09b 327
cbd4ad82 328 assertTrue("CompareTo", ts1.compareTo(ts1, false) == 0);
d18dd09b 329
cbd4ad82
FC
330 assertTrue("CompareTo", ts1.compareTo(ts2, false) < 0);
331 assertTrue("CompareTo", ts1.compareTo(ts3, false) < 0);
332 assertTrue("CompareTo", ts1.compareTo(ts4, false) < 0);
d18dd09b 333
cbd4ad82
FC
334 assertTrue("CompareTo", ts2.compareTo(ts1, false) > 0);
335 assertTrue("CompareTo", ts2.compareTo(ts3, false) < 0);
336 assertTrue("CompareTo", ts2.compareTo(ts4, false) == 0);
d18dd09b 337
cbd4ad82
FC
338 assertTrue("CompareTo", ts3.compareTo(ts1, false) > 0);
339 assertTrue("CompareTo", ts3.compareTo(ts2, false) > 0);
340 assertTrue("CompareTo", ts3.compareTo(ts4, false) > 0);
d18dd09b
ASL
341 }
342
343 public void testCompareToWithinPrecision() throws Exception {
cbd4ad82 344 TmfTimestamp ts1 = new TmfTimestamp(900, (byte) 0, 50);
d18dd09b
ASL
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);
348
cbd4ad82 349 assertTrue("CompareTo", ts1.compareTo(ts1, true) == 0);
d18dd09b 350
cbd4ad82
FC
351 assertTrue("CompareTo", ts1.compareTo(ts2, true) == 0);
352 assertTrue("CompareTo", ts1.compareTo(ts3, true) < 0);
353 assertTrue("CompareTo", ts1.compareTo(ts4, true) == 0);
d18dd09b 354
cbd4ad82
FC
355 assertTrue("CompareTo", ts2.compareTo(ts1, true) == 0);
356 assertTrue("CompareTo", ts2.compareTo(ts3, true) == 0);
357 assertTrue("CompareTo", ts2.compareTo(ts4, true) == 0);
d18dd09b 358
cbd4ad82
FC
359 assertTrue("CompareTo", ts3.compareTo(ts1, true) > 0);
360 assertTrue("CompareTo", ts3.compareTo(ts2, true) == 0);
361 assertTrue("CompareTo", ts3.compareTo(ts4, true) == 0);
d18dd09b
ASL
362 }
363
364 public void testCompareToLargeScale() throws Exception {
cbd4ad82 365 TmfTimestamp ts1 = new TmfTimestamp(-1, (byte) 100);
d18dd09b 366 TmfTimestamp ts2 = new TmfTimestamp(-1000, (byte) -100);
cbd4ad82
FC
367 TmfTimestamp ts3 = new TmfTimestamp(1, (byte) 100);
368 TmfTimestamp ts4 = new TmfTimestamp(1000, (byte) -100);
d18dd09b 369
cbd4ad82
FC
370 assertTrue("CompareTo", ts1.compareTo(ts2, false) < 0);
371 assertTrue("CompareTo", ts1.compareTo(ts3, false) < 0);
372 assertTrue("CompareTo", ts1.compareTo(ts4, false) < 0);
d18dd09b 373
cbd4ad82
FC
374 assertTrue("CompareTo", ts2.compareTo(ts1, false) > 0);
375 assertTrue("CompareTo", ts2.compareTo(ts3, false) < 0);
376 assertTrue("CompareTo", ts2.compareTo(ts4, false) < 0);
d18dd09b 377
cbd4ad82
FC
378 assertTrue("CompareTo", ts3.compareTo(ts1, false) > 0);
379 assertTrue("CompareTo", ts3.compareTo(ts2, false) > 0);
380 assertTrue("CompareTo", ts3.compareTo(ts4, false) > 0);
d18dd09b 381
cbd4ad82
FC
382 assertTrue("CompareTo", ts4.compareTo(ts1, false) > 0);
383 assertTrue("CompareTo", ts4.compareTo(ts2, false) > 0);
384 assertTrue("CompareTo", ts4.compareTo(ts3, false) < 0);
d18dd09b
ASL
385 }
386
cbd4ad82
FC
387 public void testCompareToBigRanges() throws Exception {
388 TmfTimestamp ts0a = new TmfTimestamp(0, Byte.MAX_VALUE);
389 TmfTimestamp ts0b = new TmfTimestamp(0, Byte.MIN_VALUE);
75828b1a
FC
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);
d18dd09b 394
cbd4ad82
FC
395 assertTrue("CompareTo", ts0a.compareTo(TmfTimestamp.BigBang, false) > 0);
396 assertTrue("CompareTo", ts0a.compareTo(TmfTimestamp.BigCrunch, false) < 0);
397
398 assertTrue("CompareTo", ts0b.compareTo(TmfTimestamp.BigBang, false) > 0);
399 assertTrue("CompareTo", ts0b.compareTo(TmfTimestamp.BigCrunch, false) < 0);
400
401 assertTrue("CompareTo", ts0a.compareTo(ts0b, false) == 0);
402 assertTrue("CompareTo", ts0b.compareTo(ts0a, false) == 0);
403
404 assertTrue("CompareTo", ts0a.compareTo(TmfTimestamp.BigBang, false) > 0);
405 assertTrue("CompareTo", ts0a.compareTo(TmfTimestamp.BigCrunch, false) < 0);
406
407 assertTrue("CompareTo", ts1.compareTo(TmfTimestamp.BigBang, false) > 0);
408 assertTrue("CompareTo", ts1.compareTo(TmfTimestamp.BigCrunch, false) < 0);
d18dd09b 409
cbd4ad82
FC
410 assertTrue("CompareTo", ts2.compareTo(TmfTimestamp.BigBang, false) > 0);
411 assertTrue("CompareTo", ts2.compareTo(TmfTimestamp.BigCrunch, false) < 0);
d18dd09b 412
cbd4ad82
FC
413 assertTrue("CompareTo", ts3.compareTo(TmfTimestamp.BigBang, false) > 0);
414 assertTrue("CompareTo", ts3.compareTo(TmfTimestamp.BigCrunch, false) < 0);
d18dd09b 415
cbd4ad82
FC
416 assertTrue("CompareTo", ts4.compareTo(TmfTimestamp.BigBang, false) > 0);
417 assertTrue("CompareTo", ts4.compareTo(TmfTimestamp.BigCrunch, false) < 0);
d18dd09b
ASL
418 }
419
420}
This page took 0.060238 seconds and 5 git commands to generate.