1 /*******************************************************************************
2 * Copyright (c) 2009 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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.event
;
15 import static org
.junit
.Assert
.*;
17 import org
.junit
.Test
;
20 * <b><u>TmfTimestampTest</u></b>
22 * JUnit test suite for the TmfTimestamp class.
24 public class TmfTimestampTest
{
26 // ========================================================================
28 // ========================================================================
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());
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());
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());
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());
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());
71 // ========================================================================
73 // ========================================================================
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());
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());
91 // ========================================================================
93 // ========================================================================
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);
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());
108 // ========================================================================
110 // ========================================================================
113 public void testSynchronizeOffset() throws Exception
{
114 TmfTimestamp ts0
= new TmfTimestamp(1234, (byte) 0, 1);
115 TmfTimestamp ts
= new TmfTimestamp();
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());
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());
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());
134 public void testSynchronizeScale() throws Exception
{
135 TmfTimestamp ts0
= new TmfTimestamp(1234, (byte) 0, 12);
136 TmfTimestamp ts
= new TmfTimestamp();
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());
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());
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());
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());
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());
165 public void testSynchronizeOffsetAndScale() throws Exception
{
166 TmfTimestamp ts0
= new TmfTimestamp(1234, (byte) 0, 12);
167 TmfTimestamp ts
= new TmfTimestamp();
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());
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());
181 public void testSynchronizeWithArithmeticException() throws Exception
{
182 TmfTimestamp bigBang
= new TmfTimestamp(TmfTimestamp
.BigBang
);
183 TmfTimestamp bigCrunch
= new TmfTimestamp(TmfTimestamp
.BigCrunch
);
186 bigCrunch
.synchronize(bigBang
.getValue(), bigBang
.getScale());
187 fail("Exception not thrown");
188 } catch (ArithmeticException e
) {
192 // ========================================================================
194 // ========================================================================
197 public void testGetAdjustmentSameScale() throws Exception
{
198 TmfTimestamp ts0
= new TmfTimestamp(12345, (byte) -2);
199 TmfTimestamp ts
= new TmfTimestamp(10000, (byte) -2);
201 long delta
= ts
.getAdjustment(ts0
);
202 assertEquals("delta", 2345, delta
);
206 public void testGetAdjustmentDifferentScale() throws Exception
{
207 TmfTimestamp ts0
= new TmfTimestamp(12345, (byte) -2);
208 TmfTimestamp ts
= new TmfTimestamp( 1, (byte) 2);
210 long delta
= ts
.getAdjustment(ts0
);
211 assertEquals("delta", 2345, delta
);
213 delta
= ts0
.getAdjustment(ts
);
214 assertEquals("delta", 0, delta
);
218 public void testGetAdjustmentDifferentScaleWithException() throws Exception
{
220 TmfTimestamp
.BigBang
.getAdjustment(TmfTimestamp
.BigCrunch
);
221 fail("ArithmeticException not thrown");
222 } catch (ArithmeticException e
) {
226 // ========================================================================
228 // ========================================================================
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);
237 assertTrue(ts1
.compareTo(ts1
, false) == 0);
239 assertTrue(ts1
.compareTo(ts2
, false) < 0);
240 assertTrue(ts1
.compareTo(ts3
, false) < 0);
241 assertTrue(ts1
.compareTo(ts4
, false) < 0);
243 assertTrue(ts2
.compareTo(ts1
, false) > 0);
244 assertTrue(ts2
.compareTo(ts3
, false) < 0);
245 assertTrue(ts2
.compareTo(ts4
, false) == 0);
247 assertTrue(ts3
.compareTo(ts1
, false) > 0);
248 assertTrue(ts3
.compareTo(ts2
, false) > 0);
249 assertTrue(ts3
.compareTo(ts4
, false) > 0);
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);
259 assertTrue(ts1
.compareTo(ts1
, false) == 0);
261 assertTrue(ts1
.compareTo(ts2
, false) < 0);
262 assertTrue(ts1
.compareTo(ts3
, false) < 0);
263 assertTrue(ts1
.compareTo(ts4
, false) < 0);
265 assertTrue(ts2
.compareTo(ts1
, false) > 0);
266 assertTrue(ts2
.compareTo(ts3
, false) < 0);
267 assertTrue(ts2
.compareTo(ts4
, false) == 0);
269 assertTrue(ts3
.compareTo(ts1
, false) > 0);
270 assertTrue(ts3
.compareTo(ts2
, false) > 0);
271 assertTrue(ts3
.compareTo(ts4
, false) > 0);
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);
281 assertTrue(ts1
.compareTo(ts1
, true) == 0);
283 assertTrue(ts1
.compareTo(ts2
, true) == 0);
284 assertTrue(ts1
.compareTo(ts3
, true) < 0);
285 assertTrue(ts1
.compareTo(ts4
, true) == 0);
287 assertTrue(ts2
.compareTo(ts1
, true) == 0);
288 assertTrue(ts2
.compareTo(ts3
, true) == 0);
289 assertTrue(ts2
.compareTo(ts4
, true) == 0);
291 assertTrue(ts3
.compareTo(ts1
, true) > 0);
292 assertTrue(ts3
.compareTo(ts2
, true) == 0);
293 assertTrue(ts3
.compareTo(ts4
, true) == 0);
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);
303 assertTrue(ts1
.compareTo(ts2
, false) < 0);
304 assertTrue(ts1
.compareTo(ts3
, false) < 0);
305 assertTrue(ts1
.compareTo(ts4
, false) < 0);
307 assertTrue(ts2
.compareTo(ts1
, false) > 0);
308 assertTrue(ts2
.compareTo(ts3
, false) < 0);
309 assertTrue(ts2
.compareTo(ts4
, false) < 0);
311 assertTrue(ts3
.compareTo(ts1
, false) > 0);
312 assertTrue(ts3
.compareTo(ts2
, false) > 0);
313 assertTrue(ts3
.compareTo(ts4
, false) > 0);
315 assertTrue(ts4
.compareTo(ts1
, false) > 0);
316 assertTrue(ts4
.compareTo(ts2
, false) > 0);
317 assertTrue(ts4
.compareTo(ts3
, false) < 0);
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
);
327 assertTrue(ts1
.compareTo(TmfTimestamp
.BigBang
, false) < 0);
328 assertTrue(ts1
.compareTo(TmfTimestamp
.BigCrunch
, false) < 0);
330 assertTrue(ts2
.compareTo(TmfTimestamp
.BigBang
, false) < 0);
331 assertTrue(ts2
.compareTo(TmfTimestamp
.BigCrunch
, false) < 0);
333 assertTrue(ts3
.compareTo(TmfTimestamp
.BigBang
, false) > 0);
334 assertTrue(ts3
.compareTo(TmfTimestamp
.BigCrunch
, false) < 0);
336 assertTrue(ts4
.compareTo(TmfTimestamp
.BigBang
, false) > 0);
337 assertTrue(ts4
.compareTo(TmfTimestamp
.BigCrunch
, false) < 0);
This page took 0.039642 seconds and 5 git commands to generate.