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
9 * Matthew Khouzam - Initial API and implementation
10 * Marc-Andre Laperle - Add min/maximum for validation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.ctf
.core
.tests
.types
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertNotEquals
;
17 import static org
.junit
.Assert
.assertNotNull
;
19 import java
.math
.BigInteger
;
20 import java
.nio
.ByteOrder
;
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
;
28 * The class <code>IntegerDeclarationTest</code> contains tests for the class
29 * <code>{@link IntegerDeclaration}</code>.
32 * @version $Revision: 1.0 $
34 public class IntegerDeclarationTest
{
36 private IntegerDeclaration fixture
;
39 * Perform pre-test initialization.
43 fixture
= IntegerDeclaration
.createDeclaration(1, false, 1, ByteOrder
.BIG_ENDIAN
,
44 Encoding
.ASCII
, "", 32);
48 * Run the IntegerDeclaration(int,boolean,int,ByteOrder,Encoding)
52 public void testIntegerDeclaration() {
54 boolean signed
= false;
56 ByteOrder byteOrder
= ByteOrder
.BIG_ENDIAN
;
57 Encoding encoding
= Encoding
.ASCII
;
59 IntegerDeclaration result
= IntegerDeclaration
.createDeclaration(len
, signed
, base
,
60 byteOrder
, encoding
, "", 16);
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());
73 * Test the factory part more rigorously to make sure there are no
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
) {
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
97 assertEquals(title
, bo
, intDec
.getByteOrder());
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());
112 * Run the int getBase() method test.
115 public void testGetBase() {
116 int result
= fixture
.getBase();
117 assertEquals(1, result
);
121 * Run the ByteOrder getByteOrder() method test.
124 public void testGetByteOrder() {
125 ByteOrder result
= fixture
.getByteOrder();
126 assertNotNull(result
);
127 assertEquals("BIG_ENDIAN", result
.toString());
131 * Run the Encoding getEncoding() method 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());
143 * Run the int getLength() method test.
146 public void testGetLength() {
147 int result
= fixture
.getLength();
148 assertEquals(1, result
);
152 * Run the boolean isCharacter() method test.
155 public void testIsCharacter() {
156 boolean result
= fixture
.isCharacter();
157 assertEquals(false, result
);
161 * Run the boolean isCharacter() method test.
164 public void testIsCharacter_8bytes() {
165 IntegerDeclaration fixture8
= IntegerDeclaration
.createDeclaration(8, true, 1,
166 ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 8);
168 boolean result
= fixture8
.isCharacter();
169 assertEquals(true, result
);
173 * Run the boolean isSigned() method 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
);
184 * Run the boolean isSigned() method test.
187 public void testIsSigned_unsigned() {
188 boolean result
= fixture
.isSigned();
189 assertEquals(false, result
);
193 * Run the String toString() method test.
196 public void testToString() {
197 String result
= fixture
.toString();
198 String trunc
= result
.substring(0, 22);
199 assertEquals("[declaration] integer[", trunc
);
203 * Run the long getMaxValue() method test.
206 public void testMaxValue() {
207 assertEquals(BigInteger
.ONE
, fixture
.getMaxValue());
209 IntegerDeclaration signed8bit
= IntegerDeclaration
.createDeclaration(8, true, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
210 assertEquals(BigInteger
.valueOf(127), signed8bit
.getMaxValue());
212 IntegerDeclaration unsigned8bit
= IntegerDeclaration
.createDeclaration(8, false, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
213 assertEquals(BigInteger
.valueOf(255), unsigned8bit
.getMaxValue());
215 IntegerDeclaration signed32bit
= IntegerDeclaration
.createDeclaration(32, true, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
216 assertEquals(BigInteger
.valueOf(2147483647), signed32bit
.getMaxValue());
218 IntegerDeclaration unsigned32bit
= IntegerDeclaration
.createDeclaration(32, false, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
219 assertEquals(BigInteger
.valueOf(4294967295l), unsigned32bit
.getMaxValue());
221 IntegerDeclaration signed64bit
= IntegerDeclaration
.createDeclaration(64, true, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
222 assertEquals(BigInteger
.valueOf(9223372036854775807L), signed64bit
.getMaxValue());
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());
229 * Run the long getMinValue() method test.
232 public void testMinValue() {
233 assertEquals(BigInteger
.ZERO
, fixture
.getMinValue());
235 IntegerDeclaration signed8bit
= IntegerDeclaration
.createDeclaration(8, true, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
236 assertEquals(BigInteger
.valueOf(-128), signed8bit
.getMinValue());
238 IntegerDeclaration unsigned8bit
= IntegerDeclaration
.createDeclaration(8, false, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
239 assertEquals(BigInteger
.ZERO
, unsigned8bit
.getMinValue());
241 IntegerDeclaration signed32bit
= IntegerDeclaration
.createDeclaration(32, true, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
242 assertEquals(BigInteger
.valueOf(-2147483648), signed32bit
.getMinValue());
244 IntegerDeclaration unsigned32bit
= IntegerDeclaration
.createDeclaration(32, false, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
245 assertEquals(BigInteger
.ZERO
, unsigned32bit
.getMinValue());
247 IntegerDeclaration signed64bit
= IntegerDeclaration
.createDeclaration(64, true, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
248 assertEquals(BigInteger
.valueOf(-9223372036854775808L), signed64bit
.getMinValue());
250 IntegerDeclaration unsigned64bit
= IntegerDeclaration
.createDeclaration(64, false, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
251 assertEquals(BigInteger
.ZERO
, unsigned64bit
.getMinValue());
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());
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
);
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
);