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