*
* Contributors:
* Matthew Khouzam - Initial API and implementation
+ * Marc-Andre Laperle - Add min/maximum for validation
*******************************************************************************/
package org.eclipse.linuxtools.ctf.core.tests.types;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import java.math.BigInteger;
import java.nio.ByteOrder;
import org.eclipse.linuxtools.ctf.core.event.types.Encoding;
import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
-import org.junit.After;
import org.junit.Before;
import org.junit.Test;
private IntegerDeclaration fixture;
- /**
- * Launch the test.
- *
- * @param args
- * the command line arguments
- */
- public static void main(String[] args) {
- new org.junit.runner.JUnitCore().run(IntegerDeclarationTest.class);
- }
-
/**
* Perform pre-test initialization.
*/
@Before
public void setUp() {
- fixture = new IntegerDeclaration(1, true, 1, ByteOrder.BIG_ENDIAN,
- Encoding.ASCII, null, 32);
- }
-
- /**
- * Perform post-test clean-up.
- */
- @After
- public void tearDown() {
- // Add additional tear down code here
+ fixture = IntegerDeclaration.createDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN,
+ Encoding.ASCII, "", 32);
}
/**
@Test
public void testIntegerDeclaration() {
int len = 1;
- boolean signed = true;
+ boolean signed = false;
int base = 1;
ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
Encoding encoding = Encoding.ASCII;
- IntegerDeclaration result = new IntegerDeclaration(len, signed, base,
- byteOrder, encoding, null, 16);
+ IntegerDeclaration result = IntegerDeclaration.createDeclaration(len, signed, base,
+ byteOrder, encoding, "", 16);
assertNotNull(result);
assertEquals(1, result.getBase());
assertEquals(false, result.isCharacter());
- String outputValue = "[declaration] integer["; //$NON-NLS-1$
+ String outputValue = "[declaration] integer[";
assertEquals(outputValue,
result.toString().substring(0, outputValue.length()));
assertEquals(1, result.getLength());
- assertEquals(true, result.isSigned());
+ assertEquals(false, result.isSigned());
+ }
+
+ /**
+ * Test that IntegerDeclaration throws when constructing a signed 1 bit declaration
+ */
+ @Test(expected = java.lang.IllegalArgumentException.class)
+ public void testIntegerDeclarationIllegalArgSignedBit() {
+ int len = 1;
+ boolean signed = true;
+ int base = 1;
+ ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
+ Encoding encoding = Encoding.ASCII;
+ IntegerDeclaration.createDeclaration(len, signed, base, byteOrder, encoding, "", 16);
+ }
+
+ /**
+ * Test that IntegerDeclaration throws when constructing a invalid length declaration
+ */
+ @Test(expected = java.lang.IllegalArgumentException.class)
+ public void testIntegerDeclarationIllegalArgBadLenght() {
+ int len = 0;
+ boolean signed = false;
+ int base = 1;
+ ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
+ Encoding encoding = Encoding.ASCII;
+ IntegerDeclaration.createDeclaration(len, signed, base, byteOrder, encoding, "", 16);
}
/**
public void testGetByteOrder() {
ByteOrder result = fixture.getByteOrder();
assertNotNull(result);
- assertEquals("BIG_ENDIAN", result.toString()); //$NON-NLS-1$
+ assertEquals("BIG_ENDIAN", result.toString());
}
/**
public void testGetEncoding() {
Encoding result = fixture.getEncoding();
assertNotNull(result);
- assertEquals("ASCII", result.name()); //$NON-NLS-1$
- assertEquals("ASCII", result.toString()); //$NON-NLS-1$
+ assertEquals("ASCII", result.name());
+ assertEquals("ASCII", result.toString());
assertEquals(1, result.ordinal());
}
*/
@Test
public void testIsCharacter_8bytes() {
- IntegerDeclaration fixture8 = new IntegerDeclaration(8, true, 1,
- ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8);
+ IntegerDeclaration fixture8 = IntegerDeclaration.createDeclaration(8, true, 1,
+ ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 8);
boolean result = fixture8.isCharacter();
assertEquals(true, result);
*/
@Test
public void testIsSigned_signed() {
- boolean result = fixture.isSigned();
+ IntegerDeclaration fixtureSigned = IntegerDeclaration.createDeclaration(2, true,
+ 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 8);
+ boolean result = fixtureSigned.isSigned();
assertEquals(true, result);
}
*/
@Test
public void testIsSigned_unsigned() {
- IntegerDeclaration fixture_unsigned = new IntegerDeclaration(1, false,
- 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8);
-
- boolean result = fixture_unsigned.isSigned();
+ boolean result = fixture.isSigned();
assertEquals(false, result);
}
public void testToString() {
String result = fixture.toString();
String trunc = result.substring(0, 22);
- assertEquals("[declaration] integer[", trunc); //$NON-NLS-1$
+ assertEquals("[declaration] integer[", trunc);
+ }
+
+ /**
+ * Run the long getMaxValue() method test.
+ */
+ @Test
+ public void testMaxValue() {
+ assertEquals(BigInteger.ONE, fixture.getMaxValue());
+
+ IntegerDeclaration signed8bit = IntegerDeclaration.createDeclaration(8, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
+ assertEquals(BigInteger.valueOf(127), signed8bit.getMaxValue());
+
+ IntegerDeclaration unsigned8bit = IntegerDeclaration.createDeclaration(8, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
+ assertEquals(BigInteger.valueOf(255), unsigned8bit.getMaxValue());
+
+ IntegerDeclaration signed32bit = IntegerDeclaration.createDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
+ assertEquals(BigInteger.valueOf(2147483647), signed32bit.getMaxValue());
+
+ IntegerDeclaration unsigned32bit = IntegerDeclaration.createDeclaration(32, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
+ assertEquals(BigInteger.valueOf(4294967295l), unsigned32bit.getMaxValue());
+
+ IntegerDeclaration signed64bit = IntegerDeclaration.createDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
+ assertEquals(BigInteger.valueOf(9223372036854775807L), signed64bit.getMaxValue());
+
+ IntegerDeclaration unsigned64bit = IntegerDeclaration.createDeclaration(64, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
+ assertEquals(BigInteger.valueOf(2).pow(64).subtract(BigInteger.ONE), unsigned64bit.getMaxValue());
+ }
+
+ /**
+ * Run the long getMinValue() method test.
+ */
+ @Test
+ public void testMinValue() {
+ assertEquals(BigInteger.ZERO, fixture.getMinValue());
+
+ IntegerDeclaration signed8bit = IntegerDeclaration.createDeclaration(8, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
+ assertEquals(BigInteger.valueOf(-128), signed8bit.getMinValue());
+
+ IntegerDeclaration unsigned8bit = IntegerDeclaration.createDeclaration(8, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
+ assertEquals(BigInteger.ZERO, unsigned8bit.getMinValue());
+
+ IntegerDeclaration signed32bit = IntegerDeclaration.createDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
+ assertEquals(BigInteger.valueOf(-2147483648), signed32bit.getMinValue());
+
+ IntegerDeclaration unsigned32bit = IntegerDeclaration.createDeclaration(32, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
+ assertEquals(BigInteger.ZERO, unsigned32bit.getMinValue());
+
+ IntegerDeclaration signed64bit = IntegerDeclaration.createDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
+ assertEquals(BigInteger.valueOf(-9223372036854775808L), signed64bit.getMinValue());
+
+ IntegerDeclaration unsigned64bit = IntegerDeclaration.createDeclaration(64, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
+ assertEquals(BigInteger.ZERO, unsigned64bit.getMinValue());
}
}
\ No newline at end of file