1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.util.cts;
18 
19 import static android.util.Half.*;
20 
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertNotEquals;
24 import static org.junit.Assert.assertTrue;
25 
26 import android.util.Half;
27 
28 import androidx.test.filters.SmallTest;
29 import androidx.test.runner.AndroidJUnit4;
30 
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 
34 @SmallTest
35 @RunWith(AndroidJUnit4.class)
36 public class HalfTest {
assertShortEquals(short a, short b)37     private static void assertShortEquals(short a, short b) {
38         assertEquals((long) (a & 0xffff), (long) (b & 0xffff));
39     }
40 
assertShortEquals(int a, short b)41     private static void assertShortEquals(int a, short b) {
42         assertEquals((long) (a & 0xffff), (long) (b & 0xffff));
43     }
44 
45     @Test
singleToHalf()46     public void singleToHalf() {
47         // Zeroes, NaN and infinities
48         assertShortEquals(POSITIVE_ZERO, toHalf(0.0f));
49         assertShortEquals(NEGATIVE_ZERO, toHalf(-0.0f));
50         assertShortEquals(NaN, toHalf(Float.NaN));
51         assertShortEquals(POSITIVE_INFINITY, toHalf(Float.POSITIVE_INFINITY));
52         assertShortEquals(NEGATIVE_INFINITY, toHalf(Float.NEGATIVE_INFINITY));
53         // Known values
54         assertShortEquals(0x3c01, toHalf(1.0009765625f));
55         assertShortEquals(0xc000, toHalf(-2.0f));
56         assertShortEquals(0x0400, toHalf(6.10352e-5f));
57         assertShortEquals(0x7bff, toHalf(65504.0f));
58         assertShortEquals(0x3555, toHalf(1.0f / 3.0f));
59         // Denormals
60         assertShortEquals(0x03ff, toHalf(6.09756e-5f));
61         assertShortEquals(MIN_VALUE, toHalf(5.96046e-8f));
62         assertShortEquals(0x83ff, toHalf(-6.09756e-5f));
63         assertShortEquals(0x8001, toHalf(-5.96046e-8f));
64         // Denormals (flushed to +/-0)
65         assertShortEquals(POSITIVE_ZERO, toHalf(5.96046e-9f));
66         assertShortEquals(NEGATIVE_ZERO, toHalf(-5.96046e-9f));
67         // Test for values that overflow the mantissa bits into exp bits
68         assertShortEquals(0x1000, toHalf(Float.intBitsToFloat(0x39fff000)));
69         assertShortEquals(0x0400, toHalf(Float.intBitsToFloat(0x387fe000)));
70         // Floats with absolute value above +/-65519 are rounded to +/-inf
71         // when using round-to-even
72         assertShortEquals(0x7bff, toHalf(65519.0f));
73         assertShortEquals(0x7bff, toHalf(65519.9f));
74         assertShortEquals(POSITIVE_INFINITY, toHalf(65520.0f));
75         assertShortEquals(NEGATIVE_INFINITY, toHalf(-65520.0f));
76         // Check if numbers are rounded to nearest even when they
77         // cannot be accurately represented by Half
78         assertShortEquals(0x6800, toHalf(2049.0f));
79         assertShortEquals(0x6c00, toHalf(4098.0f));
80         assertShortEquals(0x7000, toHalf(8196.0f));
81         assertShortEquals(0x7400, toHalf(16392.0f));
82         assertShortEquals(0x7800, toHalf(32784.0f));
83     }
84 
85     @Test
halfToSingle()86     public void halfToSingle() {
87         // Zeroes, NaN and infinities
88         assertEquals(0.0f, toFloat(toHalf(0.0f)), 1e-6f);
89         assertEquals(-0.0f, toFloat(toHalf(-0.0f)), 1e-6f);
90         assertEquals(Float.NaN, toFloat(toHalf(Float.NaN)), 1e-6f);
91         assertEquals(Float.POSITIVE_INFINITY, toFloat(toHalf(Float.POSITIVE_INFINITY)), 1e-6f);
92         assertEquals(Float.NEGATIVE_INFINITY, toFloat(toHalf(Float.NEGATIVE_INFINITY)), 1e-6f);
93         // Known values
94         assertEquals(1.0009765625f, toFloat(toHalf(1.0009765625f)), 1e-6f);
95         assertEquals(-2.0f, toFloat(toHalf(-2.0f)), 1e-6f);
96         assertEquals(6.1035156e-5f, toFloat(toHalf(6.10352e-5f)), 1e-6f); // Inexact
97         assertEquals(65504.0f, toFloat(toHalf(65504.0f)), 1e-6f);
98         assertEquals(0.33325195f, toFloat(toHalf(1.0f / 3.0f)), 1e-6f); // Inexact
99         // Denormals (flushed to +/-0)
100         assertEquals(6.097555e-5f, toFloat(toHalf(6.09756e-5f)), 1e-6f);
101         assertEquals(5.9604645e-8f, toFloat(toHalf(5.96046e-8f)), 1e-9f);
102         assertEquals(-6.097555e-5f, toFloat(toHalf(-6.09756e-5f)), 1e-6f);
103         assertEquals(-5.9604645e-8f, toFloat(toHalf(-5.96046e-8f)), 1e-9f);
104     }
105 
106     @Test
hexString()107     public void hexString() {
108         assertEquals("NaN", toHexString(NaN));
109         assertEquals("Infinity", toHexString(POSITIVE_INFINITY));
110         assertEquals("-Infinity", toHexString(NEGATIVE_INFINITY));
111         assertEquals("0x0.0p0", toHexString(POSITIVE_ZERO));
112         assertEquals("-0x0.0p0", toHexString(NEGATIVE_ZERO));
113         assertEquals("0x1.0p0", toHexString(toHalf(1.0f)));
114         assertEquals("-0x1.0p0", toHexString(toHalf(-1.0f)));
115         assertEquals("0x1.0p1", toHexString(toHalf(2.0f)));
116         assertEquals("0x1.0p8", toHexString(toHalf(256.0f)));
117         assertEquals("0x1.0p-1", toHexString(toHalf(0.5f)));
118         assertEquals("0x1.0p-2", toHexString(toHalf(0.25f)));
119         assertEquals("0x1.3ffp15", toHexString(MAX_VALUE));
120         assertEquals("0x0.1p-14", toHexString(MIN_VALUE));
121         assertEquals("0x1.0p-14", toHexString(MIN_NORMAL));
122         assertEquals("-0x1.3ffp15", toHexString(LOWEST_VALUE));
123     }
124 
125     @Test
string()126     public void string() {
127         assertEquals("NaN", Half.toString(NaN));
128         assertEquals("Infinity", Half.toString(POSITIVE_INFINITY));
129         assertEquals("-Infinity", Half.toString(NEGATIVE_INFINITY));
130         assertEquals("0.0", Half.toString(POSITIVE_ZERO));
131         assertEquals("-0.0", Half.toString(NEGATIVE_ZERO));
132         assertEquals("1.0", Half.toString(toHalf(1.0f)));
133         assertEquals("-1.0", Half.toString(toHalf(-1.0f)));
134         assertEquals("2.0", Half.toString(toHalf(2.0f)));
135         assertEquals("256.0", Half.toString(toHalf(256.0f)));
136         assertEquals("0.5", Half.toString(toHalf(0.5f)));
137         assertEquals("0.25", Half.toString(toHalf(0.25f)));
138         assertEquals("65504.0", Half.toString(MAX_VALUE));
139         assertEquals("5.9604645E-8", Half.toString(MIN_VALUE));
140         assertEquals("6.1035156E-5", Half.toString(MIN_NORMAL));
141         assertEquals("-65504.0", Half.toString(LOWEST_VALUE));
142     }
143 
144     @Test
exponent()145     public void exponent() {
146         assertEquals(16, getExponent(POSITIVE_INFINITY));
147         assertEquals(16, getExponent(NEGATIVE_INFINITY));
148         assertEquals(16, getExponent(NaN));
149         assertEquals(-15, getExponent(POSITIVE_ZERO));
150         assertEquals(-15, getExponent(NEGATIVE_ZERO));
151         assertEquals(0, getExponent(toHalf(1.0f)));
152         assertEquals(-4, getExponent(toHalf(0.1f)));
153         assertEquals(-10, getExponent(toHalf(0.001f)));
154         assertEquals(7, getExponent(toHalf(128.8f)));
155     }
156 
157     @Test
significand()158     public void significand() {
159         assertEquals(0, getSignificand(POSITIVE_INFINITY));
160         assertEquals(0, getSignificand(NEGATIVE_INFINITY));
161         assertEquals(512, getSignificand(NaN));
162         assertEquals(0, getSignificand(POSITIVE_ZERO));
163         assertEquals(0, getSignificand(NEGATIVE_ZERO));
164         assertEquals(614, getSignificand(toHalf(0.1f)));
165         assertEquals(25, getSignificand(toHalf(0.001f)));
166         assertEquals(6, getSignificand(toHalf(128.8f)));
167     }
168 
169     @Test
sign()170     public void sign() {
171         assertEquals(1, getSign(POSITIVE_INFINITY));
172         assertEquals(-1, getSign(NEGATIVE_INFINITY));
173         assertEquals(1, getSign(POSITIVE_ZERO));
174         assertEquals(-1, getSign(NEGATIVE_ZERO));
175         assertEquals(1, getSign(NaN));
176         assertEquals(1, getSign(toHalf(12.4f)));
177         assertEquals(-1, getSign(toHalf(-12.4f)));
178     }
179 
180     @Test
isInfinite()181     public void isInfinite() {
182         assertTrue(Half.isInfinite(POSITIVE_INFINITY));
183         assertTrue(Half.isInfinite(NEGATIVE_INFINITY));
184         assertFalse(Half.isInfinite(POSITIVE_ZERO));
185         assertFalse(Half.isInfinite(NEGATIVE_ZERO));
186         assertFalse(Half.isInfinite(NaN));
187         assertFalse(Half.isInfinite(MAX_VALUE));
188         assertFalse(Half.isInfinite(LOWEST_VALUE));
189         assertFalse(Half.isInfinite(toHalf(-128.3f)));
190         assertFalse(Half.isInfinite(toHalf(128.3f)));
191     }
192 
193     @Test
isNaN()194     public void isNaN() {
195         assertFalse(Half.isNaN(POSITIVE_INFINITY));
196         assertFalse(Half.isNaN(NEGATIVE_INFINITY));
197         assertFalse(Half.isNaN(POSITIVE_ZERO));
198         assertFalse(Half.isNaN(NEGATIVE_ZERO));
199         assertTrue(Half.isNaN(NaN));
200         assertTrue(Half.isNaN((short) 0x7c01));
201         assertTrue(Half.isNaN((short) 0x7c18));
202         assertTrue(Half.isNaN((short) 0xfc01));
203         assertTrue(Half.isNaN((short) 0xfc98));
204         assertFalse(Half.isNaN(MAX_VALUE));
205         assertFalse(Half.isNaN(LOWEST_VALUE));
206         assertFalse(Half.isNaN(toHalf(-128.3f)));
207         assertFalse(Half.isNaN(toHalf(128.3f)));
208     }
209 
210     @Test
isNormalized()211     public void isNormalized() {
212         assertFalse(Half.isNormalized(POSITIVE_INFINITY));
213         assertFalse(Half.isNormalized(NEGATIVE_INFINITY));
214         assertFalse(Half.isNormalized(POSITIVE_ZERO));
215         assertFalse(Half.isNormalized(NEGATIVE_ZERO));
216         assertFalse(Half.isNormalized(NaN));
217         assertTrue(Half.isNormalized(MAX_VALUE));
218         assertTrue(Half.isNormalized(MIN_NORMAL));
219         assertTrue(Half.isNormalized(LOWEST_VALUE));
220         assertTrue(Half.isNormalized(toHalf(-128.3f)));
221         assertTrue(Half.isNormalized(toHalf(128.3f)));
222         assertTrue(Half.isNormalized(toHalf(0.3456f)));
223         assertFalse(Half.isNormalized(MIN_VALUE));
224         assertFalse(Half.isNormalized((short) 0x3ff));
225         assertFalse(Half.isNormalized((short) 0x200));
226         assertFalse(Half.isNormalized((short) 0x100));
227     }
228 
229     @Test
abs()230     public void abs() {
231         assertShortEquals(POSITIVE_INFINITY, Half.abs(POSITIVE_INFINITY));
232         assertShortEquals(POSITIVE_INFINITY, Half.abs(NEGATIVE_INFINITY));
233         assertShortEquals(POSITIVE_ZERO, Half.abs(POSITIVE_ZERO));
234         assertShortEquals(POSITIVE_ZERO, Half.abs(NEGATIVE_ZERO));
235         assertShortEquals(NaN, Half.abs(NaN));
236         assertShortEquals(MAX_VALUE, Half.abs(LOWEST_VALUE));
237         assertShortEquals(toHalf(12.12345f), Half.abs(toHalf(-12.12345f)));
238         assertShortEquals(toHalf(12.12345f), Half.abs(toHalf( 12.12345f)));
239     }
240 
241     @Test
ceil()242     public void ceil() {
243         assertShortEquals(POSITIVE_INFINITY, Half.ceil(POSITIVE_INFINITY));
244         assertShortEquals(NEGATIVE_INFINITY, Half.ceil(NEGATIVE_INFINITY));
245         assertShortEquals(POSITIVE_ZERO, Half.ceil(POSITIVE_ZERO));
246         assertShortEquals(NEGATIVE_ZERO, Half.ceil(NEGATIVE_ZERO));
247         assertShortEquals(NaN, Half.ceil(NaN));
248         assertShortEquals(LOWEST_VALUE, Half.ceil(LOWEST_VALUE));
249         assertEquals(1.0f, toFloat(Half.ceil(MIN_NORMAL)), 1e-6f);
250         assertEquals(1.0f, toFloat(Half.ceil((short) 0x3ff)), 1e-6f);
251         assertEquals(1.0f, toFloat(Half.ceil(toHalf(0.2f))), 1e-6f);
252         assertShortEquals(NEGATIVE_ZERO, Half.ceil(toHalf(-0.2f)));
253         assertEquals(1.0f, toFloat(Half.ceil(toHalf(0.7f))), 1e-6f);
254         assertShortEquals(NEGATIVE_ZERO, Half.ceil(toHalf(-0.7f)));
255         assertEquals(125.0f, toFloat(Half.ceil(toHalf(124.7f))), 1e-6f);
256         assertEquals(-124.0f, toFloat(Half.ceil(toHalf(-124.7f))), 1e-6f);
257         assertEquals(125.0f, toFloat(Half.ceil(toHalf(124.2f))), 1e-6f);
258         assertEquals(-124.0f, toFloat(Half.ceil(toHalf(-124.2f))), 1e-6f);
259         // ceil for NaN values
260         // These tests check whether the current ceil implementation achieves
261         // bit level compatibility with the hardware implementation (ARM64).
262         assertShortEquals((short) 0x7e01, Half.ceil((short) 0x7c01));
263         assertShortEquals((short) 0x7f00, Half.ceil((short) 0x7d00));
264         assertShortEquals((short) 0xfe01, Half.ceil((short) 0xfc01));
265         assertShortEquals((short) 0xff00, Half.ceil((short) 0xfd00));
266     }
267 
268     @Test
copySign()269     public void copySign() {
270         assertShortEquals(toHalf(7.5f), Half.copySign(toHalf(-7.5f), POSITIVE_INFINITY));
271         assertShortEquals(toHalf(7.5f), Half.copySign(toHalf(-7.5f), POSITIVE_ZERO));
272         assertShortEquals(toHalf(-7.5f), Half.copySign(toHalf(7.5f), NEGATIVE_INFINITY));
273         assertShortEquals(toHalf(-7.5f), Half.copySign(toHalf(7.5f), NEGATIVE_ZERO));
274         assertShortEquals(toHalf(7.5f), Half.copySign(toHalf(7.5f), NaN));
275         assertShortEquals(toHalf(7.5f), Half.copySign(toHalf(7.5f), toHalf(12.4f)));
276         assertShortEquals(toHalf(-7.5f), Half.copySign(toHalf(7.5f), toHalf(-12.4f)));
277     }
278 
279     @Test
equals()280     public void equals() {
281         assertTrue(Half.equals(POSITIVE_INFINITY, POSITIVE_INFINITY));
282         assertTrue(Half.equals(NEGATIVE_INFINITY, NEGATIVE_INFINITY));
283         assertTrue(Half.equals(POSITIVE_ZERO, POSITIVE_ZERO));
284         assertTrue(Half.equals(NEGATIVE_ZERO, NEGATIVE_ZERO));
285         assertTrue(Half.equals(POSITIVE_ZERO, NEGATIVE_ZERO));
286         assertFalse(Half.equals(NaN, toHalf(12.4f)));
287         assertFalse(Half.equals(toHalf(12.4f), NaN));
288         assertFalse(Half.equals(NaN, NaN));
289         assertTrue(Half.equals(toHalf(12.4f), toHalf(12.4f)));
290         assertTrue(Half.equals(toHalf(-12.4f), toHalf(-12.4f)));
291         assertFalse(Half.equals(toHalf(12.4f), toHalf(0.7f)));
292 
293         //noinspection UnnecessaryBoxing
294         assertNotEquals(Half.valueOf(0.0f), Float.valueOf(0.0f));
295         assertEquals(Half.valueOf(NaN), Half.valueOf((short) 0x7c01)); // NaN, NaN
296         assertEquals(Half.valueOf(NaN), Half.valueOf((short) 0xfc98)); // NaN, NaN
297 
298         assertEquals(Half.valueOf(POSITIVE_INFINITY), Half.valueOf(POSITIVE_INFINITY));
299         assertEquals(Half.valueOf(NEGATIVE_INFINITY), Half.valueOf(NEGATIVE_INFINITY));
300         assertEquals(Half.valueOf(POSITIVE_ZERO), Half.valueOf(POSITIVE_ZERO));
301         assertEquals(Half.valueOf(NEGATIVE_ZERO), Half.valueOf(NEGATIVE_ZERO));
302         assertNotEquals(Half.valueOf(POSITIVE_ZERO), Half.valueOf(NEGATIVE_ZERO));
303         assertNotEquals(Half.valueOf(NaN), Half.valueOf(12.4f));
304         assertNotEquals(Half.valueOf(12.4f), Half.valueOf(NaN));
305         assertEquals(Half.valueOf(12.4f), Half.valueOf(12.4f));
306         assertEquals(Half.valueOf(-12.4f), Half.valueOf(-12.4f));
307         assertNotEquals(Half.valueOf(12.4f), Half.valueOf(0.7f));
308     }
309 
310     @Test
floor()311     public void floor() {
312         assertShortEquals(POSITIVE_INFINITY, Half.floor(POSITIVE_INFINITY));
313         assertShortEquals(NEGATIVE_INFINITY, Half.floor(NEGATIVE_INFINITY));
314         assertShortEquals(POSITIVE_ZERO, Half.floor(POSITIVE_ZERO));
315         assertShortEquals(NEGATIVE_ZERO, Half.floor(NEGATIVE_ZERO));
316         assertShortEquals(NaN, Half.floor(NaN));
317         assertShortEquals(LOWEST_VALUE, Half.floor(LOWEST_VALUE));
318         assertShortEquals(POSITIVE_ZERO, Half.floor(MIN_NORMAL));
319         assertShortEquals(POSITIVE_ZERO, Half.floor((short) 0x3ff));
320         assertShortEquals(POSITIVE_ZERO, Half.floor(toHalf(0.2f)));
321         assertEquals(-1.0f, toFloat(Half.floor(toHalf(-0.2f))), 1e-6f);
322         assertEquals(-1.0f, toFloat(Half.floor(toHalf(-0.7f))), 1e-6f);
323         assertShortEquals(POSITIVE_ZERO, Half.floor(toHalf(0.7f)));
324         assertEquals(124.0f, toFloat(Half.floor(toHalf(124.7f))), 1e-6f);
325         assertEquals(-125.0f, toFloat(Half.floor(toHalf(-124.7f))), 1e-6f);
326         assertEquals(124.0f, toFloat(Half.floor(toHalf(124.2f))), 1e-6f);
327         assertEquals(-125.0f, toFloat(Half.floor(toHalf(-124.2f))), 1e-6f);
328         // floor for NaN values
329         assertShortEquals((short) 0x7e01, Half.floor((short) 0x7c01));
330         assertShortEquals((short) 0x7f00, Half.floor((short) 0x7d00));
331         assertShortEquals((short) 0xfe01, Half.floor((short) 0xfc01));
332         assertShortEquals((short) 0xff00, Half.floor((short) 0xfd00));
333     }
334 
335     @Test
round()336     public void round() {
337         assertShortEquals(POSITIVE_INFINITY, Half.round(POSITIVE_INFINITY));
338         assertShortEquals(NEGATIVE_INFINITY, Half.round(NEGATIVE_INFINITY));
339         assertShortEquals(POSITIVE_ZERO, Half.round(POSITIVE_ZERO));
340         assertShortEquals(NEGATIVE_ZERO, Half.round(NEGATIVE_ZERO));
341         assertShortEquals(NaN, Half.round(NaN));
342         assertShortEquals(LOWEST_VALUE, Half.round(LOWEST_VALUE));
343         assertShortEquals(POSITIVE_ZERO, Half.round(MIN_VALUE));
344         assertShortEquals(POSITIVE_ZERO, Half.round((short) 0x200));
345         assertShortEquals(POSITIVE_ZERO, Half.round((short) 0x3ff));
346         assertShortEquals(POSITIVE_ZERO, Half.round(toHalf(0.2f)));
347         assertShortEquals(NEGATIVE_ZERO, Half.round(toHalf(-0.2f)));
348         assertEquals(1.0f, toFloat(Half.round(toHalf(0.7f))), 1e-6f);
349         assertEquals(-1.0f, toFloat(Half.round(toHalf(-0.7f))), 1e-6f);
350         assertEquals(0.0f, toFloat(Half.round(toHalf(0.5f))), 1e-6f);
351         assertEquals(-0.0f, toFloat(Half.round(toHalf(-0.5f))), 1e-6f);
352         assertEquals(2.0f, toFloat(Half.round(toHalf(1.5f))), 1e-6f);
353         assertEquals(-2.0f, toFloat(Half.round(toHalf(-1.5f))), 1e-6f);
354         assertEquals(1022.0f, toFloat(Half.round(toHalf(1022.5f))), 1e-6f);
355         assertEquals(-1022.0f, toFloat(Half.round(toHalf(-1022.5f))), 1e-6f);
356         assertEquals(125.0f, toFloat(Half.round(toHalf(124.7f))), 1e-6f);
357         assertEquals(-125.0f, toFloat(Half.round(toHalf(-124.7f))), 1e-6f);
358         assertEquals(124.0f, toFloat(Half.round(toHalf(124.2f))), 1e-6f);
359         assertEquals(-124.0f, toFloat(Half.round(toHalf(-124.2f))), 1e-6f);
360         // round for NaN values
361         // These tests check whether the current round implementation achieves
362         // bit level compatibility with the hardware implementation (ARM64).
363         assertShortEquals((short) 0x7e01, Half.round((short) 0x7c01));
364         assertShortEquals((short) 0x7f00, Half.round((short) 0x7d00));
365         assertShortEquals((short) 0xfe01, Half.round((short) 0xfc01));
366         assertShortEquals((short) 0xff00, Half.round((short) 0xfd00));
367     }
368 
369     @Test
trunc()370     public void trunc() {
371         assertShortEquals(POSITIVE_INFINITY, Half.trunc(POSITIVE_INFINITY));
372         assertShortEquals(NEGATIVE_INFINITY, Half.trunc(NEGATIVE_INFINITY));
373         assertShortEquals(POSITIVE_ZERO, Half.trunc(POSITIVE_ZERO));
374         assertShortEquals(NEGATIVE_ZERO, Half.trunc(NEGATIVE_ZERO));
375         assertShortEquals(NaN, Half.trunc(NaN));
376         assertShortEquals(LOWEST_VALUE, Half.trunc(LOWEST_VALUE));
377         assertShortEquals(POSITIVE_ZERO, Half.trunc(toHalf(0.2f)));
378         assertShortEquals(NEGATIVE_ZERO, Half.trunc(toHalf(-0.2f)));
379         assertEquals(0.0f, toFloat(Half.trunc(toHalf(0.7f))), 1e-6f);
380         assertEquals(-0.0f, toFloat(Half.trunc(toHalf(-0.7f))), 1e-6f);
381         assertEquals(124.0f, toFloat(Half.trunc(toHalf(124.7f))), 1e-6f);
382         assertEquals(-124.0f, toFloat(Half.trunc(toHalf(-124.7f))), 1e-6f);
383         assertEquals(124.0f, toFloat(Half.trunc(toHalf(124.2f))), 1e-6f);
384         assertEquals(-124.0f, toFloat(Half.trunc(toHalf(-124.2f))), 1e-6f);
385     }
386 
387     @Test
less()388     public void less() {
389         assertTrue(Half.less(NEGATIVE_INFINITY, POSITIVE_INFINITY));
390         assertTrue(Half.less(MAX_VALUE, POSITIVE_INFINITY));
391         assertFalse(Half.less(POSITIVE_INFINITY, MAX_VALUE));
392         assertFalse(Half.less(LOWEST_VALUE, NEGATIVE_INFINITY));
393         assertTrue(Half.less(NEGATIVE_INFINITY, LOWEST_VALUE));
394         assertFalse(Half.less(POSITIVE_ZERO, NEGATIVE_ZERO));
395         assertFalse(Half.less(NEGATIVE_ZERO, POSITIVE_ZERO));
396         assertFalse(Half.less(NaN, toHalf(12.3f)));
397         assertFalse(Half.less(toHalf(12.3f), NaN));
398         assertTrue(Half.less(MIN_VALUE, MIN_NORMAL));
399         assertFalse(Half.less(MIN_NORMAL, MIN_VALUE));
400         assertTrue(Half.less(toHalf(12.3f), toHalf(12.4f)));
401         assertFalse(Half.less(toHalf(12.4f), toHalf(12.3f)));
402         assertFalse(Half.less(toHalf(-12.3f), toHalf(-12.4f)));
403         assertTrue(Half.less(toHalf(-12.4f), toHalf(-12.3f)));
404         assertTrue(Half.less(MIN_VALUE, (short) 0x3ff));
405     }
406 
407     @Test
lessEquals()408     public void lessEquals() {
409         assertTrue(Half.lessEquals(NEGATIVE_INFINITY, POSITIVE_INFINITY));
410         assertTrue(Half.lessEquals(MAX_VALUE, POSITIVE_INFINITY));
411         assertFalse(Half.lessEquals(POSITIVE_INFINITY, MAX_VALUE));
412         assertFalse(Half.lessEquals(LOWEST_VALUE, NEGATIVE_INFINITY));
413         assertTrue(Half.lessEquals(NEGATIVE_INFINITY, LOWEST_VALUE));
414         assertTrue(Half.lessEquals(POSITIVE_ZERO, NEGATIVE_ZERO));
415         assertTrue(Half.lessEquals(NEGATIVE_ZERO, POSITIVE_ZERO));
416         assertFalse(Half.lessEquals(NaN, toHalf(12.3f)));
417         assertFalse(Half.lessEquals(toHalf(12.3f), NaN));
418         assertTrue(Half.lessEquals(MIN_VALUE, MIN_NORMAL));
419         assertFalse(Half.lessEquals(MIN_NORMAL, MIN_VALUE));
420         assertTrue(Half.lessEquals(toHalf(12.3f), toHalf(12.4f)));
421         assertFalse(Half.lessEquals(toHalf(12.4f), toHalf(12.3f)));
422         assertFalse(Half.lessEquals(toHalf(-12.3f), toHalf(-12.4f)));
423         assertTrue(Half.lessEquals(toHalf(-12.4f), toHalf(-12.3f)));
424         assertTrue(Half.lessEquals(MIN_VALUE, (short) 0x3ff));
425         assertTrue(Half.lessEquals(NEGATIVE_INFINITY, NEGATIVE_INFINITY));
426         assertTrue(Half.lessEquals(POSITIVE_INFINITY, POSITIVE_INFINITY));
427         assertTrue(Half.lessEquals(toHalf(12.12356f), toHalf(12.12356f)));
428         assertTrue(Half.lessEquals(toHalf(-12.12356f), toHalf(-12.12356f)));
429     }
430 
431     @Test
greater()432     public void greater() {
433         assertTrue(Half.greater(POSITIVE_INFINITY, NEGATIVE_INFINITY));
434         assertTrue(Half.greater(POSITIVE_INFINITY, MAX_VALUE));
435         assertFalse(Half.greater(MAX_VALUE, POSITIVE_INFINITY));
436         assertFalse(Half.greater(NEGATIVE_INFINITY, LOWEST_VALUE));
437         assertTrue(Half.greater(LOWEST_VALUE, NEGATIVE_INFINITY));
438         assertFalse(Half.greater(NEGATIVE_ZERO, POSITIVE_ZERO));
439         assertFalse(Half.greater(POSITIVE_ZERO, NEGATIVE_ZERO));
440         assertFalse(Half.greater(toHalf(12.3f), NaN));
441         assertFalse(Half.greater(NaN, toHalf(12.3f)));
442         assertTrue(Half.greater(MIN_NORMAL, MIN_VALUE));
443         assertFalse(Half.greater(MIN_VALUE, MIN_NORMAL));
444         assertTrue(Half.greater(toHalf(12.4f), toHalf(12.3f)));
445         assertFalse(Half.greater(toHalf(12.3f), toHalf(12.4f)));
446         assertFalse(Half.greater(toHalf(-12.4f), toHalf(-12.3f)));
447         assertTrue(Half.greater(toHalf(-12.3f), toHalf(-12.4f)));
448         assertTrue(Half.greater((short) 0x3ff, MIN_VALUE));
449     }
450 
451     @Test
greaterEquals()452     public void greaterEquals() {
453         assertTrue(Half.greaterEquals(POSITIVE_INFINITY, NEGATIVE_INFINITY));
454         assertTrue(Half.greaterEquals(POSITIVE_INFINITY, MAX_VALUE));
455         assertFalse(Half.greaterEquals(MAX_VALUE, POSITIVE_INFINITY));
456         assertFalse(Half.greaterEquals(NEGATIVE_INFINITY, LOWEST_VALUE));
457         assertTrue(Half.greaterEquals(LOWEST_VALUE, NEGATIVE_INFINITY));
458         assertTrue(Half.greaterEquals(NEGATIVE_ZERO, POSITIVE_ZERO));
459         assertTrue(Half.greaterEquals(POSITIVE_ZERO, NEGATIVE_ZERO));
460         assertFalse(Half.greaterEquals(toHalf(12.3f), NaN));
461         assertFalse(Half.greaterEquals(NaN, toHalf(12.3f)));
462         assertTrue(Half.greaterEquals(MIN_NORMAL, MIN_VALUE));
463         assertFalse(Half.greaterEquals(MIN_VALUE, MIN_NORMAL));
464         assertTrue(Half.greaterEquals(toHalf(12.4f), toHalf(12.3f)));
465         assertFalse(Half.greaterEquals(toHalf(12.3f), toHalf(12.4f)));
466         assertFalse(Half.greaterEquals(toHalf(-12.4f), toHalf(-12.3f)));
467         assertTrue(Half.greaterEquals(toHalf(-12.3f), toHalf(-12.4f)));
468         assertTrue(Half.greaterEquals((short) 0x3ff, MIN_VALUE));
469         assertTrue(Half.greaterEquals(NEGATIVE_INFINITY, NEGATIVE_INFINITY));
470         assertTrue(Half.greaterEquals(POSITIVE_INFINITY, POSITIVE_INFINITY));
471         assertTrue(Half.greaterEquals(toHalf(12.12356f), toHalf(12.12356f)));
472         assertTrue(Half.greaterEquals(toHalf(-12.12356f), toHalf(-12.12356f)));
473     }
474 
475     @Test
min()476     public void min() {
477         assertShortEquals(NEGATIVE_INFINITY, Half.min(POSITIVE_INFINITY, NEGATIVE_INFINITY));
478         assertShortEquals(NEGATIVE_ZERO, Half.min(POSITIVE_ZERO, NEGATIVE_ZERO));
479         assertShortEquals(NaN, Half.min(NaN, LOWEST_VALUE));
480         assertShortEquals(NaN, Half.min(LOWEST_VALUE, NaN));
481         assertShortEquals(NEGATIVE_INFINITY, Half.min(NEGATIVE_INFINITY, LOWEST_VALUE));
482         assertShortEquals(MAX_VALUE, Half.min(POSITIVE_INFINITY, MAX_VALUE));
483         assertShortEquals(MIN_VALUE, Half.min(MIN_VALUE, MIN_NORMAL));
484         assertShortEquals(POSITIVE_ZERO, Half.min(MIN_VALUE, POSITIVE_ZERO));
485         assertShortEquals(POSITIVE_ZERO, Half.min(MIN_NORMAL, POSITIVE_ZERO));
486         assertShortEquals(toHalf(-3.456f), Half.min(toHalf(-3.456f), toHalf(-3.453f)));
487         assertShortEquals(toHalf(3.453f), Half.min(toHalf(3.456f), toHalf(3.453f)));
488     }
489 
490     @Test
max()491     public void max() {
492         assertShortEquals(POSITIVE_INFINITY, Half.max(POSITIVE_INFINITY, NEGATIVE_INFINITY));
493         assertShortEquals(POSITIVE_ZERO, Half.max(POSITIVE_ZERO, NEGATIVE_ZERO));
494         assertShortEquals(NaN, Half.max(NaN, MAX_VALUE));
495         assertShortEquals(NaN, Half.max(MAX_VALUE, NaN));
496         assertShortEquals(LOWEST_VALUE, Half.max(NEGATIVE_INFINITY, LOWEST_VALUE));
497         assertShortEquals(POSITIVE_INFINITY, Half.max(POSITIVE_INFINITY, MAX_VALUE));
498         assertShortEquals(MIN_NORMAL, Half.max(MIN_VALUE, MIN_NORMAL));
499         assertShortEquals(MIN_VALUE, Half.max(MIN_VALUE, POSITIVE_ZERO));
500         assertShortEquals(MIN_NORMAL, Half.max(MIN_NORMAL, POSITIVE_ZERO));
501         assertShortEquals(toHalf(-3.453f), Half.max(toHalf(-3.456f), toHalf(-3.453f)));
502         assertShortEquals(toHalf(3.456f), Half.max(toHalf(3.456f), toHalf(3.453f)));
503     }
504 
505     @Test
numberInterface()506     public void numberInterface() {
507         assertEquals(12, Half.valueOf(12.57f).byteValue());
508         assertEquals(12, Half.valueOf(12.57f).shortValue());
509         assertEquals(12, Half.valueOf(12.57f).intValue());
510         assertEquals(12, Half.valueOf(12.57f).longValue());
511         assertEquals(12.57f, Half.valueOf(12.57f).floatValue(), 1e-3f);
512         assertEquals(12.57, Half.valueOf(12.57f).doubleValue(), 1e-3);
513 
514         assertEquals(-12, Half.valueOf(-12.57f).byteValue());
515         assertEquals(-12, Half.valueOf(-12.57f).shortValue());
516         assertEquals(-12, Half.valueOf(-12.57f).intValue());
517         assertEquals(-12, Half.valueOf(-12.57f).longValue());
518         assertEquals(-12.57f, Half.valueOf(-12.57f).floatValue(), 1e-3f);
519         assertEquals(-12.57, Half.valueOf(-12.57f).doubleValue(), 1e-3);
520 
521         assertEquals(0, Half.valueOf(POSITIVE_ZERO).byteValue());
522         assertEquals(0, Half.valueOf(POSITIVE_ZERO).shortValue());
523         assertEquals(0, Half.valueOf(POSITIVE_ZERO).intValue());
524         assertEquals(0, Half.valueOf(POSITIVE_ZERO).longValue());
525         assertTrue(+0.0f == Half.valueOf(POSITIVE_ZERO).floatValue());
526         assertTrue(+0.0 == Half.valueOf(POSITIVE_ZERO).doubleValue());
527 
528         assertEquals(0, Half.valueOf(NEGATIVE_ZERO).byteValue());
529         assertEquals(0, Half.valueOf(NEGATIVE_ZERO).shortValue());
530         assertEquals(0, Half.valueOf(NEGATIVE_ZERO).intValue());
531         assertEquals(0, Half.valueOf(NEGATIVE_ZERO).longValue());
532         assertTrue(-0.0f == Half.valueOf(NEGATIVE_ZERO).floatValue());
533         assertTrue(-0.0 == Half.valueOf(NEGATIVE_ZERO).doubleValue());
534 
535         assertEquals(-1, Half.valueOf(POSITIVE_INFINITY).byteValue());
536         assertEquals(-1, Half.valueOf(POSITIVE_INFINITY).shortValue());
537         assertEquals(Integer.MAX_VALUE, Half.valueOf(POSITIVE_INFINITY).intValue());
538         assertEquals(Long.MAX_VALUE, Half.valueOf(POSITIVE_INFINITY).longValue());
539         assertTrue(Float.POSITIVE_INFINITY == Half.valueOf(POSITIVE_INFINITY).floatValue());
540         assertTrue(Double.POSITIVE_INFINITY == Half.valueOf(POSITIVE_INFINITY).doubleValue());
541 
542         assertEquals(0, Half.valueOf(NEGATIVE_INFINITY).byteValue());
543         assertEquals(0, Half.valueOf(NEGATIVE_INFINITY).shortValue());
544         assertEquals(Integer.MIN_VALUE, Half.valueOf(NEGATIVE_INFINITY).intValue());
545         assertEquals(Long.MIN_VALUE, Half.valueOf(NEGATIVE_INFINITY).longValue());
546         assertTrue(Float.NEGATIVE_INFINITY == Half.valueOf(NEGATIVE_INFINITY).floatValue());
547         assertTrue(Double.NEGATIVE_INFINITY == Half.valueOf(NEGATIVE_INFINITY).doubleValue());
548 
549         assertEquals(0, Half.valueOf(NaN).byteValue());
550         assertEquals(0, Half.valueOf(NaN).shortValue());
551         assertEquals(0, Half.valueOf(NaN).intValue());
552         assertEquals(0, Half.valueOf(NaN).longValue());
553         assertEquals(Float.floatToRawIntBits(Float.NaN),
554                 Float.floatToRawIntBits(Half.valueOf(NaN).floatValue()));
555         assertEquals(Double.doubleToRawLongBits(Double.NaN),
556                 Double.doubleToRawLongBits(Half.valueOf(NaN).doubleValue()));
557     }
558 
559     @SuppressWarnings("PointlessBitwiseExpression")
560     @Test
bits()561     public void bits() {
562         assertEquals(POSITIVE_INFINITY & 0xffff, halfToRawIntBits(POSITIVE_INFINITY));
563         assertEquals(NEGATIVE_INFINITY & 0xffff, halfToRawIntBits(NEGATIVE_INFINITY));
564         assertEquals(POSITIVE_ZERO & 0xffff, halfToRawIntBits(POSITIVE_ZERO));
565         assertEquals(NEGATIVE_ZERO & 0xffff, halfToRawIntBits(NEGATIVE_ZERO));
566         assertEquals(NaN & 0xffff, halfToRawIntBits(NaN));
567         assertEquals(0xfc98, halfToRawIntBits((short) 0xfc98)); // NaN
568         assertEquals(toHalf(12.462f) & 0xffff, halfToRawIntBits(toHalf(12.462f)));
569         assertEquals(toHalf(-12.462f) & 0xffff, halfToRawIntBits(toHalf(-12.462f)));
570 
571         assertEquals(POSITIVE_INFINITY & 0xffff, halfToIntBits(POSITIVE_INFINITY));
572         assertEquals(NEGATIVE_INFINITY & 0xffff, halfToIntBits(NEGATIVE_INFINITY));
573         assertEquals(POSITIVE_ZERO & 0xffff, halfToIntBits(POSITIVE_ZERO));
574         assertEquals(NEGATIVE_ZERO & 0xffff, halfToIntBits(NEGATIVE_ZERO));
575         assertEquals(NaN & 0xffff, halfToIntBits(NaN));
576         assertEquals(NaN & 0xffff, halfToIntBits((short) 0xfc98)); // NaN
577         assertEquals(toHalf(12.462f) & 0xffff, halfToIntBits(toHalf(12.462f)));
578         assertEquals(toHalf(-12.462f) & 0xffff, halfToIntBits(toHalf(-12.462f)));
579 
580         assertShortEquals(POSITIVE_INFINITY, intBitsToHalf(halfToIntBits(POSITIVE_INFINITY)));
581         assertShortEquals(NEGATIVE_INFINITY, intBitsToHalf(halfToIntBits(NEGATIVE_INFINITY)));
582         assertShortEquals(POSITIVE_ZERO, intBitsToHalf(halfToIntBits(POSITIVE_ZERO)));
583         assertShortEquals(NEGATIVE_ZERO, intBitsToHalf(halfToIntBits(NEGATIVE_ZERO)));
584         assertShortEquals(NaN, intBitsToHalf(halfToIntBits(NaN)));
585         assertShortEquals(NaN, intBitsToHalf(halfToIntBits((short) 0xfc98)));
586         assertShortEquals(toHalf(12.462f), intBitsToHalf(halfToIntBits(toHalf(12.462f))));
587         assertShortEquals(toHalf(-12.462f), intBitsToHalf(halfToIntBits(toHalf(-12.462f))));
588 
589         assertShortEquals(POSITIVE_INFINITY, halfToShortBits(POSITIVE_INFINITY));
590         assertShortEquals(NEGATIVE_INFINITY, halfToShortBits(NEGATIVE_INFINITY));
591         assertShortEquals(POSITIVE_ZERO, halfToShortBits(POSITIVE_ZERO));
592         assertShortEquals(NEGATIVE_ZERO, halfToShortBits(NEGATIVE_ZERO));
593         assertShortEquals(NaN, halfToShortBits(NaN));
594         assertShortEquals(NaN, halfToShortBits((short) 0xfc98)); // NaN
595         assertShortEquals(toHalf(12.462f), halfToShortBits(toHalf(12.462f)));
596         assertShortEquals(toHalf(-12.462f), halfToShortBits(toHalf(-12.462f)));
597     }
598 
599     @Test
hashCodeGeneration()600     public void hashCodeGeneration() {
601         assertNotEquals(Half.hashCode(POSITIVE_INFINITY), Half.hashCode(NEGATIVE_INFINITY));
602         assertNotEquals(Half.hashCode(POSITIVE_ZERO), Half.hashCode(NEGATIVE_ZERO));
603         assertNotEquals(Half.hashCode(toHalf(1.999f)), Half.hashCode(toHalf(1.998f)));
604         assertEquals(Half.hashCode(NaN), Half.hashCode((short) 0x7c01));
605         assertEquals(Half.hashCode(NaN), Half.hashCode((short) 0xfc98));
606 
607         assertEquals(Half.hashCode(POSITIVE_INFINITY), Half.valueOf(POSITIVE_INFINITY).hashCode());
608         assertEquals(Half.hashCode(NEGATIVE_INFINITY), Half.valueOf(NEGATIVE_INFINITY).hashCode());
609         assertEquals(Half.hashCode(POSITIVE_ZERO), Half.valueOf(POSITIVE_ZERO).hashCode());
610         assertEquals(Half.hashCode(NEGATIVE_ZERO), Half.valueOf(NEGATIVE_ZERO).hashCode());
611         assertEquals(Half.hashCode(NaN), Half.valueOf(NaN).hashCode());
612         assertEquals(Half.hashCode((short) 0xfc98), Half.valueOf((short) 0xfc98).hashCode());
613         assertEquals(Half.hashCode(toHalf(1.999f)), Half.valueOf(1.999f).hashCode());
614     }
615 
616     @Test
constructors()617     public void constructors() {
618         assertEquals(POSITIVE_INFINITY, new Half(POSITIVE_INFINITY).halfValue());
619         assertEquals(NEGATIVE_INFINITY, new Half(NEGATIVE_INFINITY).halfValue());
620         assertEquals(POSITIVE_ZERO, new Half(POSITIVE_ZERO).halfValue());
621         assertEquals(NEGATIVE_ZERO, new Half(NEGATIVE_ZERO).halfValue());
622         assertEquals(NaN, new Half(NaN).halfValue());
623         assertEquals(toHalf(12.57f), new Half(toHalf(12.57f)).halfValue());
624         assertEquals(toHalf(-12.57f), new Half(toHalf(-12.57f)).halfValue());
625 
626         assertEquals(POSITIVE_INFINITY, new Half(Float.POSITIVE_INFINITY).halfValue());
627         assertEquals(NEGATIVE_INFINITY, new Half(Float.NEGATIVE_INFINITY).halfValue());
628         assertEquals(POSITIVE_ZERO, new Half(0.0f).halfValue());
629         assertEquals(NEGATIVE_ZERO, new Half(-0.0f).halfValue());
630         assertEquals(NaN, new Half(Float.NaN).halfValue());
631         assertEquals(toHalf(12.57f), new Half(12.57f).halfValue());
632         assertEquals(toHalf(-12.57f), new Half(-12.57f).halfValue());
633 
634         assertEquals(POSITIVE_INFINITY, new Half(Double.POSITIVE_INFINITY).halfValue());
635         assertEquals(NEGATIVE_INFINITY, new Half(Double.NEGATIVE_INFINITY).halfValue());
636         assertEquals(POSITIVE_ZERO, new Half(0.0).halfValue());
637         assertEquals(NEGATIVE_ZERO, new Half(-0.0).halfValue());
638         assertEquals(NaN, new Half(Double.NaN).halfValue());
639         assertEquals(toHalf(12.57f), new Half(12.57).halfValue());
640         assertEquals(toHalf(-12.57f), new Half(-12.57).halfValue());
641 
642         assertEquals(POSITIVE_INFINITY, new Half("+Infinity").halfValue());
643         assertEquals(NEGATIVE_INFINITY, new Half("-Infinity").halfValue());
644         assertEquals(POSITIVE_ZERO, new Half("0.0").halfValue());
645         assertEquals(NEGATIVE_ZERO, new Half("-0.0").halfValue());
646         assertEquals(NaN, new Half("NaN").halfValue());
647         assertEquals(toHalf(12.57f), new Half("1257e-2").halfValue());
648         assertEquals(toHalf(-12.57f), new Half("-1257e-2").halfValue());
649     }
650 
651     @Test(expected = NumberFormatException.class)
constructorFailure()652     public void constructorFailure() {
653         new Half("not a number");
654     }
655 
656     @Test
parse()657     public void parse() {
658         assertShortEquals(parseHalf("NaN"), NaN);
659         assertShortEquals(parseHalf("Infinity"), POSITIVE_INFINITY);
660         assertShortEquals(parseHalf("-Infinity"), NEGATIVE_INFINITY);
661         assertShortEquals(parseHalf("0.0"), POSITIVE_ZERO);
662         assertShortEquals(parseHalf("-0.0"), NEGATIVE_ZERO);
663         assertShortEquals(parseHalf("1.0"), toHalf(1.0f));
664         assertShortEquals(parseHalf("-1.0"), toHalf(-1.0f));
665         assertShortEquals(parseHalf("2.0"), toHalf(2.0f));
666         assertShortEquals(parseHalf("256.0"), toHalf(256.0f));
667         assertShortEquals(parseHalf("0.5"), toHalf(0.5f));
668         assertShortEquals(parseHalf("0.25"), toHalf(0.25f));
669         assertShortEquals(parseHalf("65504.0"), MAX_VALUE);
670         assertShortEquals(parseHalf("5.9604645E-8"), MIN_VALUE);
671         assertShortEquals(parseHalf("6.1035156E-5"), MIN_NORMAL);
672         assertShortEquals(parseHalf("-65504.0"), LOWEST_VALUE);
673     }
674 
675     @Test(expected = NumberFormatException.class)
parseFailure()676     public void parseFailure() {
677         parseHalf("not a number");
678     }
679 
680     @Test
valueOf()681     public void valueOf() {
682         assertEquals(POSITIVE_INFINITY, Half.valueOf(POSITIVE_INFINITY).halfValue());
683         assertEquals(NEGATIVE_INFINITY, Half.valueOf(NEGATIVE_INFINITY).halfValue());
684         assertEquals(POSITIVE_ZERO, Half.valueOf(POSITIVE_ZERO).halfValue());
685         assertEquals(NEGATIVE_ZERO, Half.valueOf(NEGATIVE_ZERO).halfValue());
686         assertEquals(NaN, Half.valueOf(NaN).halfValue());
687         assertEquals(toHalf(12.57f), Half.valueOf(toHalf(12.57f)).halfValue());
688         assertEquals(toHalf(-12.57f), Half.valueOf(toHalf(-12.57f)).halfValue());
689 
690         assertEquals(POSITIVE_INFINITY, Half.valueOf(Float.POSITIVE_INFINITY).halfValue());
691         assertEquals(NEGATIVE_INFINITY, Half.valueOf(Float.NEGATIVE_INFINITY).halfValue());
692         assertEquals(POSITIVE_ZERO, Half.valueOf(0.0f).halfValue());
693         assertEquals(NEGATIVE_ZERO, Half.valueOf(-0.0f).halfValue());
694         assertEquals(NaN, Half.valueOf(Float.NaN).halfValue());
695         assertEquals(toHalf(12.57f), Half.valueOf(12.57f).halfValue());
696         assertEquals(toHalf(-12.57f), Half.valueOf(-12.57f).halfValue());
697 
698         assertEquals(POSITIVE_INFINITY, Half.valueOf("+Infinity").halfValue());
699         assertEquals(NEGATIVE_INFINITY, Half.valueOf("-Infinity").halfValue());
700         assertEquals(POSITIVE_ZERO, Half.valueOf("0.0").halfValue());
701         assertEquals(NEGATIVE_ZERO, Half.valueOf("-0.0").halfValue());
702         assertEquals(NaN, Half.valueOf("NaN").halfValue());
703         assertEquals(toHalf(12.57f), Half.valueOf("1257e-2").halfValue());
704         assertEquals(toHalf(-12.57f), Half.valueOf("-1257e-2").halfValue());
705     }
706 
707     @Test
compare()708     public void compare() {
709         assertEquals(0, Half.compare(NaN, NaN));
710         assertEquals(0, Half.compare(NaN, (short) 0xfc98));
711         assertEquals(1, Half.compare(NaN, POSITIVE_INFINITY));
712         assertEquals(-1, Half.compare(POSITIVE_INFINITY, NaN));
713 
714         assertEquals(0, Half.compare(POSITIVE_INFINITY, POSITIVE_INFINITY));
715         assertEquals(0, Half.compare(NEGATIVE_INFINITY, NEGATIVE_INFINITY));
716         assertEquals(1, Half.compare(POSITIVE_INFINITY, NEGATIVE_INFINITY));
717         assertEquals(-1, Half.compare(NEGATIVE_INFINITY, POSITIVE_INFINITY));
718 
719         assertEquals(0, Half.compare(POSITIVE_ZERO, POSITIVE_ZERO));
720         assertEquals(0, Half.compare(NEGATIVE_ZERO, NEGATIVE_ZERO));
721         assertEquals(1, Half.compare(POSITIVE_ZERO, NEGATIVE_ZERO));
722         assertEquals(-1, Half.compare(NEGATIVE_ZERO, POSITIVE_ZERO));
723 
724         assertEquals(0, Half.compare(toHalf(12.462f), toHalf(12.462f)));
725         assertEquals(0, Half.compare(toHalf(-12.462f), toHalf(-12.462f)));
726         assertEquals(1, Half.compare(toHalf(12.462f), toHalf(-12.462f)));
727         assertEquals(-1, Half.compare(toHalf(-12.462f), toHalf(12.462f)));
728     }
729 }
730