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