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 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.ctf
.core
.tests
.types
;
14 import static org
.junit
.Assert
.assertEquals
;
15 import static org
.junit
.Assert
.assertNotNull
;
17 import java
.nio
.ByteBuffer
;
18 import java
.nio
.ByteOrder
;
20 import org
.eclipse
.jdt
.annotation
.NonNull
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.FloatDeclaration
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.FloatDefinition
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
26 import org
.junit
.Before
;
27 import org
.junit
.Test
;
30 * The class <code>IntegerDefinitionTest</code> contains tests for the class
31 * <code>{@link IntegerDefinition}</code>.
34 * @version $Revision: 1.0 $
36 @SuppressWarnings("javadoc")
37 public class FloatDefinitionTest
{
39 private FloatDefinition fixture
;
40 private FloatDefinition singleFixture
;
41 private FloatDefinition doubleFixture
; // all the way.
42 private FloatDeclaration parent
;
44 private static final String fieldName
= "float";
47 * Perform pre-test initialization.
49 * @throws CTFReaderException
50 * error creating floats
53 public void setUp() throws CTFReaderException
{
59 public void testFloat248() throws CTFReaderException
{
60 parent
= new FloatDeclaration(8, 24, ByteOrder
.nativeOrder(), 0);
61 BitBuffer bb
= create32BitFloatByteBuffer();
62 singleFixture
= parent
.createDefinition(null, fieldName
, bb
);
63 assertNotNull(singleFixture
);
67 public void testFloat5311() throws CTFReaderException
{
68 parent
= new FloatDeclaration(11, 53, ByteOrder
.nativeOrder(), 0);
69 BitBuffer bb
= create64BitFloatByteBuffer();
70 doubleFixture
= parent
.createDefinition(null, fieldName
, bb
);
71 assertNotNull(doubleFixture
);
75 public void testFloat32Bit() throws CTFReaderException
{
76 for (int i
= 1; i
< 31; i
++) {
77 parent
= new FloatDeclaration(i
, 32 - i
, ByteOrder
.nativeOrder(), 0);
79 fixture
= parent
.createDefinition(null, fieldName
, create32BitFloatByteBuffer());
80 assertNotNull(fixture
);
81 assertEquals("test" + i
, "2.0", fixture
.toString());
86 public void testFloat64Bit() throws CTFReaderException
{
87 for (int i
= 1; i
< 63; i
++) {
88 parent
= new FloatDeclaration(i
, 64 - i
, ByteOrder
.nativeOrder(), 0);
89 fixture
= parent
.createDefinition(null, fieldName
, create64BitFloatByteBuffer());
90 assertNotNull(fixture
);
92 assertEquals("test" + i
, "2.0", fixture
.toString());
94 assertEquals("test" + i
, "1.0", fixture
.toString());
96 assertNotNull(fixture
.getValue());
103 public void testFloat48Bit() throws CTFReaderException
{
104 parent
= new FloatDeclaration(12, 32, ByteOrder
.nativeOrder(), 0);
105 fixture
= parent
.createDefinition(null, fieldName
, create64BitFloatByteBuffer());
106 assertNotNull(fixture
);
107 assertEquals(Double
.NaN
, fixture
.getValue(), 0.1);
111 * Run the IntegerDeclaration getDeclaration() method test.
114 public void testGetDeclaration() {
115 FloatDeclaration result
= singleFixture
.getDeclaration();
116 assertNotNull(result
);
120 * Run the long getValue() method test.
123 public void testGetValue() {
124 double result
= singleFixture
.getValue();
125 assertEquals(2.0, result
, 0.1);
129 * Run the String toString() method test.
132 public void testToString() {
133 String result
= singleFixture
.toString();
134 assertNotNull(result
);
135 assertEquals("2.0", result
);
139 private static BitBuffer
create32BitFloatByteBuffer() {
140 float[] data
= new float[2];
143 ByteBuffer byb
= ByteBuffer
.allocate(128);
144 byb
.order(ByteOrder
.nativeOrder());
146 byb
.putFloat(data
[0]);
147 byb
.putFloat(data
[1]);
149 BitBuffer bb
= new BitBuffer(byb
);
154 private static BitBuffer
create64BitFloatByteBuffer() {
155 double[] data
= new double[2];
158 ByteBuffer byb
= ByteBuffer
.allocate(128);
159 byb
.order(ByteOrder
.nativeOrder());
161 byb
.putDouble(data
[0]);
162 byb
.putDouble(data
[1]);
164 BitBuffer bb
= new BitBuffer(byb
);