1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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 * Alexandre Montplaisir - Port to JUnit4
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.event
;
16 import static org
.junit
.Assert
.assertEquals
;
17 import static org
.junit
.Assert
.assertFalse
;
18 import static org
.junit
.Assert
.assertTrue
;
19 import static org
.junit
.Assert
.fail
;
21 import java
.text
.DateFormat
;
22 import java
.text
.SimpleDateFormat
;
23 import java
.util
.Date
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfSimpleTimestamp
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
28 import org
.junit
.Test
;
31 * Test suite for the TmfSimpleTimestampTest class.
33 @SuppressWarnings("javadoc")
34 public class TmfSimpleTimestampTest
{
36 // ------------------------------------------------------------------------
38 // ------------------------------------------------------------------------
40 private final ITmfTimestamp ts0
= new TmfSimpleTimestamp();
41 private final ITmfTimestamp ts1
= new TmfSimpleTimestamp(12345);
42 private final ITmfTimestamp ts2
= new TmfSimpleTimestamp(-1234);
44 // ------------------------------------------------------------------------
46 // ------------------------------------------------------------------------
49 public void testDefaultConstructor() {
50 assertEquals("getValue", 0, ts0
.getValue());
51 assertEquals("getscale", 0, ts0
.getScale());
52 assertEquals("getPrecision", 0, ts0
.getPrecision());
56 public void testFullConstructor() {
57 assertEquals("getValue", 12345, ts1
.getValue());
58 assertEquals("getscale", 0, ts1
.getScale());
59 assertEquals("getPrecision", 0, ts1
.getPrecision());
63 public void testCopyConstructor() {
64 final ITmfTimestamp copy
= new TmfSimpleTimestamp(ts1
);
66 assertEquals("getValue", ts1
.getValue(), copy
.getValue());
67 assertEquals("getscale", ts1
.getScale(), copy
.getScale());
68 assertEquals("getPrecision", ts1
.getPrecision(), copy
.getPrecision());
70 assertEquals("getValue", 12345, copy
.getValue());
71 assertEquals("getscale", 0, copy
.getScale());
72 assertEquals("getPrecision", 0, copy
.getPrecision());
76 public void testCopyBadTimestamp() {
78 new TmfSimpleTimestamp(null);
79 fail("TmfSimpleTimestamp: null argument");
80 } catch (final NullPointerException e
) {
84 // ------------------------------------------------------------------------
86 // ------------------------------------------------------------------------
89 public void testEqualsReflexivity() {
90 assertTrue("equals", ts0
.equals(ts0
));
91 assertTrue("equals", ts1
.equals(ts1
));
92 assertTrue("equals", ts2
.equals(ts2
));
94 assertTrue("equals", !ts0
.equals(ts1
));
95 assertTrue("equals", !ts0
.equals(ts2
));
97 assertTrue("equals", !ts1
.equals(ts0
));
98 assertTrue("equals", !ts1
.equals(ts2
));
100 assertTrue("equals", !ts2
.equals(ts0
));
101 assertTrue("equals", !ts2
.equals(ts1
));
105 public void testEqualsSymmetry() {
106 final ITmfTimestamp ts0copy
= new TmfSimpleTimestamp(ts0
);
107 assertTrue("equals", ts0
.equals(ts0copy
));
108 assertTrue("equals", ts0copy
.equals(ts0
));
110 final ITmfTimestamp ts1copy
= new TmfSimpleTimestamp(ts1
);
111 assertTrue("equals", ts1
.equals(ts1copy
));
112 assertTrue("equals", ts1copy
.equals(ts1
));
116 public void testEqualsTransivity() {
117 final ITmfTimestamp ts0copy1
= new TmfSimpleTimestamp(ts0
);
118 final ITmfTimestamp ts0copy2
= new TmfSimpleTimestamp(ts0copy1
);
119 assertTrue("equals", ts0
.equals(ts0copy1
));
120 assertTrue("equals", ts0copy1
.equals(ts0copy2
));
121 assertTrue("equals", ts0
.equals(ts0copy2
));
123 final ITmfTimestamp ts1copy1
= new TmfSimpleTimestamp(ts1
);
124 final ITmfTimestamp ts1copy2
= new TmfSimpleTimestamp(ts1copy1
);
125 assertTrue("equals", ts1
.equals(ts1copy1
));
126 assertTrue("equals", ts1copy1
.equals(ts1copy2
));
127 assertTrue("equals", ts1
.equals(ts1copy2
));
131 public void testEqualsNull() {
132 assertTrue("equals", !ts0
.equals(null));
133 assertTrue("equals", !ts1
.equals(null));
134 assertTrue("equals", !ts2
.equals(null));
138 public void testEqualsNonTimestamp() {
139 assertFalse("equals", ts0
.equals(ts0
.toString()));
142 // ------------------------------------------------------------------------
144 // ------------------------------------------------------------------------
147 public void testToString() {
148 DateFormat df
= new SimpleDateFormat("HH:mm:ss.SSS");
149 Date d0
= new Date(ts0
.getValue()*1000);
150 Date d1
= new Date(ts1
.getValue()*1000);
151 Date d2
= new Date(ts2
.getValue()*1000);
152 assertEquals("toString", df
.format(d0
) + " 000 000", ts0
.toString());
153 assertEquals("toString", df
.format(d1
) + " 000 000", ts1
.toString());
154 assertEquals("toString", df
.format(d2
) + " 000 000", ts2
.toString());
157 // ------------------------------------------------------------------------
159 // ------------------------------------------------------------------------
162 public void testHashCode() {
163 final ITmfTimestamp ts0copy
= new TmfTimestamp(ts0
);
164 final ITmfTimestamp ts1copy
= new TmfTimestamp(ts1
);
165 final ITmfTimestamp ts2copy
= new TmfTimestamp(ts2
);
167 assertTrue("hashCode", ts0
.hashCode() == ts0copy
.hashCode());
168 assertTrue("hashCode", ts1
.hashCode() == ts1copy
.hashCode());
169 assertTrue("hashCode", ts2
.hashCode() == ts2copy
.hashCode());
171 assertTrue("hashCode", ts0
.hashCode() != ts1
.hashCode());
174 // ------------------------------------------------------------------------
176 // ------------------------------------------------------------------------
179 public void testNormalizeScale0() {
180 ITmfTimestamp ts
= ts0
.normalize(0, 0);
181 assertEquals("getValue", 0, ts
.getValue());
182 assertEquals("getscale", 0, ts
.getScale());
183 assertEquals("getPrecision", 0, ts
.getPrecision());
185 ts
= ts0
.normalize(12345, 0);
186 assertEquals("getValue", 12345, ts
.getValue());
187 assertEquals("getscale", 0, ts
.getScale());
188 assertEquals("getPrecision", 0, ts
.getPrecision());
190 ts
= ts0
.normalize(10, 0);
191 assertEquals("getValue", 10, ts
.getValue());
192 assertEquals("getscale", 0, ts
.getScale());
193 assertEquals("getPrecision", 0, ts
.getPrecision());
195 ts
= ts0
.normalize(-10, 0);
196 assertEquals("getValue", -10, ts
.getValue());
197 assertEquals("getscale", 0, ts
.getScale());
198 assertEquals("getPrecision", 0, ts
.getPrecision());
202 public void testNormalizeScaleNot0() {
203 ITmfTimestamp ts
= ts0
.normalize(0, 1);
204 assertEquals("getValue", 0, ts
.getValue());
205 assertEquals("getscale", 1, ts
.getScale());
206 assertEquals("getPrecision", 0, ts
.getPrecision());
208 ts
= ts0
.normalize(12345, 1);
209 assertEquals("getValue", 12345, ts
.getValue());
210 assertEquals("getscale", 1, ts
.getScale());
211 assertEquals("getPrecision", 0, ts
.getPrecision());
213 ts
= ts0
.normalize(10, 1);
214 assertEquals("getValue", 10, ts
.getValue());
215 assertEquals("getscale", 1, ts
.getScale());
216 assertEquals("getPrecision", 0, ts
.getPrecision());
218 ts
= ts0
.normalize(-10, 1);
219 assertEquals("getValue", -10, ts
.getValue());
220 assertEquals("getscale", 1, ts
.getScale());
221 assertEquals("getPrecision", 0, ts
.getPrecision());
224 // ------------------------------------------------------------------------
226 // ------------------------------------------------------------------------
229 public void testBasicCompareTo() {
230 final ITmfTimestamp tstamp1
= new TmfSimpleTimestamp(900);
231 final ITmfTimestamp tstamp2
= new TmfSimpleTimestamp(1000);
232 final ITmfTimestamp tstamp3
= new TmfSimpleTimestamp(1100);
234 assertTrue(tstamp1
.compareTo(tstamp1
) == 0);
236 assertTrue("CompareTo", tstamp1
.compareTo(tstamp2
) < 0);
237 assertTrue("CompareTo", tstamp1
.compareTo(tstamp3
) < 0);
239 assertTrue("CompareTo", tstamp2
.compareTo(tstamp1
) > 0);
240 assertTrue("CompareTo", tstamp2
.compareTo(tstamp3
) < 0);
242 assertTrue("CompareTo", tstamp3
.compareTo(tstamp1
) > 0);
243 assertTrue("CompareTo", tstamp3
.compareTo(tstamp2
) > 0);
247 public void testCompareTo() {
248 final ITmfTimestamp ts0a
= new TmfTimestamp(0, 2, 0);
249 final ITmfTimestamp ts1a
= new TmfTimestamp(123450, -1);
250 final ITmfTimestamp ts2a
= new TmfTimestamp(-12340, -1);
252 assertTrue(ts1
.compareTo(ts1
) == 0);
254 assertTrue("CompareTo", ts0
.compareTo(ts0a
) == 0);
255 assertTrue("CompareTo", ts1
.compareTo(ts1a
) == 0);
256 assertTrue("CompareTo", ts2
.compareTo(ts2a
) == 0);
259 // ------------------------------------------------------------------------
261 // ------------------------------------------------------------------------
264 public void testDelta() {
265 // Delta for same scale and precision (delta > 0)
266 TmfTimestamp tstamp0
= new TmfSimpleTimestamp(10);
267 TmfTimestamp tstamp1
= new TmfSimpleTimestamp(5);
268 TmfTimestamp expectd
= new TmfSimpleTimestamp(5);
270 ITmfTimestamp delta
= tstamp0
.getDelta(tstamp1
);
271 assertEquals("getDelta", 0, delta
.compareTo(expectd
, false));
273 // Delta for same scale and precision (delta < 0)
274 tstamp0
= new TmfTimestamp(5);
275 tstamp1
= new TmfTimestamp(10);
276 expectd
= new TmfTimestamp(-5);
278 delta
= tstamp0
.getDelta(tstamp1
);
279 assertEquals("getDelta", 0, delta
.compareTo(expectd
, false));
283 public void testDelta2() {
284 // Delta for different scale and same precision (delta > 0)
285 final TmfTimestamp tstamp0
= new TmfSimpleTimestamp(10);
286 final TmfTimestamp tstamp1
= new TmfTimestamp(1, 1);
287 final TmfTimestamp expectd
= new TmfTimestamp(0, 0);
289 final ITmfTimestamp delta
= tstamp0
.getDelta(tstamp1
);
290 assertEquals("getDelta", 0, delta
.compareTo(expectd
, false));
This page took 0.03672 seconds and 5 git commands to generate.