Update for Bug287562 (Event Model code refresh + JUnits)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.tests / src / org / eclipse / linuxtools / tmf / event / TmfTimestampTest.java
1 /*******************************************************************************
2 * Copyright (c) 2009 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.event;
14
15 import static org.junit.Assert.*;
16
17 import org.junit.Test;
18
19 /**
20 * <b><u>TmfTimestampTest</u></b>
21 * <p>
22 * JUnit test suite for the TmfTimestamp class.
23 */
24 public class TmfTimestampTest {
25
26 // ========================================================================
27 // Constructors
28 // ========================================================================
29
30 @Test
31 public void testDefaultConstructor() throws Exception {
32 TmfTimestamp ts = new TmfTimestamp();
33 assertEquals("getValue", 0, ts.getValue());
34 assertEquals("getscale", 0, ts.getScale());
35 assertEquals("getPrecision", 0, ts.getPrecision());
36 }
37
38 @Test
39 public void testSimpleConstructor() throws Exception {
40 TmfTimestamp ts = new TmfTimestamp(12345);
41 assertEquals("getValue", 12345, ts.getValue());
42 assertEquals("getscale", 0, ts.getScale());
43 assertEquals("getPrecision", 0, ts.getPrecision());
44 }
45
46 @Test
47 public void testSimpleConstructor2() throws Exception {
48 TmfTimestamp ts = new TmfTimestamp(12345, (byte) -1);
49 assertEquals("getValue", 12345, ts.getValue());
50 assertEquals("getscale", -1, ts.getScale());
51 assertEquals("getPrecision", 0, ts.getPrecision());
52 }
53
54 @Test
55 public void testFullConstructor() throws Exception {
56 TmfTimestamp ts = new TmfTimestamp(12345, (byte) 2, 5);
57 assertEquals("getValue", 12345, ts.getValue());
58 assertEquals("getscale", 2, ts.getScale());
59 assertEquals("getPrecision", 5, ts.getPrecision());
60 }
61
62 @Test
63 public void testCopyConstructor() throws Exception {
64 TmfTimestamp ts0 = new TmfTimestamp(12345, (byte) 2, 5);
65 TmfTimestamp ts = new TmfTimestamp(ts0);
66 assertEquals("getValue", 12345, ts.getValue());
67 assertEquals("getscale", 2, ts.getScale());
68 assertEquals("getPrecision", 5, ts.getPrecision());
69 }
70
71 // ========================================================================
72 // BigBang, BigCrunch
73 // ========================================================================
74
75 @Test
76 public void testCopyConstructorBigBang() throws Exception {
77 TmfTimestamp ts = new TmfTimestamp(TmfTimestamp.BigBang);
78 assertEquals("getValue", 0, ts.getValue());
79 assertEquals("getscale", 0, ts.getScale());
80 assertEquals("getPrecision", 0, ts.getPrecision());
81 }
82
83 @Test
84 public void testCopyConstructorBigCrunch() throws Exception {
85 TmfTimestamp ts = new TmfTimestamp(TmfTimestamp.BigCrunch);
86 assertEquals("getValue", Long.MAX_VALUE, ts.getValue());
87 assertEquals("getscale", Byte.MAX_VALUE, ts.getScale());
88 assertEquals("getPrecision", 0, ts.getPrecision());
89 }
90
91 // ========================================================================
92 // toString
93 // ========================================================================
94
95 @Test
96 public void testToString() throws Exception {
97 TmfTimestamp ts1 = new TmfTimestamp();
98 TmfTimestamp ts2 = new TmfTimestamp(1234, (byte) 2);
99 TmfTimestamp ts3 = new TmfTimestamp(1234, (byte) 2, 1);
100 TmfTimestamp ts4 = new TmfTimestamp(-1234, (byte) -1, 4);
101
102 assertEquals("toString", "[TmfTimestamp:0,0,0]", ts1.toString());
103 assertEquals("toString", "[TmfTimestamp:1234,2,0]", ts2.toString());
104 assertEquals("toString", "[TmfTimestamp:1234,2,1]", ts3.toString());
105 assertEquals("toString", "[TmfTimestamp:-1234,-1,4]", ts4.toString());
106 }
107
108 // ========================================================================
109 // synchronize
110 // ========================================================================
111
112 @Test
113 public void testSynchronizeOffset() throws Exception {
114 TmfTimestamp ts0 = new TmfTimestamp(1234, (byte) 0, 1);
115 TmfTimestamp ts = new TmfTimestamp();
116
117 ts = ts0.synchronize(0, (byte) 0);
118 assertEquals("getValue", 1234, ts.getValue());
119 assertEquals("getscale", 0, ts.getScale());
120 assertEquals("getPrecision", 1, ts.getPrecision());
121
122 ts = ts0.synchronize(10, (byte) 0);
123 assertEquals("getValue", 1244, ts.getValue());
124 assertEquals("getscale", 0, ts.getScale());
125 assertEquals("getPrecision", 1, ts.getPrecision());
126
127 ts = ts0.synchronize(-10, (byte) 0);
128 assertEquals("getValue", 1224, ts.getValue());
129 assertEquals("getscale", 0, ts.getScale());
130 assertEquals("getPrecision", 1, ts.getPrecision());
131 }
132
133 @Test
134 public void testSynchronizeScale() throws Exception {
135 TmfTimestamp ts0 = new TmfTimestamp(1234, (byte) 0, 12);
136 TmfTimestamp ts = new TmfTimestamp();
137
138 ts = ts0.synchronize(0, (byte) 0);
139 assertEquals("getValue", 1234, ts.getValue());
140 assertEquals("getscale", 0, ts.getScale());
141 assertEquals("getPrecision", 12, ts.getPrecision());
142
143 ts = ts0.synchronize(0, (byte) 1);
144 assertEquals("getValue", 123, ts.getValue());
145 assertEquals("getscale", 1, ts.getScale());
146 assertEquals("getPrecision", 1, ts.getPrecision());
147
148 ts = ts0.synchronize(0, (byte) 2);
149 assertEquals("getValue", 12, ts.getValue());
150 assertEquals("getscale", 2, ts.getScale());
151 assertEquals("getPrecision", 0, ts.getPrecision());
152
153 ts = ts0.synchronize(0, (byte) 4);
154 assertEquals("getValue", 0, ts.getValue());
155 assertEquals("getscale", 4, ts.getScale());
156 assertEquals("getPrecision", 0, ts.getPrecision());
157
158 ts = ts0.synchronize(0, (byte) -2);
159 assertEquals("getValue", 123400, ts.getValue());
160 assertEquals("getscale", -2, ts.getScale());
161 assertEquals("getPrecision", 1200, ts.getPrecision());
162 }
163
164 @Test
165 public void testSynchronizeOffsetAndScale() throws Exception {
166 TmfTimestamp ts0 = new TmfTimestamp(1234, (byte) 0, 12);
167 TmfTimestamp ts = new TmfTimestamp();
168
169 ts = ts0.synchronize(10, (byte) 1);
170 assertEquals("getValue", 133, ts.getValue());
171 assertEquals("getscale", 1, ts.getScale());
172 assertEquals("getPrecision", 1, ts.getPrecision());
173
174 ts = ts0.synchronize(-10, (byte) -1);
175 assertEquals("getValue", 12330, ts.getValue());
176 assertEquals("getscale", -1, ts.getScale());
177 assertEquals("getPrecision", 120, ts.getPrecision());
178 }
179
180 @Test
181 public void testSynchronizeWithArithmeticException() throws Exception {
182 TmfTimestamp bigBang = new TmfTimestamp(TmfTimestamp.BigBang);
183 TmfTimestamp bigCrunch = new TmfTimestamp(TmfTimestamp.BigCrunch);
184
185 try {
186 bigCrunch.synchronize(bigBang.getValue(), bigBang.getScale());
187 fail("Exception not thrown");
188 } catch (ArithmeticException e) {
189 }
190 }
191
192 // ========================================================================
193 // getAdjustment
194 // ========================================================================
195
196 @Test
197 public void testGetAdjustmentSameScale() throws Exception {
198 TmfTimestamp ts0 = new TmfTimestamp(12345, (byte) -2);
199 TmfTimestamp ts = new TmfTimestamp(10000, (byte) -2);
200
201 long delta = ts.getAdjustment(ts0);
202 assertEquals("delta", 2345, delta);
203 }
204
205 @Test
206 public void testGetAdjustmentDifferentScale() throws Exception {
207 TmfTimestamp ts0 = new TmfTimestamp(12345, (byte) -2);
208 TmfTimestamp ts = new TmfTimestamp( 1, (byte) 2);
209
210 long delta = ts.getAdjustment(ts0);
211 assertEquals("delta", 2345, delta);
212
213 delta = ts0.getAdjustment(ts);
214 assertEquals("delta", 0, delta);
215 }
216
217 @Test
218 public void testGetAdjustmentDifferentScaleWithException() throws Exception {
219 try {
220 TmfTimestamp.BigBang.getAdjustment(TmfTimestamp.BigCrunch);
221 fail("ArithmeticException not thrown");
222 } catch (ArithmeticException e) {
223 }
224 }
225
226 // ========================================================================
227 // CompareTo
228 // ========================================================================
229
230 @Test
231 public void testCompareToSameScale() throws Exception {
232 TmfTimestamp ts1 = new TmfTimestamp( 900, (byte) 0, 50);
233 TmfTimestamp ts2 = new TmfTimestamp(1000, (byte) 0, 50);
234 TmfTimestamp ts3 = new TmfTimestamp(1100, (byte) 0, 50);
235 TmfTimestamp ts4 = new TmfTimestamp(1000, (byte) 0, 75);
236
237 assertTrue(ts1.compareTo(ts1, false) == 0);
238
239 assertTrue(ts1.compareTo(ts2, false) < 0);
240 assertTrue(ts1.compareTo(ts3, false) < 0);
241 assertTrue(ts1.compareTo(ts4, false) < 0);
242
243 assertTrue(ts2.compareTo(ts1, false) > 0);
244 assertTrue(ts2.compareTo(ts3, false) < 0);
245 assertTrue(ts2.compareTo(ts4, false) == 0);
246
247 assertTrue(ts3.compareTo(ts1, false) > 0);
248 assertTrue(ts3.compareTo(ts2, false) > 0);
249 assertTrue(ts3.compareTo(ts4, false) > 0);
250 }
251
252 @Test
253 public void testCompareToDifferentScale() throws Exception {
254 TmfTimestamp ts1 = new TmfTimestamp(9000, (byte) -1, 50);
255 TmfTimestamp ts2 = new TmfTimestamp(1000, (byte) 0, 50);
256 TmfTimestamp ts3 = new TmfTimestamp( 110, (byte) 1, 50);
257 TmfTimestamp ts4 = new TmfTimestamp( 1, (byte) 3, 75);
258
259 assertTrue(ts1.compareTo(ts1, false) == 0);
260
261 assertTrue(ts1.compareTo(ts2, false) < 0);
262 assertTrue(ts1.compareTo(ts3, false) < 0);
263 assertTrue(ts1.compareTo(ts4, false) < 0);
264
265 assertTrue(ts2.compareTo(ts1, false) > 0);
266 assertTrue(ts2.compareTo(ts3, false) < 0);
267 assertTrue(ts2.compareTo(ts4, false) == 0);
268
269 assertTrue(ts3.compareTo(ts1, false) > 0);
270 assertTrue(ts3.compareTo(ts2, false) > 0);
271 assertTrue(ts3.compareTo(ts4, false) > 0);
272 }
273
274 @Test
275 public void testCompareToWithinPrecision() throws Exception {
276 TmfTimestamp ts1 = new TmfTimestamp( 900, (byte) 0, 50);
277 TmfTimestamp ts2 = new TmfTimestamp(1000, (byte) 0, 50);
278 TmfTimestamp ts3 = new TmfTimestamp(1100, (byte) 0, 50);
279 TmfTimestamp ts4 = new TmfTimestamp(1000, (byte) 0, 75);
280
281 assertTrue(ts1.compareTo(ts1, true) == 0);
282
283 assertTrue(ts1.compareTo(ts2, true) == 0);
284 assertTrue(ts1.compareTo(ts3, true) < 0);
285 assertTrue(ts1.compareTo(ts4, true) == 0);
286
287 assertTrue(ts2.compareTo(ts1, true) == 0);
288 assertTrue(ts2.compareTo(ts3, true) == 0);
289 assertTrue(ts2.compareTo(ts4, true) == 0);
290
291 assertTrue(ts3.compareTo(ts1, true) > 0);
292 assertTrue(ts3.compareTo(ts2, true) == 0);
293 assertTrue(ts3.compareTo(ts4, true) == 0);
294 }
295
296 @Test
297 public void testCompareToLargeScale() throws Exception {
298 TmfTimestamp ts1 = new TmfTimestamp( -1, (byte) 100);
299 TmfTimestamp ts2 = new TmfTimestamp(-1000, (byte) -100);
300 TmfTimestamp ts3 = new TmfTimestamp( 1, (byte) 100);
301 TmfTimestamp ts4 = new TmfTimestamp( 1000, (byte) -100);
302
303 assertTrue(ts1.compareTo(ts2, false) < 0);
304 assertTrue(ts1.compareTo(ts3, false) < 0);
305 assertTrue(ts1.compareTo(ts4, false) < 0);
306
307 assertTrue(ts2.compareTo(ts1, false) > 0);
308 assertTrue(ts2.compareTo(ts3, false) < 0);
309 assertTrue(ts2.compareTo(ts4, false) < 0);
310
311 assertTrue(ts3.compareTo(ts1, false) > 0);
312 assertTrue(ts3.compareTo(ts2, false) > 0);
313 assertTrue(ts3.compareTo(ts4, false) > 0);
314
315 assertTrue(ts4.compareTo(ts1, false) > 0);
316 assertTrue(ts4.compareTo(ts2, false) > 0);
317 assertTrue(ts4.compareTo(ts3, false) < 0);
318 }
319
320 @Test
321 public void testCompareToBigGuys() throws Exception {
322 TmfTimestamp ts1 = new TmfTimestamp(-1, Byte.MAX_VALUE);
323 TmfTimestamp ts2 = new TmfTimestamp(-1, Byte.MIN_VALUE);
324 TmfTimestamp ts3 = new TmfTimestamp( 1, Byte.MAX_VALUE);
325 TmfTimestamp ts4 = new TmfTimestamp( 1, Byte.MIN_VALUE);
326
327 assertTrue(ts1.compareTo(TmfTimestamp.BigBang, false) < 0);
328 assertTrue(ts1.compareTo(TmfTimestamp.BigCrunch, false) < 0);
329
330 assertTrue(ts2.compareTo(TmfTimestamp.BigBang, false) < 0);
331 assertTrue(ts2.compareTo(TmfTimestamp.BigCrunch, false) < 0);
332
333 assertTrue(ts3.compareTo(TmfTimestamp.BigBang, false) > 0);
334 assertTrue(ts3.compareTo(TmfTimestamp.BigCrunch, false) < 0);
335
336 assertTrue(ts4.compareTo(TmfTimestamp.BigBang, false) > 0);
337 assertTrue(ts4.compareTo(TmfTimestamp.BigCrunch, false) < 0);
338 }
339
340 }
This page took 0.039642 seconds and 5 git commands to generate.