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