292f227606b7417711be0e5d6918b9c612a5f847
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / event / TmfSimpleTimestampTest.java
1 /*******************************************************************************
2 * Copyright (c) 2012 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.core.tests.event;
14
15 import junit.framework.TestCase;
16
17 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
18 import org.eclipse.linuxtools.tmf.core.event.TmfSimpleTimestamp;
19 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
20
21 /**
22 * Test suite for the TmfSimpleTimestampTest class.
23 */
24 @SuppressWarnings("nls")
25 public class TmfSimpleTimestampTest extends TestCase {
26
27 // ------------------------------------------------------------------------
28 // Variables
29 // ------------------------------------------------------------------------
30
31 private final ITmfTimestamp ts0 = new TmfSimpleTimestamp();
32 private final ITmfTimestamp ts1 = new TmfSimpleTimestamp(12345);
33 private final ITmfTimestamp ts2 = new TmfSimpleTimestamp(-1234);
34
35 // ------------------------------------------------------------------------
36 // Housekeping
37 // ------------------------------------------------------------------------
38
39 /**
40 * @param name the test name
41 */
42 public TmfSimpleTimestampTest(final String name) {
43 super(name);
44 }
45
46 @Override
47 protected void setUp() throws Exception {
48 super.setUp();
49 }
50
51 @Override
52 protected void tearDown() throws Exception {
53 super.tearDown();
54 }
55
56 // ------------------------------------------------------------------------
57 // Constructors
58 // ------------------------------------------------------------------------
59
60 /**
61 *
62 */
63 public void testDefaultConstructor() {
64 assertEquals("getValue", 0, ts0.getValue());
65 assertEquals("getscale", 0, ts0.getScale());
66 assertEquals("getPrecision", 0, ts0.getPrecision());
67 }
68
69 /**
70 *
71 */
72 public void testFullConstructor() {
73 assertEquals("getValue", 12345, ts1.getValue());
74 assertEquals("getscale", 0, ts1.getScale());
75 assertEquals("getPrecision", 0, ts1.getPrecision());
76 }
77
78 /**
79 *
80 */
81 public void testCopyConstructor() {
82 final ITmfTimestamp copy = new TmfSimpleTimestamp(ts1);
83
84 assertEquals("getValue", ts1.getValue(), copy.getValue());
85 assertEquals("getscale", ts1.getScale(), copy.getScale());
86 assertEquals("getPrecision", ts1.getPrecision(), copy.getPrecision());
87
88 assertEquals("getValue", 12345, copy.getValue());
89 assertEquals("getscale", 0, copy.getScale());
90 assertEquals("getPrecision", 0, copy.getPrecision());
91 }
92
93 /**
94 *
95 */
96 public void testCopyBadTimestamp() {
97 final ITmfTimestamp ts0a = new TmfTimestamp(0, 100, 0);
98
99 try {
100 new TmfSimpleTimestamp(null);
101 fail("TmfSimpleTimestamp: null argument");
102 } catch (final IllegalArgumentException e) {
103 }
104
105 try {
106 new TmfSimpleTimestamp(ts0a);
107 fail("TmfSimpleTimestamp: bad scale");
108 } catch (final ArithmeticException e) {
109 }
110 }
111
112 // ------------------------------------------------------------------------
113 // clone
114 // ------------------------------------------------------------------------
115
116 private static class MyTimestamp extends TmfSimpleTimestamp {
117
118 @Override
119 public boolean equals(final Object other) {
120 return super.equals(other);
121 }
122
123 @Override
124 public MyTimestamp clone() {
125 return (MyTimestamp) super.clone();
126 }
127 }
128
129 /**
130 *
131 */
132 public void testClone() {
133 final ITmfTimestamp clone = ts0.clone();
134
135 assertTrue("clone", ts0.clone().equals(ts0));
136 assertTrue("clone", clone.clone().equals(clone));
137
138 assertEquals("clone", clone, ts0);
139 assertEquals("clone", ts0, clone);
140 }
141
142 /**
143 *
144 */
145 public void testClone2() {
146 final MyTimestamp timestamp = new MyTimestamp();
147 final MyTimestamp clone = timestamp.clone();
148
149 assertTrue("clone", timestamp.clone().equals(timestamp));
150 assertTrue("clone", clone.clone().equals(clone));
151
152 assertEquals("clone", clone, timestamp);
153 assertEquals("clone", timestamp, clone);
154 }
155
156 // ------------------------------------------------------------------------
157 // equals
158 // ------------------------------------------------------------------------
159
160 /**
161 *
162 */
163 public void testEqualsReflexivity() {
164 assertTrue("equals", ts0.equals(ts0));
165 assertTrue("equals", ts1.equals(ts1));
166 assertTrue("equals", ts2.equals(ts2));
167
168 assertTrue("equals", !ts0.equals(ts1));
169 assertTrue("equals", !ts0.equals(ts2));
170
171 assertTrue("equals", !ts1.equals(ts0));
172 assertTrue("equals", !ts1.equals(ts2));
173
174 assertTrue("equals", !ts2.equals(ts0));
175 assertTrue("equals", !ts2.equals(ts1));
176 }
177
178 /**
179 *
180 */
181 public void testEqualsSymmetry() {
182 final ITmfTimestamp ts0copy = new TmfSimpleTimestamp(ts0);
183 assertTrue("equals", ts0.equals(ts0copy));
184 assertTrue("equals", ts0copy.equals(ts0));
185
186 final ITmfTimestamp ts1copy = new TmfSimpleTimestamp(ts1);
187 assertTrue("equals", ts1.equals(ts1copy));
188 assertTrue("equals", ts1copy.equals(ts1));
189 }
190
191 /**
192 *
193 */
194 public void testEqualsTransivity() {
195 final ITmfTimestamp ts0copy1 = new TmfSimpleTimestamp(ts0);
196 final ITmfTimestamp ts0copy2 = new TmfSimpleTimestamp(ts0copy1);
197 assertTrue("equals", ts0.equals(ts0copy1));
198 assertTrue("equals", ts0copy1.equals(ts0copy2));
199 assertTrue("equals", ts0.equals(ts0copy2));
200
201 final ITmfTimestamp ts1copy1 = new TmfSimpleTimestamp(ts1);
202 final ITmfTimestamp ts1copy2 = new TmfSimpleTimestamp(ts1copy1);
203 assertTrue("equals", ts1.equals(ts1copy1));
204 assertTrue("equals", ts1copy1.equals(ts1copy2));
205 assertTrue("equals", ts1.equals(ts1copy2));
206 }
207
208 /**
209 *
210 */
211 public void testEqualsNull() {
212 assertTrue("equals", !ts0.equals(null));
213 assertTrue("equals", !ts1.equals(null));
214 assertTrue("equals", !ts2.equals(null));
215 }
216
217 /**
218 *
219 */
220 public void testEqualsNonTimestamp() {
221 assertFalse("equals", ts0.equals(ts0.toString()));
222 }
223
224 // ------------------------------------------------------------------------
225 // toString
226 // ------------------------------------------------------------------------
227
228 /**
229 *
230 */
231 public void testToString() {
232 assertEquals("toString", "00:00:00.000_000_000", ts0.toString());
233 assertEquals("toString", "03:25:45.000_000_000", ts1.toString());
234 assertEquals("toString", "23:39:26.000_000_000", ts2.toString());
235 }
236
237 // ------------------------------------------------------------------------
238 // hashCode
239 // ------------------------------------------------------------------------
240
241 /**
242 *
243 */
244 public void testHashCode() {
245 final ITmfTimestamp ts0copy = new TmfTimestamp(ts0);
246 final ITmfTimestamp ts1copy = new TmfTimestamp(ts1);
247 final ITmfTimestamp ts2copy = new TmfTimestamp(ts2);
248
249 assertTrue("hashCode", ts0.hashCode() == ts0copy.hashCode());
250 assertTrue("hashCode", ts1.hashCode() == ts1copy.hashCode());
251 assertTrue("hashCode", ts2.hashCode() == ts2copy.hashCode());
252
253 assertTrue("hashCode", ts0.hashCode() != ts1.hashCode());
254 }
255
256 // ------------------------------------------------------------------------
257 // normalize
258 // ------------------------------------------------------------------------
259
260 /**
261 *
262 */
263 public void testNormalizeScale0() {
264 ITmfTimestamp ts = ts0.normalize(0, 0);
265 assertEquals("getValue", 0, ts.getValue());
266 assertEquals("getscale", 0, ts.getScale());
267 assertEquals("getPrecision", 0, ts.getPrecision());
268
269 ts = ts0.normalize(12345, 0);
270 assertEquals("getValue", 12345, ts.getValue());
271 assertEquals("getscale", 0, ts.getScale());
272 assertEquals("getPrecision", 0, ts.getPrecision());
273
274 ts = ts0.normalize(10, 0);
275 assertEquals("getValue", 10, ts.getValue());
276 assertEquals("getscale", 0, ts.getScale());
277 assertEquals("getPrecision", 0, ts.getPrecision());
278
279 ts = ts0.normalize(-10, 0);
280 assertEquals("getValue", -10, ts.getValue());
281 assertEquals("getscale", 0, ts.getScale());
282 assertEquals("getPrecision", 0, ts.getPrecision());
283 }
284
285 /**
286 *
287 */
288 public void testNormalizeScaleNot0() {
289 ITmfTimestamp ts = ts0.normalize(0, 1);
290 assertEquals("getValue", 0, ts.getValue());
291 assertEquals("getscale", 1, ts.getScale());
292 assertEquals("getPrecision", 0, ts.getPrecision());
293
294 ts = ts0.normalize(12345, 1);
295 assertEquals("getValue", 12345, ts.getValue());
296 assertEquals("getscale", 1, ts.getScale());
297 assertEquals("getPrecision", 0, ts.getPrecision());
298
299 ts = ts0.normalize(10, 1);
300 assertEquals("getValue", 10, ts.getValue());
301 assertEquals("getscale", 1, ts.getScale());
302 assertEquals("getPrecision", 0, ts.getPrecision());
303
304 ts = ts0.normalize(-10, 1);
305 assertEquals("getValue", -10, ts.getValue());
306 assertEquals("getscale", 1, ts.getScale());
307 assertEquals("getPrecision", 0, ts.getPrecision());
308 }
309
310 // ------------------------------------------------------------------------
311 // compareTo
312 // ------------------------------------------------------------------------
313
314 /**
315 *
316 */
317 public void testBasicCompareTo() {
318 final ITmfTimestamp tstamp1 = new TmfSimpleTimestamp(900);
319 final ITmfTimestamp tstamp2 = new TmfSimpleTimestamp(1000);
320 final ITmfTimestamp tstamp3 = new TmfSimpleTimestamp(1100);
321
322 assertTrue(tstamp1.compareTo(tstamp1) == 0);
323
324 assertTrue("CompareTo", tstamp1.compareTo(tstamp2) < 0);
325 assertTrue("CompareTo", tstamp1.compareTo(tstamp3) < 0);
326
327 assertTrue("CompareTo", tstamp2.compareTo(tstamp1) > 0);
328 assertTrue("CompareTo", tstamp2.compareTo(tstamp3) < 0);
329
330 assertTrue("CompareTo", tstamp3.compareTo(tstamp1) > 0);
331 assertTrue("CompareTo", tstamp3.compareTo(tstamp2) > 0);
332 }
333
334 /**
335 *
336 */
337 public void testCompareTo() {
338 final ITmfTimestamp ts0a = new TmfTimestamp(0, 2, 0);
339 final ITmfTimestamp ts1a = new TmfTimestamp(123450, -1);
340 final ITmfTimestamp ts2a = new TmfTimestamp(-12340, -1);
341
342 assertTrue(ts1.compareTo(ts1) == 0);
343
344 assertTrue("CompareTo", ts0.compareTo(ts0a) == 0);
345 assertTrue("CompareTo", ts1.compareTo(ts1a) == 0);
346 assertTrue("CompareTo", ts2.compareTo(ts2a) == 0);
347 }
348
349 // ------------------------------------------------------------------------
350 // getDelta
351 // ------------------------------------------------------------------------
352
353 /**
354 *
355 */
356 public void testDelta() {
357 // Delta for same scale and precision (delta > 0)
358 TmfTimestamp tstamp0 = new TmfSimpleTimestamp(10);
359 TmfTimestamp tstamp1 = new TmfSimpleTimestamp(5);
360 TmfTimestamp expectd = new TmfSimpleTimestamp(5);
361
362 ITmfTimestamp delta = tstamp0.getDelta(tstamp1);
363 assertEquals("getDelta", 0, delta.compareTo(expectd, false));
364
365 // Delta for same scale and precision (delta < 0)
366 tstamp0 = new TmfTimestamp(5);
367 tstamp1 = new TmfTimestamp(10);
368 expectd = new TmfTimestamp(-5);
369
370 delta = tstamp0.getDelta(tstamp1);
371 assertEquals("getDelta", 0, delta.compareTo(expectd, false));
372 }
373
374 /**
375 *
376 */
377 public void testDelta2() {
378 // Delta for different scale and same precision (delta > 0)
379 final TmfTimestamp tstamp0 = new TmfSimpleTimestamp(10);
380 final TmfTimestamp tstamp1 = new TmfTimestamp(1, 1);
381 final TmfTimestamp expectd = new TmfTimestamp(0, 0);
382
383 final ITmfTimestamp delta = tstamp0.getDelta(tstamp1);
384 assertEquals("getDelta", 0, delta.compareTo(expectd, false));
385 }
386
387 }
This page took 0.03909 seconds and 4 git commands to generate.