683ccd2c62f804b78fdfd805d20af53035b89c34
[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 * <b><u>TmfSimpleTimestampTest</u></b>
23 * <p>
24 * Test suite for the TmfSimpleTimestampTest class.
25 */
26 @SuppressWarnings("nls")
27 public class TmfSimpleTimestampTest extends TestCase {
28
29 // ------------------------------------------------------------------------
30 // Variables
31 // ------------------------------------------------------------------------
32
33 private final ITmfTimestamp ts0 = new TmfSimpleTimestamp();
34 private final ITmfTimestamp ts1 = new TmfSimpleTimestamp(12345);
35 private final ITmfTimestamp ts2 = new TmfSimpleTimestamp(-1234);
36
37 // ------------------------------------------------------------------------
38 // Housekeping
39 // ------------------------------------------------------------------------
40
41 /**
42 * @param name the test name
43 */
44 public TmfSimpleTimestampTest(String name) {
45 super(name);
46 }
47
48 @Override
49 protected void setUp() throws Exception {
50 super.setUp();
51 }
52
53 @Override
54 protected void tearDown() throws Exception {
55 super.tearDown();
56 }
57
58 // ------------------------------------------------------------------------
59 // Constructors
60 // ------------------------------------------------------------------------
61
62 public void testDefaultConstructor() throws Exception {
63 assertEquals("getValue", 0, ts0.getValue());
64 assertEquals("getscale", 0, ts0.getScale());
65 assertEquals("getPrecision", 0, ts0.getPrecision());
66 }
67
68 public void testFullConstructor() throws Exception {
69 assertEquals("getValue", 12345, ts1.getValue());
70 assertEquals("getscale", 0, ts1.getScale());
71 assertEquals("getPrecision", 0, ts1.getPrecision());
72 }
73
74 public void testCopyConstructor() throws Exception {
75 ITmfTimestamp copy = new TmfSimpleTimestamp(ts1);
76
77 assertEquals("getValue", ts1.getValue(), copy.getValue());
78 assertEquals("getscale", ts1.getScale(), copy.getScale());
79 assertEquals("getPrecision", ts1.getPrecision(), copy.getPrecision());
80
81 assertEquals("getValue", 12345, copy.getValue());
82 assertEquals("getscale", 0, copy.getScale());
83 assertEquals("getPrecision", 0, copy.getPrecision());
84 }
85
86 public void testCopyBadTimestamp() throws Exception {
87 ITmfTimestamp ts0a = new TmfTimestamp(0, 1, 0);
88 ITmfTimestamp ts0b = new TmfTimestamp(0, 0, 1);
89
90 try {
91 new TmfSimpleTimestamp(null);
92 fail("TmfSimpleTimestamp: null argument");
93 } catch (IllegalArgumentException e) {
94 }
95
96 try {
97 new TmfSimpleTimestamp(ts0a);
98 fail("TmfSimpleTimestamp: bad scale");
99 } catch (IllegalArgumentException e) {
100 }
101
102 try {
103 new TmfSimpleTimestamp(ts0b);
104 fail("TmfSimpleTimestamp: bad precision");
105 } catch (IllegalArgumentException e) {
106 }
107 }
108
109 // ------------------------------------------------------------------------
110 // clone
111 // ------------------------------------------------------------------------
112
113 public static class MyTimestamp extends TmfSimpleTimestamp {
114
115 @Override
116 public boolean equals(Object other) {
117 return super.equals(other);
118 }
119
120 @Override
121 public MyTimestamp clone() {
122 return (MyTimestamp) super.clone();
123 }
124 }
125
126 public void testClone() throws Exception {
127 ITmfTimestamp timestamp = ts0.clone();
128 assertEquals("clone", timestamp, ts0);
129 }
130
131 public void testClone2() throws Exception {
132 MyTimestamp timestamp = new MyTimestamp();
133 MyTimestamp clone = timestamp.clone();
134 assertEquals("clone", clone, timestamp);
135 }
136
137 // ------------------------------------------------------------------------
138 // equals
139 // ------------------------------------------------------------------------
140
141 public void testEqualsReflexivity() throws Exception {
142 assertTrue("equals", ts0.equals(ts0));
143 assertTrue("equals", ts1.equals(ts1));
144 assertTrue("equals", ts2.equals(ts2));
145
146 assertTrue("equals", !ts0.equals(ts1));
147 assertTrue("equals", !ts0.equals(ts2));
148
149 assertTrue("equals", !ts1.equals(ts0));
150 assertTrue("equals", !ts1.equals(ts2));
151
152 assertTrue("equals", !ts2.equals(ts0));
153 assertTrue("equals", !ts2.equals(ts1));
154 }
155
156 public void testEqualsSymmetry() throws Exception {
157 ITmfTimestamp ts0copy = new TmfSimpleTimestamp(ts0);
158 assertTrue("equals", ts0.equals(ts0copy));
159 assertTrue("equals", ts0copy.equals(ts0));
160
161 ITmfTimestamp ts1copy = new TmfSimpleTimestamp(ts1);
162 assertTrue("equals", ts1.equals(ts1copy));
163 assertTrue("equals", ts1copy.equals(ts1));
164 }
165
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));
172
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));
178 }
179
180 public void testEqualsNull() throws Exception {
181 assertTrue("equals", !ts0.equals(null));
182 assertTrue("equals", !ts1.equals(null));
183 assertTrue("equals", !ts2.equals(null));
184 }
185
186 public void testEqualsNonTimestamp() throws Exception {
187 assertFalse("equals", ts0.equals(ts0.toString()));
188 }
189
190 // ------------------------------------------------------------------------
191 // toString
192 // ------------------------------------------------------------------------
193
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());
198 }
199
200 // ------------------------------------------------------------------------
201 // hashCode
202 // ------------------------------------------------------------------------
203
204 public void testHashCode() throws Exception {
205 ITmfTimestamp ts0copy = new TmfTimestamp(ts0);
206 ITmfTimestamp ts1copy = new TmfTimestamp(ts1);
207 ITmfTimestamp ts2copy = new TmfTimestamp(ts2);
208
209 assertTrue("hashCode", ts0.hashCode() == ts0copy.hashCode());
210 assertTrue("hashCode", ts1.hashCode() == ts1copy.hashCode());
211 assertTrue("hashCode", ts2.hashCode() == ts2copy.hashCode());
212
213 assertTrue("hashCode", ts0.hashCode() != ts1.hashCode());
214 }
215
216 // ------------------------------------------------------------------------
217 // normalize
218 // ------------------------------------------------------------------------
219
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());
225
226 ts = ts0.normalize(12345, 0);
227 assertEquals("getValue", 12345, ts.getValue());
228 assertEquals("getscale", 0, ts.getScale());
229 assertEquals("getPrecision", 0, ts.getPrecision());
230
231 ts = ts0.normalize(10, 0);
232 assertEquals("getValue", 10, ts.getValue());
233 assertEquals("getscale", 0, ts.getScale());
234 assertEquals("getPrecision", 0, ts.getPrecision());
235
236 ts = ts0.normalize(-10, 0);
237 assertEquals("getValue", -10, ts.getValue());
238 assertEquals("getscale", 0, ts.getScale());
239 assertEquals("getPrecision", 0, ts.getPrecision());
240 }
241
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());
247
248 ts = ts0.normalize(12345, 1);
249 assertEquals("getValue", 12345, ts.getValue());
250 assertEquals("getscale", 1, ts.getScale());
251 assertEquals("getPrecision", 0, ts.getPrecision());
252
253 ts = ts0.normalize(10, 1);
254 assertEquals("getValue", 10, ts.getValue());
255 assertEquals("getscale", 1, ts.getScale());
256 assertEquals("getPrecision", 0, ts.getPrecision());
257
258 ts = ts0.normalize(-10, 1);
259 assertEquals("getValue", -10, ts.getValue());
260 assertEquals("getscale", 1, ts.getScale());
261 assertEquals("getPrecision", 0, ts.getPrecision());
262 }
263
264 // ------------------------------------------------------------------------
265 // compareTo
266 // ------------------------------------------------------------------------
267
268 public void testBasicCompareTo() throws Exception {
269 ITmfTimestamp ts1 = new TmfSimpleTimestamp(900);
270 ITmfTimestamp ts2 = new TmfSimpleTimestamp(1000);
271 ITmfTimestamp ts3 = new TmfSimpleTimestamp(1100);
272
273 assertTrue(ts1.compareTo(ts1) == 0);
274
275 assertTrue("CompareTo", ts1.compareTo(ts2) < 0);
276 assertTrue("CompareTo", ts1.compareTo(ts3) < 0);
277
278 assertTrue("CompareTo", ts2.compareTo(ts1) > 0);
279 assertTrue("CompareTo", ts2.compareTo(ts3) < 0);
280
281 assertTrue("CompareTo", ts3.compareTo(ts1) > 0);
282 assertTrue("CompareTo", ts3.compareTo(ts2) > 0);
283 }
284
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);
289
290 assertTrue(ts1.compareTo(ts1) == 0);
291
292 assertTrue("CompareTo", ts0.compareTo(ts0a) == 0);
293 assertTrue("CompareTo", ts1.compareTo(ts1a) == 0);
294 assertTrue("CompareTo", ts2.compareTo(ts2a) == 0);
295 }
296
297 // ------------------------------------------------------------------------
298 // getDelta
299 // ------------------------------------------------------------------------
300
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);
306
307 ITmfTimestamp delta = ts0.getDelta(ts1);
308 assertEquals("getDelta", 0, delta.compareTo(exp, false));
309
310 // Delta for same scale and precision (delta < 0)
311 ts0 = new TmfTimestamp(5);
312 ts1 = new TmfTimestamp(10);
313 exp = new TmfTimestamp(-5);
314
315 delta = ts0.getDelta(ts1);
316 assertEquals("getDelta", 0, delta.compareTo(exp, false));
317 }
318
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);
324
325 ITmfTimestamp delta = ts0.getDelta(ts1);
326 assertEquals("getDelta", 0, delta.compareTo(exp, false));
327 }
328
329 }
This page took 0.038994 seconds and 5 git commands to generate.