Commit | Line | Data |
---|---|---|
4bd7f2db AM |
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 | |
7 | * | |
8 | * Contributors: | |
9 | * Matthew Khouzam - Initial API and implementation | |
10 | *******************************************************************************/ | |
11 | ||
51209785 MK |
12 | package org.eclipse.linuxtools.ctf.core.tests.types; |
13 | ||
14 | import static org.junit.Assert.assertEquals; | |
15 | import static org.junit.Assert.assertNotNull; | |
51209785 | 16 | |
a4fa4e36 | 17 | import java.nio.ByteBuffer; |
51209785 MK |
18 | import java.nio.ByteOrder; |
19 | ||
a4fa4e36 | 20 | import org.eclipse.jdt.annotation.NonNull; |
486efb2e | 21 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; |
51209785 MK |
22 | import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration; |
23 | import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition; | |
a34d8eee | 24 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; |
db8e8f7d | 25 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
51209785 MK |
26 | import org.junit.Before; |
27 | import org.junit.Test; | |
28 | ||
51209785 MK |
29 | /** |
30 | * The class <code>IntegerDefinitionTest</code> contains tests for the class | |
31 | * <code>{@link IntegerDefinition}</code>. | |
607e4c98 | 32 | * |
51209785 MK |
33 | * @author ematkho |
34 | * @version $Revision: 1.0 $ | |
35 | */ | |
be6df2d8 | 36 | @SuppressWarnings("javadoc") |
51209785 | 37 | public class FloatDefinitionTest { |
4a9c1f07 | 38 | |
51209785 MK |
39 | private FloatDefinition fixture; |
40 | private FloatDefinition singleFixture; | |
a4fa4e36 | 41 | private FloatDefinition doubleFixture; // all the way. |
51209785 | 42 | private FloatDeclaration parent; |
a4fa4e36 | 43 | @NonNull |
4a9c1f07 AM |
44 | private static final String fieldName = "float"; |
45 | ||
51209785 | 46 | /** |
a34d8eee | 47 | * Perform pre-test initialization. |
607e4c98 | 48 | * |
a4fa4e36 MK |
49 | * @throws CTFReaderException |
50 | * error creating floats | |
51209785 MK |
51 | */ |
52 | @Before | |
a4fa4e36 | 53 | public void setUp() throws CTFReaderException { |
51209785 MK |
54 | testFloat248(); |
55 | testFloat5311(); | |
56 | } | |
57 | ||
51209785 | 58 | @Test |
a4fa4e36 | 59 | public void testFloat248() throws CTFReaderException { |
07002e0a | 60 | parent = new FloatDeclaration(8, 24, ByteOrder.nativeOrder(), 0); |
a4fa4e36 MK |
61 | BitBuffer bb = create32BitFloatByteBuffer(); |
62 | singleFixture = parent.createDefinition(null, fieldName, bb); | |
51209785 MK |
63 | assertNotNull(singleFixture); |
64 | } | |
65 | ||
51209785 | 66 | @Test |
a4fa4e36 | 67 | public void testFloat5311() throws CTFReaderException { |
07002e0a | 68 | parent = new FloatDeclaration(11, 53, ByteOrder.nativeOrder(), 0); |
a4fa4e36 MK |
69 | BitBuffer bb = create64BitFloatByteBuffer(); |
70 | doubleFixture = parent.createDefinition(null, fieldName, bb); | |
51209785 MK |
71 | assertNotNull(doubleFixture); |
72 | } | |
73 | ||
74 | @Test | |
a4fa4e36 MK |
75 | public void testFloat32Bit() throws CTFReaderException { |
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()); | |
51209785 | 80 | assertNotNull(fixture); |
a4fa4e36 | 81 | assertEquals("test" + i, "2.0", fixture.toString()); |
51209785 MK |
82 | } |
83 | } | |
84 | ||
85 | @Test | |
a4fa4e36 MK |
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()); | |
51209785 | 90 | assertNotNull(fixture); |
a4fa4e36 MK |
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 | ||
51209785 MK |
99 | } |
100 | } | |
101 | ||
102 | @Test | |
a4fa4e36 | 103 | public void testFloat48Bit() throws CTFReaderException { |
07002e0a | 104 | parent = new FloatDeclaration(12, 32, ByteOrder.nativeOrder(), 0); |
a4fa4e36 | 105 | fixture = parent.createDefinition(null, fieldName, create64BitFloatByteBuffer()); |
51209785 | 106 | assertNotNull(fixture); |
a4fa4e36 | 107 | assertEquals(Double.NaN, fixture.getValue(), 0.1); |
51209785 | 108 | } |
a34d8eee | 109 | |
51209785 MK |
110 | /** |
111 | * Run the IntegerDeclaration getDeclaration() method test. | |
112 | */ | |
113 | @Test | |
114 | public void testGetDeclaration() { | |
51209785 MK |
115 | FloatDeclaration result = singleFixture.getDeclaration(); |
116 | assertNotNull(result); | |
117 | } | |
118 | ||
119 | /** | |
120 | * Run the long getValue() method test. | |
121 | */ | |
51209785 MK |
122 | @Test |
123 | public void testGetValue() { | |
51209785 | 124 | double result = singleFixture.getValue(); |
a4fa4e36 | 125 | assertEquals(2.0, result, 0.1); |
51209785 MK |
126 | } |
127 | ||
128 | /** | |
129 | * Run the String toString() method test. | |
130 | */ | |
131 | @Test | |
132 | public void testToString() { | |
51209785 MK |
133 | String result = singleFixture.toString(); |
134 | assertNotNull(result); | |
a4fa4e36 MK |
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); | |
733c614c | 144 | byb.order(ByteOrder.nativeOrder()); |
a4fa4e36 MK |
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); | |
733c614c | 159 | byb.order(ByteOrder.nativeOrder()); |
a4fa4e36 MK |
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; | |
51209785 MK |
166 | } |
167 | } |