Commit | Line | Data |
---|---|---|
16035098 PT |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 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 | * Patrick Tasse - Modified from TmfSimpleTimestamp to use nanosecond scale | |
12 | *******************************************************************************/ | |
13 | ||
14 | package org.eclipse.linuxtools.tmf.core.tests.event; | |
15 | ||
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; | |
20 | ||
21 | import java.text.DateFormat; | |
22 | import java.text.SimpleDateFormat; | |
23 | import java.util.Date; | |
24 | ||
25 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; | |
26 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfNanoTimestamp; | |
27 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; | |
28 | import org.junit.Test; | |
29 | ||
30 | /** | |
31 | * Test suite for the TmfNanoTimestampTest class. | |
32 | */ | |
33 | @SuppressWarnings("javadoc") | |
34 | public class TmfNanoTimestampTest { | |
35 | ||
36 | // ------------------------------------------------------------------------ | |
37 | // Variables | |
38 | // ------------------------------------------------------------------------ | |
39 | ||
40 | private final ITmfTimestamp ts0 = new TmfNanoTimestamp(); | |
41 | private final ITmfTimestamp ts1 = new TmfNanoTimestamp(12345); | |
42 | private final ITmfTimestamp ts2 = new TmfNanoTimestamp(-1234); | |
43 | ||
44 | // ------------------------------------------------------------------------ | |
45 | // Constructors | |
46 | // ------------------------------------------------------------------------ | |
47 | ||
48 | @Test | |
49 | public void testDefaultConstructor() { | |
50 | assertEquals("getValue", 0, ts0.getValue()); | |
51 | assertEquals("getscale", -9, ts0.getScale()); | |
52 | assertEquals("getPrecision", 0, ts0.getPrecision()); | |
53 | } | |
54 | ||
55 | @Test | |
56 | public void testFullConstructor() { | |
57 | assertEquals("getValue", 12345, ts1.getValue()); | |
58 | assertEquals("getscale", -9, ts1.getScale()); | |
59 | assertEquals("getPrecision", 0, ts1.getPrecision()); | |
60 | } | |
61 | ||
62 | @Test | |
63 | public void testCopyConstructor() { | |
64 | final ITmfTimestamp copy = new TmfNanoTimestamp(ts1); | |
65 | ||
66 | assertEquals("getValue", ts1.getValue(), copy.getValue()); | |
67 | assertEquals("getscale", ts1.getScale(), copy.getScale()); | |
68 | assertEquals("getPrecision", ts1.getPrecision(), copy.getPrecision()); | |
69 | ||
70 | assertEquals("getValue", 12345, copy.getValue()); | |
71 | assertEquals("getscale", -9, copy.getScale()); | |
72 | assertEquals("getPrecision", 0, copy.getPrecision()); | |
73 | } | |
74 | ||
75 | @Test | |
76 | public void testCopyBadTimestamp() { | |
77 | try { | |
78 | new TmfNanoTimestamp(null); | |
79 | fail("TmfNanoTimestamp: null argument"); | |
80 | } catch (final NullPointerException e) { | |
81 | } | |
82 | } | |
83 | ||
84 | // ------------------------------------------------------------------------ | |
85 | // equals | |
86 | // ------------------------------------------------------------------------ | |
87 | ||
88 | @Test | |
89 | public void testEqualsReflexivity() { | |
90 | assertTrue("equals", ts0.equals(ts0)); | |
91 | assertTrue("equals", ts1.equals(ts1)); | |
92 | assertTrue("equals", ts2.equals(ts2)); | |
93 | ||
94 | assertTrue("equals", !ts0.equals(ts1)); | |
95 | assertTrue("equals", !ts0.equals(ts2)); | |
96 | ||
97 | assertTrue("equals", !ts1.equals(ts0)); | |
98 | assertTrue("equals", !ts1.equals(ts2)); | |
99 | ||
100 | assertTrue("equals", !ts2.equals(ts0)); | |
101 | assertTrue("equals", !ts2.equals(ts1)); | |
102 | } | |
103 | ||
104 | @Test | |
105 | public void testEqualsSymmetry() { | |
106 | final ITmfTimestamp ts0copy = new TmfNanoTimestamp(ts0); | |
107 | assertTrue("equals", ts0.equals(ts0copy)); | |
108 | assertTrue("equals", ts0copy.equals(ts0)); | |
109 | ||
110 | final ITmfTimestamp ts1copy = new TmfNanoTimestamp(ts1); | |
111 | assertTrue("equals", ts1.equals(ts1copy)); | |
112 | assertTrue("equals", ts1copy.equals(ts1)); | |
113 | } | |
114 | ||
115 | @Test | |
116 | public void testEqualsTransivity() { | |
117 | final ITmfTimestamp ts0copy1 = new TmfNanoTimestamp(ts0); | |
118 | final ITmfTimestamp ts0copy2 = new TmfNanoTimestamp(ts0copy1); | |
119 | assertTrue("equals", ts0.equals(ts0copy1)); | |
120 | assertTrue("equals", ts0copy1.equals(ts0copy2)); | |
121 | assertTrue("equals", ts0.equals(ts0copy2)); | |
122 | ||
123 | final ITmfTimestamp ts1copy1 = new TmfNanoTimestamp(ts1); | |
124 | final ITmfTimestamp ts1copy2 = new TmfNanoTimestamp(ts1copy1); | |
125 | assertTrue("equals", ts1.equals(ts1copy1)); | |
126 | assertTrue("equals", ts1copy1.equals(ts1copy2)); | |
127 | assertTrue("equals", ts1.equals(ts1copy2)); | |
128 | } | |
129 | ||
130 | @Test | |
131 | public void testEqualsNull() { | |
132 | assertTrue("equals", !ts0.equals(null)); | |
133 | assertTrue("equals", !ts1.equals(null)); | |
134 | assertTrue("equals", !ts2.equals(null)); | |
135 | } | |
136 | ||
137 | @Test | |
138 | public void testEqualsNonTimestamp() { | |
139 | assertFalse("equals", ts0.equals(ts0.toString())); | |
140 | } | |
141 | ||
142 | // ------------------------------------------------------------------------ | |
143 | // toString | |
144 | // ------------------------------------------------------------------------ | |
145 | ||
146 | @Test | |
147 | public void testToString() { | |
148 | DateFormat df = new SimpleDateFormat("HH:mm:ss.SSS"); | |
149 | Date d0 = new Date(ts0.getValue() / 1000000); | |
150 | Date d1 = new Date(ts1.getValue() / 1000000); | |
151 | Date d2 = new Date(ts2.getValue() / 1000000 - 1); | |
152 | assertEquals("toString", df.format(d0) + " 000 000", ts0.toString()); | |
153 | assertEquals("toString", df.format(d1) + " 012 345", ts1.toString()); | |
154 | assertEquals("toString", df.format(d2) + " 998 766", ts2.toString()); | |
155 | } | |
156 | ||
157 | // ------------------------------------------------------------------------ | |
158 | // hashCode | |
159 | // ------------------------------------------------------------------------ | |
160 | ||
161 | @Test | |
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); | |
166 | ||
167 | assertTrue("hashCode", ts0.hashCode() == ts0copy.hashCode()); | |
168 | assertTrue("hashCode", ts1.hashCode() == ts1copy.hashCode()); | |
169 | assertTrue("hashCode", ts2.hashCode() == ts2copy.hashCode()); | |
170 | ||
171 | assertTrue("hashCode", ts0.hashCode() != ts1.hashCode()); | |
172 | } | |
173 | ||
174 | // ------------------------------------------------------------------------ | |
175 | // normalize | |
176 | // ------------------------------------------------------------------------ | |
177 | ||
178 | @Test | |
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()); | |
184 | ||
185 | ts = ts0.normalize(12345, 0); | |
186 | assertEquals("getValue", 12345, ts.getValue()); | |
187 | assertEquals("getscale", 0, ts.getScale()); | |
188 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
189 | ||
190 | ts = ts0.normalize(10, 0); | |
191 | assertEquals("getValue", 10, ts.getValue()); | |
192 | assertEquals("getscale", 0, ts.getScale()); | |
193 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
194 | ||
195 | ts = ts0.normalize(-10, 0); | |
196 | assertEquals("getValue", -10, ts.getValue()); | |
197 | assertEquals("getscale", 0, ts.getScale()); | |
198 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
199 | } | |
200 | ||
201 | @Test | |
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()); | |
207 | ||
208 | ts = ts0.normalize(12345, 1); | |
209 | assertEquals("getValue", 12345, ts.getValue()); | |
210 | assertEquals("getscale", 1, ts.getScale()); | |
211 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
212 | ||
213 | ts = ts0.normalize(10, 1); | |
214 | assertEquals("getValue", 10, ts.getValue()); | |
215 | assertEquals("getscale", 1, ts.getScale()); | |
216 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
217 | ||
218 | ts = ts0.normalize(-10, 1); | |
219 | assertEquals("getValue", -10, ts.getValue()); | |
220 | assertEquals("getscale", 1, ts.getScale()); | |
221 | assertEquals("getPrecision", 0, ts.getPrecision()); | |
222 | } | |
223 | ||
224 | // ------------------------------------------------------------------------ | |
225 | // compareTo | |
226 | // ------------------------------------------------------------------------ | |
227 | ||
228 | @Test | |
229 | public void testBasicCompareTo() { | |
230 | final ITmfTimestamp tstamp1 = new TmfNanoTimestamp(900); | |
231 | final ITmfTimestamp tstamp2 = new TmfNanoTimestamp(1000); | |
232 | final ITmfTimestamp tstamp3 = new TmfNanoTimestamp(1100); | |
233 | ||
234 | assertTrue(tstamp1.compareTo(tstamp1) == 0); | |
235 | ||
236 | assertTrue("CompareTo", tstamp1.compareTo(tstamp2) < 0); | |
237 | assertTrue("CompareTo", tstamp1.compareTo(tstamp3) < 0); | |
238 | ||
239 | assertTrue("CompareTo", tstamp2.compareTo(tstamp1) > 0); | |
240 | assertTrue("CompareTo", tstamp2.compareTo(tstamp3) < 0); | |
241 | ||
242 | assertTrue("CompareTo", tstamp3.compareTo(tstamp1) > 0); | |
243 | assertTrue("CompareTo", tstamp3.compareTo(tstamp2) > 0); | |
244 | } | |
245 | ||
246 | @Test | |
247 | public void testCompareTo() { | |
248 | final ITmfTimestamp ts0a = new TmfTimestamp(0, 2, 0); | |
249 | final ITmfTimestamp ts1a = new TmfTimestamp(123450, -10); | |
250 | final ITmfTimestamp ts2a = new TmfTimestamp(-12340, -10); | |
251 | ||
252 | assertTrue(ts1.compareTo(ts1) == 0); | |
253 | ||
254 | assertTrue("CompareTo", ts0.compareTo(ts0a) == 0); | |
255 | assertTrue("CompareTo", ts1.compareTo(ts1a) == 0); | |
256 | assertTrue("CompareTo", ts2.compareTo(ts2a) == 0); | |
257 | } | |
258 | ||
259 | // ------------------------------------------------------------------------ | |
260 | // getDelta | |
261 | // ------------------------------------------------------------------------ | |
262 | ||
263 | @Test | |
264 | public void testDelta() { | |
265 | // Delta for same scale and precision (delta > 0) | |
266 | TmfTimestamp tstamp0 = new TmfNanoTimestamp(10); | |
267 | TmfTimestamp tstamp1 = new TmfNanoTimestamp(5); | |
268 | TmfTimestamp expectd = new TmfNanoTimestamp(5); | |
269 | ||
270 | ITmfTimestamp delta = tstamp0.getDelta(tstamp1); | |
271 | assertEquals("getDelta", 0, delta.compareTo(expectd, false)); | |
272 | ||
273 | // Delta for same scale and precision (delta < 0) | |
274 | tstamp0 = new TmfTimestamp(5); | |
275 | tstamp1 = new TmfTimestamp(10); | |
276 | expectd = new TmfTimestamp(-5); | |
277 | ||
278 | delta = tstamp0.getDelta(tstamp1); | |
279 | assertEquals("getDelta", 0, delta.compareTo(expectd, false)); | |
280 | } | |
281 | ||
282 | @Test | |
283 | public void testDelta2() { | |
284 | // Delta for different scale and same precision (delta > 0) | |
285 | final TmfTimestamp tstamp0 = new TmfNanoTimestamp(10); | |
286 | final TmfTimestamp tstamp1 = new TmfTimestamp(1, -8); | |
287 | final TmfTimestamp expectd = new TmfTimestamp(0, 0); | |
288 | ||
289 | final ITmfTimestamp delta = tstamp0.getDelta(tstamp1); | |
290 | assertEquals("getDelta", 0, delta.compareTo(expectd, false)); | |
291 | } | |
292 | ||
293 | } |