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