rcp: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / types / IntegerDeclarationTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Matthew Khouzam - Initial API and implementation
10 * Marc-Andre Laperle - Add min/maximum for validation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.ctf.core.tests.types;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertNotEquals;
17 import static org.junit.Assert.assertNotNull;
18
19 import java.math.BigInteger;
20 import java.nio.ByteOrder;
21
22 import org.eclipse.tracecompass.ctf.core.event.types.Encoding;
23 import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
24 import org.junit.Before;
25 import org.junit.Test;
26
27 /**
28 * The class <code>IntegerDeclarationTest</code> contains tests for the class
29 * <code>{@link IntegerDeclaration}</code>.
30 *
31 * @author ematkho
32 * @version $Revision: 1.0 $
33 */
34 public class IntegerDeclarationTest {
35
36 private IntegerDeclaration fixture;
37
38 /**
39 * Perform pre-test initialization.
40 */
41 @Before
42 public void setUp() {
43 fixture = IntegerDeclaration.createDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN,
44 Encoding.ASCII, "", 32);
45 }
46
47 /**
48 * Run the IntegerDeclaration(int,boolean,int,ByteOrder,Encoding)
49 * constructor test.
50 */
51 @Test
52 public void testIntegerDeclaration() {
53 int len = 1;
54 boolean signed = false;
55 int base = 1;
56 ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
57 Encoding encoding = Encoding.ASCII;
58
59 IntegerDeclaration result = IntegerDeclaration.createDeclaration(len, signed, base,
60 byteOrder, encoding, "", 16);
61
62 assertNotNull(result);
63 assertEquals(1, result.getBase());
64 assertEquals(false, result.isCharacter());
65 String outputValue = "[declaration] integer[";
66 assertEquals(outputValue,
67 result.toString().substring(0, outputValue.length()));
68 assertEquals(1, result.getLength());
69 assertEquals(false, result.isSigned());
70 }
71
72 /**
73 * Test the factory part more rigorously to make sure there are no
74 * regressions
75 */
76 @Test
77 public void testIntegerDeclarationBruteForce() {
78 ByteOrder[] bos = { ByteOrder.LITTLE_ENDIAN, ByteOrder.BIG_ENDIAN };
79 Encoding[] encodings = { Encoding.ASCII, Encoding.NONE, Encoding.UTF8 };
80 boolean[] signeds = { true, false }; // not a real word
81 String[] clocks = { "something", "" };
82 int[] bases = { 2, 4, 6, 8, 10, 12, 16 };
83 for (int len = 2; len < 65; len++) {
84 for (ByteOrder bo : bos) {
85 for (boolean signed : signeds) {
86 for (int base : bases) {
87 for (Encoding enc : encodings) {
88 for (String clock : clocks) {
89 assertNotNull(enc);
90 assertNotNull(clock);
91 IntegerDeclaration intDec = IntegerDeclaration.createDeclaration(len, signed, base, bo, enc, clock, 8);
92 String title = Integer.toString(len) + " " + bo + " " + signed + " " + base + " " + enc;
93 assertEquals(title, signed, intDec.isSigned());
94 assertEquals(title, base, intDec.getBase());
95 // at len 8 le and be are the same
96 if (len != 8) {
97 assertEquals(title, bo, intDec.getByteOrder());
98 }
99 assertEquals(title, len, intDec.getLength());
100 assertEquals(title, len, intDec.getMaximumSize());
101 assertEquals(title, clock, intDec.getClock());
102 assertEquals(title, !signed && len == 8, intDec.isUnsignedByte());
103 }
104 }
105 }
106 }
107 }
108 }
109 }
110
111 /**
112 * Run the int getBase() method test.
113 */
114 @Test
115 public void testGetBase() {
116 int result = fixture.getBase();
117 assertEquals(1, result);
118 }
119
120 /**
121 * Run the ByteOrder getByteOrder() method test.
122 */
123 @Test
124 public void testGetByteOrder() {
125 ByteOrder result = fixture.getByteOrder();
126 assertNotNull(result);
127 assertEquals("BIG_ENDIAN", result.toString());
128 }
129
130 /**
131 * Run the Encoding getEncoding() method test.
132 */
133 @Test
134 public void testGetEncoding() {
135 Encoding result = fixture.getEncoding();
136 assertNotNull(result);
137 assertEquals("ASCII", result.name());
138 assertEquals("ASCII", result.toString());
139 assertEquals(1, result.ordinal());
140 }
141
142 /**
143 * Run the int getLength() method test.
144 */
145 @Test
146 public void testGetLength() {
147 int result = fixture.getLength();
148 assertEquals(1, result);
149 }
150
151 /**
152 * Run the boolean isCharacter() method test.
153 */
154 @Test
155 public void testIsCharacter() {
156 boolean result = fixture.isCharacter();
157 assertEquals(false, result);
158 }
159
160 /**
161 * Run the boolean isCharacter() method test.
162 */
163 @Test
164 public void testIsCharacter_8bytes() {
165 IntegerDeclaration fixture8 = IntegerDeclaration.createDeclaration(8, true, 1,
166 ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 8);
167
168 boolean result = fixture8.isCharacter();
169 assertEquals(true, result);
170 }
171
172 /**
173 * Run the boolean isSigned() method test.
174 */
175 @Test
176 public void testIsSigned_signed() {
177 IntegerDeclaration fixtureSigned = IntegerDeclaration.createDeclaration(2, true,
178 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 8);
179 boolean result = fixtureSigned.isSigned();
180 assertEquals(true, result);
181 }
182
183 /**
184 * Run the boolean isSigned() method test.
185 */
186 @Test
187 public void testIsSigned_unsigned() {
188 boolean result = fixture.isSigned();
189 assertEquals(false, result);
190 }
191
192 /**
193 * Run the String toString() method test.
194 */
195 @Test
196 public void testToString() {
197 String result = fixture.toString();
198 String trunc = result.substring(0, 22);
199 assertEquals("[declaration] integer[", trunc);
200 }
201
202 /**
203 * Run the long getMaxValue() method test.
204 */
205 @Test
206 public void testMaxValue() {
207 assertEquals(BigInteger.ONE, fixture.getMaxValue());
208
209 IntegerDeclaration signed8bit = IntegerDeclaration.createDeclaration(8, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
210 assertEquals(BigInteger.valueOf(127), signed8bit.getMaxValue());
211
212 IntegerDeclaration unsigned8bit = IntegerDeclaration.createDeclaration(8, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
213 assertEquals(BigInteger.valueOf(255), unsigned8bit.getMaxValue());
214
215 IntegerDeclaration signed32bit = IntegerDeclaration.createDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
216 assertEquals(BigInteger.valueOf(2147483647), signed32bit.getMaxValue());
217
218 IntegerDeclaration unsigned32bit = IntegerDeclaration.createDeclaration(32, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
219 assertEquals(BigInteger.valueOf(4294967295l), unsigned32bit.getMaxValue());
220
221 IntegerDeclaration signed64bit = IntegerDeclaration.createDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
222 assertEquals(BigInteger.valueOf(9223372036854775807L), signed64bit.getMaxValue());
223
224 IntegerDeclaration unsigned64bit = IntegerDeclaration.createDeclaration(64, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
225 assertEquals(BigInteger.valueOf(2).pow(64).subtract(BigInteger.ONE), unsigned64bit.getMaxValue());
226 }
227
228 /**
229 * Run the long getMinValue() method test.
230 */
231 @Test
232 public void testMinValue() {
233 assertEquals(BigInteger.ZERO, fixture.getMinValue());
234
235 IntegerDeclaration signed8bit = IntegerDeclaration.createDeclaration(8, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
236 assertEquals(BigInteger.valueOf(-128), signed8bit.getMinValue());
237
238 IntegerDeclaration unsigned8bit = IntegerDeclaration.createDeclaration(8, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
239 assertEquals(BigInteger.ZERO, unsigned8bit.getMinValue());
240
241 IntegerDeclaration signed32bit = IntegerDeclaration.createDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
242 assertEquals(BigInteger.valueOf(-2147483648), signed32bit.getMinValue());
243
244 IntegerDeclaration unsigned32bit = IntegerDeclaration.createDeclaration(32, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
245 assertEquals(BigInteger.ZERO, unsigned32bit.getMinValue());
246
247 IntegerDeclaration signed64bit = IntegerDeclaration.createDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
248 assertEquals(BigInteger.valueOf(-9223372036854775808L), signed64bit.getMinValue());
249
250 IntegerDeclaration unsigned64bit = IntegerDeclaration.createDeclaration(64, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
251 assertEquals(BigInteger.ZERO, unsigned64bit.getMinValue());
252 }
253
254 /**
255 * Test the hashcode
256 */
257 @Test
258 public void hashcodeTest() {
259 IntegerDeclaration a = IntegerDeclaration.createDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 32);
260 IntegerDeclaration i = IntegerDeclaration.createDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 32);
261 assertEquals(a.hashCode(), i.hashCode());
262 assertEquals(a.hashCode(), a.hashCode());
263 }
264
265 /**
266 * Test the equals
267 */
268 @Test
269 public void equalsTest() {
270 IntegerDeclaration a = IntegerDeclaration.createDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 32);
271 IntegerDeclaration b = IntegerDeclaration.createDeclaration(8, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 32);
272 IntegerDeclaration c = IntegerDeclaration.createDeclaration(32, true, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 32);
273 IntegerDeclaration d = IntegerDeclaration.createDeclaration(32, false, 16, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 32);
274 IntegerDeclaration e = IntegerDeclaration.createDeclaration(32, false, 10, ByteOrder.LITTLE_ENDIAN, Encoding.NONE, "", 32);
275 IntegerDeclaration f = IntegerDeclaration.createDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.UTF8, "", 32);
276 IntegerDeclaration g = IntegerDeclaration.createDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "hi", 32);
277 IntegerDeclaration h = IntegerDeclaration.createDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 16);
278 IntegerDeclaration i = IntegerDeclaration.createDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 32);
279 assertNotEquals(a, null);
280 assertNotEquals(a, new Object());
281 assertNotEquals(a, b);
282 assertNotEquals(a, c);
283 assertNotEquals(a, d);
284 assertNotEquals(a, e);
285 assertNotEquals(a, f);
286 assertNotEquals(a, g);
287 assertNotEquals(a, h);
288 assertEquals(a, i);
289 assertNotEquals(b, a);
290 assertNotEquals(c, a);
291 assertNotEquals(d, a);
292 assertNotEquals(e, a);
293 assertNotEquals(f, a);
294 assertNotEquals(g, a);
295 assertNotEquals(h, a);
296 assertEquals(i, a);
297 assertEquals(a, a);
298 }
299
300 }
This page took 0.045438 seconds and 5 git commands to generate.