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