tmf.core: Internalize timestamp types
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / event / TmfSecondTimestampTest.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 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 * Alexandre Montplaisir - Port to JUnit4
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
20 import java.text.DateFormat;
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23
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;
28
29 /**
30 * Test suite for the {@link TmfSecondTimestamp} class.
31 */
32 @SuppressWarnings("javadoc")
33 public class TmfSecondTimestampTest {
34
35 // ------------------------------------------------------------------------
36 // Variables
37 // ------------------------------------------------------------------------
38
39 private final ITmfTimestamp ts0 = new TmfSecondTimestamp(0);
40 private final ITmfTimestamp ts1 = new TmfSecondTimestamp(12345);
41 private final ITmfTimestamp ts2 = new TmfSecondTimestamp(-1234);
42
43 // ------------------------------------------------------------------------
44 // Constructors
45 // ------------------------------------------------------------------------
46
47 @Test
48 public void testDefaultConstructor() {
49 assertEquals("getValue", 0, ts0.getValue());
50 assertEquals("getscale", 0, ts0.getScale());
51 }
52
53 @Test
54 public void testFullConstructor() {
55 assertEquals("getValue", 12345, ts1.getValue());
56 assertEquals("getscale", 0, ts1.getScale());
57 }
58
59 // ------------------------------------------------------------------------
60 // equals
61 // ------------------------------------------------------------------------
62
63 @Test
64 public void testEqualsReflexivity() {
65 assertEquals("equals", ts0, ts0);
66 assertEquals("equals", ts1, ts1);
67 assertEquals("equals", ts2, ts2);
68
69 assertFalse("different", ts0.equals(ts1));
70 assertFalse("different", ts0.equals(ts2));
71
72 assertFalse("different", ts1.equals(ts0));
73 assertFalse("different", ts1.equals(ts2));
74
75 assertFalse("different", ts2.equals(ts0));
76 assertFalse("different", ts2.equals(ts1));
77 }
78
79 @Test
80 public void testEqualsNull() {
81 assertTrue("different", !ts0.equals(null));
82 assertTrue("different", !ts1.equals(null));
83 assertTrue("different", !ts2.equals(null));
84 }
85
86 @Test
87 public void testEqualsNonTimestamp() {
88 assertFalse("equals", ts0.equals(ts0.toString()));
89 }
90
91 // ------------------------------------------------------------------------
92 // toString
93 // ------------------------------------------------------------------------
94
95 @Test
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());
104 }
105
106 // ------------------------------------------------------------------------
107 // hashCode
108 // ------------------------------------------------------------------------
109
110 @Test
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());
115
116 assertEquals("hashCode", ts0.hashCode(), ts0copy.hashCode());
117 assertEquals("hashCode", ts1.hashCode(), ts1copy.hashCode());
118 assertEquals("hashCode", ts2.hashCode(), ts2copy.hashCode());
119 }
120
121 // ------------------------------------------------------------------------
122 // normalize
123 // ------------------------------------------------------------------------
124
125 @Test
126 public void testNormalizeScale0() {
127 ITmfTimestamp ts = ts0.normalize(0, 0);
128 assertEquals("getValue", 0, ts.getValue());
129 assertEquals("getscale", 0, ts.getScale());
130
131 ts = ts0.normalize(12345, 0);
132 assertEquals("getValue", 12345, ts.getValue());
133 assertEquals("getscale", 0, ts.getScale());
134
135 ts = ts0.normalize(10, 0);
136 assertEquals("getValue", 10, ts.getValue());
137 assertEquals("getscale", 0, ts.getScale());
138
139 ts = ts0.normalize(-10, 0);
140 assertEquals("getValue", -10, ts.getValue());
141 assertEquals("getscale", 0, ts.getScale());
142 }
143
144 @Test
145 public void testNormalizeScaleNot0() {
146 ITmfTimestamp ts = ts0.normalize(0, 1);
147 assertEquals("Zero test", TmfTimestamp.ZERO, ts);
148
149 ts = ts0.normalize(12345, 1);
150 assertEquals("getValue", 12345, ts.getValue());
151 assertEquals("getscale", 1, ts.getScale());
152
153 ts = ts0.normalize(10, 1);
154 assertEquals("getValue", 10, ts.getValue());
155 assertEquals("getscale", 1, ts.getScale());
156
157 ts = ts0.normalize(-10, 1);
158 assertEquals("getValue", -10, ts.getValue());
159 assertEquals("getscale", 1, ts.getScale());
160 }
161
162 // ------------------------------------------------------------------------
163 // compareTo
164 // ------------------------------------------------------------------------
165
166 @Test
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);
171
172 assertTrue(tstamp1.compareTo(tstamp1) == 0);
173
174 assertTrue("CompareTo", tstamp1.compareTo(tstamp2) < 0);
175 assertTrue("CompareTo", tstamp1.compareTo(tstamp3) < 0);
176
177 assertTrue("CompareTo", tstamp2.compareTo(tstamp1) > 0);
178 assertTrue("CompareTo", tstamp2.compareTo(tstamp3) < 0);
179
180 assertTrue("CompareTo", tstamp3.compareTo(tstamp1) > 0);
181 assertTrue("CompareTo", tstamp3.compareTo(tstamp2) > 0);
182 }
183
184 @Test
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);
189
190 assertTrue(ts1.compareTo(ts1) == 0);
191
192 assertTrue("CompareTo", ts0.compareTo(ts0a) == 0);
193 assertTrue("CompareTo", ts1.compareTo(ts1a) == 0);
194 assertTrue("CompareTo", ts2.compareTo(ts2a) == 0);
195 }
196
197 // ------------------------------------------------------------------------
198 // getDelta
199 // ------------------------------------------------------------------------
200
201 @Test
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);
207
208 ITmfTimestamp delta = tstamp0.getDelta(tstamp1);
209 assertEquals("getDelta", 0, delta.compareTo(expectd));
210
211 // Delta for same scale and precision (delta < 0)
212 tstamp0 = TmfTimestamp.fromSeconds(5);
213 tstamp1 = TmfTimestamp.fromSeconds(10);
214 expectd = TmfTimestamp.fromSeconds(-5);
215
216 delta = tstamp0.getDelta(tstamp1);
217 assertEquals("getDelta", 0, delta.compareTo(expectd));
218 }
219
220 @Test
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);
226
227 final ITmfTimestamp delta = tstamp0.getDelta(tstamp1);
228 assertEquals("getDelta", 0, delta.compareTo(expectd));
229 }
230
231 }
This page took 0.055275 seconds and 5 git commands to generate.