FloatTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. 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 17package org.apache.harmony.luni.tests.java.lang; 18 19import dalvik.annotation.TestInfo; 20import dalvik.annotation.TestLevel; 21import dalvik.annotation.TestTarget; 22import dalvik.annotation.TestTargetClass; 23 24import junit.framework.TestCase; 25 26@TestTargetClass(Float.class) 27public class FloatTest extends TestCase { 28 29 private static final int rawBitsFor3_4eN38To38[] = { 0x1394470, 0x2e7958c, 0x490bd77, 0x634ecd5, 30 0x7e2280b, 0x98d5907, 0xb30af48, 0xcdcdb1a, 0xe8a08f0, 0x102c8b2d, 0x11d7adf8, 31 0x1386ccbb, 0x15287fe9, 0x16d29fe4, 0x1883a3ee, 0x1a248cea, 0x1bcdb025, 0x1d808e17, 32 0x1f20b19d, 0x20c8de04, 0x227b1585, 0x241ced73, 0x25c428d0, 0x27753303, 0x29193fe2, 33 0x2abf8fdb, 0x2c6f73d1, 0x2e15a863, 0x2fbb127c, 0x3169d71a, 0x33122671, 0x34b6b00d, 34 0x36645c10, 0x380eb98a, 0x39b267ec, 0x3b5f01e8, 0x3d0b6131, 0x3eae397d, 0x4059c7dc, 35 0x42081cea, 0x43aa2424, 0x4554ad2d, 0x4704ec3c, 0x48a6274b, 0x4a4fb11e, 0x4c01ceb3, 36 0x4da2425f, 0x4f4ad2f7, 0x50fd87b5, 0x529e74d1, 0x54461205, 0x55f79687, 0x579abe14, 37 0x59416d99, 0x5af1c900, 0x5c971da0, 0x5e3ce508, 0x5fec1e4a, 0x619392ee, 0x633877a9, 38 0x64e69594, 0x66901d7c, 0x683424dc, 0x69e12e12, 0x6b8cbccb, 0x6d2febfe, 0x6edbe6fe, 39 0x7089705f, 0x722bcc76, 0x73d6bf94, 0x758637bc, 0x7727c5ac, 0x78d1b717, 0x7a83126e, 40 0x7c23d70a, 0x7dcccccc, 0x7f7fffff }; 41 42 private static final String expectedStringFor3_4eN38To38[] = { "3.4028235E-38", "3.4028235E-37", 43 "3.4028233E-36", "3.4028234E-35", "3.4028236E-34", "3.4028236E-33", 44 "3.4028234E-32", "3.4028234E-31", "3.4028233E-30", "3.4028236E-29", 45 "3.4028235E-28", "3.4028235E-27", "3.4028233E-26", "3.4028235E-25", 46 "3.4028233E-24", "3.4028235E-23", "3.4028236E-22", "3.4028235E-21", 47 "3.4028236E-20", "3.4028236E-19", "3.4028236E-18", "3.4028235E-17", 48 "3.4028236E-16", "3.4028234E-15", "3.4028234E-14", "3.4028235E-13", 49 "3.4028234E-12", "3.4028235E-11", "3.4028236E-10", "3.4028234E-9", "3.4028236E-8", 50 "3.4028236E-7", "3.4028235E-6", "3.4028235E-5", "3.4028233E-4", "0.0034028236", 51 "0.034028236", "0.34028235", "3.4028234", "34.028236", "340.28235", "3402.8235", 52 "34028.234", "340282.34", "3402823.5", "3.4028236E7", "3.40282336E8", 53 "3.40282342E9", "3.40282348E10", "3.40282343E11", "3.40282337E12", "3.40282353E13", 54 "3.4028234E14", "3.4028234E15", "3.40282356E16", "3.40282356E17", "3.40282356E18", 55 "3.4028236E19", "3.4028235E20", "3.4028233E21", "3.4028235E22", "3.4028233E23", 56 "3.4028236E24", "3.4028234E25", "3.4028233E26", "3.4028234E27", "3.4028235E28", 57 "3.4028236E29", "3.4028233E30", "3.4028235E31", "3.4028233E32", "3.4028236E33", 58 "3.4028236E34", "3.4028234E35", "3.4028236E36", "3.4028235E37", "3.4028235E38" }; 59 60 private static final int rawBitsFor1_17eN38To38[] = { 0x80800000, 0x82200000, 0x83c80000, 61 0x857a0000, 0x871c4000, 0x88c35000, 0x8a742400, 0x8c189680, 0x8dbebc20, 0x8f6e6b28, 62 0x911502f9, 0x92ba43b7, 0x9468d4a5, 0x961184e7, 0x97b5e621, 0x99635fa9, 0x9b0e1bca, 63 0x9cb1a2bc, 0x9e5e0b6b, 0xa00ac723, 0xa1ad78ec, 0xa358d727, 0xa5078678, 0xa6a96816, 64 0xa853c21c, 0xaa045951, 0xaba56fa6, 0xad4ecb8f, 0xaf013f39, 0xb0a18f08, 0xb249f2ca, 65 0xb3fc6f7c, 0xb59dc5ae, 0xb7453719, 0xb8f684df, 0xba9a130c, 0xbc4097ce, 0xbdf0bdc2, 66 0xbf967699, 0xc13c1440, 0xc2eb1950, 0xc492efd2, 0xc637abc6, 0xc7e596b8, 0xc98f7e33, 67 0xcb335dc0, 0xcce0352f, 0xce8c213e, 0xd02f298d, 0xd1daf3f0, 0xd388d876, 0xd52b0e94, 68 0xd6d5d239, 0xd885a363, 0xda270c3c, 0xdbd0cf4b, 0xdd82818f, 0xdf2321f3, 0xe0cbea70, 69 0xe27ee50b, 0xe41f4f27, 0xe5c722f1, 0xe778ebad, 0xe91b934c, 0xeac2781f, 0xec731627, 70 0xee17edd8, 0xefbde94f, 0xf16d63a2, 0xf3145e45, 0xf4b975d7, 0xf667d34c, 0xf810e410, 71 0xf9b51d14, 0xfb626459, 0xfd0d7eb7, 0xfeb0de65 }; 72 73 private static final String expectedStringFor1_17eN38To38[] = { "-1.17549435E-38", 74 "-1.1754944E-37", "-1.17549435E-36", "-1.17549435E-35", "-1.1754944E-34", 75 "-1.17549435E-33", "-1.17549435E-32", "-1.1754944E-31", "-1.17549435E-30", 76 "-1.17549435E-29", "-1.1754944E-28", "-1.1754943E-27", "-1.17549435E-26", 77 "-1.1754943E-25", "-1.1754944E-24", "-1.1754943E-23", "-1.1754944E-22", 78 "-1.1754943E-21", "-1.1754943E-20", "-1.1754943E-19", "-1.1754944E-18", 79 "-1.1754944E-17", "-1.1754943E-16", "-1.1754943E-15", "-1.1754944E-14", 80 "-1.1754943E-13", "-1.1754944E-12", "-1.1754943E-11", "-1.1754943E-10", 81 "-1.1754944E-9", "-1.1754944E-8", "-1.1754943E-7", "-1.1754944E-6", 82 "-1.1754943E-5", "-1.1754943E-4", "-0.0011754944", "-0.011754943", "-0.117549434", 83 "-1.1754943", "-11.754944", "-117.54944", "-1175.4944", "-11754.943", "-117549.44", 84 "-1175494.4", "-1.1754944E7", "-1.17549432E8", "-1.1754944E9", "-1.17549435E10", 85 "-1.17549433E11", "-1.17549433E12", "-1.17549438E13", "-1.17549438E14", 86 "-1.1754943E15", "-1.17549432E16", "-1.17549432E17", "-1.17549434E18", 87 "-1.1754944E19", "-1.1754944E20", "-1.1754943E21", "-1.1754943E22", 88 "-1.1754944E23", "-1.17549434E24", "-1.1754943E25", "-1.1754943E26", 89 "-1.17549434E27", "-1.1754943E28", "-1.1754944E29", "-1.1754943E30", 90 "-1.1754943E31", "-1.1754944E32", "-1.1754943E33", "-1.1754944E34", 91 "-1.1754944E35", "-1.1754944E36", "-1.1754943E37", "-1.1754943E38" }; 92 93 private void doTestCompareRawBits(String originalFloatString, int expectedRawBits, 94 String expectedString) { 95 int rawBits; 96 float result = Float.parseFloat(originalFloatString); 97 rawBits = Float.floatToIntBits(result); 98 assertEquals("Original float(" + originalFloatString + ") Converted float(" + result 99 + ") Expecting:" + Integer.toHexString(expectedRawBits) + " Got: " 100 + Integer.toHexString(rawBits), expectedRawBits, rawBits); 101 } 102 103 /** 104 * @tests java.lang.Float#Float(float) 105 */ 106 @TestInfo( 107 level = TestLevel.COMPLETE, 108 purpose = "", 109 targets = { 110 @TestTarget( 111 methodName = "Float", 112 methodArgs = {float.class} 113 ) 114 }) 115 public void test_ConstructorF() { 116 // Test for method java.lang.Float(float) 117 118 Float f = new Float(900.89f); 119 assertTrue("Created incorrect float", f.floatValue() == 900.89f); 120 } 121 122 /** 123 * @tests java.lang.Float#Float(java.lang.String) 124 */ 125 @TestInfo( 126 level = TestLevel.PARTIAL, 127 purpose = "Doesn't check illegal cases, NumberFormatException.", 128 targets = { 129 @TestTarget( 130 methodName = "Float", 131 methodArgs = {java.lang.String.class} 132 ) 133 }) 134 public void test_ConstructorLjava_lang_String() { 135 // Test for method java.lang.Float(java.lang.String) 136 137 Float f = new Float("900.89"); 138 assertTrue("Created incorrect Float", f.floatValue() == 900.89f); 139 } 140 141 /** 142 * @tests java.lang.Float#byteValue() 143 */ 144 @TestInfo( 145 level = TestLevel.COMPLETE, 146 purpose = "", 147 targets = { 148 @TestTarget( 149 methodName = "byteValue", 150 methodArgs = {} 151 ) 152 }) 153 public void test_byteValue() { 154 // Test for method byte java.lang.Float.byteValue() 155 Float f = new Float(0.46874f); 156 Float f2 = new Float(90.8f); 157 assertTrue("Returned incorrect byte value", f.byteValue() == 0 && f2.byteValue() == 90); 158 } 159 160 /** 161 * @tests java.lang.Float#compareTo(java.lang.Float) 162 * @tests java.lang.Float#compare(float, float) 163 */ 164 @TestInfo( 165 level = TestLevel.COMPLETE, 166 purpose = "", 167 targets = { 168 @TestTarget( 169 methodName = "compare", 170 methodArgs = {float.class, float.class} 171 ) 172 }) 173 public void test_compare() { 174 float[] values = new float[] { Float.NEGATIVE_INFINITY, -Float.MAX_VALUE, -2f, 175 -Float.MIN_VALUE, -0f, 0f, Float.MIN_VALUE, 2f, Float.MAX_VALUE, 176 Float.POSITIVE_INFINITY, Float.NaN }; 177 for (int i = 0; i < values.length; i++) { 178 float f1 = values[i]; 179 assertTrue("compare() should be equal: " + f1, Float.compare(f1, f1) == 0); 180 Float F1 = new Float(f1); 181 assertTrue("compareTo() should be equal: " + f1, F1.compareTo(F1) == 0); 182 for (int j = i + 1; j < values.length; j++) { 183 float f2 = values[j]; 184 assertTrue("compare() " + f1 + " should be less " + f2, 185 Float.compare(f1, f2) == -1); 186 assertTrue("compare() " + f2 + " should be greater " + f1, Float 187 .compare(f2, f1) == 1); 188 Float F2 = new Float(f2); 189 assertTrue("compareTo() " + f1 + " should be less " + f2, 190 F1.compareTo(F2) == -1); 191 assertTrue("compareTo() " + f2 + " should be greater " + f1, 192 F2.compareTo(F1) == 1); 193 } 194 } 195 196 try { 197 new Float(0.0F).compareTo(null); 198 fail("No NPE"); 199 } catch (NullPointerException e) { 200 } 201 } 202 203 /** 204 * @tests java.lang.Float#doubleValue() 205 */ 206 @TestInfo( 207 level = TestLevel.COMPLETE, 208 purpose = "", 209 targets = { 210 @TestTarget( 211 methodName = "doubleValue", 212 methodArgs = {} 213 ) 214 }) 215 public void test_doubleValue() { 216 // Test for method double java.lang.Float.doubleValue() 217 assertTrue("Incorrect double value returned", Math.abs(new Float(999999.999f) 218 .doubleValue() - 999999.999d) < 1); 219 } 220 221 /** 222 * @tests java.lang.Float#floatToIntBits(float) 223 */ 224 @TestInfo( 225 level = TestLevel.PARTIAL, 226 purpose = "Doesn't verify boundary values according to specification.", 227 targets = { 228 @TestTarget( 229 methodName = "floatToIntBits", 230 methodArgs = {float.class} 231 ) 232 }) 233 public void test_floatToIntBitsF() { 234 float f = 9876.2345f; 235 int bits = Float.floatToIntBits(f); 236 float r = Float.intBitsToFloat(bits); 237 assertTrue("Incorrect intBits returned", f == r); 238 } 239 240 /** 241 * @tests java.lang.Float#floatToRawIntBits(float) 242 */ 243 @TestInfo( 244 level = TestLevel.PARTIAL, 245 purpose = "Doesn't verify boundary values according to specification.", 246 targets = { 247 @TestTarget( 248 methodName = "floatToRawIntBits", 249 methodArgs = {float.class} 250 ) 251 }) 252 public void test_floatToRawIntBitsF() { 253 int i = 0x7fc004d2; 254 float f = Float.intBitsToFloat(i); 255 assertTrue("Wrong raw bits", Float.floatToRawIntBits(f) == i); 256 } 257 258 /** 259 * @tests java.lang.Float#floatValue() 260 */ 261 @TestInfo( 262 level = TestLevel.COMPLETE, 263 purpose = "", 264 targets = { 265 @TestTarget( 266 methodName = "floatValue", 267 methodArgs = {} 268 ) 269 }) 270 public void test_floatValue() { 271 // Test for method float java.lang.Float.floatValue() 272 Float f = new Float(87.657f); 273 Float f2 = new Float(-0.876f); 274 assertTrue("Returned incorrect floatValue", f.floatValue() == 87.657f 275 && (f2.floatValue() == -0.876f)); 276 277 } 278 279 /** 280 * @tests java.lang.Float#hashCode() 281 */ 282 @TestInfo( 283 level = TestLevel.PARTIAL, 284 purpose = "Doesn't verify hash codes of non equal objects.", 285 targets = { 286 @TestTarget( 287 methodName = "hashCode", 288 methodArgs = {} 289 ) 290 }) 291 public void test_hashCode() { 292 // Test for method int java.lang.Float.hashCode() 293 Float f = new Float(1908.8786f); 294 assertTrue("Returned invalid hash code for 1908.8786f", f.hashCode() == Float 295 .floatToIntBits(1908.8786f)); 296 297 f = new Float(-1.112f); 298 assertTrue("Returned invalid hash code for -1.112", f.hashCode() == Float 299 .floatToIntBits(-1.112f)); 300 301 f = new Float(0f); 302 assertTrue("Returned invalid hash code for 0", f.hashCode() == Float.floatToIntBits(0f)); 303 304 } 305 306 /** 307 * @tests java.lang.Float#intBitsToFloat(int) 308 */ 309 @TestInfo( 310 level = TestLevel.PARTIAL, 311 purpose = "Doesn't verify boundary values according to specification.", 312 targets = { 313 @TestTarget( 314 methodName = "intBitsToFloat", 315 methodArgs = {int.class} 316 ) 317 }) 318 public void test_intBitsToFloatI() { 319 float f = 9876.2345f; 320 int bits = Float.floatToIntBits(f); 321 float r = Float.intBitsToFloat(bits); 322 assertEquals("Incorrect intBits returned", f, r, 0F); 323 } 324 325 /** 326 * @tests java.lang.Float#intValue() 327 */ 328 @TestInfo( 329 level = TestLevel.PARTIAL, 330 purpose = "Doesn't verify boundary values.", 331 targets = { 332 @TestTarget( 333 methodName = "intValue", 334 methodArgs = {} 335 ) 336 }) 337 public void test_intValue() { 338 // Test for method int java.lang.Float.intValue() 339 Float f = new Float(0.46874f); 340 Float f2 = new Float(90.8f); 341 assertTrue("Returned incorrect int value", f.intValue() == 0 && f2.intValue() == 90); 342 } 343 344 /** 345 * @tests java.lang.Float#isInfinite() 346 */ 347 @TestInfo( 348 level = TestLevel.COMPLETE, 349 purpose = "", 350 targets = { 351 @TestTarget( 352 methodName = "isInfinite", 353 methodArgs = {} 354 ) 355 }) 356 public void test_isInfinite() { 357 // Test for method boolean java.lang.Float.isInfinite() 358 assertTrue("Infinity check failed", 359 (new Float(Float.POSITIVE_INFINITY).isInfinite() && new Float( 360 Float.NEGATIVE_INFINITY).isInfinite()) 361 && !(new Float(0.13131414f).isInfinite())); 362 } 363 364 /** 365 * @tests java.lang.Float#isInfinite(float) 366 */ 367 @TestInfo( 368 level = TestLevel.COMPLETE, 369 purpose = "", 370 targets = { 371 @TestTarget( 372 methodName = "isInfinite", 373 methodArgs = {float.class} 374 ) 375 }) 376 public void test_isInfiniteF() { 377 // Test for method boolean java.lang.Float.isInfinite(float) 378 379 assertTrue("Infinity check failed", Float.isInfinite(Float.POSITIVE_INFINITY) 380 && (Float.isInfinite(Float.NEGATIVE_INFINITY)) && !(Float.isInfinite(1.0f))); 381 } 382 383 /** 384 * @tests java.lang.Float#isNaN() 385 */ 386 @TestInfo( 387 level = TestLevel.COMPLETE, 388 purpose = "", 389 targets = { 390 @TestTarget( 391 methodName = "isNaN", 392 methodArgs = {} 393 ) 394 }) 395 public void test_isNaN() { 396 // Test for method boolean java.lang.Float.isNaN() 397 assertTrue("NAN check failed", new Float(Float.NaN).isNaN() 398 && !(new Float(1.0f).isNaN())); 399 } 400 401 /** 402 * @tests java.lang.Float#isNaN(float) 403 */ 404 @TestInfo( 405 level = TestLevel.PARTIAL, 406 purpose = "Doesn't check boundary values.", 407 targets = { 408 @TestTarget( 409 methodName = "isNaN", 410 methodArgs = {float.class} 411 ) 412 }) 413 public void test_isNaNF() { 414 // Test for method boolean java.lang.Float.isNaN(float) 415 assertTrue("NaN check failed", Float.isNaN(Float.NaN) && !(Float.isNaN(12.09f))); 416 } 417 418 /** 419 * @tests java.lang.Float#longValue() 420 */ 421 @TestInfo( 422 level = TestLevel.PARTIAL, 423 purpose = "Doesn't verify boundary values.", 424 targets = { 425 @TestTarget( 426 methodName = "longValue", 427 methodArgs = {} 428 ) 429 }) 430 public void test_longValue() { 431 // Test for method long java.lang.Float.longValue() 432 Float f = new Float(0.46874f); 433 Float f2 = new Float(90.8f); 434 assertTrue("Returned incorrect long value", f.longValue() == 0 && f2.longValue() == 90); 435 } 436 437 /** 438 * @tests java.lang.Float#parseFloat(java.lang.String) 439 */ 440 @TestInfo( 441 level = TestLevel.PARTIAL, 442 purpose = "Verifies boundary cases.", 443 targets = { 444 @TestTarget( 445 methodName = "parseFloat", 446 methodArgs = {java.lang.String.class} 447 ) 448 }) 449 public void test_parseFloatLjava_lang_String() { 450 assertEquals("Incorrect float returned, expected zero.", 0.0, Float 451 .parseFloat("7.0064923216240853546186479164495e-46"), 0.0); 452 assertEquals("Incorrect float returned, expected minimum float.", Float.MIN_VALUE, 453 Float.parseFloat("7.0064923216240853546186479164496e-46"), 0.0); 454 455 doTestCompareRawBits( 456 "0.000000000000000000000000000000000000011754942807573642917278829910357665133228589927589904276829631184250030649651730385585324256680905818939208984375", 457 0x800000, "1.17549435E-38"); 458 doTestCompareRawBits( 459 "0.00000000000000000000000000000000000001175494280757364291727882991035766513322858992758990427682963118425003064965173038558532425668090581893920898437499999f", 460 0x7fffff, "1.1754942E-38"); 461 462 /* Test a set of regular floats with exponents from -38 to +38 */ 463 for (int i = 38; i > 3; i--) { 464 String testString; 465 testString = "3.4028234663852886e-" + i; 466 doTestCompareRawBits(testString, rawBitsFor3_4eN38To38[38 - i], 467 expectedStringFor3_4eN38To38[38 - i]); 468 } 469 doTestCompareRawBits("3.4028234663852886e-3", rawBitsFor3_4eN38To38[38 - 3], 470 expectedStringFor3_4eN38To38[38 - 3]); 471 doTestCompareRawBits("3.4028234663852886e-2", rawBitsFor3_4eN38To38[38 - 2], 472 expectedStringFor3_4eN38To38[38 - 2]); 473 doTestCompareRawBits("3.4028234663852886e-1", rawBitsFor3_4eN38To38[38 - 1], 474 expectedStringFor3_4eN38To38[38 - 1]); 475 doTestCompareRawBits("3.4028234663852886e-0", rawBitsFor3_4eN38To38[38 - 0], 476 expectedStringFor3_4eN38To38[38 - 0]); 477 doTestCompareRawBits("3.4028234663852886e+1", rawBitsFor3_4eN38To38[38 + 1], 478 expectedStringFor3_4eN38To38[38 + 1]); 479 doTestCompareRawBits("3.4028234663852886e+2", rawBitsFor3_4eN38To38[38 + 2], 480 expectedStringFor3_4eN38To38[38 + 2]); 481 doTestCompareRawBits("3.4028234663852886e+3", rawBitsFor3_4eN38To38[38 + 3], 482 expectedStringFor3_4eN38To38[38 + 3]); 483 doTestCompareRawBits("3.4028234663852886e+4", rawBitsFor3_4eN38To38[38 + 4], 484 expectedStringFor3_4eN38To38[38 + 4]); 485 doTestCompareRawBits("3.4028234663852886e+5", rawBitsFor3_4eN38To38[38 + 5], 486 expectedStringFor3_4eN38To38[38 + 5]); 487 doTestCompareRawBits("3.4028234663852886e+6", rawBitsFor3_4eN38To38[38 + 6], 488 expectedStringFor3_4eN38To38[38 + 6]); 489 490 for (int i = 7; i < 39; i++) { 491 String testString; 492 testString = "3.4028234663852886e+" + i; 493 doTestCompareRawBits(testString, rawBitsFor3_4eN38To38[38 + i], 494 expectedStringFor3_4eN38To38[38 + i]); 495 } 496 497 /* Test another set of regular floats with exponents from -38 to +38 */ 498 for (int i = 38; i > 3; i--) { 499 String testString; 500 testString = "-1.1754943508222875e-" + i; 501 doTestCompareRawBits(testString, rawBitsFor1_17eN38To38[38 - i], 502 expectedStringFor1_17eN38To38[38 - i]); 503 } 504 doTestCompareRawBits("-1.1754943508222875e-3", rawBitsFor1_17eN38To38[38 - 3], 505 expectedStringFor1_17eN38To38[38 - 3]); 506 doTestCompareRawBits("-1.1754943508222875e-2", rawBitsFor1_17eN38To38[38 - 2], 507 expectedStringFor1_17eN38To38[38 - 2]); 508 doTestCompareRawBits("-1.1754943508222875e-1", rawBitsFor1_17eN38To38[38 - 1], 509 expectedStringFor1_17eN38To38[38 - 1]); 510 doTestCompareRawBits("-1.1754943508222875e-0", rawBitsFor1_17eN38To38[38 - 0], 511 expectedStringFor1_17eN38To38[38 - 0]); 512 doTestCompareRawBits("-1.1754943508222875e+1", rawBitsFor1_17eN38To38[38 + 1], 513 expectedStringFor1_17eN38To38[38 + 1]); 514 doTestCompareRawBits("-1.1754943508222875e+2", rawBitsFor1_17eN38To38[38 + 2], 515 expectedStringFor1_17eN38To38[38 + 2]); 516 doTestCompareRawBits("-1.1754943508222875e+3", rawBitsFor1_17eN38To38[38 + 3], 517 expectedStringFor1_17eN38To38[38 + 3]); 518 doTestCompareRawBits("-1.1754943508222875e+4", rawBitsFor1_17eN38To38[38 + 4], 519 expectedStringFor1_17eN38To38[38 + 4]); 520 doTestCompareRawBits("-1.1754943508222875e+5", rawBitsFor1_17eN38To38[38 + 5], 521 expectedStringFor1_17eN38To38[38 + 5]); 522 doTestCompareRawBits("-1.1754943508222875e+6", rawBitsFor1_17eN38To38[38 + 6], 523 expectedStringFor1_17eN38To38[38 + 6]); 524 525 for (int i = 7; i < 39; i++) { 526 String testString; 527 testString = "-1.1754943508222875e+" + i; 528 doTestCompareRawBits(testString, rawBitsFor1_17eN38To38[38 + i], 529 expectedStringFor1_17eN38To38[38 + i]); 530 } 531 532 /* Test denormalized floats (floats with exponents <= -38 */ 533 doTestCompareRawBits("1.1012984643248170E-45", 1, "1.4E-45"); 534 doTestCompareRawBits("-1.1012984643248170E-45", 0x80000001, "-1.4E-45"); 535 doTestCompareRawBits("1.0E-45", 1, "1.4E-45"); 536 doTestCompareRawBits("-1.0E-45", 0x80000001, "-1.4E-45"); 537 doTestCompareRawBits("0.9E-45", 1, "1.4E-45"); 538 doTestCompareRawBits("-0.9E-45", 0x80000001, "-1.4E-45"); 539 doTestCompareRawBits("4.203895392974451e-45", 3, "4.2E-45"); 540 doTestCompareRawBits("-4.203895392974451e-45", 0x80000003, "-4.2E-45"); 541 doTestCompareRawBits("0.004E-45", 0, "0.0"); 542 doTestCompareRawBits("-0.004E-45", 0x80000000, "-0.0"); 543 544 /* 545 * Test for large floats close to and greater than 3.4028235E38 and 546 * -3.4028235E38 547 */ 548 doTestCompareRawBits("1.2E+38", 0x7eb48e52, "1.2E38"); 549 doTestCompareRawBits("-1.2E+38", 0xfeb48e52, "-1.2E38"); 550 doTestCompareRawBits("3.2E+38", 0x7f70bdc2, "3.2E38"); 551 doTestCompareRawBits("-3.2E+38", 0xff70bdc2, "-3.2E38"); 552 doTestCompareRawBits("3.4E+38", 0x7f7fc99e, "3.4E38"); 553 doTestCompareRawBits("-3.4E+38", 0xff7fc99e, "-3.4E38"); 554 doTestCompareRawBits("3.4028234663852886E+38", 0x7f7fffff, "3.4028235E38"); 555 doTestCompareRawBits("-3.4028234663852886E+38", 0xff7fffff, "-3.4028235E38"); 556 doTestCompareRawBits("3.405E+38", 0x7f800000, "Infinity"); 557 doTestCompareRawBits("-3.405E+38", 0xff800000, "-Infinity"); 558 doTestCompareRawBits("3.41E+38", 0x7f800000, "Infinity"); 559 doTestCompareRawBits("-3.41E+38", 0xff800000, "-Infinity"); 560 doTestCompareRawBits("3.42E+38", 0x7f800000, "Infinity"); 561 doTestCompareRawBits("-3.42E+38", 0xff800000, "-Infinity"); 562 doTestCompareRawBits("1.0E+39", 0x7f800000, "Infinity"); 563 doTestCompareRawBits("-1.0E+39", 0xff800000, "-Infinity"); 564 } 565 566 /** 567 * @tests java.lang.Float#parseFloat(java.lang.String) 568 */ 569 @TestInfo( 570 level = TestLevel.PARTIAL, 571 purpose = "Verifies boundary values.", 572 targets = { 573 @TestTarget( 574 methodName = "parseFloat", 575 methodArgs = {java.lang.String.class} 576 ) 577 }) 578 public void test_parseFloat_LString_Unusual() { 579 float actual; 580 581 actual = Float.parseFloat("0x00000000000000000000000000000000000000000.0000000000000000000000000000000000000p0000000000000000000000000000000000"); 582 assertEquals("Returned incorrect value", 0.0f, actual, 0.0F); 583 584 actual = Float.parseFloat("+0Xfffff.fffffffffffffffffffffffffffffffp+99F"); 585 assertEquals("Returned incorrect value", 6.64614E35f, actual, 0.0F); 586 587 actual = Float.parseFloat("-0X.123456789abcdefp+99f"); 588 assertEquals("Returned incorrect value", -4.5072022E28f, actual, 0.0F); 589 590 actual = Float.parseFloat("-0X123456789abcdef.p+1f"); 591 assertEquals("Returned incorrect value", -1.63971062E17f, actual, 0.0F); 592 593 actual = Float.parseFloat("-0X000000000000000000000000000001abcdef.0000000000000000000000000001abefp00000000000000000000000000000000000000000004f"); 594 assertEquals("Returned incorrect value", -4.48585472E8f, actual, 0.0F); 595 596 actual = Float.parseFloat("0X0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001234p600f"); 597 assertEquals("Returned incorrect value", 5.907252E33f, actual, 0.0F); 598 599 actual = Float.parseFloat("0x1.p9223372036854775807"); 600 assertEquals("Returned incorrect value", Float.POSITIVE_INFINITY, actual, 0.0F); 601 602 actual = Float.parseFloat("0x1.p9223372036854775808"); 603 assertEquals("Returned incorrect value", Float.POSITIVE_INFINITY, actual, 0.0F); 604 605 actual = Float.parseFloat("0x10.p9223372036854775808"); 606 assertEquals("Returned incorrect value", Float.POSITIVE_INFINITY, actual, 0.0F); 607 608 actual = Float.parseFloat("0xabcd.ffffffffp+2000"); 609 assertEquals("Returned incorrect value", Float.POSITIVE_INFINITY, actual, 0.0F); 610 611 actual = Float.parseFloat("0x1.p-9223372036854775808"); 612 assertEquals("Returned incorrect value", 0.0f, actual, 0.0F); 613 614 actual = Float.parseFloat("0x1.p-9223372036854775809"); 615 assertEquals("Returned incorrect value", 0.0f, actual, 0.0F); 616 617 actual = Float.parseFloat("0x.1p-9223372036854775809"); 618 assertEquals("Returned incorrect value", 0.0f, actual, 0.0F); 619 } 620 621 /** 622 * @tests java.lang.Float#parseFloat(java.lang.String) 623 */ 624 @TestInfo( 625 level = TestLevel.PARTIAL, 626 purpose = "Verifies positive exponent.", 627 targets = { 628 @TestTarget( 629 methodName = "parseFloat", 630 methodArgs = {java.lang.String.class} 631 ) 632 }) 633 public void test_parseFloat_LString_NormalPositiveExponent() { 634 int[] expecteds = { 635 0x3991a2b4, 0x43cc0247, 0x47909009, 636 0x4ac0c009, 0x4e109005, 0x5140c005, 637 0x5458d805, 0x57848402, 0x5a909002, 638 0x5da8a802, 0x60c0c002, 0x63cccc02, 639 0x66e4e402, 0x69f0f002, 0x6d048401, 640 0x70109001, 0x73169601, 0x76810810, 641 0x79840840, 0x7c8a08a0, 0x7f800000, 642 0x7f800000, 0x7f800000, 0x7f800000, 643 0x7f800000, 644 }; 645 646 for (int i = 0; i < expecteds.length; i++) { 647 int part = i * 6; 648 String inputString = "0x" + part + "." + part + "0123456789abcdefp" + part; 649 650 float actual = Float.parseFloat(inputString); 651 float expected = Float.intBitsToFloat(expecteds[i]); 652 653 String expectedString = Integer.toHexString(Float.floatToIntBits(expected)); 654 String actualString = Integer.toHexString(Float.floatToIntBits(actual)); 655 String errorMsg = i + "th input string is:<" + inputString 656 + ">.The expected result should be:<" + expectedString 657 + ">, but was: <" + actualString + ">. "; 658 659 assertEquals(errorMsg, expected, actual, 0.0F); 660 } 661 } 662 663 /** 664 * @tests java.lang.Float#parseFloat(java.lang.String) 665 */ 666 @TestInfo( 667 level = TestLevel.PARTIAL, 668 purpose = "Verifies negative exponent.", 669 targets = { 670 @TestTarget( 671 methodName = "parseFloat", 672 methodArgs = {java.lang.String.class} 673 ) 674 }) 675 public void test_parseFloat_LString_NormalNegativeExponent() { 676 int[] expecteds = { 677 0x3991a2b4, 678 0x3d6e0247, 679 0x3aa0a009, 680 0x37848405, 681 0x3420a005, 682 0x30d4d405, 683 0x2d848402, 684 0x2a129202, 685 0x26acac02, 686 0x2346c602, 687 0x1fe0e002, 688 0x1c6eee02, 689 0x19048401, 690 0x15919101, 691 0x12189801, 692 0xf028828, 693 0xb890890, 694 0x80c88c8, 695 0x4930930, 696 0x1198998, 697 0x28028, 698 0x51c, 699 0xb, 700 0x0, 701 0x0, 702 }; 703 704 for (int i = 0; i < expecteds.length; i++) { 705 int part = i * 7; 706 String inputString = "0x" + part + "." + part + "0123456789abcdefp-" + part; 707 708 float actual = Float.parseFloat(inputString); 709 float expected = Float.intBitsToFloat(expecteds[i]); 710 711 String expectedString = Integer.toHexString(Float.floatToIntBits(expected)); 712 String actualString = Integer.toHexString(Float.floatToIntBits(actual)); 713 String errorMsg = i + "th input string is:<" + inputString 714 + ">.The expected result should be:<" + expectedString 715 + ">, but was: <" + actualString + ">. "; 716 717 assertEquals(errorMsg, expected, actual, 0.0F); 718 } 719 } 720 721 /** 722 * @tests java.lang.Float#parseFloat(java.lang.String) 723 */ 724 @TestInfo( 725 level = TestLevel.PARTIAL, 726 purpose = "Verifies max boundary value. " + 727 "Doesn't verify NumberFormatException.", 728 targets = { 729 @TestTarget( 730 methodName = "parseFloat", 731 methodArgs = {java.lang.String.class} 732 ) 733 }) 734 public void test_parseFloat_LString_MaxNormalBoundary() { 735 int[] expecteds ={ 736 0x7f7fffff, 737 0x7f7fffff, 738 0x7f7fffff, 739 0x7f800000, 740 0x7f800000, 741 0x7f800000, 742 743 0xff7fffff, 744 0xff7fffff, 745 0xff7fffff, 746 0xff800000, 747 0xff800000, 748 0xff800000, 749 }; 750 751 String[] inputs = { 752 "0x1.fffffep127", 753 "0x1.fffffe000000000000000000000000000000000000000000000001p127", 754 "0x1.fffffeffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp127", 755 "0x1.ffffffp127", 756 "0x1.ffffff000000000000000000000000000000000000000000000001p127", 757 "0x1.ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp127", 758 759 "-0x1.fffffep127", 760 "-0x1.fffffe000000000000000000000000000000000000000000000001p127", 761 "-0x1.fffffeffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp127", 762 "-0x1.ffffffp127", 763 "-0x1.ffffff000000000000000000000000000000000000000000000001p127", 764 "-0x1.ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp127", 765 }; 766 767 for (int i = 0; i < inputs.length; i++) { 768 float actual = Float.parseFloat(inputs[i]); 769 float expected = Float.intBitsToFloat(expecteds[i]); 770 771 String expectedString = Integer.toHexString(Float.floatToIntBits(expected)); 772 String actualString = Integer.toHexString(Float.floatToIntBits(actual)); 773 String errorMsg = i + "th input string is:<" + inputs[i] 774 + ">.The expected result should be:<" + expectedString 775 + ">, but was: <" + actualString + ">. "; 776 777 assertEquals(errorMsg, expected, actual, 0.0F); 778 } 779 } 780 781 /** 782 * @tests java.lang.Float#parseFloat(java.lang.String) 783 */ 784 @TestInfo( 785 level = TestLevel.PARTIAL, 786 purpose = "Verifies min boundary value.", 787 targets = { 788 @TestTarget( 789 methodName = "parseFloat", 790 methodArgs = {java.lang.String.class} 791 ) 792 }) 793 public void test_parseFloat_LString_MinNormalBoundary() { 794 int expecteds[] = { 795 0x800000, 796 0x800000, 797 0x800000, 798 0x800000, 799 0x800001, 800 0x800001, 801 802 0x80800000, 803 0x80800000, 804 0x80800000, 805 0x80800000, 806 0x80800001, 807 0x80800001, 808 }; 809 810 String inputs[] = { 811 "0x1.0p-126", 812 "0x1.00000000000000000000000000000000000000000000001p-126", 813 "0x1.000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 814 "0x1.000001p-126", 815 "0x1.000001000000000000000000000000000000000000000001p-126", 816 "0x1.000001fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 817 818 "-0x1.0p-126", 819 "-0x1.00000000000000000000000000000000000000000000001p-126", 820 "-0x1.000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 821 "-0x1.000001p-126", 822 "-0x1.000001000000000000000000000000000000000000000001p-126", 823 "-0x1.000001fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 824 }; 825 826 for (int i = 0; i < inputs.length; i++) { 827 float actual = Float.parseFloat(inputs[i]); 828 float expected = Float.intBitsToFloat(expecteds[i]); 829 830 String expectedString = Integer.toHexString(Float.floatToIntBits(expected)); 831 String actualString = Integer.toHexString(Float.floatToIntBits(actual)); 832 String errorMsg = i + "th input string is:<" + inputs[i] 833 + ">.The expected result should be:<" + expectedString 834 + ">, but was: <" + actualString + ">. "; 835 836 assertEquals(errorMsg, expected, actual, 0.0F); 837 } 838 } 839 840 /** 841 * @tests java.lang.Float#parseFloat(java.lang.String) 842 */ 843 @TestInfo( 844 level = TestLevel.PARTIAL, 845 purpose = "Verifies max boundary value.", 846 targets = { 847 @TestTarget( 848 methodName = "parseFloat", 849 methodArgs = {java.lang.String.class} 850 ) 851 }) 852 public void test_parseFloat_LString_MaxSubNormalBoundary() { 853 int expecteds[] = { 854 0x7fffff, 855 0x7fffff, 856 0x7fffff, 857 0x800000, 858 0x800000, 859 0x800000, 860 861 0x807fffff, 862 0x807fffff, 863 0x807fffff, 864 0x80800000, 865 0x80800000, 866 0x80800000, 867 }; 868 869 String inputs[] = { 870 "0x0.fffffep-126", 871 "0x0.fffffe000000000000000000000000000000000000000000000000000001p-126", 872 "0x0.fffffefffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 873 "0x0.ffffffp-126", 874 "0x0.ffffff0000000000000000000000000000000000000000000000000000001p-126", 875 "0x0.ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 876 877 "-0x0.fffffep-126", 878 "-0x0.fffffe000000000000000000000000000000000000000000000000000001p-126", 879 "-0x0.fffffefffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 880 "-0x0.ffffffp-126", 881 "-0x0.ffffff0000000000000000000000000000000000000000000000000000001p-126", 882 "-0x0.ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 883 }; 884 885 for (int i = 0; i < inputs.length; i++) { 886 float actual = Float.parseFloat(inputs[i]); 887 float expected = Float.intBitsToFloat(expecteds[i]); 888 889 String expectedString = Integer.toHexString(Float.floatToIntBits(expected)); 890 String actualString = Integer.toHexString(Float.floatToIntBits(actual)); 891 String errorMsg = i + "th input string is:<" + inputs[i] 892 + ">.The expected result should be:<" + expectedString 893 + ">, but was: <" + actualString + ">. "; 894 895 assertEquals(errorMsg, expected, actual, 0.0F); 896 } 897 } 898 899 /** 900 * @tests java.lang.Float#parseFloat(java.lang.String) 901 */ 902 @TestInfo( 903 level = TestLevel.PARTIAL, 904 purpose = "Verifies min boundary value.", 905 targets = { 906 @TestTarget( 907 methodName = "parseFloat", 908 methodArgs = {java.lang.String.class} 909 ) 910 }) 911 public void test_parseFloat_LString_MinSubNormalBoundary() { 912 int expecteds[] = { 913 0x1, 914 0x1, 915 0x1, 916 0x2, 917 0x2, 918 0x2, 919 920 0x80000001, 921 0x80000001, 922 0x80000001, 923 0x80000002, 924 0x80000002, 925 0x80000002, 926 }; 927 928 String inputs[] = { 929 "0x0.000002p-126", 930 "0x0.00000200000000000000000000000000000000000001p-126", 931 "0x0.000002ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 932 "0x0.000003p-126", 933 "0x0.000003000000000000000000000000000000000000001p-126", 934 "0x0.000003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 935 936 "-0x0.000002p-126", 937 "-0x0.00000200000000000000000000000000000000000001p-126", 938 "-0x0.000002ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 939 "-0x0.000003p-126", 940 "-0x0.000003000000000000000000000000000000000000001p-126", 941 "-0x0.000003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 942 }; 943 944 for (int i = 0; i < inputs.length; i++) { 945 float actual = Float.parseFloat(inputs[i]); 946 float expected = Float.intBitsToFloat(expecteds[i]); 947 948 String expectedString = Integer.toHexString(Float.floatToIntBits(expected)); 949 String actualString = Integer.toHexString(Float.floatToIntBits(actual)); 950 String errorMsg = i + "th input string is:<" + inputs[i] 951 + ">.The expected result should be:<" + expectedString 952 + ">, but was: <" + actualString + ">. "; 953 954 assertEquals(errorMsg, expected, actual, 0.0F); 955 } 956 } 957 958 /** 959 * @tests java.lang.Float#parseFloat(java.lang.String) 960 */ 961 @TestInfo( 962 level = TestLevel.PARTIAL, 963 purpose = "Verifies zero boundary value.", 964 targets = { 965 @TestTarget( 966 methodName = "parseFloat", 967 methodArgs = {java.lang.String.class} 968 ) 969 }) 970 public void test_parseFloat_LString_ZeroBoundary() { 971 int expecteds[] = { 972 0x0, 973 0x0, 974 0x0, 975 0x0, 976 0x1, 977 0x1, 978 979 0x80000000, 980 0x80000000, 981 0x80000000, 982 0x80000000, 983 0x80000001, 984 0x80000001, 985 }; 986 987 String inputs[] = { 988 "0x0.000000000000000p-126", 989 "0x0.000000000000000000000000000000000000000000000001p-126", 990 "0x0.000000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 991 "0x0.000001p-126", 992 "0x0.000001000000000000000000000000000000000000000001p-126", 993 "0x0.000001fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 994 995 "-0x0.000000000000000p-126", 996 "-0x0.000000000000000000000000000000000000000000000001p-126", 997 "-0x0.000000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 998 "-0x0.000001p-126", 999 "-0x0.000001000000000000000000000000000000000000000001p-126", 1000 "-0x0.000001fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 1001 }; 1002 1003 for (int i = 0; i < inputs.length; i++) { 1004 float actual = Float.parseFloat(inputs[i]); 1005 float expected = Float.intBitsToFloat(expecteds[i]); 1006 1007 String expectedString = Integer.toHexString(Float.floatToIntBits(expected)); 1008 String actualString = Integer.toHexString(Float.floatToIntBits(actual)); 1009 String errorMsg = i + "th input string is:<" + inputs[i] 1010 + ">.The expected result should be:<" + expectedString 1011 + ">, but was: <" + actualString + ">. "; 1012 1013 assertEquals(errorMsg, expected, actual, 0.0F); 1014 } 1015 } 1016 1017 /** 1018 * @tests java.lang.Float#shortValue() 1019 */ 1020 @TestInfo( 1021 level = TestLevel.COMPLETE, 1022 purpose = "", 1023 targets = { 1024 @TestTarget( 1025 methodName = "shortValue", 1026 methodArgs = {} 1027 ) 1028 }) 1029 public void test_shortValue() { 1030 // Test for method short java.lang.Float.shortValue() 1031 Float f = new Float(0.46874f); 1032 Float f2 = new Float(90.8f); 1033 assertTrue("Returned incorrect short value", f.shortValue() == 0 1034 && f2.shortValue() == 90); 1035 } 1036 1037 /** 1038 * @tests java.lang.Float#toString() 1039 */ 1040 @TestInfo( 1041 level = TestLevel.COMPLETE, 1042 purpose = "", 1043 targets = { 1044 @TestTarget( 1045 methodName = "toString", 1046 methodArgs = {} 1047 ) 1048 }) 1049 public void test_toString() { 1050 // Test for method java.lang.String java.lang.Float.toString() 1051 1052 test_toString(12.90898f, "12.90898"); 1053 1054 test_toString(1.7014118346046924e+38F, "1.7014118E38"); 1055 } 1056 1057 /** 1058 * @tests java.lang.Float#toString(float) 1059 */ 1060 @TestInfo( 1061 level = TestLevel.COMPLETE, 1062 purpose = "", 1063 targets = { 1064 @TestTarget( 1065 methodName = "toString", 1066 methodArgs = {float.class} 1067 ) 1068 }) 1069 public void test_toStringF() { 1070 // Test for method java.lang.String java.lang.Float.toString(float) 1071 1072 float ff; 1073 String answer; 1074 1075 ff = 12.90898f; 1076 answer = "12.90898"; 1077 assertTrue("Incorrect String representation want " + answer + ", got " 1078 + Float.toString(ff), Float.toString(ff).equals(answer)); 1079 1080 ff = Float.MAX_VALUE; 1081 answer = "3.4028235E38"; 1082 assertTrue("Incorrect String representation want " + answer + ", got " 1083 + Float.toString(ff), Float.toString(ff).equals(answer)); 1084 } 1085 1086 /** 1087 * @tests java.lang.Float#valueOf(java.lang.String) 1088 */ 1089 @TestInfo( 1090 level = TestLevel.COMPLETE, 1091 purpose = "", 1092 targets = { 1093 @TestTarget( 1094 methodName = "valueOf", 1095 methodArgs = {java.lang.String.class} 1096 ) 1097 }) 1098 public void test_valueOfLjava_lang_String() { 1099 // Test for method java.lang.Float 1100 // java.lang.Float.valueOf(java.lang.String) 1101 1102 Float wanted = new Float(432.1235f); 1103 Float got = Float.valueOf("432.1235"); 1104 assertTrue("Incorrect float returned--wanted: " + wanted + " but got: " + got, got 1105 .equals(wanted)); 1106 1107 wanted = new Float(0f); 1108 got = Float.valueOf("0"); 1109 assertTrue("Incorrect float returned--wanted: " + wanted + " but got: " + got, got 1110 .equals(wanted)); 1111 1112 wanted = new Float(-1212.3232f); 1113 got = Float.valueOf("-1212.3232"); 1114 assertTrue("Incorrect float returned--wanted: " + wanted + " but got: " + got, got 1115 .equals(wanted)); 1116 1117 try { 1118 Float.valueOf(null); 1119 fail("Expected Float.valueOf(null) to throw NPE."); 1120 } catch (NullPointerException ex) { 1121 // expected 1122 } catch (Exception ex) { 1123 fail("Expected Float.valueOf(null) to throw NPE not " + ex.getClass().getName()); 1124 } 1125 1126 try { 1127 Float.valueOf(""); 1128 fail("Expected Float.valueOf(\"\") to throw NFE"); 1129 } catch (NumberFormatException e) { 1130 // expected 1131 } 1132 1133 Float posZero = Float.valueOf("+0.0"); 1134 Float negZero = Float.valueOf("-0.0"); 1135 assertFalse("Floattest0", posZero.equals(negZero)); 1136 assertTrue("Floattest1", 0.0f == -0.0f); 1137 1138 // Tests for float values by name. 1139 Float expectedNaN = new Float(Float.NaN); 1140 1141 Float posNaN = Float.valueOf("NaN"); 1142 assertTrue("Floattest2", posNaN.equals(expectedNaN)); 1143 1144 Float posNaNSigned = Float.valueOf("+NaN"); 1145 assertTrue("Floattest3", posNaNSigned.equals(expectedNaN)); 1146 1147 Float negNaNSigned = Float.valueOf("-NaN"); 1148 assertTrue("Floattest4", negNaNSigned.equals(expectedNaN)); 1149 1150 Float posInfinite = Float.valueOf("Infinity"); 1151 assertTrue("Floattest5", posInfinite.equals(new Float(Float.POSITIVE_INFINITY))); 1152 1153 Float posInfiniteSigned = Float.valueOf("+Infinity"); 1154 assertTrue("Floattest6", posInfiniteSigned.equals(new Float(Float.POSITIVE_INFINITY))); 1155 1156 Float negInfiniteSigned = Float.valueOf("-Infinity"); 1157 assertTrue("Floattest7", negInfiniteSigned.equals(new Float(Float.NEGATIVE_INFINITY))); 1158 } 1159 1160 private void test_toString(float ff, String answer) { 1161 // Test for method java.lang.String java.lang.Double.toString(double) 1162 assertTrue("Incorrect String representation want " + answer + ", got (" 1163 + Float.toString(ff) + ")", Float.toString(ff).equals(answer)); 1164 Float f = new Float(ff); 1165 assertTrue("Incorrect String representation want " + answer + ", got (" 1166 + Float.toString(f.floatValue()) + ")", Float.toString(f.floatValue()).equals( 1167 answer)); 1168 assertTrue("Incorrect String representation want " + answer + ", got (" + f.toString() 1169 + ")", f.toString().equals(answer)); 1170 } 1171 1172 /** 1173 * @tests java.lang.Float#compareTo(java.lang.Float) 1174 * @tests java.lang.Float#compare(float, float) 1175 */ 1176 @TestInfo( 1177 level = TestLevel.COMPLETE, 1178 purpose = "", 1179 targets = { 1180 @TestTarget( 1181 methodName = "compareTo", 1182 methodArgs = {java.lang.Float.class} 1183 ) 1184 }) 1185 public void test_compareToLjava_lang_Float() { 1186 // A selection of float values in ascending order. 1187 float[] values = new float[] { Float.NEGATIVE_INFINITY, -Float.MAX_VALUE, -2f, 1188 -Float.MIN_VALUE, -0f, 0f, Float.MIN_VALUE, 2f, Float.MAX_VALUE, 1189 Float.POSITIVE_INFINITY, Float.NaN }; 1190 1191 for (int i = 0; i < values.length; i++) { 1192 float f1 = values[i]; 1193 1194 // Test that each value compares equal to itself; and each object is 1195 // equal to another object 1196 // like itself 1197 assertTrue("Assert 0: compare() should be equal: " + f1, Float.compare(f1, f1) == 0); 1198 Float objFloat = new Float(f1); 1199 assertTrue("Assert 1: compareTo() should be equal: " + objFloat, objFloat 1200 .compareTo(objFloat) == 0); 1201 1202 // Test that the Float-defined order is respected 1203 for (int j = i + 1; j < values.length; j++) { 1204 float f2 = values[j]; 1205 assertTrue("Assert 2: compare() " + f1 + " should be less " + f2, Float 1206 .compare(f1, f2) == -1); 1207 assertTrue("Assert 3: compare() " + f2 + " should be greater " + f1, Float 1208 .compare(f2, f1) == 1); 1209 1210 Float F2 = new Float(f2); 1211 assertTrue("Assert 4: compareTo() " + f1 + " should be less " + f2, objFloat 1212 .compareTo(F2) == -1); 1213 assertTrue("Assert 5: compareTo() " + f2 + " should be greater " + f1, F2 1214 .compareTo(objFloat) == 1); 1215 } 1216 } 1217 } 1218 1219 /** 1220 * @tests java.lang.Float#equals(java.lang.Object) 1221 */ 1222 @TestInfo( 1223 level = TestLevel.COMPLETE, 1224 purpose = "", 1225 targets = { 1226 @TestTarget( 1227 methodName = "equals", 1228 methodArgs = {java.lang.Object.class} 1229 ) 1230 }) 1231 public void test_equalsLjava_lang_Object() { 1232 Float f1 = new Float(8765.4321f); 1233 Float f2 = new Float(8765.4321f); 1234 Float f3 = new Float(-1.0f); 1235 assertTrue("Assert 0: Equality test failed", f1.equals(f2) && !(f1.equals(f3))); 1236 1237 assertTrue("Assert 1: NaN should not be == Nan", Float.NaN != Float.NaN); 1238 assertTrue("Assert 2: NaN should not be == Nan", new Float(Float.NaN).equals(new Float( 1239 Float.NaN))); 1240 assertTrue("Assert 3: -0f should be == 0f", 0f == -0f); 1241 assertTrue("Assert 4: -0f should not be equals() 0f", !new Float(0f).equals(new Float( 1242 -0f))); 1243 1244 f1 = new Float(1098.576f); 1245 f2 = new Float(1098.576f); 1246 f3 = new Float(1.0f); 1247 assertTrue("Equality test failed", f1.equals(f2) && !(f1.equals(f3))); 1248 1249 assertTrue("NaN should not be == Nan", Float.NaN != Float.NaN); 1250 assertTrue("NaN should not be == Nan", new Float(Float.NaN) 1251 .equals(new Float(Float.NaN))); 1252 assertTrue("-0f should be == 0f", 0f == -0f); 1253 assertTrue("-0f should not be equals() 0f", !new Float(0f).equals(new Float(-0f))); 1254 } 1255 1256 /** 1257 * @tests java.lang.Float#toHexString(float) 1258 */ 1259 @TestInfo( 1260 level = TestLevel.COMPLETE, 1261 purpose = "", 1262 targets = { 1263 @TestTarget( 1264 methodName = "toHexString", 1265 methodArgs = {float.class} 1266 ) 1267 }) 1268 public void test_toHexStringF() { 1269 // the follow values comes from the Float Javadoc/Spec 1270 assertEquals("0x0.0p0", Float.toHexString(0.0F)); 1271 assertEquals("-0x0.0p0", Float.toHexString(-0.0F)); 1272 assertEquals("0x1.0p0", Float.toHexString(1.0F)); 1273 assertEquals("-0x1.0p0", Float.toHexString(-1.0F)); 1274 assertEquals("0x1.0p1", Float.toHexString(2.0F)); 1275 assertEquals("0x1.8p1", Float.toHexString(3.0F)); 1276 assertEquals("0x1.0p-1", Float.toHexString(0.5F)); 1277 assertEquals("0x1.0p-2", Float.toHexString(0.25F)); 1278 assertEquals("0x1.fffffep127", Float.toHexString(Float.MAX_VALUE)); 1279 assertEquals("0x0.000002p-126", Float.toHexString(Float.MIN_VALUE)); 1280 1281 // test edge cases 1282 assertEquals("NaN", Float.toHexString(Float.NaN)); 1283 assertEquals("-Infinity", Float.toHexString(Float.NEGATIVE_INFINITY)); 1284 assertEquals("Infinity", Float.toHexString(Float.POSITIVE_INFINITY)); 1285 1286 // test various numbers 1287 assertEquals("-0x1.da8p6", Float.toHexString(-118.625F)); 1288 assertEquals("0x1.295788p23", Float.toHexString(9743299.65F)); 1289 assertEquals("0x1.295788p23", Float.toHexString(9743299.65000F)); 1290 assertEquals("0x1.295788p23", Float.toHexString(9743299.650001234F)); 1291 assertEquals("0x1.700d1p33", Float.toHexString(12349743299.65000F)); 1292 1293 // test HARMONY-2132 1294 assertEquals("0x1.01p10", Float.toHexString(0x1.01p10f)); 1295 } 1296 1297 /** 1298 * @tests java.lang.Float#valueOf(float) 1299 */ 1300 @TestInfo( 1301 level = TestLevel.COMPLETE, 1302 purpose = "", 1303 targets = { 1304 @TestTarget( 1305 methodName = "valueOf", 1306 methodArgs = {float.class} 1307 ) 1308 }) 1309 public void test_valueOfF() { 1310 assertEquals(new Float(Float.MIN_VALUE), Float.valueOf(Float.MIN_VALUE)); 1311 assertEquals(new Float(Float.MAX_VALUE), Float.valueOf(Float.MAX_VALUE)); 1312 assertEquals(new Float(0), Float.valueOf(0)); 1313 1314 int s = -128; 1315 while (s < 128) { 1316 assertEquals(new Float(s), Float.valueOf(s)); 1317 assertEquals(new Float(s + 0.1F), Float.valueOf(s + 0.1F)); 1318 assertEquals(Float.valueOf(s + 0.1F), Float.valueOf(s + 0.1F)); 1319 s++; 1320 } 1321 } 1322} 1323