Refactor TmfTimestamp as per the updated TMF Event Model
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / event / TmfTimestampTest.java
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
13 package org.eclipse.linuxtools.tmf.core.tests.event;
14
15 import junit.framework.TestCase;
16
17 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
18 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
19
20 /**
21 * <b><u>TmfTimestampTest</u></b>
22 * <p>
23 * Test suite for the TmfTimestamp class.
24 */
25 @SuppressWarnings("nls")
26 public class TmfTimestampTest extends TestCase {
27
28 // ------------------------------------------------------------------------
29 // Variables
30 // ------------------------------------------------------------------------
31
32 private final TmfTimestamp ts0 = new TmfTimestamp();
33 private final TmfTimestamp ts1 = new TmfTimestamp(12345);
34 private final TmfTimestamp ts2 = new TmfTimestamp(12345, -1);
35 private final TmfTimestamp ts3 = new TmfTimestamp(12345, 2, 5);
36
37 private final TmfTimestamp ts0copy = new TmfTimestamp();
38 private final TmfTimestamp ts1copy = new TmfTimestamp(12345);
39
40 private final TmfTimestamp ts0copy2 = new TmfTimestamp();
41 private final TmfTimestamp ts1copy2 = new TmfTimestamp(12345);
42
43 private final TmfTimestamp bigBang = new TmfTimestamp(TmfTimestamp.BigBang);
44 private final TmfTimestamp bigCrunch = new TmfTimestamp(TmfTimestamp.BigCrunch);
45
46 // ------------------------------------------------------------------------
47 // Housekeping
48 // ------------------------------------------------------------------------
49
50 /**
51 * @param name the test name
52 */
53 public TmfTimestampTest(String name) {
54 super(name);
55 }
56
57 @Override
58 protected void setUp() throws Exception {
59 super.setUp();
60 }
61
62 @Override
63 protected void tearDown() throws Exception {
64 super.tearDown();
65 }
66
67 // ------------------------------------------------------------------------
68 // Constructors
69 // ------------------------------------------------------------------------
70
71 public void testDefaultConstructor() throws Exception {
72 assertEquals("getValue", 0, ts0.getValue());
73 assertEquals("getscale", 0, ts0.getScale());
74 assertEquals("getPrecision", 0, ts0.getPrecision());
75 }
76
77 public void testSimpleConstructor() throws Exception {
78 assertEquals("getValue", 12345, ts1.getValue());
79 assertEquals("getscale", 0, ts1.getScale());
80 assertEquals("getPrecision", 0, ts1.getPrecision());
81 }
82
83 public void testSimpleConstructor2() throws Exception {
84 assertEquals("getValue", 12345, ts2.getValue());
85 assertEquals("getscale", -1, ts2.getScale());
86 assertEquals("getPrecision", 0, ts2.getPrecision());
87 }
88
89 public void testFullConstructor() throws Exception {
90 assertEquals("getValue", 12345, ts3.getValue());
91 assertEquals("getscale", 2, ts3.getScale());
92 assertEquals("getPrecision", 5, ts3.getPrecision());
93 }
94
95 public void testCopyConstructor() throws Exception {
96 TmfTimestamp ts0 = new TmfTimestamp(12345, 2, 5);
97 TmfTimestamp ts = new TmfTimestamp(ts0);
98 assertEquals("getValue", 12345, ts.getValue());
99 assertEquals("getscale", 2, ts.getScale());
100 assertEquals("getPrecision", 5, ts.getPrecision());
101 }
102
103 public void testCopyConstructor2() throws Exception {
104 try {
105 @SuppressWarnings("unused")
106 TmfTimestamp timestamp = new TmfTimestamp(null);
107 fail("null copy");
108 }
109 catch (IllegalArgumentException e) {
110 // Success
111 }
112 }
113
114 public void testCopyConstructorBigBang() throws Exception {
115 assertEquals("getValue", TmfTimestamp.BigBang.getValue(), bigBang.getValue());
116 assertEquals("getscale", TmfTimestamp.BigBang.getScale(), bigBang.getScale());
117 assertEquals("getPrecision", TmfTimestamp.BigBang.getPrecision(), bigBang.getPrecision());
118 }
119
120 public void testCopyConstructorBigCrunch() throws Exception {
121 assertEquals("getValue", TmfTimestamp.BigCrunch.getValue(), bigCrunch.getValue());
122 assertEquals("getscale", TmfTimestamp.BigCrunch.getScale(), bigCrunch.getScale());
123 assertEquals("getPrecision", TmfTimestamp.BigCrunch.getPrecision(), bigCrunch.getPrecision());
124 }
125
126 // ------------------------------------------------------------------------
127 // equals
128 // ------------------------------------------------------------------------
129
130 public void testEqualsReflexivity() throws Exception {
131 assertTrue("equals", ts0.equals(ts0));
132 assertTrue("equals", ts1.equals(ts1));
133
134 assertTrue("equals", !ts0.equals(ts1));
135 assertTrue("equals", !ts1.equals(ts0));
136 }
137
138 public void testEqualsSymmetry() throws Exception {
139 assertTrue("equals", ts0.equals(ts0copy));
140 assertTrue("equals", ts0copy.equals(ts0));
141
142 assertTrue("equals", ts1.equals(ts1copy));
143 assertTrue("equals", ts1copy.equals(ts1));
144 }
145
146 public void testEqualsTransivity() throws Exception {
147 assertTrue("equals", ts0.equals(ts0copy));
148 assertTrue("equals", ts0copy.equals(ts0copy2));
149 assertTrue("equals", ts0.equals(ts0copy2));
150
151 assertTrue("equals", ts1.equals(ts1copy));
152 assertTrue("equals", ts1copy.equals(ts1copy2));
153 assertTrue("equals", ts1.equals(ts1copy2));
154 }
155
156 public void testEqualsNull() throws Exception {
157 assertTrue("equals", !ts0.equals(null));
158 assertTrue("equals", !ts1.equals(null));
159 }
160
161 // ------------------------------------------------------------------------
162 // hashCode
163 // ------------------------------------------------------------------------
164
165 public void testHashCode() throws Exception {
166 assertTrue("hashCode", ts0.hashCode() == ts0copy.hashCode());
167 assertTrue("hashCode", ts1.hashCode() == ts1copy.hashCode());
168
169 assertTrue("hashCode", ts0.hashCode() != ts1.hashCode());
170 }
171
172 // ------------------------------------------------------------------------
173 // toString
174 // ------------------------------------------------------------------------
175
176 public void testToString() throws Exception {
177 assertEquals("toString", "TmfTimestamp [fValue=0, fScale=0, fPrecision=0]", ts0.toString());
178 assertEquals("toString", "TmfTimestamp [fValue=12345, fScale=0, fPrecision=0]", ts1.toString());
179 assertEquals("toString", "TmfTimestamp [fValue=12345, fScale=-1, fPrecision=0]", ts2.toString());
180 assertEquals("toString", "TmfTimestamp [fValue=12345, fScale=2, fPrecision=5]", ts3.toString());
181 }
182
183 // ------------------------------------------------------------------------
184 // clone
185 // ------------------------------------------------------------------------
186
187 public class MyTimestamp extends TmfTimestamp {
188 @Override
189 public boolean equals(Object other) {
190 return super.equals(other);
191 }
192 @Override
193 public MyTimestamp clone() {
194 return (MyTimestamp) super.clone();
195 }
196 }
197
198 public void testClone() throws Exception {
199 TmfTimestamp timestamp = ts0.clone();
200 assertEquals("clone", timestamp, ts0);
201 }
202
203 public void testClone2() throws Exception {
204 MyTimestamp timestamp = new MyTimestamp();
205 MyTimestamp clone = timestamp.clone();
206 assertEquals("clone", clone, timestamp);
207 }
208
209 // ------------------------------------------------------------------------
210 // normalize
211 // ------------------------------------------------------------------------
212
213 public void testNormalizeOffset() throws Exception {
214
215 ITmfTimestamp ts = ts0.normalize(0, 0);
216 assertEquals("getValue", 0, ts.getValue());
217 assertEquals("getscale", 0, ts.getScale());
218 assertEquals("getPrecision", 0, ts.getPrecision());
219
220 ts = ts0.normalize(12345, 0);
221 assertEquals("getValue", 12345, ts.getValue());
222 assertEquals("getscale", 0, ts.getScale());
223 assertEquals("getPrecision", 0, ts.getPrecision());
224
225 ts = ts0.normalize(10, 0);
226 assertEquals("getValue", 10, ts.getValue());
227 assertEquals("getscale", 0, ts.getScale());
228 assertEquals("getPrecision", 0, ts.getPrecision());
229
230 ts = ts0.normalize(-10, 0);
231 assertEquals("getValue", -10, ts.getValue());
232 assertEquals("getscale", 0, ts.getScale());
233 assertEquals("getPrecision", 0, ts.getPrecision());
234 }
235
236 public void testNormalizeScale() throws Exception {
237
238 ITmfTimestamp ts = ts0.normalize(0, 10);
239 assertEquals("getValue", 0, ts.getValue());
240 assertEquals("getscale", 10, ts.getScale());
241 assertEquals("getPrecision", 0, ts.getPrecision());
242
243 ts = ts0.normalize(0, -10);
244 assertEquals("getValue", 0, ts.getValue());
245 assertEquals("getscale", -10, ts.getScale());
246 assertEquals("getPrecision", 0, ts.getPrecision());
247 }
248
249 public void testNormalizeOffsetAndScale() throws Exception {
250 int SCALE = 12;
251
252 ITmfTimestamp ts = ts0.normalize(0, SCALE);
253 assertEquals("getValue", 0, ts.getValue());
254 assertEquals("getscale", SCALE, ts.getScale());
255 assertEquals("getPrecision", 0, ts.getPrecision());
256
257 ts = ts0.normalize(12345, SCALE);
258 assertEquals("getValue", 12345, ts.getValue());
259 assertEquals("getscale", SCALE, ts.getScale());
260 assertEquals("getPrecision", 0, ts.getPrecision());
261
262 ts = ts0.normalize(10, SCALE);
263 assertEquals("getValue", 10, ts.getValue());
264 assertEquals("getscale", SCALE, ts.getScale());
265 assertEquals("getPrecision", 0, ts.getPrecision());
266
267 ts = ts0.normalize(-10, SCALE);
268 assertEquals("getValue", -10, ts.getValue());
269 assertEquals("getscale", SCALE, ts.getScale());
270 assertEquals("getPrecision", 0, ts.getPrecision());
271 }
272
273 // ------------------------------------------------------------------------
274 // compareTo
275 // ------------------------------------------------------------------------
276
277 public void testCompareToSameScale() throws Exception {
278 TmfTimestamp ts1 = new TmfTimestamp(900, 0, 50);
279 TmfTimestamp ts2 = new TmfTimestamp(1000, 0, 50);
280 TmfTimestamp ts3 = new TmfTimestamp(1100, 0, 50);
281 TmfTimestamp ts4 = new TmfTimestamp(1000, 0, 75);
282
283 assertTrue(ts1.compareTo(ts1, false) == 0);
284
285 assertTrue("CompareTo", ts1.compareTo(ts2, false) < 0);
286 assertTrue("CompareTo", ts1.compareTo(ts3, false) < 0);
287 assertTrue("CompareTo", ts1.compareTo(ts4, false) < 0);
288
289 assertTrue("CompareTo", ts2.compareTo(ts1, false) > 0);
290 assertTrue("CompareTo", ts2.compareTo(ts3, false) < 0);
291 assertTrue("CompareTo", ts2.compareTo(ts4, false) == 0);
292
293 assertTrue("CompareTo", ts3.compareTo(ts1, false) > 0);
294 assertTrue("CompareTo", ts3.compareTo(ts2, false) > 0);
295 assertTrue("CompareTo", ts3.compareTo(ts4, false) > 0);
296 }
297
298 public void testCompareToDifferentScale() throws Exception {
299 TmfTimestamp ts1 = new TmfTimestamp(9000, -1, 50);
300 TmfTimestamp ts2 = new TmfTimestamp(1000, 0, 50);
301 TmfTimestamp ts3 = new TmfTimestamp(110, 1, 50);
302 TmfTimestamp ts4 = new TmfTimestamp(1, 3, 75);
303
304 assertTrue("CompareTo", ts1.compareTo(ts1, false) == 0);
305
306 assertTrue("CompareTo", ts1.compareTo(ts2, false) < 0);
307 assertTrue("CompareTo", ts1.compareTo(ts3, false) < 0);
308 assertTrue("CompareTo", ts1.compareTo(ts4, false) < 0);
309
310 assertTrue("CompareTo", ts2.compareTo(ts1, false) > 0);
311 assertTrue("CompareTo", ts2.compareTo(ts3, false) < 0);
312 assertTrue("CompareTo", ts2.compareTo(ts4, false) == 0);
313
314 assertTrue("CompareTo", ts3.compareTo(ts1, false) > 0);
315 assertTrue("CompareTo", ts3.compareTo(ts2, false) > 0);
316 assertTrue("CompareTo", ts3.compareTo(ts4, false) > 0);
317 }
318
319 public void testCompareToWithinPrecision() throws Exception {
320 TmfTimestamp ts1 = new TmfTimestamp(900, 0, 50);
321 TmfTimestamp ts2 = new TmfTimestamp(1000, 0, 50);
322 TmfTimestamp ts3 = new TmfTimestamp(1100, 0, 50);
323 TmfTimestamp ts4 = new TmfTimestamp(1000, 0, 75);
324
325 assertTrue("CompareTo", ts1.compareTo(ts1, true) == 0);
326
327 assertTrue("CompareTo", ts1.compareTo(ts2, true) == 0);
328 assertTrue("CompareTo", ts1.compareTo(ts3, true) < 0);
329 assertTrue("CompareTo", ts1.compareTo(ts4, true) == 0);
330
331 assertTrue("CompareTo", ts2.compareTo(ts1, true) == 0);
332 assertTrue("CompareTo", ts2.compareTo(ts3, true) == 0);
333 assertTrue("CompareTo", ts2.compareTo(ts4, true) == 0);
334
335 assertTrue("CompareTo", ts3.compareTo(ts1, true) > 0);
336 assertTrue("CompareTo", ts3.compareTo(ts2, true) == 0);
337 assertTrue("CompareTo", ts3.compareTo(ts4, true) == 0);
338 }
339
340 public void testCompareToLargeScale() throws Exception {
341 TmfTimestamp ts1 = new TmfTimestamp(-1, 100);
342 TmfTimestamp ts2 = new TmfTimestamp(-1000, -100);
343 TmfTimestamp ts3 = new TmfTimestamp(1, 100);
344 TmfTimestamp ts4 = new TmfTimestamp(1000, -100);
345
346 assertTrue("CompareTo", ts1.compareTo(ts2, false) < 0);
347 assertTrue("CompareTo", ts1.compareTo(ts3, false) < 0);
348 assertTrue("CompareTo", ts1.compareTo(ts4, false) < 0);
349
350 assertTrue("CompareTo", ts2.compareTo(ts1, false) > 0);
351 assertTrue("CompareTo", ts2.compareTo(ts3, false) < 0);
352 assertTrue("CompareTo", ts2.compareTo(ts4, false) < 0);
353
354 assertTrue("CompareTo", ts3.compareTo(ts1, false) > 0);
355 assertTrue("CompareTo", ts3.compareTo(ts2, false) > 0);
356 assertTrue("CompareTo", ts3.compareTo(ts4, false) > 0);
357
358 assertTrue("CompareTo", ts4.compareTo(ts1, false) > 0);
359 assertTrue("CompareTo", ts4.compareTo(ts2, false) > 0);
360 assertTrue("CompareTo", ts4.compareTo(ts3, false) < 0);
361 }
362
363 public void testCompareToBigRanges() throws Exception {
364 TmfTimestamp ts0a = new TmfTimestamp( 0, Integer.MAX_VALUE);
365 TmfTimestamp ts0b = new TmfTimestamp( 0, Integer.MIN_VALUE);
366 TmfTimestamp ts1 = new TmfTimestamp(-1, Integer.MAX_VALUE);
367 TmfTimestamp ts2 = new TmfTimestamp(-1, Integer.MIN_VALUE);
368 TmfTimestamp ts3 = new TmfTimestamp( 1, Integer.MAX_VALUE);
369 TmfTimestamp ts4 = new TmfTimestamp( 1, Integer.MIN_VALUE);
370
371 assertEquals("CompareTo", 1, ts0a.compareTo(TmfTimestamp.BigBang, false));
372 assertEquals("CompareTo", -1, ts0a.compareTo(TmfTimestamp.BigCrunch, false));
373
374 assertEquals("CompareTo", 1, ts0b.compareTo(TmfTimestamp.BigBang, false));
375 assertEquals("CompareTo", -1, ts0b.compareTo(TmfTimestamp.BigCrunch, false));
376
377 assertEquals("CompareTo", 0, ts0a.compareTo(ts0b, false));
378 assertEquals("CompareTo", 0, ts0b.compareTo(ts0a, false));
379
380 assertEquals("CompareTo", 1, ts0a.compareTo(TmfTimestamp.BigBang, false));
381 assertEquals("CompareTo", -1, ts0a.compareTo(TmfTimestamp.BigCrunch, false));
382
383 assertEquals("CompareTo", 1, ts1.compareTo(TmfTimestamp.BigBang, false));
384 assertEquals("CompareTo", -1, ts1.compareTo(TmfTimestamp.BigCrunch, false));
385
386 assertEquals("CompareTo", 1, ts2.compareTo(TmfTimestamp.BigBang, false));
387 assertEquals("CompareTo", -1, ts2.compareTo(TmfTimestamp.BigCrunch, false));
388
389 assertEquals("CompareTo", 1, ts3.compareTo(TmfTimestamp.BigBang, false));
390 assertEquals("CompareTo", -1, ts3.compareTo(TmfTimestamp.BigCrunch, false));
391
392 assertEquals("CompareTo", 1, ts4.compareTo(TmfTimestamp.BigBang, false));
393 assertEquals("CompareTo", -1, ts4.compareTo(TmfTimestamp.BigCrunch, false));
394 }
395
396 // ------------------------------------------------------------------------
397 // getDelta
398 // ------------------------------------------------------------------------
399
400 public void testDelta() throws Exception {
401 // Delta for same scale and precision (delta > 0)
402 TmfTimestamp ts0 = new TmfTimestamp(10, 9);
403 TmfTimestamp ts1 = new TmfTimestamp(5, 9);
404 TmfTimestamp exp = new TmfTimestamp(5, 9);
405
406 ITmfTimestamp delta = ts0.getDelta(ts1);
407 assertEquals("getDelta", 0, delta.compareTo(exp, false));
408
409 // Delta for same scale and precision (delta < 0)
410 ts0 = new TmfTimestamp( 5, 9);
411 ts1 = new TmfTimestamp(10, 9);
412 exp = new TmfTimestamp(-5, 9);
413
414 delta = ts0.getDelta(ts1);
415 assertEquals("getDelta", 0, delta.compareTo(exp, false));
416
417 // Delta for different scale and same precision (delta > 0)
418 ts0 = new TmfTimestamp( 5, 9);
419 ts1 = new TmfTimestamp(10, 8);
420 exp = new TmfTimestamp( 4, 9);
421
422 delta = ts0.getDelta(ts1);
423 assertEquals("getDelta", 0, delta.compareTo(exp, false));
424
425 // Delta for different scale and same precision (delta > 0)
426 ts0 = new TmfTimestamp( 5, 9);
427 ts1 = new TmfTimestamp(10, 7);
428 exp = new TmfTimestamp( 5, 9);
429
430 delta = ts0.getDelta(ts1);
431 assertEquals("getDelta", 0, delta.compareTo(exp, false));
432
433 // Delta for different scale and same precision
434 ts0 = new TmfTimestamp(10, 9);
435 ts1 = new TmfTimestamp( 5, 8);
436 exp = new TmfTimestamp(10, 9);
437
438 delta = ts0.getDelta(ts1);
439 assertEquals("getDelta", 0, delta.compareTo(exp, false));
440
441 // Delta for same scale and different precision
442 ts0 = new TmfTimestamp(10, 9, 1);
443 ts1 = new TmfTimestamp( 5, 9, 2);
444 exp = new TmfTimestamp( 5, 9, 3);
445
446 delta = ts0.getDelta(ts1);
447 assertEquals("getDelta", 0, delta.compareTo(exp, true));
448 assertEquals("precision", 3, delta.getPrecision());
449
450 // Delta for same scale and different precision
451 ts0 = new TmfTimestamp( 5, 9, 2);
452 ts1 = new TmfTimestamp(10, 9, 1);
453 exp = new TmfTimestamp(-5, 9, 3);
454
455 delta = ts0.getDelta(ts1);
456 assertEquals("getDelta", 0, delta.compareTo(exp, true));
457 assertEquals("precision", 3, delta.getPrecision());
458
459 // Delta for different scale and different precision
460 ts0 = new TmfTimestamp( 5, 9, 2);
461 ts1 = new TmfTimestamp(10, 8, 1);
462 exp = new TmfTimestamp( 4, 9, 3);
463 delta = ts0.getDelta(ts1);
464 assertEquals("getDelta", 0, delta.compareTo(exp, true));
465 assertEquals("precision", 2, delta.getPrecision());
466 }
467 }
This page took 0.043219 seconds and 6 git commands to generate.