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
.linuxtools
.ctf
.core
.tests
.types
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertNotNull
;
18 import java
.math
.BigInteger
;
19 import java
.nio
.ByteOrder
;
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
;
27 * The class <code>IntegerDeclarationTest</code> contains tests for the class
28 * <code>{@link IntegerDeclaration}</code>.
31 * @version $Revision: 1.0 $
33 public class IntegerDeclarationTest
{
35 private IntegerDeclaration fixture
;
38 * Perform pre-test initialization.
42 fixture
= IntegerDeclaration
.createDeclaration(1, false, 1, ByteOrder
.BIG_ENDIAN
,
43 Encoding
.ASCII
, "", 32);
47 * Run the IntegerDeclaration(int,boolean,int,ByteOrder,Encoding)
51 public void testIntegerDeclaration() {
53 boolean signed
= false;
55 ByteOrder byteOrder
= ByteOrder
.BIG_ENDIAN
;
56 Encoding encoding
= Encoding
.ASCII
;
58 IntegerDeclaration result
= IntegerDeclaration
.createDeclaration(len
, signed
, base
,
59 byteOrder
, encoding
, "", 16);
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());
72 * Test that IntegerDeclaration throws when constructing a signed 1 bit
75 @Test(expected
= java
.lang
.IllegalArgumentException
.class)
76 public void testIntegerDeclarationIllegalArgSignedBit() {
78 boolean signed
= true;
80 ByteOrder byteOrder
= ByteOrder
.BIG_ENDIAN
;
81 Encoding encoding
= Encoding
.ASCII
;
82 IntegerDeclaration
.createDeclaration(len
, signed
, base
, byteOrder
, encoding
, "", 16);
86 * Test that IntegerDeclaration throws when constructing a invalid length
89 @Test(expected
= java
.lang
.IllegalArgumentException
.class)
90 public void testIntegerDeclarationIllegalArgBadLenght() {
92 boolean signed
= false;
94 ByteOrder byteOrder
= ByteOrder
.BIG_ENDIAN
;
95 Encoding encoding
= Encoding
.ASCII
;
96 IntegerDeclaration
.createDeclaration(len
, signed
, base
, byteOrder
, encoding
, "", 16);
100 * Test the factory part more rigorously to make sure there are no
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
) {
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
124 assertEquals(title
, bo
, intDec
.getByteOrder());
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());
139 * Run the int getBase() method test.
142 public void testGetBase() {
143 int result
= fixture
.getBase();
144 assertEquals(1, result
);
148 * Run the ByteOrder getByteOrder() method test.
151 public void testGetByteOrder() {
152 ByteOrder result
= fixture
.getByteOrder();
153 assertNotNull(result
);
154 assertEquals("BIG_ENDIAN", result
.toString());
158 * Run the Encoding getEncoding() method 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());
170 * Run the int getLength() method test.
173 public void testGetLength() {
174 int result
= fixture
.getLength();
175 assertEquals(1, result
);
179 * Run the boolean isCharacter() method test.
182 public void testIsCharacter() {
183 boolean result
= fixture
.isCharacter();
184 assertEquals(false, result
);
188 * Run the boolean isCharacter() method test.
191 public void testIsCharacter_8bytes() {
192 IntegerDeclaration fixture8
= IntegerDeclaration
.createDeclaration(8, true, 1,
193 ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 8);
195 boolean result
= fixture8
.isCharacter();
196 assertEquals(true, result
);
200 * Run the boolean isSigned() method 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
);
211 * Run the boolean isSigned() method test.
214 public void testIsSigned_unsigned() {
215 boolean result
= fixture
.isSigned();
216 assertEquals(false, result
);
220 * Run the String toString() method test.
223 public void testToString() {
224 String result
= fixture
.toString();
225 String trunc
= result
.substring(0, 22);
226 assertEquals("[declaration] integer[", trunc
);
230 * Run the long getMaxValue() method test.
233 public void testMaxValue() {
234 assertEquals(BigInteger
.ONE
, fixture
.getMaxValue());
236 IntegerDeclaration signed8bit
= IntegerDeclaration
.createDeclaration(8, true, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
237 assertEquals(BigInteger
.valueOf(127), signed8bit
.getMaxValue());
239 IntegerDeclaration unsigned8bit
= IntegerDeclaration
.createDeclaration(8, false, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
240 assertEquals(BigInteger
.valueOf(255), unsigned8bit
.getMaxValue());
242 IntegerDeclaration signed32bit
= IntegerDeclaration
.createDeclaration(32, true, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
243 assertEquals(BigInteger
.valueOf(2147483647), signed32bit
.getMaxValue());
245 IntegerDeclaration unsigned32bit
= IntegerDeclaration
.createDeclaration(32, false, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
246 assertEquals(BigInteger
.valueOf(4294967295l), unsigned32bit
.getMaxValue());
248 IntegerDeclaration signed64bit
= IntegerDeclaration
.createDeclaration(64, true, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
249 assertEquals(BigInteger
.valueOf(9223372036854775807L), signed64bit
.getMaxValue());
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());
256 * Run the long getMinValue() method test.
259 public void testMinValue() {
260 assertEquals(BigInteger
.ZERO
, fixture
.getMinValue());
262 IntegerDeclaration signed8bit
= IntegerDeclaration
.createDeclaration(8, true, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
263 assertEquals(BigInteger
.valueOf(-128), signed8bit
.getMinValue());
265 IntegerDeclaration unsigned8bit
= IntegerDeclaration
.createDeclaration(8, false, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
266 assertEquals(BigInteger
.ZERO
, unsigned8bit
.getMinValue());
268 IntegerDeclaration signed32bit
= IntegerDeclaration
.createDeclaration(32, true, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
269 assertEquals(BigInteger
.valueOf(-2147483648), signed32bit
.getMinValue());
271 IntegerDeclaration unsigned32bit
= IntegerDeclaration
.createDeclaration(32, false, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
272 assertEquals(BigInteger
.ZERO
, unsigned32bit
.getMinValue());
274 IntegerDeclaration signed64bit
= IntegerDeclaration
.createDeclaration(64, true, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
275 assertEquals(BigInteger
.valueOf(-9223372036854775808L), signed64bit
.getMinValue());
277 IntegerDeclaration unsigned64bit
= IntegerDeclaration
.createDeclaration(64, false, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
278 assertEquals(BigInteger
.ZERO
, unsigned64bit
.getMinValue());