1 /*******************************************************************************
2 * Copyright (c) 2013 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 declaration
74 @Test(expected
= java
.lang
.IllegalArgumentException
.class)
75 public void testIntegerDeclarationIllegalArgSignedBit() {
77 boolean signed
= true;
79 ByteOrder byteOrder
= ByteOrder
.BIG_ENDIAN
;
80 Encoding encoding
= Encoding
.ASCII
;
81 IntegerDeclaration
.createDeclaration(len
, signed
, base
, byteOrder
, encoding
, "", 16);
85 * Test that IntegerDeclaration throws when constructing a invalid length declaration
87 @Test(expected
= java
.lang
.IllegalArgumentException
.class)
88 public void testIntegerDeclarationIllegalArgBadLenght() {
90 boolean signed
= false;
92 ByteOrder byteOrder
= ByteOrder
.BIG_ENDIAN
;
93 Encoding encoding
= Encoding
.ASCII
;
94 IntegerDeclaration
.createDeclaration(len
, signed
, base
, byteOrder
, encoding
, "", 16);
98 * Run the int getBase() method test.
101 public void testGetBase() {
102 int result
= fixture
.getBase();
103 assertEquals(1, result
);
107 * Run the ByteOrder getByteOrder() method test.
110 public void testGetByteOrder() {
111 ByteOrder result
= fixture
.getByteOrder();
112 assertNotNull(result
);
113 assertEquals("BIG_ENDIAN", result
.toString());
117 * Run the Encoding getEncoding() method test.
120 public void testGetEncoding() {
121 Encoding result
= fixture
.getEncoding();
122 assertNotNull(result
);
123 assertEquals("ASCII", result
.name());
124 assertEquals("ASCII", result
.toString());
125 assertEquals(1, result
.ordinal());
129 * Run the int getLength() method test.
132 public void testGetLength() {
133 int result
= fixture
.getLength();
134 assertEquals(1, result
);
138 * Run the boolean isCharacter() method test.
141 public void testIsCharacter() {
142 boolean result
= fixture
.isCharacter();
143 assertEquals(false, result
);
147 * Run the boolean isCharacter() method test.
150 public void testIsCharacter_8bytes() {
151 IntegerDeclaration fixture8
= IntegerDeclaration
.createDeclaration(8, true, 1,
152 ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 8);
154 boolean result
= fixture8
.isCharacter();
155 assertEquals(true, result
);
159 * Run the boolean isSigned() method test.
162 public void testIsSigned_signed() {
163 IntegerDeclaration fixtureSigned
= IntegerDeclaration
.createDeclaration(2, true,
164 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 8);
165 boolean result
= fixtureSigned
.isSigned();
166 assertEquals(true, result
);
170 * Run the boolean isSigned() method test.
173 public void testIsSigned_unsigned() {
174 boolean result
= fixture
.isSigned();
175 assertEquals(false, result
);
180 * Run the String toString() method test.
183 public void testToString() {
184 String result
= fixture
.toString();
185 String trunc
= result
.substring(0, 22);
186 assertEquals("[declaration] integer[", trunc
);
190 * Run the long getMaxValue() method test.
193 public void testMaxValue() {
194 assertEquals(BigInteger
.ONE
, fixture
.getMaxValue());
196 IntegerDeclaration signed8bit
= IntegerDeclaration
.createDeclaration(8, true, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
197 assertEquals(BigInteger
.valueOf(127), signed8bit
.getMaxValue());
199 IntegerDeclaration unsigned8bit
= IntegerDeclaration
.createDeclaration(8, false, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
200 assertEquals(BigInteger
.valueOf(255), unsigned8bit
.getMaxValue());
202 IntegerDeclaration signed32bit
= IntegerDeclaration
.createDeclaration(32, true, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
203 assertEquals(BigInteger
.valueOf(2147483647), signed32bit
.getMaxValue());
205 IntegerDeclaration unsigned32bit
= IntegerDeclaration
.createDeclaration(32, false, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
206 assertEquals(BigInteger
.valueOf(4294967295l), unsigned32bit
.getMaxValue());
208 IntegerDeclaration signed64bit
= IntegerDeclaration
.createDeclaration(64, true, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
209 assertEquals(BigInteger
.valueOf(9223372036854775807L), signed64bit
.getMaxValue());
211 IntegerDeclaration unsigned64bit
= IntegerDeclaration
.createDeclaration(64, false, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
212 assertEquals(BigInteger
.valueOf(2).pow(64).subtract(BigInteger
.ONE
), unsigned64bit
.getMaxValue());
216 * Run the long getMinValue() method test.
219 public void testMinValue() {
220 assertEquals(BigInteger
.ZERO
, fixture
.getMinValue());
222 IntegerDeclaration signed8bit
= IntegerDeclaration
.createDeclaration(8, true, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
223 assertEquals(BigInteger
.valueOf(-128), signed8bit
.getMinValue());
225 IntegerDeclaration unsigned8bit
= IntegerDeclaration
.createDeclaration(8, false, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
226 assertEquals(BigInteger
.ZERO
, unsigned8bit
.getMinValue());
228 IntegerDeclaration signed32bit
= IntegerDeclaration
.createDeclaration(32, true, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
229 assertEquals(BigInteger
.valueOf(-2147483648), signed32bit
.getMinValue());
231 IntegerDeclaration unsigned32bit
= IntegerDeclaration
.createDeclaration(32, false, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
232 assertEquals(BigInteger
.ZERO
, unsigned32bit
.getMinValue());
234 IntegerDeclaration signed64bit
= IntegerDeclaration
.createDeclaration(64, true, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
235 assertEquals(BigInteger
.valueOf(-9223372036854775808L), signed64bit
.getMinValue());
237 IntegerDeclaration unsigned64bit
= IntegerDeclaration
.createDeclaration(64, false, 1, ByteOrder
.BIG_ENDIAN
, Encoding
.ASCII
, "", 32);
238 assertEquals(BigInteger
.ZERO
, unsigned64bit
.getMinValue());