f65b18df7ae1e1422aa93cf398b21d05e88065cd
[deliverable/tracecompass.git] / org.eclipse.tracecompass.statesystem.core.tests / src / org / eclipse / tracecompass / statesystem / core / tests / statevalue / StateValueCompareToTest.java
1 /*******************************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made availabComparisonOperator.LE under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is availabComparisonOperator.LE at
7 * http://www.eclipse.org/ComparisonOperator.LEgal/epl-v10.html
8 *
9 * Contributors:
10 * Naser Ezzati - Initial API and implementation
11 ******************************************************************************/
12
13 package org.eclipse.tracecompass.statesystem.core.tests.statevalue;
14
15 import static org.junit.Assert.assertTrue;
16
17 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
18 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
19 import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
20 import org.junit.Test;
21
22 /**
23 * Unit test for the {@link ITmfStateValue#compareTo(ITmfStateValue)} method
24 *
25 * @author Naser Ezzati
26 */
27 public class StateValueCompareToTest {
28
29 // ------------------------------------------------------------------------
30 // Static fields
31 // ------------------------------------------------------------------------
32
33 /* State values that will be used */
34 private static final ITmfStateValue BASE_INT_VALUE = TmfStateValue.newValueInt(10);
35 private static final ITmfStateValue BIGGER_INT_VALUE = TmfStateValue.newValueInt(20);
36 private static final ITmfStateValue SMALLER_INT_VALUE = TmfStateValue.newValueInt(6);
37
38 private static final ITmfStateValue BASE_LONG_VALUE = TmfStateValue.newValueLong(10);
39 private static final ITmfStateValue BIGGER_LONG_VALUE = TmfStateValue.newValueLong(20);
40 private static final ITmfStateValue SMALLER_LONG_VALUE = TmfStateValue.newValueLong(6);
41 private static final ITmfStateValue MIN_LONG_VALUE = TmfStateValue.newValueLong(Long.MIN_VALUE);
42 private static final ITmfStateValue MAX_LONG_VALUE = TmfStateValue.newValueLong(Long.MAX_VALUE);
43
44 private static final ITmfStateValue BASE_DOUBLE_VALUE = TmfStateValue.newValueDouble(10.00);
45 private static final ITmfStateValue BIGGER_DOUBLE_VALUE1 = TmfStateValue.newValueDouble(20.00);
46 private static final ITmfStateValue BIGGER_DOUBLE_VALUE2 = TmfStateValue.newValueDouble(10.03);
47 private static final ITmfStateValue SMALLER_DOUBLE_VALUE1 = TmfStateValue.newValueDouble(6.00);
48 private static final ITmfStateValue SMALLER_DOUBLE_VALUE2 = TmfStateValue.newValueDouble(9.99);
49 private static final ITmfStateValue MIN_DOUBLE_VALUE = TmfStateValue.newValueDouble(Double.MIN_VALUE);
50 private static final ITmfStateValue MAX_DOUBLE_VALUE = TmfStateValue.newValueDouble(Double.MAX_VALUE);
51 private static final ITmfStateValue POSITIVE_INFINITY = TmfStateValue.newValueDouble(Double.POSITIVE_INFINITY);
52 private static final ITmfStateValue NEGATIVE_INFINITY = TmfStateValue.newValueDouble(Double.NEGATIVE_INFINITY);
53
54 private static final ITmfStateValue BASE_STRING_VALUE = TmfStateValue.newValueString("D");
55 private static final ITmfStateValue BIGGER_STRING_VALUE = TmfStateValue.newValueString("Z");
56 private static final ITmfStateValue SMALLER_STRING_VALUE = TmfStateValue.newValueString("A");
57
58 private static final ITmfStateValue NULL_VALUE = TmfStateValue.nullValue();
59
60 // ------------------------------------------------------------------------
61 // Comparisons of Integer state values
62 // ------------------------------------------------------------------------
63
64 /**
65 * Compare Integer state values together
66 */
67 @Test
68 public void compareIntWithInt() {
69 assertTrue(BASE_INT_VALUE.compareTo(BASE_INT_VALUE) == 0);
70 assertTrue(BASE_INT_VALUE.compareTo(BIGGER_INT_VALUE) < 0);
71 assertTrue(BASE_INT_VALUE.compareTo(SMALLER_INT_VALUE) > 0);
72 }
73
74 /**
75 * Compare Integer with Long state values
76 */
77 @Test
78 public void compareIntWithLong() {
79 assertTrue(BASE_INT_VALUE.compareTo(BASE_LONG_VALUE) == 0);
80 assertTrue(BASE_INT_VALUE.compareTo(BIGGER_LONG_VALUE) < 0);
81 assertTrue(BASE_INT_VALUE.compareTo(MAX_LONG_VALUE) < 0);
82
83 assertTrue(BASE_INT_VALUE.compareTo(SMALLER_LONG_VALUE) > 0);
84 assertTrue(BASE_INT_VALUE.compareTo(MIN_LONG_VALUE) > 0);
85 }
86
87 /**
88 * Compare Integer with Double state values
89 */
90 @Test
91 public void compareIntWithDouble() {
92 assertTrue(BASE_INT_VALUE.compareTo(BASE_DOUBLE_VALUE) == 0);
93 assertTrue(BASE_INT_VALUE.compareTo(BIGGER_DOUBLE_VALUE1) < 0);
94 assertTrue(BASE_INT_VALUE.compareTo(BIGGER_DOUBLE_VALUE2) < 0);
95 assertTrue(BASE_INT_VALUE.compareTo(MAX_DOUBLE_VALUE) < 0);
96 assertTrue(BASE_INT_VALUE.compareTo(POSITIVE_INFINITY) < 0);
97 assertTrue(BASE_INT_VALUE.compareTo(SMALLER_DOUBLE_VALUE1) > 0);
98 assertTrue(BASE_INT_VALUE.compareTo(SMALLER_DOUBLE_VALUE2) > 0);
99 assertTrue(BASE_INT_VALUE.compareTo(MIN_DOUBLE_VALUE) > 0);
100 assertTrue(BASE_INT_VALUE.compareTo(NEGATIVE_INFINITY) > 0);
101 }
102
103 /**
104 * Compare Integer with Null state values
105 */
106 @Test
107 public void compareIntWithNull() {
108 assertTrue(BASE_INT_VALUE.compareTo(NULL_VALUE) > 0);
109 }
110
111 /**
112 * Compare Integer with String state values (should fail)
113 */
114 @Test(expected = StateValueTypeException.class)
115 public void tcompareIntWithString() {
116 BASE_INT_VALUE.compareTo(BASE_STRING_VALUE);
117 }
118
119 // ------------------------------------------------------------------------
120 // Comparisons of Long state values
121 // ------------------------------------------------------------------------
122
123 /**
124 * Compare Long with Integer state values
125 */
126 @Test
127 public void compareLongWithInt() {
128 // with Integer
129 assertTrue(BASE_LONG_VALUE.compareTo(BASE_INT_VALUE) == 0);
130 assertTrue(BASE_LONG_VALUE.compareTo(BIGGER_INT_VALUE) < 0);
131 assertTrue(BASE_LONG_VALUE.compareTo(SMALLER_INT_VALUE) > 0);
132 }
133
134 /**
135 * Compare Long state values together
136 */
137 @Test
138 public void compareLongWithLong() {
139 assertTrue(BASE_LONG_VALUE.compareTo(BASE_LONG_VALUE) == 0);
140 assertTrue(BASE_LONG_VALUE.compareTo(BIGGER_LONG_VALUE) < 0);
141 assertTrue(BASE_LONG_VALUE.compareTo(MAX_LONG_VALUE) < 0);
142 assertTrue(BASE_LONG_VALUE.compareTo(SMALLER_LONG_VALUE) > 0);
143 assertTrue(BASE_LONG_VALUE.compareTo(MIN_LONG_VALUE) > 0);
144 }
145
146 /**
147 * Compare Long with Double state values
148 */
149 @Test
150 public void compareLongWithDouble() {
151 assertTrue(BASE_LONG_VALUE.compareTo(BASE_DOUBLE_VALUE) == 0);
152 assertTrue(BASE_LONG_VALUE.compareTo(BIGGER_DOUBLE_VALUE1) < 0);
153 assertTrue(BASE_LONG_VALUE.compareTo(BIGGER_DOUBLE_VALUE2) < 0);
154 assertTrue(BASE_LONG_VALUE.compareTo(MAX_DOUBLE_VALUE) < 0);
155 assertTrue(BASE_LONG_VALUE.compareTo(POSITIVE_INFINITY) < 0);
156 assertTrue(BASE_LONG_VALUE.compareTo(SMALLER_DOUBLE_VALUE1) > 0);
157 assertTrue(BASE_LONG_VALUE.compareTo(SMALLER_DOUBLE_VALUE2) > 0);
158 assertTrue(BASE_LONG_VALUE.compareTo(MIN_DOUBLE_VALUE) > 0);
159 assertTrue(BASE_LONG_VALUE.compareTo(NEGATIVE_INFINITY) > 0);
160 }
161
162 /**
163 * Compare Long with Null state values
164 */
165 @Test
166 public void compareLongWithNull() {
167 assertTrue(BASE_LONG_VALUE.compareTo(NULL_VALUE) > 0);
168 }
169
170 /**
171 * Compare Long with String state values (should fail)
172 */
173 @Test(expected = StateValueTypeException.class)
174 public void compareLongWithString() {
175 BASE_LONG_VALUE.compareTo(BASE_STRING_VALUE);
176 }
177
178 // ------------------------------------------------------------------------
179 // Comparisons of Double state values
180 // ------------------------------------------------------------------------
181
182 /**
183 * Compare Double with Integer state values
184 */
185 @Test
186 public void compareDoubleWithInt() {
187 assertTrue(BASE_DOUBLE_VALUE.compareTo(BASE_INT_VALUE) == 0);
188 assertTrue(BASE_DOUBLE_VALUE.compareTo(BIGGER_INT_VALUE) < 0);
189 assertTrue(BASE_DOUBLE_VALUE.compareTo(SMALLER_INT_VALUE) > 0);
190 }
191
192 /**
193 * Compare Double with Long state values
194 */
195 @Test
196 public void compareDoubleWithLong() {
197 assertTrue(BASE_DOUBLE_VALUE.compareTo(BASE_LONG_VALUE) == 0);
198 assertTrue(BASE_DOUBLE_VALUE.compareTo(BIGGER_LONG_VALUE) < 0);
199 assertTrue(SMALLER_DOUBLE_VALUE2.compareTo(BASE_LONG_VALUE) < 0);
200 assertTrue(BASE_DOUBLE_VALUE.compareTo(MAX_LONG_VALUE) < 0);
201 assertTrue(BIGGER_DOUBLE_VALUE1.compareTo(SMALLER_LONG_VALUE) > 0);
202 assertTrue(BIGGER_DOUBLE_VALUE2.compareTo(BASE_LONG_VALUE) > 0);
203 assertTrue(BASE_DOUBLE_VALUE.compareTo(MIN_LONG_VALUE) > 0);
204 }
205
206 /**
207 * Compare Double state values together
208 */
209 @Test
210 public void compareDoubleWithDouble() {
211 assertTrue(BASE_DOUBLE_VALUE.compareTo(BASE_DOUBLE_VALUE) == 0);
212 assertTrue(BASE_DOUBLE_VALUE.compareTo(BIGGER_DOUBLE_VALUE2) < 0);
213 assertTrue(BASE_DOUBLE_VALUE.compareTo(MAX_DOUBLE_VALUE) < 0);
214 assertTrue(BASE_DOUBLE_VALUE.compareTo(SMALLER_DOUBLE_VALUE2) > 0);
215 assertTrue(BASE_DOUBLE_VALUE.compareTo(MIN_DOUBLE_VALUE) > 0);
216 }
217
218 /**
219 * Compare Double with Null state values
220 */
221 @Test
222 public void compareDoubleWithNull() {
223 /* NullValue.unboxDouble returns NaN */
224 assertTrue(BASE_DOUBLE_VALUE.compareTo(NULL_VALUE) < 0);
225 }
226
227 /**
228 * Compare Double with String state values (should fail)
229 */
230 @Test(expected = StateValueTypeException.class)
231 public void compareDoubleWithString() {
232 BASE_DOUBLE_VALUE.compareTo(BASE_STRING_VALUE);
233 }
234
235 // ------------------------------------------------------------------------
236 // Comparisons of String state values
237 // ------------------------------------------------------------------------
238
239 /**
240 * Compare String with Integer state values (should fail)
241 */
242 @Test(expected = StateValueTypeException.class)
243 public void compareStringWithInt() {
244 BASE_STRING_VALUE.compareTo(BASE_INT_VALUE);
245 }
246
247 /**
248 * Compare String with Long state values (should fail)
249 */
250 @Test(expected = StateValueTypeException.class)
251 public void compareStringWithLong() {
252 BASE_STRING_VALUE.compareTo(BASE_LONG_VALUE);
253 }
254
255 /**
256 * Compare String with Double state values (should fail)
257 */
258 @Test(expected = StateValueTypeException.class)
259 public void compareStringWithDouble() {
260 BASE_STRING_VALUE.compareTo(BASE_DOUBLE_VALUE);
261 }
262
263 /**
264 * Compare String state values together
265 */
266 @Test
267 public void compareStringWithString() {
268 assertTrue(BASE_STRING_VALUE.compareTo(BASE_STRING_VALUE) == 0);
269 assertTrue(BASE_STRING_VALUE.compareTo(SMALLER_STRING_VALUE) > 0);
270 assertTrue(BASE_STRING_VALUE.compareTo(BIGGER_STRING_VALUE) < 0);
271 }
272
273 /**
274 * Compare String with Null state values
275 */
276 @Test
277 public void compareStringWithNull() {
278 assertTrue(BASE_STRING_VALUE.compareTo(NULL_VALUE) > 0);
279 }
280
281 // ------------------------------------------------------------------------
282 // Comparisons of Null state values
283 // ------------------------------------------------------------------------
284
285 /**
286 * Compare Null with Integer state values
287 */
288 @Test
289 public void compareNullWithInt() {
290 assertTrue(NULL_VALUE.compareTo(BASE_INT_VALUE) < 0);
291 }
292
293 /**
294 * Compare Null with Long state values
295 */
296 @Test
297 public void compareNullWithLong() {
298 assertTrue(NULL_VALUE.compareTo(BASE_LONG_VALUE) < 0);
299 }
300
301 /**
302 * Compare Null with Double state values
303 */
304 @Test
305 public void compareNullWithDouble() {
306 /* NullValue.unboxDouble returns NaN */
307 assertTrue(NULL_VALUE.compareTo(BASE_DOUBLE_VALUE) > 0);
308 }
309
310 /**
311 * Compare Null with String state values
312 */
313 @Test
314 public void compareNullWithString() {
315 assertTrue(NULL_VALUE.compareTo(BASE_STRING_VALUE) < 0);
316 }
317
318 /**
319 * Compare Null state values together
320 */
321 @Test
322 public void compareNullWithNull() {
323 assertTrue(NULL_VALUE.compareTo(NULL_VALUE) == 0);
324 }
325
326 }
This page took 0.038958 seconds and 4 git commands to generate.