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