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 new TmfSimpleTimestamp(null);
92 fail("TmfSimpleTimestamp: null argument");
93 } catch (IllegalArgumentException e
) {
97 new TmfSimpleTimestamp(ts0a
);
98 fail("TmfSimpleTimestamp: bad scale");
99 } catch (IllegalArgumentException e
) {
103 new TmfSimpleTimestamp(ts0b
);
104 fail("TmfSimpleTimestamp: bad precision");
105 } catch (IllegalArgumentException e
) {
109 // ------------------------------------------------------------------------
111 // ------------------------------------------------------------------------
113 public static class MyTimestamp
extends TmfSimpleTimestamp
{
116 public boolean equals(Object other
) {
117 return super.equals(other
);
121 public MyTimestamp
clone() {
122 return (MyTimestamp
) super.clone();
126 public void testClone() throws Exception
{
127 ITmfTimestamp timestamp
= ts0
.clone();
128 assertEquals("clone", timestamp
, ts0
);
131 public void testClone2() throws Exception
{
132 MyTimestamp timestamp
= new MyTimestamp();
133 MyTimestamp clone
= timestamp
.clone();
134 assertEquals("clone", clone
, timestamp
);
137 // ------------------------------------------------------------------------
139 // ------------------------------------------------------------------------
141 public void testEqualsReflexivity() throws Exception
{
142 assertTrue("equals", ts0
.equals(ts0
));
143 assertTrue("equals", ts1
.equals(ts1
));
144 assertTrue("equals", ts2
.equals(ts2
));
146 assertTrue("equals", !ts0
.equals(ts1
));
147 assertTrue("equals", !ts0
.equals(ts2
));
149 assertTrue("equals", !ts1
.equals(ts0
));
150 assertTrue("equals", !ts1
.equals(ts2
));
152 assertTrue("equals", !ts2
.equals(ts0
));
153 assertTrue("equals", !ts2
.equals(ts1
));
156 public void testEqualsSymmetry() throws Exception
{
157 ITmfTimestamp ts0copy
= new TmfSimpleTimestamp(ts0
);
158 assertTrue("equals", ts0
.equals(ts0copy
));
159 assertTrue("equals", ts0copy
.equals(ts0
));
161 ITmfTimestamp ts1copy
= new TmfSimpleTimestamp(ts1
);
162 assertTrue("equals", ts1
.equals(ts1copy
));
163 assertTrue("equals", ts1copy
.equals(ts1
));
166 public void testEqualsTransivity() throws Exception
{
167 ITmfTimestamp ts0copy1
= new TmfSimpleTimestamp(ts0
);
168 ITmfTimestamp ts0copy2
= new TmfSimpleTimestamp(ts0copy1
);
169 assertTrue("equals", ts0
.equals(ts0copy1
));
170 assertTrue("equals", ts0copy1
.equals(ts0copy2
));
171 assertTrue("equals", ts0
.equals(ts0copy2
));
173 ITmfTimestamp ts1copy1
= new TmfSimpleTimestamp(ts1
);
174 ITmfTimestamp ts1copy2
= new TmfSimpleTimestamp(ts1copy1
);
175 assertTrue("equals", ts1
.equals(ts1copy1
));
176 assertTrue("equals", ts1copy1
.equals(ts1copy2
));
177 assertTrue("equals", ts1
.equals(ts1copy2
));
180 public void testEqualsNull() throws Exception
{
181 assertTrue("equals", !ts0
.equals(null));
182 assertTrue("equals", !ts1
.equals(null));
183 assertTrue("equals", !ts2
.equals(null));
186 public void testEqualsNonTimestamp() throws Exception
{
187 assertFalse("equals", ts0
.equals(ts0
.toString()));
190 // ------------------------------------------------------------------------
192 // ------------------------------------------------------------------------
194 public void testToString() throws Exception
{
195 assertEquals("toString", "TmfSimpleTimestamp [fValue=0]", ts0
.toString());
196 assertEquals("toString", "TmfSimpleTimestamp [fValue=12345]", ts1
.toString());
197 assertEquals("toString", "TmfSimpleTimestamp [fValue=-1234]", ts2
.toString());
200 // ------------------------------------------------------------------------
202 // ------------------------------------------------------------------------
204 public void testHashCode() throws Exception
{
205 ITmfTimestamp ts0copy
= new TmfTimestamp(ts0
);
206 ITmfTimestamp ts1copy
= new TmfTimestamp(ts1
);
207 ITmfTimestamp ts2copy
= new TmfTimestamp(ts2
);
209 assertTrue("hashCode", ts0
.hashCode() == ts0copy
.hashCode());
210 assertTrue("hashCode", ts1
.hashCode() == ts1copy
.hashCode());
211 assertTrue("hashCode", ts2
.hashCode() == ts2copy
.hashCode());
213 assertTrue("hashCode", ts0
.hashCode() != ts1
.hashCode());
216 // ------------------------------------------------------------------------
218 // ------------------------------------------------------------------------
220 public void testNormalizeScale0() throws Exception
{
221 ITmfTimestamp ts
= ts0
.normalize(0, 0);
222 assertEquals("getValue", 0, ts
.getValue());
223 assertEquals("getscale", 0, ts
.getScale());
224 assertEquals("getPrecision", 0, ts
.getPrecision());
226 ts
= ts0
.normalize(12345, 0);
227 assertEquals("getValue", 12345, ts
.getValue());
228 assertEquals("getscale", 0, ts
.getScale());
229 assertEquals("getPrecision", 0, ts
.getPrecision());
231 ts
= ts0
.normalize(10, 0);
232 assertEquals("getValue", 10, ts
.getValue());
233 assertEquals("getscale", 0, ts
.getScale());
234 assertEquals("getPrecision", 0, ts
.getPrecision());
236 ts
= ts0
.normalize(-10, 0);
237 assertEquals("getValue", -10, ts
.getValue());
238 assertEquals("getscale", 0, ts
.getScale());
239 assertEquals("getPrecision", 0, ts
.getPrecision());
242 public void testNormalizeScaleNot0() throws Exception
{
243 ITmfTimestamp ts
= ts0
.normalize(0, 1);
244 assertEquals("getValue", 0, ts
.getValue());
245 assertEquals("getscale", 1, ts
.getScale());
246 assertEquals("getPrecision", 0, ts
.getPrecision());
248 ts
= ts0
.normalize(12345, 1);
249 assertEquals("getValue", 12345, ts
.getValue());
250 assertEquals("getscale", 1, ts
.getScale());
251 assertEquals("getPrecision", 0, ts
.getPrecision());
253 ts
= ts0
.normalize(10, 1);
254 assertEquals("getValue", 10, ts
.getValue());
255 assertEquals("getscale", 1, ts
.getScale());
256 assertEquals("getPrecision", 0, ts
.getPrecision());
258 ts
= ts0
.normalize(-10, 1);
259 assertEquals("getValue", -10, ts
.getValue());
260 assertEquals("getscale", 1, ts
.getScale());
261 assertEquals("getPrecision", 0, ts
.getPrecision());
264 // ------------------------------------------------------------------------
266 // ------------------------------------------------------------------------
268 public void testBasicCompareTo() throws Exception
{
269 ITmfTimestamp ts1
= new TmfSimpleTimestamp(900);
270 ITmfTimestamp ts2
= new TmfSimpleTimestamp(1000);
271 ITmfTimestamp ts3
= new TmfSimpleTimestamp(1100);
273 assertTrue(ts1
.compareTo(ts1
) == 0);
275 assertTrue("CompareTo", ts1
.compareTo(ts2
) < 0);
276 assertTrue("CompareTo", ts1
.compareTo(ts3
) < 0);
278 assertTrue("CompareTo", ts2
.compareTo(ts1
) > 0);
279 assertTrue("CompareTo", ts2
.compareTo(ts3
) < 0);
281 assertTrue("CompareTo", ts3
.compareTo(ts1
) > 0);
282 assertTrue("CompareTo", ts3
.compareTo(ts2
) > 0);
285 public void testCompareTo() throws Exception
{
286 ITmfTimestamp ts0a
= new TmfTimestamp(0, 2, 0);
287 ITmfTimestamp ts1a
= new TmfTimestamp(123450, -1);
288 ITmfTimestamp ts2a
= new TmfTimestamp(-12340, -1);
290 assertTrue(ts1
.compareTo(ts1
) == 0);
292 assertTrue("CompareTo", ts0
.compareTo(ts0a
) == 0);
293 assertTrue("CompareTo", ts1
.compareTo(ts1a
) == 0);
294 assertTrue("CompareTo", ts2
.compareTo(ts2a
) == 0);
297 // ------------------------------------------------------------------------
299 // ------------------------------------------------------------------------
301 public void testDelta() throws Exception
{
302 // Delta for same scale and precision (delta > 0)
303 TmfTimestamp ts0
= new TmfSimpleTimestamp(10);
304 TmfTimestamp ts1
= new TmfSimpleTimestamp(5);
305 TmfTimestamp exp
= new TmfSimpleTimestamp(5);
307 ITmfTimestamp delta
= ts0
.getDelta(ts1
);
308 assertEquals("getDelta", 0, delta
.compareTo(exp
, false));
310 // Delta for same scale and precision (delta < 0)
311 ts0
= new TmfTimestamp(5);
312 ts1
= new TmfTimestamp(10);
313 exp
= new TmfTimestamp(-5);
315 delta
= ts0
.getDelta(ts1
);
316 assertEquals("getDelta", 0, delta
.compareTo(exp
, false));
319 public void testDelta2() throws Exception
{
320 // Delta for different scale and same precision (delta > 0)
321 TmfTimestamp ts0
= new TmfSimpleTimestamp(10);
322 TmfTimestamp ts1
= new TmfTimestamp(1, 1);
323 TmfTimestamp exp
= new TmfTimestamp(0, 0);
325 ITmfTimestamp delta
= ts0
.getDelta(ts1
);
326 assertEquals("getDelta", 0, delta
.compareTo(exp
, false));