1 /*******************************************************************************
2 * Copyright (c) 2012 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.event
;
15 import junit
.framework
.TestCase
;
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
;
22 * Test suite for the TmfSimpleTimestampTest class.
24 @SuppressWarnings("nls")
25 public class TmfSimpleTimestampTest
extends TestCase
{
27 // ------------------------------------------------------------------------
29 // ------------------------------------------------------------------------
31 private final ITmfTimestamp ts0
= new TmfSimpleTimestamp();
32 private final ITmfTimestamp ts1
= new TmfSimpleTimestamp(12345);
33 private final ITmfTimestamp ts2
= new TmfSimpleTimestamp(-1234);
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
40 * @param name the test name
42 public TmfSimpleTimestampTest(final String name
) {
47 protected void setUp() throws Exception
{
52 protected void tearDown() throws Exception
{
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
60 public void testDefaultConstructor() throws Exception
{
61 assertEquals("getValue", 0, ts0
.getValue());
62 assertEquals("getscale", 0, ts0
.getScale());
63 assertEquals("getPrecision", 0, ts0
.getPrecision());
66 public void testFullConstructor() throws Exception
{
67 assertEquals("getValue", 12345, ts1
.getValue());
68 assertEquals("getscale", 0, ts1
.getScale());
69 assertEquals("getPrecision", 0, ts1
.getPrecision());
72 public void testCopyConstructor() throws Exception
{
73 final ITmfTimestamp copy
= new TmfSimpleTimestamp(ts1
);
75 assertEquals("getValue", ts1
.getValue(), copy
.getValue());
76 assertEquals("getscale", ts1
.getScale(), copy
.getScale());
77 assertEquals("getPrecision", ts1
.getPrecision(), copy
.getPrecision());
79 assertEquals("getValue", 12345, copy
.getValue());
80 assertEquals("getscale", 0, copy
.getScale());
81 assertEquals("getPrecision", 0, copy
.getPrecision());
84 public void testCopyBadTimestamp() throws Exception
{
85 final ITmfTimestamp ts0a
= new TmfTimestamp(0, 1, 0);
86 final ITmfTimestamp ts0b
= new TmfTimestamp(0, 0, 1);
89 new TmfSimpleTimestamp(null);
90 fail("TmfSimpleTimestamp: null argument");
91 } catch (final IllegalArgumentException e
) {
95 new TmfSimpleTimestamp(ts0a
);
96 fail("TmfSimpleTimestamp: bad scale");
97 } catch (final IllegalArgumentException e
) {
101 new TmfSimpleTimestamp(ts0b
);
102 fail("TmfSimpleTimestamp: bad precision");
103 } catch (final IllegalArgumentException e
) {
107 // ------------------------------------------------------------------------
109 // ------------------------------------------------------------------------
111 public static class MyTimestamp
extends TmfSimpleTimestamp
{
114 public boolean equals(final Object other
) {
115 return super.equals(other
);
119 public MyTimestamp
clone() {
120 return (MyTimestamp
) super.clone();
124 public void testClone() throws Exception
{
125 final ITmfTimestamp clone
= ts0
.clone();
127 assertTrue("clone", ts0
.clone().equals(ts0
));
128 assertTrue("clone", clone
.clone().equals(clone
));
130 assertEquals("clone", clone
, ts0
);
131 assertEquals("clone", ts0
, clone
);
134 public void testClone2() throws Exception
{
135 final MyTimestamp timestamp
= new MyTimestamp();
136 final MyTimestamp clone
= timestamp
.clone();
138 assertTrue("clone", timestamp
.clone().equals(timestamp
));
139 assertTrue("clone", clone
.clone().equals(clone
));
141 assertEquals("clone", clone
, timestamp
);
142 assertEquals("clone", timestamp
, clone
);
145 // ------------------------------------------------------------------------
147 // ------------------------------------------------------------------------
149 public void testEqualsReflexivity() throws Exception
{
150 assertTrue("equals", ts0
.equals(ts0
));
151 assertTrue("equals", ts1
.equals(ts1
));
152 assertTrue("equals", ts2
.equals(ts2
));
154 assertTrue("equals", !ts0
.equals(ts1
));
155 assertTrue("equals", !ts0
.equals(ts2
));
157 assertTrue("equals", !ts1
.equals(ts0
));
158 assertTrue("equals", !ts1
.equals(ts2
));
160 assertTrue("equals", !ts2
.equals(ts0
));
161 assertTrue("equals", !ts2
.equals(ts1
));
164 public void testEqualsSymmetry() throws Exception
{
165 final ITmfTimestamp ts0copy
= new TmfSimpleTimestamp(ts0
);
166 assertTrue("equals", ts0
.equals(ts0copy
));
167 assertTrue("equals", ts0copy
.equals(ts0
));
169 final ITmfTimestamp ts1copy
= new TmfSimpleTimestamp(ts1
);
170 assertTrue("equals", ts1
.equals(ts1copy
));
171 assertTrue("equals", ts1copy
.equals(ts1
));
174 public void testEqualsTransivity() throws Exception
{
175 final ITmfTimestamp ts0copy1
= new TmfSimpleTimestamp(ts0
);
176 final ITmfTimestamp ts0copy2
= new TmfSimpleTimestamp(ts0copy1
);
177 assertTrue("equals", ts0
.equals(ts0copy1
));
178 assertTrue("equals", ts0copy1
.equals(ts0copy2
));
179 assertTrue("equals", ts0
.equals(ts0copy2
));
181 final ITmfTimestamp ts1copy1
= new TmfSimpleTimestamp(ts1
);
182 final ITmfTimestamp ts1copy2
= new TmfSimpleTimestamp(ts1copy1
);
183 assertTrue("equals", ts1
.equals(ts1copy1
));
184 assertTrue("equals", ts1copy1
.equals(ts1copy2
));
185 assertTrue("equals", ts1
.equals(ts1copy2
));
188 public void testEqualsNull() throws Exception
{
189 assertTrue("equals", !ts0
.equals(null));
190 assertTrue("equals", !ts1
.equals(null));
191 assertTrue("equals", !ts2
.equals(null));
194 public void testEqualsNonTimestamp() throws Exception
{
195 assertFalse("equals", ts0
.equals(ts0
.toString()));
198 // ------------------------------------------------------------------------
200 // ------------------------------------------------------------------------
202 public void testToString() throws Exception
{
203 assertEquals("toString", "TmfSimpleTimestamp [fValue=0]", ts0
.toString());
204 assertEquals("toString", "TmfSimpleTimestamp [fValue=12345]", ts1
.toString());
205 assertEquals("toString", "TmfSimpleTimestamp [fValue=-1234]", ts2
.toString());
208 // ------------------------------------------------------------------------
210 // ------------------------------------------------------------------------
212 public void testHashCode() throws Exception
{
213 final ITmfTimestamp ts0copy
= new TmfTimestamp(ts0
);
214 final ITmfTimestamp ts1copy
= new TmfTimestamp(ts1
);
215 final ITmfTimestamp ts2copy
= new TmfTimestamp(ts2
);
217 assertTrue("hashCode", ts0
.hashCode() == ts0copy
.hashCode());
218 assertTrue("hashCode", ts1
.hashCode() == ts1copy
.hashCode());
219 assertTrue("hashCode", ts2
.hashCode() == ts2copy
.hashCode());
221 assertTrue("hashCode", ts0
.hashCode() != ts1
.hashCode());
224 // ------------------------------------------------------------------------
226 // ------------------------------------------------------------------------
228 public void testNormalizeScale0() throws Exception
{
229 ITmfTimestamp ts
= ts0
.normalize(0, 0);
230 assertEquals("getValue", 0, ts
.getValue());
231 assertEquals("getscale", 0, ts
.getScale());
232 assertEquals("getPrecision", 0, ts
.getPrecision());
234 ts
= ts0
.normalize(12345, 0);
235 assertEquals("getValue", 12345, ts
.getValue());
236 assertEquals("getscale", 0, ts
.getScale());
237 assertEquals("getPrecision", 0, ts
.getPrecision());
239 ts
= ts0
.normalize(10, 0);
240 assertEquals("getValue", 10, ts
.getValue());
241 assertEquals("getscale", 0, ts
.getScale());
242 assertEquals("getPrecision", 0, ts
.getPrecision());
244 ts
= ts0
.normalize(-10, 0);
245 assertEquals("getValue", -10, ts
.getValue());
246 assertEquals("getscale", 0, ts
.getScale());
247 assertEquals("getPrecision", 0, ts
.getPrecision());
250 public void testNormalizeScaleNot0() throws Exception
{
251 ITmfTimestamp ts
= ts0
.normalize(0, 1);
252 assertEquals("getValue", 0, ts
.getValue());
253 assertEquals("getscale", 1, ts
.getScale());
254 assertEquals("getPrecision", 0, ts
.getPrecision());
256 ts
= ts0
.normalize(12345, 1);
257 assertEquals("getValue", 12345, ts
.getValue());
258 assertEquals("getscale", 1, ts
.getScale());
259 assertEquals("getPrecision", 0, ts
.getPrecision());
261 ts
= ts0
.normalize(10, 1);
262 assertEquals("getValue", 10, ts
.getValue());
263 assertEquals("getscale", 1, ts
.getScale());
264 assertEquals("getPrecision", 0, ts
.getPrecision());
266 ts
= ts0
.normalize(-10, 1);
267 assertEquals("getValue", -10, ts
.getValue());
268 assertEquals("getscale", 1, ts
.getScale());
269 assertEquals("getPrecision", 0, ts
.getPrecision());
272 // ------------------------------------------------------------------------
274 // ------------------------------------------------------------------------
276 public void testBasicCompareTo() throws Exception
{
277 final ITmfTimestamp ts1
= new TmfSimpleTimestamp(900);
278 final ITmfTimestamp ts2
= new TmfSimpleTimestamp(1000);
279 final ITmfTimestamp ts3
= new TmfSimpleTimestamp(1100);
281 assertTrue(ts1
.compareTo(ts1
) == 0);
283 assertTrue("CompareTo", ts1
.compareTo(ts2
) < 0);
284 assertTrue("CompareTo", ts1
.compareTo(ts3
) < 0);
286 assertTrue("CompareTo", ts2
.compareTo(ts1
) > 0);
287 assertTrue("CompareTo", ts2
.compareTo(ts3
) < 0);
289 assertTrue("CompareTo", ts3
.compareTo(ts1
) > 0);
290 assertTrue("CompareTo", ts3
.compareTo(ts2
) > 0);
293 public void testCompareTo() throws Exception
{
294 final ITmfTimestamp ts0a
= new TmfTimestamp(0, 2, 0);
295 final ITmfTimestamp ts1a
= new TmfTimestamp(123450, -1);
296 final ITmfTimestamp ts2a
= new TmfTimestamp(-12340, -1);
298 assertTrue(ts1
.compareTo(ts1
) == 0);
300 assertTrue("CompareTo", ts0
.compareTo(ts0a
) == 0);
301 assertTrue("CompareTo", ts1
.compareTo(ts1a
) == 0);
302 assertTrue("CompareTo", ts2
.compareTo(ts2a
) == 0);
305 // ------------------------------------------------------------------------
307 // ------------------------------------------------------------------------
309 public void testDelta() throws Exception
{
310 // Delta for same scale and precision (delta > 0)
311 TmfTimestamp ts0
= new TmfSimpleTimestamp(10);
312 TmfTimestamp ts1
= new TmfSimpleTimestamp(5);
313 TmfTimestamp exp
= new TmfSimpleTimestamp(5);
315 ITmfTimestamp delta
= ts0
.getDelta(ts1
);
316 assertEquals("getDelta", 0, delta
.compareTo(exp
, false));
318 // Delta for same scale and precision (delta < 0)
319 ts0
= new TmfTimestamp(5);
320 ts1
= new TmfTimestamp(10);
321 exp
= new TmfTimestamp(-5);
323 delta
= ts0
.getDelta(ts1
);
324 assertEquals("getDelta", 0, delta
.compareTo(exp
, false));
327 public void testDelta2() throws Exception
{
328 // Delta for different scale and same precision (delta > 0)
329 final TmfTimestamp ts0
= new TmfSimpleTimestamp(10);
330 final TmfTimestamp ts1
= new TmfTimestamp(1, 1);
331 final TmfTimestamp exp
= new TmfTimestamp(0, 0);
333 final ITmfTimestamp delta
= ts0
.getDelta(ts1
);
334 assertEquals("getDelta", 0, delta
.compareTo(exp
, false));