rcp: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / types / FloatDefinitionTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 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 *******************************************************************************/
11
12 package org.eclipse.tracecompass.ctf.core.tests.types;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16
17 import java.nio.ByteBuffer;
18 import java.nio.ByteOrder;
19
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.tracecompass.ctf.core.CTFException;
22 import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
23 import org.eclipse.tracecompass.ctf.core.event.types.FloatDeclaration;
24 import org.eclipse.tracecompass.ctf.core.event.types.FloatDefinition;
25 import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
26 import org.junit.Before;
27 import org.junit.Test;
28
29 /**
30 * The class <code>IntegerDefinitionTest</code> contains tests for the class
31 * <code>{@link IntegerDefinition}</code>.
32 *
33 * @author ematkho
34 * @version $Revision: 1.0 $
35 */
36 @SuppressWarnings("javadoc")
37 public class FloatDefinitionTest {
38
39 private FloatDefinition fixture;
40 private FloatDefinition singleFixture;
41 private FloatDefinition doubleFixture; // all the way.
42 private FloatDeclaration parent;
43 @NonNull
44 private static final String fieldName = "float";
45
46 /**
47 * Perform pre-test initialization.
48 *
49 * @throws CTFException
50 * error creating floats
51 */
52 @Before
53 public void setUp() throws CTFException {
54 testFloat248();
55 testFloat5311();
56 }
57
58 @Test
59 public void testFloat248() throws CTFException {
60 parent = new FloatDeclaration(8, 24, ByteOrder.nativeOrder(), 0);
61 BitBuffer bb = create32BitFloatByteBuffer();
62 singleFixture = parent.createDefinition(null, fieldName, bb);
63 assertNotNull(singleFixture);
64 }
65
66 @Test
67 public void testFloat5311() throws CTFException {
68 parent = new FloatDeclaration(11, 53, ByteOrder.nativeOrder(), 0);
69 BitBuffer bb = create64BitFloatByteBuffer();
70 doubleFixture = parent.createDefinition(null, fieldName, bb);
71 assertNotNull(doubleFixture);
72 }
73
74 @Test
75 public void testFloat32Bit() throws CTFException {
76 for (int i = 1; i < 31; i++) {
77 parent = new FloatDeclaration(i, 32 - i, ByteOrder.nativeOrder(), 0);
78
79 fixture = parent.createDefinition(null, fieldName, create32BitFloatByteBuffer());
80 assertNotNull(fixture);
81 assertEquals("test" + i, "2.0", fixture.toString());
82 }
83 }
84
85 @Test
86 public void testFloat64Bit() throws CTFException {
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);
91 if (i <= 32) {
92 assertEquals("test" + i, "2.0", fixture.toString());
93 } else if (i == 33) {
94 assertEquals("test" + i, "1.0", fixture.toString());
95 } else {
96 assertNotNull(fixture.getValue());
97 }
98
99 }
100 }
101
102 @Test
103 public void testFloat48Bit() throws CTFException {
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);
108 }
109
110 /**
111 * Run the IntegerDeclaration getDeclaration() method test.
112 */
113 @Test
114 public void testGetDeclaration() {
115 FloatDeclaration result = singleFixture.getDeclaration();
116 assertNotNull(result);
117 }
118
119 /**
120 * Run the long getValue() method test.
121 */
122 @Test
123 public void testGetValue() {
124 double result = singleFixture.getValue();
125 assertEquals(2.0, result, 0.1);
126 }
127
128 /**
129 * Run the String toString() method test.
130 */
131 @Test
132 public void testToString() {
133 String result = singleFixture.toString();
134 assertNotNull(result);
135 assertEquals("2.0", result);
136 }
137
138 @NonNull
139 private static BitBuffer create32BitFloatByteBuffer() {
140 float[] data = new float[2];
141 data[0] = 2.0f;
142 data[1] = 3.14f;
143 ByteBuffer byb = ByteBuffer.allocate(128);
144 byb.order(ByteOrder.nativeOrder());
145 byb.mark();
146 byb.putFloat(data[0]);
147 byb.putFloat(data[1]);
148 byb.reset();
149 BitBuffer bb = new BitBuffer(byb);
150 return bb;
151 }
152
153 @NonNull
154 private static BitBuffer create64BitFloatByteBuffer() {
155 double[] data = new double[2];
156 data[0] = 2.0f;
157 data[1] = 3.14f;
158 ByteBuffer byb = ByteBuffer.allocate(128);
159 byb.order(ByteOrder.nativeOrder());
160 byb.mark();
161 byb.putDouble(data[0]);
162 byb.putDouble(data[1]);
163 byb.reset();
164 BitBuffer bb = new BitBuffer(byb);
165 return bb;
166 }
167 }
This page took 0.035349 seconds and 5 git commands to generate.