Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
d7dbf09a | 2 | * Copyright (c) 2009, 2012 Ericsson |
54a7a54c | 3 | * |
d18dd09b ASL |
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 | |
54a7a54c | 8 | * |
d18dd09b ASL |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
9ee9135e | 11 | * Francois Chouinard - Adjusted for new Event Model |
d18dd09b ASL |
12 | *******************************************************************************/ |
13 | ||
6c13869b | 14 | package org.eclipse.linuxtools.tmf.core.tests.event; |
d18dd09b | 15 | |
75828b1a FC |
16 | import junit.framework.TestCase; |
17 | ||
5179fc01 | 18 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; |
6c13869b | 19 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; |
f8177ba2 | 20 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestampFormat; |
cbd4ad82 | 21 | |
d18dd09b | 22 | /** |
cbd4ad82 | 23 | * Test suite for the TmfTimestamp class. |
d18dd09b | 24 | */ |
3b38ea61 | 25 | @SuppressWarnings("nls") |
d18dd09b ASL |
26 | public class TmfTimestampTest extends TestCase { |
27 | ||
085d898f FC |
28 | // ------------------------------------------------------------------------ |
29 | // Variables | |
30 | // ------------------------------------------------------------------------ | |
31 | ||
32 | private final ITmfTimestamp ts0 = new TmfTimestamp(); | |
f8177ba2 | 33 | private final ITmfTimestamp ts1 = new TmfTimestamp(12345, 0); |
085d898f | 34 | private final ITmfTimestamp ts2 = new TmfTimestamp(12345, -1); |
f8177ba2 | 35 | private final ITmfTimestamp ts3 = new TmfTimestamp(12345, 2, 5); |
085d898f FC |
36 | |
37 | // ------------------------------------------------------------------------ | |
38 | // Housekeping | |
39 | // ------------------------------------------------------------------------ | |
40 | ||
41 | /** | |
42 | * @param name the test name | |
43 | */ | |
44 | public TmfTimestampTest(final String name) { | |
45 | super(name); | |
46 | } | |
47 | ||
48 | @Override | |
49 | protected void setUp() throws Exception { | |
50 | super.setUp(); | |
51 | } | |
52 | ||
53 | @Override | |
54 | protected void tearDown() throws Exception { | |
55 | super.tearDown(); | |
56 | } | |
57 | ||
58 | // ------------------------------------------------------------------------ | |
59 | // Constructors | |
60 | // ------------------------------------------------------------------------ | |
d18dd09b | 61 | |
54a7a54c FC |
62 | /** |
63 | * | |
64 | */ | |
65 | public void testDefaultConstructor() { | |
f2dd0808 FC |
66 | assertEquals("getValue", 0, ts0.getValue()); |
67 | assertEquals("getscale", 0, ts0.getScale()); | |
68 | assertEquals("getPrecision", 0, ts0.getPrecision()); | |
69 | } | |
70 | ||
54a7a54c FC |
71 | /** |
72 | * | |
73 | */ | |
74 | public void testValueConstructor() { | |
f2dd0808 FC |
75 | assertEquals("getValue", 12345, ts1.getValue()); |
76 | assertEquals("getscale", 0, ts1.getScale()); | |
77 | assertEquals("getPrecision", 0, ts1.getPrecision()); | |
78 | } | |
79 | ||
54a7a54c FC |
80 | /** |
81 | * | |
82 | */ | |
83 | public void testValueScaleConstructor() { | |
f2dd0808 FC |
84 | assertEquals("getValue", 12345, ts2.getValue()); |
85 | assertEquals("getscale", -1, ts2.getScale()); | |
86 | assertEquals("getPrecision", 0, ts2.getPrecision()); | |
87 | } | |
88 | ||
54a7a54c FC |
89 | /** |
90 | * | |
91 | */ | |
92 | public void testFullConstructor() { | |
f2dd0808 FC |
93 | assertEquals("getValue", 12345, ts3.getValue()); |
94 | assertEquals("getscale", 2, ts3.getScale()); | |
95 | assertEquals("getPrecision", 5, ts3.getPrecision()); | |
96 | } | |
97 | ||
54a7a54c FC |
98 | /** |
99 | * | |
100 | */ | |
101 | public void testCopyConstructor() { | |
085d898f FC |
102 | final ITmfTimestamp ts = new TmfTimestamp(12345, 2, 5); |
103 | final ITmfTimestamp copy = new TmfTimestamp(ts); | |
f2dd0808 FC |
104 | |
105 | assertEquals("getValue", ts.getValue(), copy.getValue()); | |
106 | assertEquals("getscale", ts.getScale(), copy.getScale()); | |
107 | assertEquals("getPrecision", ts.getPrecision(), copy.getPrecision()); | |
108 | ||
109 | assertEquals("getValue", 12345, copy.getValue()); | |
110 | assertEquals("getscale", 2, copy.getScale()); | |
111 | assertEquals("getPrecision", 5, copy.getPrecision()); | |
112 | } | |
113 | ||
54a7a54c FC |
114 | /** |
115 | * | |
116 | */ | |
117 | public void testCopyNullConstructor() { | |
f2dd0808 | 118 | try { |
6e85c58d | 119 | new TmfTimestamp(null); |
75d42a16 | 120 | fail("TmfTimestamp: null argument"); |
085d898f | 121 | } catch (final IllegalArgumentException e) { |
f2dd0808 FC |
122 | } |
123 | } | |
124 | ||
54a7a54c FC |
125 | /** |
126 | * | |
127 | */ | |
128 | public void testCopyConstructorBigBang() { | |
085d898f | 129 | final ITmfTimestamp ts = new TmfTimestamp(TmfTimestamp.BIG_BANG); |
a4115405 FC |
130 | assertEquals("getValue", TmfTimestamp.BIG_BANG.getValue(), ts.getValue()); |
131 | assertEquals("getscale", TmfTimestamp.BIG_BANG.getScale(), ts.getScale()); | |
132 | assertEquals("getPrecision", TmfTimestamp.BIG_BANG.getPrecision(), ts.getPrecision()); | |
f2dd0808 FC |
133 | } |
134 | ||
54a7a54c FC |
135 | /** |
136 | * | |
137 | */ | |
138 | public void testCopyConstructorBigCrunch() { | |
085d898f | 139 | final ITmfTimestamp ts = new TmfTimestamp(TmfTimestamp.BIG_CRUNCH); |
a4115405 FC |
140 | assertEquals("getValue", TmfTimestamp.BIG_CRUNCH.getValue(), ts.getValue()); |
141 | assertEquals("getscale", TmfTimestamp.BIG_CRUNCH.getScale(), ts.getScale()); | |
142 | assertEquals("getPrecision", TmfTimestamp.BIG_CRUNCH.getPrecision(), ts.getPrecision()); | |
f2dd0808 FC |
143 | } |
144 | ||
54a7a54c FC |
145 | /** |
146 | * | |
147 | */ | |
148 | public void testCopyConstructorZero() { | |
085d898f | 149 | final ITmfTimestamp ts = new TmfTimestamp(TmfTimestamp.ZERO); |
a4115405 FC |
150 | assertEquals("getValue", TmfTimestamp.ZERO.getValue(), ts.getValue()); |
151 | assertEquals("getscale", TmfTimestamp.ZERO.getScale(), ts.getScale()); | |
152 | assertEquals("getPrecision", TmfTimestamp.ZERO.getPrecision(), ts.getPrecision()); | |
f2dd0808 | 153 | } |
d18dd09b | 154 | |
9ee9135e FC |
155 | // ------------------------------------------------------------------------ |
156 | // clone | |
157 | // ------------------------------------------------------------------------ | |
158 | ||
54a7a54c | 159 | private static class MyTimestamp extends TmfTimestamp { |
9ee9135e FC |
160 | |
161 | @Override | |
085d898f | 162 | public boolean equals(final Object other) { |
9ee9135e FC |
163 | return super.equals(other); |
164 | } | |
165 | ||
166 | @Override | |
167 | public MyTimestamp clone() { | |
168 | return (MyTimestamp) super.clone(); | |
169 | } | |
170 | } | |
171 | ||
54a7a54c FC |
172 | /** |
173 | * | |
174 | */ | |
175 | public void testClone() { | |
085d898f | 176 | final ITmfTimestamp clone = ts0.clone(); |
de126dbb FC |
177 | |
178 | assertTrue("clone", ts0.clone().equals(ts0)); | |
179 | assertTrue("clone", clone.clone().equals(clone)); | |
180 | ||
9ee9135e | 181 | assertEquals("clone", clone, ts0); |
de126dbb | 182 | assertEquals("clone", ts0, clone); |
9ee9135e FC |
183 | } |
184 | ||
54a7a54c FC |
185 | /** |
186 | * | |
187 | */ | |
188 | public void testClone2() { | |
085d898f FC |
189 | final MyTimestamp timestamp = new MyTimestamp(); |
190 | final MyTimestamp clone = timestamp.clone(); | |
de126dbb FC |
191 | |
192 | assertTrue("clone", timestamp.clone().equals(timestamp)); | |
193 | assertTrue("clone", clone.clone().equals(clone)); | |
194 | ||
9ee9135e | 195 | assertEquals("clone", clone, timestamp); |
de126dbb | 196 | assertEquals("clone", timestamp, clone); |
9ee9135e FC |
197 | } |
198 | ||
199 | // ------------------------------------------------------------------------ | |
200 | // hashCode | |
201 | // ------------------------------------------------------------------------ | |
202 | ||
54a7a54c FC |
203 | /** |
204 | * | |
205 | */ | |
206 | public void testHashCode() { | |
085d898f FC |
207 | final ITmfTimestamp ts0copy = new TmfTimestamp(ts0); |
208 | final ITmfTimestamp ts1copy = new TmfTimestamp(ts1); | |
209 | final ITmfTimestamp ts2copy = new TmfTimestamp(ts2); | |
9ee9135e FC |
210 | |
211 | assertTrue("hashCode", ts0.hashCode() == ts0copy.hashCode()); | |
212 | assertTrue("hashCode", ts1.hashCode() == ts1copy.hashCode()); | |
213 | assertTrue("hashCode", ts2.hashCode() == ts2copy.hashCode()); | |
214 | ||
215 | assertTrue("hashCode", ts0.hashCode() != ts1.hashCode()); | |
216 | } | |
085d898f | 217 | |
f2dd0808 FC |
218 | // ------------------------------------------------------------------------ |
219 | // equals | |
220 | // ------------------------------------------------------------------------ | |
d18dd09b | 221 | |
54a7a54c FC |
222 | /** |
223 | * | |
224 | */ | |
225 | public void testEqualsReflexivity() { | |
f2dd0808 FC |
226 | assertTrue("equals", ts0.equals(ts0)); |
227 | assertTrue("equals", ts1.equals(ts1)); | |
228 | ||
229 | assertTrue("equals", !ts0.equals(ts1)); | |
230 | assertTrue("equals", !ts1.equals(ts0)); | |
231 | } | |
232 | ||
54a7a54c FC |
233 | /** |
234 | * | |
235 | */ | |
236 | public void testEqualsSymmetry() { | |
085d898f | 237 | final ITmfTimestamp ts0copy = new TmfTimestamp(ts0); |
f2dd0808 FC |
238 | assertTrue("equals", ts0.equals(ts0copy)); |
239 | assertTrue("equals", ts0copy.equals(ts0)); | |
240 | ||
085d898f | 241 | final ITmfTimestamp ts1copy = new TmfTimestamp(ts1); |
f2dd0808 FC |
242 | assertTrue("equals", ts1.equals(ts1copy)); |
243 | assertTrue("equals", ts1copy.equals(ts1)); | |
244 | ||
085d898f | 245 | final ITmfTimestamp ts2copy = new TmfTimestamp(ts2); |
f2dd0808 FC |
246 | assertTrue("equals", ts2.equals(ts2copy)); |
247 | assertTrue("equals", ts2copy.equals(ts2)); | |
248 | } | |
249 | ||
54a7a54c FC |
250 | /** |
251 | * | |
252 | */ | |
253 | public void testEqualsTransivity() { | |
085d898f FC |
254 | final ITmfTimestamp ts0copy1 = new TmfTimestamp(ts0); |
255 | final ITmfTimestamp ts0copy2 = new TmfTimestamp(ts0copy1); | |
f2dd0808 FC |
256 | assertTrue("equals", ts0.equals(ts0copy1)); |
257 | assertTrue("equals", ts0copy1.equals(ts0copy2)); | |
258 | assertTrue("equals", ts0.equals(ts0copy2)); | |
259 | ||
085d898f FC |
260 | final ITmfTimestamp ts1copy1 = new TmfTimestamp(ts1); |
261 | final ITmfTimestamp ts1copy2 = new TmfTimestamp(ts1copy1); | |
f2dd0808 FC |
262 | assertTrue("equals", ts1.equals(ts1copy1)); |
263 | assertTrue("equals", ts1copy1.equals(ts1copy2)); | |
264 | assertTrue("equals", ts1.equals(ts1copy2)); | |
265 | ||
085d898f FC |
266 | final ITmfTimestamp ts2copy1 = new TmfTimestamp(ts2); |
267 | final ITmfTimestamp ts2copy2 = new TmfTimestamp(ts2copy1); | |
f2dd0808 FC |
268 | assertTrue("equals", ts2.equals(ts2copy1)); |
269 | assertTrue("equals", ts2copy1.equals(ts2copy2)); | |
270 | assertTrue("equals", ts2.equals(ts2copy2)); | |
271 | } | |
272 | ||
54a7a54c FC |
273 | /** |
274 | * | |
275 | */ | |
276 | public void testEqualsNull() { | |
f2dd0808 FC |
277 | assertTrue("equals", !ts0.equals(null)); |
278 | assertTrue("equals", !ts1.equals(null)); | |
279 | } | |
280 | ||
54a7a54c FC |
281 | /** |
282 | * | |
283 | */ | |
284 | public void testEqualsNonTimestamp() { | |
f2dd0808 FC |
285 | assertFalse("equals", ts0.equals(ts0.toString())); |
286 | } | |
085d898f | 287 | |
f2dd0808 FC |
288 | // ------------------------------------------------------------------------ |
289 | // toString | |
290 | // ------------------------------------------------------------------------ | |
d18dd09b | 291 | |
54a7a54c FC |
292 | /** |
293 | * | |
294 | */ | |
f8177ba2 | 295 | public void testToStringDefault() { |
d96e9054 FC |
296 | assertEquals("toString", "00:00:00.000 000 000", ts0.toString()); |
297 | assertEquals("toString", "03:25:45.000 000 000", ts1.toString()); | |
298 | assertEquals("toString", "00:20:34.500 000 000", ts2.toString()); | |
299 | assertEquals("toString", "06:55:00.000 000 000", ts3.toString()); | |
f2dd0808 | 300 | } |
d18dd09b | 301 | |
f8177ba2 FC |
302 | /** |
303 | * | |
304 | */ | |
305 | public void testToStringInterval() { | |
d96e9054 FC |
306 | assertEquals("toString", "000.000 000 000", ts0.toString(TmfTimestampFormat.getDefaulIntervalFormat())); |
307 | assertEquals("toString", "12345.000 000 000", ts1.toString(TmfTimestampFormat.getDefaulIntervalFormat())); | |
308 | assertEquals("toString", "1234.500 000 000", ts2.toString(TmfTimestampFormat.getDefaulIntervalFormat())); | |
309 | assertEquals("toString", "1234500.000 000 000", ts3.toString(TmfTimestampFormat.getDefaulIntervalFormat())); | |
f8177ba2 FC |
310 | } |
311 | ||
f2dd0808 | 312 | // ------------------------------------------------------------------------ |
f2dd0808 FC |
313 | // normalize |
314 | // ------------------------------------------------------------------------ | |
d18dd09b | 315 | |
54a7a54c FC |
316 | /** |
317 | * | |
318 | */ | |
319 | public void testNormalizeOffset() { | |
f2dd0808 FC |
320 | ITmfTimestamp ts = ts0.normalize(0, 0); |
321 | assertEquals("getValue", 0, ts.getValue()); | |
322 | assertEquals("getscale", 0, ts.getScale()); | |
323 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
324 | ||
325 | ts = ts0.normalize(12345, 0); | |
326 | assertEquals("getValue", 12345, ts.getValue()); | |
327 | assertEquals("getscale", 0, ts.getScale()); | |
328 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
329 | ||
330 | ts = ts0.normalize(10, 0); | |
331 | assertEquals("getValue", 10, ts.getValue()); | |
332 | assertEquals("getscale", 0, ts.getScale()); | |
333 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
334 | ||
335 | ts = ts0.normalize(-10, 0); | |
336 | assertEquals("getValue", -10, ts.getValue()); | |
337 | assertEquals("getscale", 0, ts.getScale()); | |
338 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
339 | } | |
340 | ||
54a7a54c FC |
341 | /** |
342 | * | |
343 | */ | |
344 | public void testNormalizeOffsetLowerLimits() { | |
f8177ba2 | 345 | final ITmfTimestamp ref = new TmfTimestamp(Long.MIN_VALUE + 5, 0); |
f2dd0808 FC |
346 | |
347 | ITmfTimestamp ts = ref.normalize(-4, 0); | |
348 | assertEquals("getValue", Long.MIN_VALUE + 1, ts.getValue()); | |
349 | assertEquals("getscale", 0, ts.getScale()); | |
350 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
351 | ||
352 | ts = ref.normalize(-5, 0); | |
353 | assertEquals("getValue", Long.MIN_VALUE, ts.getValue()); | |
354 | assertEquals("getscale", 0, ts.getScale()); | |
355 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
356 | ||
357 | ts = ref.normalize(-6, 0); | |
358 | assertEquals("getValue", Long.MIN_VALUE, ts.getValue()); | |
359 | assertEquals("getscale", 0, ts.getScale()); | |
360 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
361 | } | |
362 | ||
54a7a54c FC |
363 | /** |
364 | * | |
365 | */ | |
366 | public void testNormalizeOffsetUpperLimits() { | |
f8177ba2 | 367 | final ITmfTimestamp ref = new TmfTimestamp(Long.MAX_VALUE - 5, 0); |
f2dd0808 FC |
368 | |
369 | ITmfTimestamp ts = ref.normalize(4, 0); | |
370 | assertEquals("getValue", Long.MAX_VALUE - 1, ts.getValue()); | |
371 | assertEquals("getscale", 0, ts.getScale()); | |
372 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
373 | ||
374 | ts = ref.normalize(5, 0); | |
375 | assertEquals("getValue", Long.MAX_VALUE, ts.getValue()); | |
376 | assertEquals("getscale", 0, ts.getScale()); | |
377 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
378 | ||
379 | ts = ref.normalize(6, 0); | |
380 | assertEquals("getValue", Long.MAX_VALUE, ts.getValue()); | |
381 | assertEquals("getscale", 0, ts.getScale()); | |
382 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
383 | } | |
384 | ||
54a7a54c FC |
385 | /** |
386 | * | |
387 | */ | |
388 | public void testNormalizeScale() { | |
f2dd0808 FC |
389 | ITmfTimestamp ts = ts0.normalize(0, 10); |
390 | assertEquals("getValue", 0, ts.getValue()); | |
391 | assertEquals("getscale", 10, ts.getScale()); | |
392 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
393 | ||
394 | ts = ts0.normalize(0, -10); | |
395 | assertEquals("getValue", 0, ts.getValue()); | |
396 | assertEquals("getscale", -10, ts.getScale()); | |
397 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
398 | } | |
399 | ||
54a7a54c FC |
400 | /** |
401 | * | |
402 | */ | |
403 | public void testNormalizedScaleLimits() { | |
085d898f | 404 | final int MAX_SCALE_DIFF = 19; |
f2dd0808 FC |
405 | |
406 | // Test below limit | |
407 | try { | |
408 | ts1.normalize(0, +MAX_SCALE_DIFF - 1); | |
409 | ts1.normalize(0, -MAX_SCALE_DIFF + 1); | |
085d898f | 410 | } catch (final ArithmeticException e) { |
75d42a16 | 411 | fail("normalize: scale error"); |
f2dd0808 FC |
412 | } |
413 | ||
414 | // Test at limit | |
415 | try { | |
416 | ts1.normalize(0, +MAX_SCALE_DIFF); | |
75d42a16 | 417 | fail("normalize: scale error"); |
f2dd0808 | 418 | ts1.normalize(0, -MAX_SCALE_DIFF); |
75d42a16 | 419 | fail("normalize: scale error"); |
085d898f | 420 | } catch (final ArithmeticException e) { |
f2dd0808 FC |
421 | } |
422 | ||
423 | // Test over limit | |
424 | try { | |
425 | ts1.normalize(0, +MAX_SCALE_DIFF + 1); | |
75d42a16 | 426 | fail("normalize: scale error"); |
f2dd0808 | 427 | ts1.normalize(0, -MAX_SCALE_DIFF - 1); |
75d42a16 | 428 | fail("normalize: scale error"); |
085d898f | 429 | } catch (final ArithmeticException e) { |
f2dd0808 FC |
430 | } |
431 | } | |
432 | ||
54a7a54c FC |
433 | /** |
434 | * | |
435 | */ | |
436 | public void testNormalizeOffsetAndScaleTrivial() { | |
085d898f | 437 | final ITmfTimestamp ts = ts0.normalize(0, 0); |
f2dd0808 FC |
438 | assertEquals("getValue", 0, ts.getValue()); |
439 | assertEquals("getscale", 0, ts.getScale()); | |
440 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
441 | } | |
442 | ||
54a7a54c FC |
443 | /** |
444 | * | |
445 | */ | |
446 | public void testNormalizeOffsetAndScale() { | |
085d898f | 447 | final int SCALE = 12; |
f2dd0808 FC |
448 | |
449 | ITmfTimestamp ts = ts0.normalize(0, SCALE); | |
450 | assertEquals("getValue", 0, ts.getValue()); | |
451 | assertEquals("getscale", SCALE, ts.getScale()); | |
452 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
453 | ||
454 | ts = ts0.normalize(12345, SCALE); | |
455 | assertEquals("getValue", 12345, ts.getValue()); | |
456 | assertEquals("getscale", SCALE, ts.getScale()); | |
457 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
458 | ||
459 | ts = ts0.normalize(10, SCALE); | |
460 | assertEquals("getValue", 10, ts.getValue()); | |
461 | assertEquals("getscale", SCALE, ts.getScale()); | |
462 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
463 | ||
464 | ts = ts0.normalize(-10, SCALE); | |
465 | assertEquals("getValue", -10, ts.getValue()); | |
466 | assertEquals("getscale", SCALE, ts.getScale()); | |
467 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
468 | } | |
469 | ||
54a7a54c FC |
470 | /** |
471 | * | |
472 | */ | |
473 | public void testNormalizeOffsetAndScale2() { | |
f2dd0808 FC |
474 | int SCALE = 2; |
475 | ITmfTimestamp ts = ts1.normalize(0, SCALE); | |
476 | assertEquals("getValue", 123, ts.getValue()); | |
477 | assertEquals("getscale", SCALE, ts.getScale()); | |
478 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
479 | ||
480 | ts = ts1.normalize(12345, SCALE); | |
481 | assertEquals("getValue", 12468, ts.getValue()); | |
482 | assertEquals("getscale", SCALE, ts.getScale()); | |
483 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
484 | ||
485 | SCALE = -2; | |
486 | ts = ts1.normalize(0, SCALE); | |
487 | assertEquals("getValue", 1234500, ts.getValue()); | |
488 | assertEquals("getscale", SCALE, ts.getScale()); | |
489 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
490 | ||
491 | ts = ts1.normalize(67, SCALE); | |
492 | assertEquals("getValue", 1234567, ts.getValue()); | |
493 | assertEquals("getscale", SCALE, ts.getScale()); | |
494 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
495 | } | |
085d898f | 496 | |
f2dd0808 FC |
497 | // ------------------------------------------------------------------------ |
498 | // compareTo | |
499 | // ------------------------------------------------------------------------ | |
d18dd09b | 500 | |
54a7a54c FC |
501 | /** |
502 | * | |
503 | */ | |
504 | @SuppressWarnings("hiding") | |
505 | public void testBasicCompareTo() { | |
085d898f FC |
506 | final ITmfTimestamp ts1 = new TmfTimestamp(900, 0, 50); |
507 | final ITmfTimestamp ts2 = new TmfTimestamp(1000, 0, 50); | |
508 | final ITmfTimestamp ts3 = new TmfTimestamp(1100, 0, 50); | |
509 | final ITmfTimestamp ts4 = new TmfTimestamp(1000, 0, 75); | |
d18dd09b | 510 | |
f2dd0808 FC |
511 | assertTrue(ts1.compareTo(ts1) == 0); |
512 | ||
513 | assertTrue("CompareTo", ts1.compareTo(ts2) < 0); | |
514 | assertTrue("CompareTo", ts1.compareTo(ts3) < 0); | |
515 | assertTrue("CompareTo", ts1.compareTo(ts4) < 0); | |
516 | ||
517 | assertTrue("CompareTo", ts2.compareTo(ts1) > 0); | |
518 | assertTrue("CompareTo", ts2.compareTo(ts3) < 0); | |
519 | assertTrue("CompareTo", ts2.compareTo(ts4) == 0); | |
520 | ||
521 | assertTrue("CompareTo", ts3.compareTo(ts1) > 0); | |
522 | assertTrue("CompareTo", ts3.compareTo(ts2) > 0); | |
523 | assertTrue("CompareTo", ts3.compareTo(ts4) > 0); | |
524 | } | |
525 | ||
54a7a54c FC |
526 | /** |
527 | * | |
528 | */ | |
529 | public void testCompareToCornerCases1() { | |
085d898f FC |
530 | final ITmfTimestamp ts0a = new TmfTimestamp(ts0); |
531 | final ITmfTimestamp ts0b = new TmfTimestamp(ts0.getValue(), ts0.getScale() + 1); | |
532 | final ITmfTimestamp ts0c = new TmfTimestamp(ts0.getValue() + 1, ts0.getScale()); | |
533 | final ITmfTimestamp ts0d = new TmfTimestamp(ts0.getValue() + 1, ts0.getScale() + 1); | |
f2dd0808 FC |
534 | |
535 | assertTrue("compareTo", ts0.compareTo(ts0, false) == 0); | |
536 | assertTrue("compareTo", ts0.compareTo(ts0a, false) == 0); | |
537 | assertTrue("compareTo", ts0.compareTo(ts0b, false) == 0); | |
1703b536 | 538 | assertTrue("compareTo", ts0.compareTo(ts0c, false) == -1); |
f2dd0808 FC |
539 | assertTrue("compareTo", ts0.compareTo(ts0d, false) == -1); |
540 | } | |
541 | ||
54a7a54c FC |
542 | /** |
543 | * | |
544 | */ | |
545 | public void testCompareToCornerCases2() { | |
085d898f FC |
546 | final ITmfTimestamp ts0a = new TmfTimestamp(Long.MAX_VALUE, Integer.MAX_VALUE - 1); |
547 | final ITmfTimestamp ts0b = new TmfTimestamp(0, Integer.MAX_VALUE); | |
548 | final ITmfTimestamp ts0c = new TmfTimestamp(Long.MAX_VALUE, Integer.MAX_VALUE); | |
f2dd0808 | 549 | |
1703b536 FC |
550 | assertTrue("compareTo", ts0a.compareTo(ts0b, false) == 1); |
551 | assertTrue("compareTo", ts0a.compareTo(ts0c, false) == -1); | |
f2dd0808 | 552 | |
1703b536 FC |
553 | assertTrue("compareTo", ts0b.compareTo(ts0a, false) == -1); |
554 | assertTrue("compareTo", ts0b.compareTo(ts0c, false) == -1); | |
f2dd0808 | 555 | |
1703b536 FC |
556 | assertTrue("compareTo", ts0c.compareTo(ts0a, false) == 1); |
557 | assertTrue("compareTo", ts0c.compareTo(ts0b, false) == 1); | |
f2dd0808 FC |
558 | } |
559 | ||
54a7a54c FC |
560 | /** |
561 | * | |
562 | */ | |
563 | public void testCompareToCornerCases3() { | |
085d898f FC |
564 | final ITmfTimestamp ts0a = new TmfTimestamp(Long.MIN_VALUE, Integer.MAX_VALUE - 1); |
565 | final ITmfTimestamp ts0b = new TmfTimestamp(0, Integer.MAX_VALUE); | |
566 | final ITmfTimestamp ts0c = new TmfTimestamp(Long.MIN_VALUE, Integer.MAX_VALUE); | |
f2dd0808 | 567 | |
1703b536 FC |
568 | assertTrue("compareTo", ts0a.compareTo(ts0b, false) == -1); |
569 | assertTrue("compareTo", ts0a.compareTo(ts0c, false) == 1); | |
f2dd0808 | 570 | |
1703b536 FC |
571 | assertTrue("compareTo", ts0b.compareTo(ts0a, false) == 1); |
572 | assertTrue("compareTo", ts0b.compareTo(ts0c, false) == 1); | |
f2dd0808 | 573 | |
1703b536 FC |
574 | assertTrue("compareTo", ts0c.compareTo(ts0a, false) == -1); |
575 | assertTrue("compareTo", ts0c.compareTo(ts0b, false) == -1); | |
576 | } | |
577 | ||
54a7a54c FC |
578 | /** |
579 | * | |
580 | */ | |
581 | public void testCompareToCornerCases4() { | |
1703b536 FC |
582 | assertTrue("compareTo", ts0.compareTo(null, false) == 1); |
583 | assertTrue("compareTo", ts0.compareTo(null, true) == 1); | |
f2dd0808 FC |
584 | } |
585 | ||
54a7a54c FC |
586 | /** |
587 | * | |
588 | */ | |
589 | @SuppressWarnings("hiding") | |
590 | public void testCompareToSameScale() { | |
085d898f FC |
591 | final ITmfTimestamp ts1 = new TmfTimestamp(900, 0, 50); |
592 | final ITmfTimestamp ts2 = new TmfTimestamp(1000, 0, 50); | |
593 | final ITmfTimestamp ts3 = new TmfTimestamp(1100, 0, 50); | |
594 | final ITmfTimestamp ts4 = new TmfTimestamp(1000, 0, 75); | |
f2dd0808 FC |
595 | |
596 | assertTrue(ts1.compareTo(ts1, false) == 0); | |
597 | ||
598 | assertTrue("CompareTo", ts1.compareTo(ts2, false) < 0); | |
599 | assertTrue("CompareTo", ts1.compareTo(ts3, false) < 0); | |
600 | assertTrue("CompareTo", ts1.compareTo(ts4, false) < 0); | |
601 | ||
602 | assertTrue("CompareTo", ts2.compareTo(ts1, false) > 0); | |
603 | assertTrue("CompareTo", ts2.compareTo(ts3, false) < 0); | |
604 | assertTrue("CompareTo", ts2.compareTo(ts4, false) == 0); | |
605 | ||
606 | assertTrue("CompareTo", ts3.compareTo(ts1, false) > 0); | |
607 | assertTrue("CompareTo", ts3.compareTo(ts2, false) > 0); | |
608 | assertTrue("CompareTo", ts3.compareTo(ts4, false) > 0); | |
609 | } | |
610 | ||
54a7a54c FC |
611 | /** |
612 | * | |
613 | */ | |
614 | @SuppressWarnings("hiding") | |
615 | public void testCompareToDifferentScale() { | |
085d898f FC |
616 | final ITmfTimestamp ts1 = new TmfTimestamp(9000, -1, 50); |
617 | final ITmfTimestamp ts2 = new TmfTimestamp(1000, 0, 50); | |
618 | final ITmfTimestamp ts3 = new TmfTimestamp(110, 1, 50); | |
619 | final ITmfTimestamp ts4 = new TmfTimestamp(1, 3, 75); | |
f2dd0808 FC |
620 | |
621 | assertTrue("CompareTo", ts1.compareTo(ts1, false) == 0); | |
622 | ||
623 | assertTrue("CompareTo", ts1.compareTo(ts2, false) < 0); | |
624 | assertTrue("CompareTo", ts1.compareTo(ts3, false) < 0); | |
625 | assertTrue("CompareTo", ts1.compareTo(ts4, false) < 0); | |
626 | ||
627 | assertTrue("CompareTo", ts2.compareTo(ts1, false) > 0); | |
628 | assertTrue("CompareTo", ts2.compareTo(ts3, false) < 0); | |
629 | assertTrue("CompareTo", ts2.compareTo(ts4, false) == 0); | |
630 | ||
631 | assertTrue("CompareTo", ts3.compareTo(ts1, false) > 0); | |
632 | assertTrue("CompareTo", ts3.compareTo(ts2, false) > 0); | |
633 | assertTrue("CompareTo", ts3.compareTo(ts4, false) > 0); | |
634 | } | |
635 | ||
54a7a54c FC |
636 | /** |
637 | * | |
638 | */ | |
639 | @SuppressWarnings("hiding") | |
640 | public void testCompareToWithinPrecision() { | |
085d898f FC |
641 | final ITmfTimestamp ts1 = new TmfTimestamp(900, 0, 50); |
642 | final ITmfTimestamp ts2 = new TmfTimestamp(1000, 0, 50); | |
643 | final ITmfTimestamp ts3 = new TmfTimestamp(1100, 0, 50); | |
644 | final ITmfTimestamp ts4 = new TmfTimestamp(1000, 0, 75); | |
f2dd0808 FC |
645 | |
646 | assertTrue("CompareTo", ts1.compareTo(ts1, true) == 0); | |
647 | ||
648 | assertTrue("CompareTo", ts1.compareTo(ts2, true) == 0); | |
649 | assertTrue("CompareTo", ts1.compareTo(ts3, true) < 0); | |
650 | assertTrue("CompareTo", ts1.compareTo(ts4, true) == 0); | |
651 | ||
652 | assertTrue("CompareTo", ts2.compareTo(ts1, true) == 0); | |
653 | assertTrue("CompareTo", ts2.compareTo(ts3, true) == 0); | |
654 | assertTrue("CompareTo", ts2.compareTo(ts4, true) == 0); | |
655 | ||
656 | assertTrue("CompareTo", ts3.compareTo(ts1, true) > 0); | |
657 | assertTrue("CompareTo", ts3.compareTo(ts2, true) == 0); | |
658 | assertTrue("CompareTo", ts3.compareTo(ts4, true) == 0); | |
659 | } | |
cbd4ad82 | 660 | |
54a7a54c FC |
661 | /** |
662 | * | |
663 | */ | |
664 | @SuppressWarnings("hiding") | |
665 | public void testCompareToLargeScale1() { | |
085d898f FC |
666 | final ITmfTimestamp ts1 = new TmfTimestamp(-1, 100); |
667 | final ITmfTimestamp ts2 = new TmfTimestamp(-1000, -100); | |
668 | final ITmfTimestamp ts3 = new TmfTimestamp(1, 100); | |
669 | final ITmfTimestamp ts4 = new TmfTimestamp(1000, -100); | |
cbd4ad82 | 670 | |
f2dd0808 FC |
671 | assertTrue("CompareTo", ts1.compareTo(ts2, false) < 0); |
672 | assertTrue("CompareTo", ts1.compareTo(ts3, false) < 0); | |
673 | assertTrue("CompareTo", ts1.compareTo(ts4, false) < 0); | |
cbd4ad82 | 674 | |
f2dd0808 FC |
675 | assertTrue("CompareTo", ts2.compareTo(ts1, false) > 0); |
676 | assertTrue("CompareTo", ts2.compareTo(ts3, false) < 0); | |
677 | assertTrue("CompareTo", ts2.compareTo(ts4, false) < 0); | |
cbd4ad82 | 678 | |
f2dd0808 FC |
679 | assertTrue("CompareTo", ts3.compareTo(ts1, false) > 0); |
680 | assertTrue("CompareTo", ts3.compareTo(ts2, false) > 0); | |
681 | assertTrue("CompareTo", ts3.compareTo(ts4, false) > 0); | |
d18dd09b | 682 | |
f2dd0808 FC |
683 | assertTrue("CompareTo", ts4.compareTo(ts1, false) > 0); |
684 | assertTrue("CompareTo", ts4.compareTo(ts2, false) > 0); | |
685 | assertTrue("CompareTo", ts4.compareTo(ts3, false) < 0); | |
686 | } | |
687 | ||
54a7a54c FC |
688 | /** |
689 | * | |
690 | */ | |
691 | public void testCompareToLargeScale2() { | |
085d898f FC |
692 | final ITmfTimestamp ts0a = new TmfTimestamp(0, Integer.MAX_VALUE); |
693 | final ITmfTimestamp ts0b = new TmfTimestamp(1, Integer.MAX_VALUE); | |
d18dd09b | 694 | |
f2dd0808 FC |
695 | assertTrue("CompareTo", ts0a.compareTo(ts0, false) == 0); |
696 | assertTrue("CompareTo", ts0.compareTo(ts0a, false) == 0); | |
d18dd09b | 697 | |
f2dd0808 FC |
698 | assertTrue("CompareTo", ts0b.compareTo(ts0, false) == 1); |
699 | assertTrue("CompareTo", ts0.compareTo(ts0b, false) == -1); | |
700 | } | |
d18dd09b | 701 | |
73005152 BH |
702 | // ------------------------------------------------------------------------ |
703 | // getDelta | |
704 | // ------------------------------------------------------------------------ | |
085d898f | 705 | |
54a7a54c FC |
706 | /** |
707 | * | |
708 | */ | |
709 | @SuppressWarnings("hiding") | |
710 | public void testDelta() { | |
f2dd0808 | 711 | // Delta for same scale and precision (delta > 0) |
7656d70a FC |
712 | ITmfTimestamp ts0 = new TmfTimestamp(10, 9); |
713 | ITmfTimestamp ts1 = new TmfTimestamp(5, 9); | |
714 | ITmfTimestamp exp = new TmfTimestamp(5, 9); | |
f2dd0808 FC |
715 | |
716 | ITmfTimestamp delta = ts0.getDelta(ts1); | |
717 | assertEquals("getDelta", 0, delta.compareTo(exp, false)); | |
718 | ||
719 | // Delta for same scale and precision (delta < 0) | |
720 | ts0 = new TmfTimestamp(5, 9); | |
721 | ts1 = new TmfTimestamp(10, 9); | |
722 | exp = new TmfTimestamp(-5, 9); | |
723 | ||
724 | delta = ts0.getDelta(ts1); | |
725 | assertEquals("getDelta", 0, delta.compareTo(exp, false)); | |
726 | ||
727 | // Delta for different scale and same precision (delta > 0) | |
728 | ts0 = new TmfTimestamp(5, 9); | |
5179fc01 | 729 | ts1 = new TmfTimestamp(10, 8); |
f2dd0808 | 730 | exp = new TmfTimestamp(4, 9); |
73005152 BH |
731 | |
732 | delta = ts0.getDelta(ts1); | |
733 | assertEquals("getDelta", 0, delta.compareTo(exp, false)); | |
734 | ||
735 | // Delta for different scale and same precision (delta > 0) | |
f2dd0808 | 736 | ts0 = new TmfTimestamp(5, 9); |
5179fc01 | 737 | ts1 = new TmfTimestamp(10, 7); |
f2dd0808 | 738 | exp = new TmfTimestamp(5, 9); |
73005152 BH |
739 | |
740 | delta = ts0.getDelta(ts1); | |
741 | assertEquals("getDelta", 0, delta.compareTo(exp, false)); | |
742 | ||
743 | // Delta for different scale and same precision | |
5179fc01 | 744 | ts0 = new TmfTimestamp(10, 9); |
f2dd0808 | 745 | ts1 = new TmfTimestamp(5, 8); |
5179fc01 | 746 | exp = new TmfTimestamp(10, 9); |
73005152 BH |
747 | |
748 | delta = ts0.getDelta(ts1); | |
749 | assertEquals("getDelta", 0, delta.compareTo(exp, false)); | |
750 | ||
751 | // Delta for same scale and different precision | |
5179fc01 | 752 | ts0 = new TmfTimestamp(10, 9, 1); |
f2dd0808 FC |
753 | ts1 = new TmfTimestamp(5, 9, 2); |
754 | exp = new TmfTimestamp(5, 9, 3); | |
73005152 BH |
755 | |
756 | delta = ts0.getDelta(ts1); | |
757 | assertEquals("getDelta", 0, delta.compareTo(exp, true)); | |
5179fc01 | 758 | assertEquals("precision", 3, delta.getPrecision()); |
f2dd0808 | 759 | |
73005152 | 760 | // Delta for same scale and different precision |
f2dd0808 | 761 | ts0 = new TmfTimestamp(5, 9, 2); |
5179fc01 FC |
762 | ts1 = new TmfTimestamp(10, 9, 1); |
763 | exp = new TmfTimestamp(-5, 9, 3); | |
73005152 BH |
764 | |
765 | delta = ts0.getDelta(ts1); | |
f2dd0808 | 766 | assertEquals("getDelta", 0, delta.compareTo(exp, true)); |
5179fc01 | 767 | assertEquals("precision", 3, delta.getPrecision()); |
73005152 BH |
768 | |
769 | // Delta for different scale and different precision | |
f2dd0808 | 770 | ts0 = new TmfTimestamp(5, 9, 2); |
5179fc01 | 771 | ts1 = new TmfTimestamp(10, 8, 1); |
f2dd0808 | 772 | exp = new TmfTimestamp(4, 9, 3); |
73005152 BH |
773 | delta = ts0.getDelta(ts1); |
774 | assertEquals("getDelta", 0, delta.compareTo(exp, true)); | |
775 | assertEquals("precision", 2, delta.getPrecision()); | |
f2dd0808 FC |
776 | } |
777 | ||
d18dd09b | 778 | } |