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 * <b><u>TmfSimpleTimestampTest</u></b>
24 * Test suite for the TmfSimpleTimestampTest class.
26 @SuppressWarnings("nls")
27 public class TmfSimpleTimestampTest
extends TestCase
{
29 // ------------------------------------------------------------------------
31 // ------------------------------------------------------------------------
33 private final ITmfTimestamp ts0
= new TmfSimpleTimestamp();
34 private final ITmfTimestamp ts1
= new TmfSimpleTimestamp(12345);
35 private final ITmfTimestamp ts2
= new TmfSimpleTimestamp(-1234);
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
42 * @param name the test name
44 public TmfSimpleTimestampTest(String name
) {
49 protected void setUp() throws Exception
{
54 protected void tearDown() throws Exception
{
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
62 public void testDefaultConstructor() throws Exception
{
63 assertEquals("getValue", 0, ts0
.getValue());
64 assertEquals("getscale", 0, ts0
.getScale());
65 assertEquals("getPrecision", 0, ts0
.getPrecision());
68 public void testFullConstructor() throws Exception
{
69 assertEquals("getValue", 12345, ts1
.getValue());
70 assertEquals("getscale", 0, ts1
.getScale());
71 assertEquals("getPrecision", 0, ts1
.getPrecision());
74 public void testCopyConstructor() throws Exception
{
75 ITmfTimestamp copy
= new TmfSimpleTimestamp(ts1
);
77 assertEquals("getValue", ts1
.getValue(), copy
.getValue());
78 assertEquals("getscale", ts1
.getScale(), copy
.getScale());
79 assertEquals("getPrecision", ts1
.getPrecision(), copy
.getPrecision());
81 assertEquals("getValue", 12345, copy
.getValue());
82 assertEquals("getscale", 0, copy
.getScale());
83 assertEquals("getPrecision", 0, copy
.getPrecision());
86 public void testCopyBadTimestamp() throws Exception
{
87 ITmfTimestamp ts0a
= new TmfTimestamp(0, 1, 0);
88 ITmfTimestamp ts0b
= new TmfTimestamp(0, 0, 1);
91 @SuppressWarnings("unused")
92 ITmfTimestamp timestamp
= new TmfSimpleTimestamp(null);
93 fail("TmfSimpleTimestamp: null argument");
94 } catch (IllegalArgumentException e
) {
98 @SuppressWarnings("unused")
99 ITmfTimestamp ts
= new TmfSimpleTimestamp(ts0a
);
100 fail("TmfSimpleTimestamp: bad scale");
101 } catch (IllegalArgumentException e
) {
105 @SuppressWarnings("unused")
106 ITmfTimestamp ts
= new TmfSimpleTimestamp(ts0b
);
107 fail("TmfSimpleTimestamp: bad precision");
108 } catch (IllegalArgumentException e
) {
112 // ------------------------------------------------------------------------
114 // ------------------------------------------------------------------------
116 public static class MyTimestamp
extends TmfSimpleTimestamp
{
119 public boolean equals(Object other
) {
120 return super.equals(other
);
124 public MyTimestamp
clone() {
125 return (MyTimestamp
) super.clone();
129 public void testClone() throws Exception
{
130 ITmfTimestamp timestamp
= ts0
.clone();
131 assertEquals("clone", timestamp
, ts0
);
134 public void testClone2() throws Exception
{
135 MyTimestamp timestamp
= new MyTimestamp();
136 MyTimestamp clone
= timestamp
.clone();
137 assertEquals("clone", clone
, timestamp
);
140 // ------------------------------------------------------------------------
142 // ------------------------------------------------------------------------
144 public void testEqualsReflexivity() throws Exception
{
145 assertTrue("equals", ts0
.equals(ts0
));
146 assertTrue("equals", ts1
.equals(ts1
));
147 assertTrue("equals", ts2
.equals(ts2
));
149 assertTrue("equals", !ts0
.equals(ts1
));
150 assertTrue("equals", !ts0
.equals(ts2
));
152 assertTrue("equals", !ts1
.equals(ts0
));
153 assertTrue("equals", !ts1
.equals(ts2
));
155 assertTrue("equals", !ts2
.equals(ts0
));
156 assertTrue("equals", !ts2
.equals(ts1
));
159 public void testEqualsSymmetry() throws Exception
{
160 ITmfTimestamp ts0copy
= new TmfSimpleTimestamp(ts0
);
161 assertTrue("equals", ts0
.equals(ts0copy
));
162 assertTrue("equals", ts0copy
.equals(ts0
));
164 ITmfTimestamp ts1copy
= new TmfSimpleTimestamp(ts1
);
165 assertTrue("equals", ts1
.equals(ts1copy
));
166 assertTrue("equals", ts1copy
.equals(ts1
));
169 public void testEqualsTransivity() throws Exception
{
170 ITmfTimestamp ts0copy1
= new TmfSimpleTimestamp(ts0
);
171 ITmfTimestamp ts0copy2
= new TmfSimpleTimestamp(ts0copy1
);
172 assertTrue("equals", ts0
.equals(ts0copy1
));
173 assertTrue("equals", ts0copy1
.equals(ts0copy2
));
174 assertTrue("equals", ts0
.equals(ts0copy2
));
176 ITmfTimestamp ts1copy1
= new TmfSimpleTimestamp(ts1
);
177 ITmfTimestamp ts1copy2
= new TmfSimpleTimestamp(ts1copy1
);
178 assertTrue("equals", ts1
.equals(ts1copy1
));
179 assertTrue("equals", ts1copy1
.equals(ts1copy2
));
180 assertTrue("equals", ts1
.equals(ts1copy2
));
183 public void testEqualsNull() throws Exception
{
184 assertTrue("equals", !ts0
.equals(null));
185 assertTrue("equals", !ts1
.equals(null));
186 assertTrue("equals", !ts2
.equals(null));
189 public void testEqualsNonTimestamp() throws Exception
{
190 assertFalse("equals", ts0
.equals(ts0
.toString()));
193 // ------------------------------------------------------------------------
195 // ------------------------------------------------------------------------
197 public void testToString() throws Exception
{
198 assertEquals("toString", "TmfSimpleTimestamp [fValue=0]", ts0
.toString());
199 assertEquals("toString", "TmfSimpleTimestamp [fValue=12345]", ts1
.toString());
200 assertEquals("toString", "TmfSimpleTimestamp [fValue=-1234]", ts2
.toString());
203 // ------------------------------------------------------------------------
205 // ------------------------------------------------------------------------
207 public void testHashCode() throws Exception
{
208 ITmfTimestamp ts0copy
= new TmfTimestamp(ts0
);
209 ITmfTimestamp ts1copy
= new TmfTimestamp(ts1
);
210 ITmfTimestamp ts2copy
= new TmfTimestamp(ts2
);
212 assertTrue("hashCode", ts0
.hashCode() == ts0copy
.hashCode());
213 assertTrue("hashCode", ts1
.hashCode() == ts1copy
.hashCode());
214 assertTrue("hashCode", ts2
.hashCode() == ts2copy
.hashCode());
216 assertTrue("hashCode", ts0
.hashCode() != ts1
.hashCode());
219 // ------------------------------------------------------------------------
221 // ------------------------------------------------------------------------
223 public void testNormalizeScale0() throws Exception
{
224 ITmfTimestamp ts
= ts0
.normalize(0, 0);
225 assertEquals("getValue", 0, ts
.getValue());
226 assertEquals("getscale", 0, ts
.getScale());
227 assertEquals("getPrecision", 0, ts
.getPrecision());
229 ts
= ts0
.normalize(12345, 0);
230 assertEquals("getValue", 12345, ts
.getValue());
231 assertEquals("getscale", 0, ts
.getScale());
232 assertEquals("getPrecision", 0, ts
.getPrecision());
234 ts
= ts0
.normalize(10, 0);
235 assertEquals("getValue", 10, 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());
245 public void testNormalizeScaleNot0() throws Exception
{
246 ITmfTimestamp ts
= ts0
.normalize(0, 1);
247 assertEquals("getValue", 0, ts
.getValue());
248 assertEquals("getscale", 1, ts
.getScale());
249 assertEquals("getPrecision", 0, ts
.getPrecision());
251 ts
= ts0
.normalize(12345, 1);
252 assertEquals("getValue", 12345, ts
.getValue());
253 assertEquals("getscale", 1, ts
.getScale());
254 assertEquals("getPrecision", 0, ts
.getPrecision());
256 ts
= ts0
.normalize(10, 1);
257 assertEquals("getValue", 10, 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());
267 // ------------------------------------------------------------------------
269 // ------------------------------------------------------------------------
271 public void testBasicCompareTo() throws Exception
{
272 ITmfTimestamp ts1
= new TmfSimpleTimestamp(900);
273 ITmfTimestamp ts2
= new TmfSimpleTimestamp(1000);
274 ITmfTimestamp ts3
= new TmfSimpleTimestamp(1100);
276 assertTrue(ts1
.compareTo(ts1
) == 0);
278 assertTrue("CompareTo", ts1
.compareTo(ts2
) < 0);
279 assertTrue("CompareTo", ts1
.compareTo(ts3
) < 0);
281 assertTrue("CompareTo", ts2
.compareTo(ts1
) > 0);
282 assertTrue("CompareTo", ts2
.compareTo(ts3
) < 0);
284 assertTrue("CompareTo", ts3
.compareTo(ts1
) > 0);
285 assertTrue("CompareTo", ts3
.compareTo(ts2
) > 0);
288 public void testCompareTo() throws Exception
{
289 ITmfTimestamp ts0a
= new TmfTimestamp(0, 2, 0);
290 ITmfTimestamp ts1a
= new TmfTimestamp(123450, -1);
291 ITmfTimestamp ts2a
= new TmfTimestamp(-12340, -1);
293 assertTrue(ts1
.compareTo(ts1
) == 0);
295 assertTrue("CompareTo", ts0
.compareTo(ts0a
) == 0);
296 assertTrue("CompareTo", ts1
.compareTo(ts1a
) == 0);
297 assertTrue("CompareTo", ts2
.compareTo(ts2a
) == 0);
300 // ------------------------------------------------------------------------
302 // ------------------------------------------------------------------------
304 public void testDelta() throws Exception
{
305 // Delta for same scale and precision (delta > 0)
306 TmfTimestamp ts0
= new TmfSimpleTimestamp(10);
307 TmfTimestamp ts1
= new TmfSimpleTimestamp(5);
308 TmfTimestamp exp
= new TmfSimpleTimestamp(5);
310 ITmfTimestamp delta
= ts0
.getDelta(ts1
);
311 assertEquals("getDelta", 0, delta
.compareTo(exp
, false));
313 // Delta for same scale and precision (delta < 0)
314 ts0
= new TmfTimestamp(5);
315 ts1
= new TmfTimestamp(10);
316 exp
= new TmfTimestamp(-5);
318 delta
= ts0
.getDelta(ts1
);
319 assertEquals("getDelta", 0, delta
.compareTo(exp
, false));
322 public void testDelta2() throws Exception
{
323 // Delta for different scale and same precision (delta > 0)
324 TmfTimestamp ts0
= new TmfSimpleTimestamp(10);
325 TmfTimestamp ts1
= new TmfTimestamp(1, 1);
326 TmfTimestamp exp
= new TmfTimestamp(0, 0);
328 ITmfTimestamp delta
= ts0
.getDelta(ts1
);
329 assertEquals("getDelta", 0, delta
.compareTo(exp
, false));