Commit | Line | Data |
---|---|---|
4bd7f2db | 1 | /******************************************************************************* |
0f231de2 | 2 | * Copyright (c) 2013, 2014 Ericsson |
4bd7f2db AM |
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 | |
4311ac8b | 10 | * Marc-Andre Laperle - Add min/maximum for validation |
4bd7f2db AM |
11 | *******************************************************************************/ |
12 | ||
866e5b51 FC |
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 | ||
4311ac8b | 18 | import java.math.BigInteger; |
866e5b51 FC |
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; | |
866e5b51 FC |
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>. | |
284fdee8 | 29 | * |
866e5b51 FC |
30 | * @author ematkho |
31 | * @version $Revision: 1.0 $ | |
32 | */ | |
33 | public class IntegerDeclarationTest { | |
34 | ||
35 | private IntegerDeclaration fixture; | |
36 | ||
866e5b51 FC |
37 | /** |
38 | * Perform pre-test initialization. | |
39 | */ | |
40 | @Before | |
41 | public void setUp() { | |
a4fa4e36 MK |
42 | fixture = IntegerDeclaration.createDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, |
43 | Encoding.ASCII, "", 32); | |
866e5b51 FC |
44 | } |
45 | ||
866e5b51 FC |
46 | /** |
47 | * Run the IntegerDeclaration(int,boolean,int,ByteOrder,Encoding) | |
48 | * constructor test. | |
49 | */ | |
50 | @Test | |
51 | public void testIntegerDeclaration() { | |
52 | int len = 1; | |
4311ac8b | 53 | boolean signed = false; |
866e5b51 FC |
54 | int base = 1; |
55 | ByteOrder byteOrder = ByteOrder.BIG_ENDIAN; | |
56 | Encoding encoding = Encoding.ASCII; | |
57 | ||
a4fa4e36 MK |
58 | IntegerDeclaration result = IntegerDeclaration.createDeclaration(len, signed, base, |
59 | byteOrder, encoding, "", 16); | |
866e5b51 FC |
60 | |
61 | assertNotNull(result); | |
62 | assertEquals(1, result.getBase()); | |
63 | assertEquals(false, result.isCharacter()); | |
4a9c1f07 | 64 | String outputValue = "[declaration] integer["; |
866e5b51 FC |
65 | assertEquals(outputValue, |
66 | result.toString().substring(0, outputValue.length())); | |
67 | assertEquals(1, result.getLength()); | |
4311ac8b MAL |
68 | assertEquals(false, result.isSigned()); |
69 | } | |
70 | ||
71 | /** | |
0f231de2 MK |
72 | * Test that IntegerDeclaration throws when constructing a signed 1 bit |
73 | * declaration | |
4311ac8b MAL |
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; | |
a4fa4e36 | 82 | IntegerDeclaration.createDeclaration(len, signed, base, byteOrder, encoding, "", 16); |
4311ac8b MAL |
83 | } |
84 | ||
85 | /** | |
0f231de2 MK |
86 | * Test that IntegerDeclaration throws when constructing a invalid length |
87 | * declaration | |
4311ac8b MAL |
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; | |
a4fa4e36 | 96 | IntegerDeclaration.createDeclaration(len, signed, base, byteOrder, encoding, "", 16); |
866e5b51 FC |
97 | } |
98 | ||
0f231de2 MK |
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 | ||
866e5b51 FC |
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); | |
4a9c1f07 | 154 | assertEquals("BIG_ENDIAN", result.toString()); |
866e5b51 FC |
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); | |
4a9c1f07 AM |
164 | assertEquals("ASCII", result.name()); |
165 | assertEquals("ASCII", result.toString()); | |
866e5b51 FC |
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() { | |
a4fa4e36 MK |
192 | IntegerDeclaration fixture8 = IntegerDeclaration.createDeclaration(8, true, 1, |
193 | ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 8); | |
866e5b51 FC |
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() { | |
a4fa4e36 MK |
204 | IntegerDeclaration fixtureSigned = IntegerDeclaration.createDeclaration(2, true, |
205 | 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 8); | |
4311ac8b | 206 | boolean result = fixtureSigned.isSigned(); |
866e5b51 FC |
207 | assertEquals(true, result); |
208 | } | |
209 | ||
210 | /** | |
211 | * Run the boolean isSigned() method test. | |
212 | */ | |
213 | @Test | |
214 | public void testIsSigned_unsigned() { | |
4311ac8b | 215 | boolean result = fixture.isSigned(); |
866e5b51 FC |
216 | assertEquals(false, result); |
217 | } | |
218 | ||
866e5b51 FC |
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); | |
4a9c1f07 | 226 | assertEquals("[declaration] integer[", trunc); |
866e5b51 | 227 | } |
4311ac8b MAL |
228 | |
229 | /** | |
230 | * Run the long getMaxValue() method test. | |
231 | */ | |
232 | @Test | |
233 | public void testMaxValue() { | |
234 | assertEquals(BigInteger.ONE, fixture.getMaxValue()); | |
235 | ||
a4fa4e36 | 236 | IntegerDeclaration signed8bit = IntegerDeclaration.createDeclaration(8, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); |
4311ac8b MAL |
237 | assertEquals(BigInteger.valueOf(127), signed8bit.getMaxValue()); |
238 | ||
a4fa4e36 | 239 | IntegerDeclaration unsigned8bit = IntegerDeclaration.createDeclaration(8, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); |
4311ac8b MAL |
240 | assertEquals(BigInteger.valueOf(255), unsigned8bit.getMaxValue()); |
241 | ||
a4fa4e36 | 242 | IntegerDeclaration signed32bit = IntegerDeclaration.createDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); |
4311ac8b MAL |
243 | assertEquals(BigInteger.valueOf(2147483647), signed32bit.getMaxValue()); |
244 | ||
a4fa4e36 | 245 | IntegerDeclaration unsigned32bit = IntegerDeclaration.createDeclaration(32, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); |
4311ac8b MAL |
246 | assertEquals(BigInteger.valueOf(4294967295l), unsigned32bit.getMaxValue()); |
247 | ||
a4fa4e36 | 248 | IntegerDeclaration signed64bit = IntegerDeclaration.createDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); |
4311ac8b MAL |
249 | assertEquals(BigInteger.valueOf(9223372036854775807L), signed64bit.getMaxValue()); |
250 | ||
a4fa4e36 | 251 | IntegerDeclaration unsigned64bit = IntegerDeclaration.createDeclaration(64, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); |
4311ac8b MAL |
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 | ||
a4fa4e36 | 262 | IntegerDeclaration signed8bit = IntegerDeclaration.createDeclaration(8, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); |
4311ac8b MAL |
263 | assertEquals(BigInteger.valueOf(-128), signed8bit.getMinValue()); |
264 | ||
a4fa4e36 | 265 | IntegerDeclaration unsigned8bit = IntegerDeclaration.createDeclaration(8, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); |
4311ac8b MAL |
266 | assertEquals(BigInteger.ZERO, unsigned8bit.getMinValue()); |
267 | ||
a4fa4e36 | 268 | IntegerDeclaration signed32bit = IntegerDeclaration.createDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); |
4311ac8b MAL |
269 | assertEquals(BigInteger.valueOf(-2147483648), signed32bit.getMinValue()); |
270 | ||
a4fa4e36 | 271 | IntegerDeclaration unsigned32bit = IntegerDeclaration.createDeclaration(32, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); |
4311ac8b MAL |
272 | assertEquals(BigInteger.ZERO, unsigned32bit.getMinValue()); |
273 | ||
a4fa4e36 | 274 | IntegerDeclaration signed64bit = IntegerDeclaration.createDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); |
4311ac8b MAL |
275 | assertEquals(BigInteger.valueOf(-9223372036854775808L), signed64bit.getMinValue()); |
276 | ||
a4fa4e36 | 277 | IntegerDeclaration unsigned64bit = IntegerDeclaration.createDeclaration(64, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32); |
4311ac8b MAL |
278 | assertEquals(BigInteger.ZERO, unsigned64bit.getMinValue()); |
279 | } | |
866e5b51 | 280 | } |