77fb2bb35d8185a932b17ad6a7497c3947d0c027
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / event / TmfNanoTimestampTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 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.tracecompass.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.tracecompass.tmf.core.timestamp.ITmfTimestamp;
26 import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp;
27 import org.eclipse.tracecompass.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 }
53
54 @Test
55 public void testFullConstructor() {
56 assertEquals("getValue", 12345, ts1.getValue());
57 assertEquals("getscale", -9, ts1.getScale());
58 }
59
60 @Test
61 public void testCopyConstructor() {
62 final ITmfTimestamp copy = new TmfNanoTimestamp(ts1);
63
64 assertEquals("getValue", ts1.getValue(), copy.getValue());
65 assertEquals("getscale", ts1.getScale(), copy.getScale());
66
67 assertEquals("getValue", 12345, copy.getValue());
68 assertEquals("getscale", -9, copy.getScale());
69 }
70
71 @Test
72 public void testCopyBadTimestamp() {
73 try {
74 new TmfNanoTimestamp(null);
75 fail("TmfNanoTimestamp: null argument");
76 } catch (final NullPointerException e) {
77 }
78 }
79
80 // ------------------------------------------------------------------------
81 // equals
82 // ------------------------------------------------------------------------
83
84 @Test
85 public void testEqualsReflexivity() {
86 assertTrue("equals", ts0.equals(ts0));
87 assertTrue("equals", ts1.equals(ts1));
88 assertTrue("equals", ts2.equals(ts2));
89
90 assertTrue("equals", !ts0.equals(ts1));
91 assertTrue("equals", !ts0.equals(ts2));
92
93 assertTrue("equals", !ts1.equals(ts0));
94 assertTrue("equals", !ts1.equals(ts2));
95
96 assertTrue("equals", !ts2.equals(ts0));
97 assertTrue("equals", !ts2.equals(ts1));
98 }
99
100 @Test
101 public void testEqualsSymmetry() {
102 final ITmfTimestamp ts0copy = new TmfNanoTimestamp(ts0);
103 assertTrue("equals", ts0.equals(ts0copy));
104 assertTrue("equals", ts0copy.equals(ts0));
105
106 final ITmfTimestamp ts1copy = new TmfNanoTimestamp(ts1);
107 assertTrue("equals", ts1.equals(ts1copy));
108 assertTrue("equals", ts1copy.equals(ts1));
109 }
110
111 @Test
112 public void testEqualsTransivity() {
113 final ITmfTimestamp ts0copy1 = new TmfNanoTimestamp(ts0);
114 final ITmfTimestamp ts0copy2 = new TmfNanoTimestamp(ts0copy1);
115 assertTrue("equals", ts0.equals(ts0copy1));
116 assertTrue("equals", ts0copy1.equals(ts0copy2));
117 assertTrue("equals", ts0.equals(ts0copy2));
118
119 final ITmfTimestamp ts1copy1 = new TmfNanoTimestamp(ts1);
120 final ITmfTimestamp ts1copy2 = new TmfNanoTimestamp(ts1copy1);
121 assertTrue("equals", ts1.equals(ts1copy1));
122 assertTrue("equals", ts1copy1.equals(ts1copy2));
123 assertTrue("equals", ts1.equals(ts1copy2));
124 }
125
126 @Test
127 public void testEqualsNull() {
128 assertTrue("equals", !ts0.equals(null));
129 assertTrue("equals", !ts1.equals(null));
130 assertTrue("equals", !ts2.equals(null));
131 }
132
133 @Test
134 public void testEqualsNonTimestamp() {
135 assertFalse("equals", ts0.equals(ts0.toString()));
136 }
137
138 // ------------------------------------------------------------------------
139 // toString
140 // ------------------------------------------------------------------------
141
142 @Test
143 public void testToString() {
144 DateFormat df = new SimpleDateFormat("HH:mm:ss.SSS");
145 Date d0 = new Date(ts0.getValue() / 1000000);
146 Date d1 = new Date(ts1.getValue() / 1000000);
147 Date d2 = new Date(ts2.getValue() / 1000000 - 1);
148 assertEquals("toString", df.format(d0) + " 000 000", ts0.toString());
149 assertEquals("toString", df.format(d1) + " 012 345", ts1.toString());
150 assertEquals("toString", df.format(d2) + " 998 766", ts2.toString());
151 }
152
153 // ------------------------------------------------------------------------
154 // hashCode
155 // ------------------------------------------------------------------------
156
157 @Test
158 public void testHashCode() {
159 final ITmfTimestamp ts0copy = new TmfTimestamp(ts0);
160 final ITmfTimestamp ts1copy = new TmfTimestamp(ts1);
161 final ITmfTimestamp ts2copy = new TmfTimestamp(ts2);
162
163 assertTrue("hashCode", ts0.hashCode() == ts0copy.hashCode());
164 assertTrue("hashCode", ts1.hashCode() == ts1copy.hashCode());
165 assertTrue("hashCode", ts2.hashCode() == ts2copy.hashCode());
166
167 assertTrue("hashCode", ts0.hashCode() != ts1.hashCode());
168 }
169
170 // ------------------------------------------------------------------------
171 // normalize
172 // ------------------------------------------------------------------------
173
174 @Test
175 public void testNormalizeScale0() {
176 ITmfTimestamp ts = ts0.normalize(0, 0);
177 assertEquals("getValue", 0, ts.getValue());
178 assertEquals("getscale", 0, ts.getScale());
179
180 ts = ts0.normalize(12345, 0);
181 assertEquals("getValue", 12345, ts.getValue());
182 assertEquals("getscale", 0, ts.getScale());
183
184 ts = ts0.normalize(10, 0);
185 assertEquals("getValue", 10, ts.getValue());
186 assertEquals("getscale", 0, ts.getScale());
187
188 ts = ts0.normalize(-10, 0);
189 assertEquals("getValue", -10, ts.getValue());
190 assertEquals("getscale", 0, ts.getScale());
191 }
192
193 @Test
194 public void testNormalizeScaleNot0() {
195 ITmfTimestamp ts = ts0.normalize(0, 1);
196 assertEquals("getValue", 0, ts.getValue());
197 assertEquals("getscale", 1, ts.getScale());
198
199 ts = ts0.normalize(12345, 1);
200 assertEquals("getValue", 12345, ts.getValue());
201 assertEquals("getscale", 1, ts.getScale());
202
203 ts = ts0.normalize(10, 1);
204 assertEquals("getValue", 10, ts.getValue());
205 assertEquals("getscale", 1, ts.getScale());
206
207 ts = ts0.normalize(-10, 1);
208 assertEquals("getValue", -10, ts.getValue());
209 assertEquals("getscale", 1, ts.getScale());
210 }
211
212 // ------------------------------------------------------------------------
213 // compareTo
214 // ------------------------------------------------------------------------
215
216 @Test
217 public void testBasicCompareTo() {
218 final ITmfTimestamp tstamp1 = new TmfNanoTimestamp(900);
219 final ITmfTimestamp tstamp2 = new TmfNanoTimestamp(1000);
220 final ITmfTimestamp tstamp3 = new TmfNanoTimestamp(1100);
221
222 assertTrue(tstamp1.compareTo(tstamp1) == 0);
223
224 assertTrue("CompareTo", tstamp1.compareTo(tstamp2) < 0);
225 assertTrue("CompareTo", tstamp1.compareTo(tstamp3) < 0);
226
227 assertTrue("CompareTo", tstamp2.compareTo(tstamp1) > 0);
228 assertTrue("CompareTo", tstamp2.compareTo(tstamp3) < 0);
229
230 assertTrue("CompareTo", tstamp3.compareTo(tstamp1) > 0);
231 assertTrue("CompareTo", tstamp3.compareTo(tstamp2) > 0);
232 }
233
234 @Test
235 public void testCompareTo() {
236 final ITmfTimestamp ts0a = new TmfTimestamp(0, 2);
237 final ITmfTimestamp ts1a = new TmfTimestamp(123450, -10);
238 final ITmfTimestamp ts2a = new TmfTimestamp(-12340, -10);
239
240 assertTrue(ts1.compareTo(ts1) == 0);
241
242 assertTrue("CompareTo", ts0.compareTo(ts0a) == 0);
243 assertTrue("CompareTo", ts1.compareTo(ts1a) == 0);
244 assertTrue("CompareTo", ts2.compareTo(ts2a) == 0);
245 }
246
247 // ------------------------------------------------------------------------
248 // getDelta
249 // ------------------------------------------------------------------------
250
251 @Test
252 public void testDelta() {
253 // Delta for same scale and precision (delta > 0)
254 TmfTimestamp tstamp0 = new TmfNanoTimestamp(10);
255 TmfTimestamp tstamp1 = new TmfNanoTimestamp(5);
256 TmfTimestamp expectd = new TmfNanoTimestamp(5);
257
258 ITmfTimestamp delta = tstamp0.getDelta(tstamp1);
259 assertEquals("getDelta", 0, delta.compareTo(expectd));
260
261 // Delta for same scale and precision (delta < 0)
262 tstamp0 = new TmfTimestamp(5);
263 tstamp1 = new TmfTimestamp(10);
264 expectd = new TmfTimestamp(-5);
265
266 delta = tstamp0.getDelta(tstamp1);
267 assertEquals("getDelta", 0, delta.compareTo(expectd));
268 }
269
270 @Test
271 public void testDelta2() {
272 // Delta for different scale and same precision (delta > 0)
273 final TmfTimestamp tstamp0 = new TmfNanoTimestamp(10);
274 final TmfTimestamp tstamp1 = new TmfTimestamp(1, -8);
275 final TmfTimestamp expectd = new TmfTimestamp(0, 0);
276
277 final ITmfTimestamp delta = tstamp0.getDelta(tstamp1);
278 assertEquals("getDelta", 0, delta.compareTo(expectd));
279 }
280
281 }
This page took 0.037562 seconds and 4 git commands to generate.