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