ctf: Make events immutable
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / types / IntegerDeclarationTest.java
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
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 declaration
73 */
74 @Test(expected = java.lang.IllegalArgumentException.class)
75 public void testIntegerDeclarationIllegalArgSignedBit() {
76 int len = 1;
77 boolean signed = true;
78 int base = 1;
79 ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
80 Encoding encoding = Encoding.ASCII;
81 IntegerDeclaration.createDeclaration(len, signed, base, byteOrder, encoding, "", 16);
82 }
83
84 /**
85 * Test that IntegerDeclaration throws when constructing a invalid length declaration
86 */
87 @Test(expected = java.lang.IllegalArgumentException.class)
88 public void testIntegerDeclarationIllegalArgBadLenght() {
89 int len = 0;
90 boolean signed = false;
91 int base = 1;
92 ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
93 Encoding encoding = Encoding.ASCII;
94 IntegerDeclaration.createDeclaration(len, signed, base, byteOrder, encoding, "", 16);
95 }
96
97 /**
98 * Run the int getBase() method test.
99 */
100 @Test
101 public void testGetBase() {
102 int result = fixture.getBase();
103 assertEquals(1, result);
104 }
105
106 /**
107 * Run the ByteOrder getByteOrder() method test.
108 */
109 @Test
110 public void testGetByteOrder() {
111 ByteOrder result = fixture.getByteOrder();
112 assertNotNull(result);
113 assertEquals("BIG_ENDIAN", result.toString());
114 }
115
116 /**
117 * Run the Encoding getEncoding() method test.
118 */
119 @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());
126 }
127
128 /**
129 * Run the int getLength() method test.
130 */
131 @Test
132 public void testGetLength() {
133 int result = fixture.getLength();
134 assertEquals(1, result);
135 }
136
137 /**
138 * Run the boolean isCharacter() method test.
139 */
140 @Test
141 public void testIsCharacter() {
142 boolean result = fixture.isCharacter();
143 assertEquals(false, result);
144 }
145
146 /**
147 * Run the boolean isCharacter() method test.
148 */
149 @Test
150 public void testIsCharacter_8bytes() {
151 IntegerDeclaration fixture8 = IntegerDeclaration.createDeclaration(8, true, 1,
152 ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 8);
153
154 boolean result = fixture8.isCharacter();
155 assertEquals(true, result);
156 }
157
158 /**
159 * Run the boolean isSigned() method test.
160 */
161 @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);
167 }
168
169 /**
170 * Run the boolean isSigned() method test.
171 */
172 @Test
173 public void testIsSigned_unsigned() {
174 boolean result = fixture.isSigned();
175 assertEquals(false, result);
176 }
177
178
179 /**
180 * Run the String toString() method test.
181 */
182 @Test
183 public void testToString() {
184 String result = fixture.toString();
185 String trunc = result.substring(0, 22);
186 assertEquals("[declaration] integer[", trunc);
187 }
188
189 /**
190 * Run the long getMaxValue() method test.
191 */
192 @Test
193 public void testMaxValue() {
194 assertEquals(BigInteger.ONE, fixture.getMaxValue());
195
196 IntegerDeclaration signed8bit = IntegerDeclaration.createDeclaration(8, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
197 assertEquals(BigInteger.valueOf(127), signed8bit.getMaxValue());
198
199 IntegerDeclaration unsigned8bit = IntegerDeclaration.createDeclaration(8, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
200 assertEquals(BigInteger.valueOf(255), unsigned8bit.getMaxValue());
201
202 IntegerDeclaration signed32bit = IntegerDeclaration.createDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
203 assertEquals(BigInteger.valueOf(2147483647), signed32bit.getMaxValue());
204
205 IntegerDeclaration unsigned32bit = IntegerDeclaration.createDeclaration(32, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
206 assertEquals(BigInteger.valueOf(4294967295l), unsigned32bit.getMaxValue());
207
208 IntegerDeclaration signed64bit = IntegerDeclaration.createDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
209 assertEquals(BigInteger.valueOf(9223372036854775807L), signed64bit.getMaxValue());
210
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());
213 }
214
215 /**
216 * Run the long getMinValue() method test.
217 */
218 @Test
219 public void testMinValue() {
220 assertEquals(BigInteger.ZERO, fixture.getMinValue());
221
222 IntegerDeclaration signed8bit = IntegerDeclaration.createDeclaration(8, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
223 assertEquals(BigInteger.valueOf(-128), signed8bit.getMinValue());
224
225 IntegerDeclaration unsigned8bit = IntegerDeclaration.createDeclaration(8, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
226 assertEquals(BigInteger.ZERO, unsigned8bit.getMinValue());
227
228 IntegerDeclaration signed32bit = IntegerDeclaration.createDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
229 assertEquals(BigInteger.valueOf(-2147483648), signed32bit.getMinValue());
230
231 IntegerDeclaration unsigned32bit = IntegerDeclaration.createDeclaration(32, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
232 assertEquals(BigInteger.ZERO, unsigned32bit.getMinValue());
233
234 IntegerDeclaration signed64bit = IntegerDeclaration.createDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
235 assertEquals(BigInteger.valueOf(-9223372036854775808L), signed64bit.getMinValue());
236
237 IntegerDeclaration unsigned64bit = IntegerDeclaration.createDeclaration(64, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
238 assertEquals(BigInteger.ZERO, unsigned64bit.getMinValue());
239 }
240 }
This page took 0.037985 seconds and 5 git commands to generate.