Commit | Line | Data |
---|---|---|
7656d70a FC |
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 | @SuppressWarnings("unused") | |
92 | ITmfTimestamp timestamp = new TmfSimpleTimestamp(null); | |
75d42a16 | 93 | fail("TmfSimpleTimestamp: null argument"); |
7656d70a FC |
94 | } catch (IllegalArgumentException e) { |
95 | } | |
96 | ||
97 | try { | |
98 | @SuppressWarnings("unused") | |
99 | ITmfTimestamp ts = new TmfSimpleTimestamp(ts0a); | |
75d42a16 | 100 | fail("TmfSimpleTimestamp: bad scale"); |
7656d70a FC |
101 | } catch (IllegalArgumentException e) { |
102 | } | |
103 | ||
104 | try { | |
105 | @SuppressWarnings("unused") | |
106 | ITmfTimestamp ts = new TmfSimpleTimestamp(ts0b); | |
75d42a16 | 107 | fail("TmfSimpleTimestamp: bad precision"); |
7656d70a FC |
108 | } catch (IllegalArgumentException e) { |
109 | } | |
110 | } | |
111 | ||
9ee9135e FC |
112 | // ------------------------------------------------------------------------ |
113 | // clone | |
114 | // ------------------------------------------------------------------------ | |
115 | ||
116 | public class MyTimestamp extends TmfSimpleTimestamp { | |
117 | ||
118 | @Override | |
119 | public boolean equals(Object other) { | |
120 | return super.equals(other); | |
121 | } | |
122 | ||
123 | @Override | |
124 | public MyTimestamp clone() { | |
125 | return (MyTimestamp) super.clone(); | |
126 | } | |
127 | } | |
128 | ||
129 | public void testClone() throws Exception { | |
130 | ITmfTimestamp timestamp = ts0.clone(); | |
131 | assertEquals("clone", timestamp, ts0); | |
132 | } | |
133 | ||
134 | public void testClone2() throws Exception { | |
135 | MyTimestamp timestamp = new MyTimestamp(); | |
136 | MyTimestamp clone = timestamp.clone(); | |
137 | assertEquals("clone", clone, timestamp); | |
138 | } | |
139 | ||
7656d70a FC |
140 | // ------------------------------------------------------------------------ |
141 | // equals | |
142 | // ------------------------------------------------------------------------ | |
143 | ||
144 | public void testEqualsReflexivity() throws Exception { | |
145 | assertTrue("equals", ts0.equals(ts0)); | |
146 | assertTrue("equals", ts1.equals(ts1)); | |
147 | assertTrue("equals", ts2.equals(ts2)); | |
148 | ||
149 | assertTrue("equals", !ts0.equals(ts1)); | |
150 | assertTrue("equals", !ts0.equals(ts2)); | |
151 | ||
152 | assertTrue("equals", !ts1.equals(ts0)); | |
153 | assertTrue("equals", !ts1.equals(ts2)); | |
154 | ||
155 | assertTrue("equals", !ts2.equals(ts0)); | |
156 | assertTrue("equals", !ts2.equals(ts1)); | |
157 | } | |
158 | ||
159 | public void testEqualsSymmetry() throws Exception { | |
160 | ITmfTimestamp ts0copy = new TmfSimpleTimestamp(ts0); | |
161 | assertTrue("equals", ts0.equals(ts0copy)); | |
162 | assertTrue("equals", ts0copy.equals(ts0)); | |
163 | ||
164 | ITmfTimestamp ts1copy = new TmfSimpleTimestamp(ts1); | |
165 | assertTrue("equals", ts1.equals(ts1copy)); | |
166 | assertTrue("equals", ts1copy.equals(ts1)); | |
167 | } | |
168 | ||
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)); | |
175 | ||
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)); | |
181 | } | |
182 | ||
183 | public void testEqualsNull() throws Exception { | |
184 | assertTrue("equals", !ts0.equals(null)); | |
185 | assertTrue("equals", !ts1.equals(null)); | |
186 | assertTrue("equals", !ts2.equals(null)); | |
187 | } | |
188 | ||
189 | public void testEqualsNonTimestamp() throws Exception { | |
190 | assertFalse("equals", ts0.equals(ts0.toString())); | |
191 | } | |
192 | ||
7656d70a FC |
193 | // ------------------------------------------------------------------------ |
194 | // toString | |
195 | // ------------------------------------------------------------------------ | |
196 | ||
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()); | |
201 | } | |
202 | ||
203 | // ------------------------------------------------------------------------ | |
204 | // hashCode | |
205 | // ------------------------------------------------------------------------ | |
206 | ||
207 | public void testHashCode() throws Exception { | |
208 | ITmfTimestamp ts0copy = new TmfTimestamp(ts0); | |
209 | ITmfTimestamp ts1copy = new TmfTimestamp(ts1); | |
210 | ITmfTimestamp ts2copy = new TmfTimestamp(ts2); | |
211 | ||
212 | assertTrue("hashCode", ts0.hashCode() == ts0copy.hashCode()); | |
213 | assertTrue("hashCode", ts1.hashCode() == ts1copy.hashCode()); | |
214 | assertTrue("hashCode", ts2.hashCode() == ts2copy.hashCode()); | |
215 | ||
216 | assertTrue("hashCode", ts0.hashCode() != ts1.hashCode()); | |
217 | } | |
218 | ||
219 | // ------------------------------------------------------------------------ | |
220 | // normalize | |
221 | // ------------------------------------------------------------------------ | |
222 | ||
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()); | |
228 | ||
229 | ts = ts0.normalize(12345, 0); | |
230 | assertEquals("getValue", 12345, ts.getValue()); | |
231 | assertEquals("getscale", 0, ts.getScale()); | |
232 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
233 | ||
234 | ts = ts0.normalize(10, 0); | |
235 | assertEquals("getValue", 10, ts.getValue()); | |
236 | assertEquals("getscale", 0, ts.getScale()); | |
237 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
238 | ||
239 | ts = ts0.normalize(-10, 0); | |
240 | assertEquals("getValue", -10, ts.getValue()); | |
241 | assertEquals("getscale", 0, ts.getScale()); | |
242 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
243 | } | |
244 | ||
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()); | |
250 | ||
251 | ts = ts0.normalize(12345, 1); | |
252 | assertEquals("getValue", 12345, ts.getValue()); | |
253 | assertEquals("getscale", 1, ts.getScale()); | |
254 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
255 | ||
256 | ts = ts0.normalize(10, 1); | |
257 | assertEquals("getValue", 10, ts.getValue()); | |
258 | assertEquals("getscale", 1, ts.getScale()); | |
259 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
260 | ||
261 | ts = ts0.normalize(-10, 1); | |
262 | assertEquals("getValue", -10, ts.getValue()); | |
263 | assertEquals("getscale", 1, ts.getScale()); | |
264 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
265 | } | |
266 | ||
267 | // ------------------------------------------------------------------------ | |
268 | // compareTo | |
269 | // ------------------------------------------------------------------------ | |
270 | ||
271 | public void testBasicCompareTo() throws Exception { | |
272 | ITmfTimestamp ts1 = new TmfSimpleTimestamp(900); | |
273 | ITmfTimestamp ts2 = new TmfSimpleTimestamp(1000); | |
274 | ITmfTimestamp ts3 = new TmfSimpleTimestamp(1100); | |
275 | ||
276 | assertTrue(ts1.compareTo(ts1) == 0); | |
277 | ||
278 | assertTrue("CompareTo", ts1.compareTo(ts2) < 0); | |
279 | assertTrue("CompareTo", ts1.compareTo(ts3) < 0); | |
280 | ||
281 | assertTrue("CompareTo", ts2.compareTo(ts1) > 0); | |
282 | assertTrue("CompareTo", ts2.compareTo(ts3) < 0); | |
283 | ||
284 | assertTrue("CompareTo", ts3.compareTo(ts1) > 0); | |
285 | assertTrue("CompareTo", ts3.compareTo(ts2) > 0); | |
286 | } | |
287 | ||
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); | |
292 | ||
293 | assertTrue(ts1.compareTo(ts1) == 0); | |
294 | ||
295 | assertTrue("CompareTo", ts0.compareTo(ts0a) == 0); | |
296 | assertTrue("CompareTo", ts1.compareTo(ts1a) == 0); | |
297 | assertTrue("CompareTo", ts2.compareTo(ts2a) == 0); | |
298 | } | |
299 | ||
300 | // ------------------------------------------------------------------------ | |
301 | // getDelta | |
302 | // ------------------------------------------------------------------------ | |
303 | ||
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); | |
309 | ||
310 | ITmfTimestamp delta = ts0.getDelta(ts1); | |
311 | assertEquals("getDelta", 0, delta.compareTo(exp, false)); | |
312 | ||
313 | // Delta for same scale and precision (delta < 0) | |
314 | ts0 = new TmfTimestamp(5); | |
315 | ts1 = new TmfTimestamp(10); | |
316 | exp = new TmfTimestamp(-5); | |
317 | ||
318 | delta = ts0.getDelta(ts1); | |
319 | assertEquals("getDelta", 0, delta.compareTo(exp, false)); | |
320 | } | |
321 | ||
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); | |
327 | ||
328 | ITmfTimestamp delta = ts0.getDelta(ts1); | |
329 | assertEquals("getDelta", 0, delta.compareTo(exp, false)); | |
330 | } | |
331 | ||
332 | } |