1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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 * Alexandre Montplaisir - Port to JUnit4
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.event
;
16 import static org
.junit
.Assert
.assertEquals
;
17 import static org
.junit
.Assert
.assertFalse
;
18 import static org
.junit
.Assert
.assertTrue
;
20 import java
.text
.DateFormat
;
21 import java
.text
.SimpleDateFormat
;
22 import java
.util
.Date
;
24 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.timestamp
.TmfSecondTimestamp
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
27 import org
.junit
.Test
;
30 * Test suite for the {@link TmfSecondTimestamp} class.
32 @SuppressWarnings("javadoc")
33 public class TmfSecondTimestampTest
{
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
39 private final ITmfTimestamp ts0
= new TmfSecondTimestamp(0);
40 private final ITmfTimestamp ts1
= new TmfSecondTimestamp(12345);
41 private final ITmfTimestamp ts2
= new TmfSecondTimestamp(-1234);
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
48 public void testDefaultConstructor() {
49 assertEquals("getValue", 0, ts0
.getValue());
50 assertEquals("getscale", 0, ts0
.getScale());
54 public void testFullConstructor() {
55 assertEquals("getValue", 12345, ts1
.getValue());
56 assertEquals("getscale", 0, ts1
.getScale());
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
64 public void testEqualsReflexivity() {
65 assertEquals("equals", ts0
, ts0
);
66 assertEquals("equals", ts1
, ts1
);
67 assertEquals("equals", ts2
, ts2
);
69 assertFalse("different", ts0
.equals(ts1
));
70 assertFalse("different", ts0
.equals(ts2
));
72 assertFalse("different", ts1
.equals(ts0
));
73 assertFalse("different", ts1
.equals(ts2
));
75 assertFalse("different", ts2
.equals(ts0
));
76 assertFalse("different", ts2
.equals(ts1
));
80 public void testEqualsNull() {
81 assertTrue("different", !ts0
.equals(null));
82 assertTrue("different", !ts1
.equals(null));
83 assertTrue("different", !ts2
.equals(null));
87 public void testEqualsNonTimestamp() {
88 assertFalse("equals", ts0
.equals(ts0
.toString()));
91 // ------------------------------------------------------------------------
93 // ------------------------------------------------------------------------
96 public void testToString() {
97 DateFormat df
= new SimpleDateFormat("HH:mm:ss.SSS");
98 Date d0
= new Date(ts0
.getValue() * 1000);
99 Date d1
= new Date(ts1
.getValue() * 1000);
100 Date d2
= new Date(ts2
.getValue() * 1000);
101 assertEquals("toString", df
.format(d0
) + " 000 000", ts0
.toString());
102 assertEquals("toString", df
.format(d1
) + " 000 000", ts1
.toString());
103 assertEquals("toString", df
.format(d2
) + " 000 000", ts2
.toString());
106 // ------------------------------------------------------------------------
108 // ------------------------------------------------------------------------
111 public void testHashCode() {
112 final ITmfTimestamp ts0copy
= TmfTimestamp
.create(ts0
.getValue(), ts0
.getScale());
113 final ITmfTimestamp ts1copy
= TmfTimestamp
.create(ts1
.getValue(), ts1
.getScale());
114 final ITmfTimestamp ts2copy
= TmfTimestamp
.create(ts2
.getValue(), ts2
.getScale());
116 assertEquals("hashCode", ts0
.hashCode(), ts0copy
.hashCode());
117 assertEquals("hashCode", ts1
.hashCode(), ts1copy
.hashCode());
118 assertEquals("hashCode", ts2
.hashCode(), ts2copy
.hashCode());
121 // ------------------------------------------------------------------------
123 // ------------------------------------------------------------------------
126 public void testNormalizeScale0() {
127 ITmfTimestamp ts
= ts0
.normalize(0, 0);
128 assertEquals("getValue", 0, ts
.getValue());
129 assertEquals("getscale", 0, ts
.getScale());
131 ts
= ts0
.normalize(12345, 0);
132 assertEquals("getValue", 12345, ts
.getValue());
133 assertEquals("getscale", 0, ts
.getScale());
135 ts
= ts0
.normalize(10, 0);
136 assertEquals("getValue", 10, ts
.getValue());
137 assertEquals("getscale", 0, ts
.getScale());
139 ts
= ts0
.normalize(-10, 0);
140 assertEquals("getValue", -10, ts
.getValue());
141 assertEquals("getscale", 0, ts
.getScale());
145 public void testNormalizeScaleNot0() {
146 ITmfTimestamp ts
= ts0
.normalize(0, 1);
147 assertEquals("Zero test", TmfTimestamp
.ZERO
, ts
);
149 ts
= ts0
.normalize(12345, 1);
150 assertEquals("getValue", 12345, ts
.getValue());
151 assertEquals("getscale", 1, ts
.getScale());
153 ts
= ts0
.normalize(10, 1);
154 assertEquals("getValue", 10, ts
.getValue());
155 assertEquals("getscale", 1, ts
.getScale());
157 ts
= ts0
.normalize(-10, 1);
158 assertEquals("getValue", -10, ts
.getValue());
159 assertEquals("getscale", 1, ts
.getScale());
162 // ------------------------------------------------------------------------
164 // ------------------------------------------------------------------------
167 public void testBasicCompareTo() {
168 final ITmfTimestamp tstamp1
= TmfTimestamp
.fromSeconds(900);
169 final ITmfTimestamp tstamp2
= TmfTimestamp
.fromSeconds(1000);
170 final ITmfTimestamp tstamp3
= TmfTimestamp
.fromSeconds(1100);
172 assertTrue(tstamp1
.compareTo(tstamp1
) == 0);
174 assertTrue("CompareTo", tstamp1
.compareTo(tstamp2
) < 0);
175 assertTrue("CompareTo", tstamp1
.compareTo(tstamp3
) < 0);
177 assertTrue("CompareTo", tstamp2
.compareTo(tstamp1
) > 0);
178 assertTrue("CompareTo", tstamp2
.compareTo(tstamp3
) < 0);
180 assertTrue("CompareTo", tstamp3
.compareTo(tstamp1
) > 0);
181 assertTrue("CompareTo", tstamp3
.compareTo(tstamp2
) > 0);
185 public void testCompareTo() {
186 final ITmfTimestamp ts0a
= TmfTimestamp
.create(0, 2);
187 final ITmfTimestamp ts1a
= TmfTimestamp
.create(123450, -1);
188 final ITmfTimestamp ts2a
= TmfTimestamp
.create(-12340, -1);
190 assertTrue(ts1
.compareTo(ts1
) == 0);
192 assertTrue("CompareTo", ts0
.compareTo(ts0a
) == 0);
193 assertTrue("CompareTo", ts1
.compareTo(ts1a
) == 0);
194 assertTrue("CompareTo", ts2
.compareTo(ts2a
) == 0);
197 // ------------------------------------------------------------------------
199 // ------------------------------------------------------------------------
202 public void testDelta() {
203 // Delta for same scale and precision (delta > 0)
204 ITmfTimestamp tstamp0
= TmfTimestamp
.fromSeconds(10);
205 ITmfTimestamp tstamp1
= TmfTimestamp
.fromSeconds(5);
206 ITmfTimestamp expectd
= TmfTimestamp
.fromSeconds(5);
208 ITmfTimestamp delta
= tstamp0
.getDelta(tstamp1
);
209 assertEquals("getDelta", 0, delta
.compareTo(expectd
));
211 // Delta for same scale and precision (delta < 0)
212 tstamp0
= TmfTimestamp
.fromSeconds(5);
213 tstamp1
= TmfTimestamp
.fromSeconds(10);
214 expectd
= TmfTimestamp
.fromSeconds(-5);
216 delta
= tstamp0
.getDelta(tstamp1
);
217 assertEquals("getDelta", 0, delta
.compareTo(expectd
));
221 public void testDelta2() {
222 // Delta for different scale and same precision (delta > 0)
223 final ITmfTimestamp tstamp0
= TmfTimestamp
.fromSeconds(10);
224 final ITmfTimestamp tstamp1
= TmfTimestamp
.create(1, 1);
225 final ITmfTimestamp expectd
= TmfTimestamp
.create(0, 0);
227 final ITmfTimestamp delta
= tstamp0
.getDelta(tstamp1
);
228 assertEquals("getDelta", 0, delta
.compareTo(expectd
));
This page took 0.055275 seconds and 5 git commands to generate.