import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
+import java.nio.ByteBuffer;
import java.nio.ByteOrder;
+import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration;
import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition;
/**
* The class <code>IntegerDefinitionTest</code> contains tests for the class
* <code>{@link IntegerDefinition}</code>.
- *
+ *
* @author ematkho
* @version $Revision: 1.0 $
*/
private FloatDefinition fixture;
private FloatDefinition singleFixture;
- private FloatDefinition doubleFixture; //all the way.
+ private FloatDefinition doubleFixture; // all the way.
private FloatDeclaration parent;
+ @NonNull
private static final String fieldName = "float";
/**
* Perform pre-test initialization.
+ *
+ * @throws CTFReaderException
+ * error creating floats
*/
@Before
- public void setUp(){
+ public void setUp() throws CTFReaderException {
testFloat248();
testFloat5311();
}
@Test
- public void testFloat248() {
+ public void testFloat248() throws CTFReaderException {
parent = new FloatDeclaration(8, 24, ByteOrder.nativeOrder(), 0);
- singleFixture = parent.createDefinition(null, fieldName);
+ BitBuffer bb = create32BitFloatByteBuffer();
+ singleFixture = parent.createDefinition(null, fieldName, bb);
assertNotNull(singleFixture);
}
-
-
@Test
- public void testFloat5311() {
+ public void testFloat5311() throws CTFReaderException {
parent = new FloatDeclaration(11, 53, ByteOrder.nativeOrder(), 0);
- doubleFixture = parent.createDefinition(null, fieldName);
+ BitBuffer bb = create64BitFloatByteBuffer();
+ doubleFixture = parent.createDefinition(null, fieldName, bb);
assertNotNull(doubleFixture);
}
@Test
- public void testFloat32Bit(){
- for(int i = 1; i < 31 ; i++) {
- parent = new FloatDeclaration(i, 32-i, ByteOrder.nativeOrder(), 0);
- fixture = parent.createDefinition(null, fieldName);
+ public void testFloat32Bit() throws CTFReaderException {
+ for (int i = 1; i < 31; i++) {
+ parent = new FloatDeclaration(i, 32 - i, ByteOrder.nativeOrder(), 0);
+
+ fixture = parent.createDefinition(null, fieldName, create32BitFloatByteBuffer());
assertNotNull(fixture);
- fixture.setValue(2.0);
- assertTrue(fixture.toString().contains("2"));
+ assertEquals("test" + i, "2.0", fixture.toString());
}
}
@Test
- public void testFloat64Bit() throws CTFReaderException{
- for(int i = 1; i < 63 ; i++) {
- parent = new FloatDeclaration(i, 64-i, ByteOrder.nativeOrder(), 0);
- fixture = parent.createDefinition(null, fieldName);
+ public void testFloat64Bit() throws CTFReaderException {
+ for (int i = 1; i < 63; i++) {
+ parent = new FloatDeclaration(i, 64 - i, ByteOrder.nativeOrder(), 0);
+ fixture = parent.createDefinition(null, fieldName, create64BitFloatByteBuffer());
assertNotNull(fixture);
- BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128));
- fixture.read(input);
- fixture.setValue(2.0);
- assertTrue(fixture.toString().contains("2"));
+ if (i <= 32) {
+ assertEquals("test" + i, "2.0", fixture.toString());
+ } else if (i == 33) {
+ assertEquals("test" + i, "1.0", fixture.toString());
+ } else {
+ assertNotNull(fixture.getValue());
+ }
+
}
}
@Test
- public void testFloat48Bit() throws CTFReaderException{
+ public void testFloat48Bit() throws CTFReaderException {
parent = new FloatDeclaration(12, 32, ByteOrder.nativeOrder(), 0);
- fixture = parent.createDefinition(null, fieldName);
+ fixture = parent.createDefinition(null, fieldName, create64BitFloatByteBuffer());
assertNotNull(fixture);
- BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128));
- fixture.read(input);
-
- assertEquals(Double.NaN ,fixture.getValue(),0.1);
+ assertEquals(Double.NaN, fixture.getValue(), 0.1);
}
/**
*/
@Test
public void testGetDeclaration() {
- singleFixture.setValue(2.0);
FloatDeclaration result = singleFixture.getDeclaration();
assertNotNull(result);
}
*/
@Test
public void testGetValue() {
- singleFixture.setValue(2.0);
double result = singleFixture.getValue();
- assertEquals(2.0, result,0.1);
- }
-
- /**
- * Run the void read(BitBuffer) method test.
- * @throws CTFReaderException error
- */
- @Test
- public void testRead() throws CTFReaderException {
- singleFixture.setValue(2.0);
- BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128));
- singleFixture.read(input);
+ assertEquals(2.0, result, 0.1);
}
/**
*/
@Test
public void testToString() {
- singleFixture.setValue(222.22);
String result = singleFixture.toString();
assertNotNull(result);
+ assertEquals("2.0", result);
+ }
+
+ @NonNull
+ private static BitBuffer create32BitFloatByteBuffer() {
+ float[] data = new float[2];
+ data[0] = 2.0f;
+ data[1] = 3.14f;
+ ByteBuffer byb = ByteBuffer.allocate(128);
+ byb.mark();
+ byb.putFloat(data[0]);
+ byb.putFloat(data[1]);
+ byb.reset();
+ BitBuffer bb = new BitBuffer(byb);
+ return bb;
+ }
+
+ @NonNull
+ private static BitBuffer create64BitFloatByteBuffer() {
+ double[] data = new double[2];
+ data[0] = 2.0f;
+ data[1] = 3.14f;
+ ByteBuffer byb = ByteBuffer.allocate(128);
+ byb.mark();
+ byb.putDouble(data[0]);
+ byb.putDouble(data[1]);
+ byb.reset();
+ BitBuffer bb = new BitBuffer(byb);
+ return bb;
}
}