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.TestTargets; 20import dalvik.annotation.TestLevel; 21import dalvik.annotation.TestTargetNew; 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 @TestTargetNew( 107 level = TestLevel.COMPLETE, 108 notes = "", 109 method = "Float", 110 args = {float.class} 111 ) 112 public void test_ConstructorF() { 113 // Test for method java.lang.Float(float) 114 115 Float f = new Float(900.89f); 116 assertTrue("Created incorrect float", f.floatValue() == 900.89f); 117 } 118 119 @TestTargetNew( 120 level = TestLevel.COMPLETE, 121 notes = "", 122 method = "Float", 123 args = {double.class} 124 ) 125 public void test_ConstructorD() { 126 127 Float f = new Float(Double.MAX_VALUE); 128 assertTrue("Created incorrect float", f.floatValue() == Float.POSITIVE_INFINITY); 129 } 130 131 /** 132 * @tests java.lang.Float#Float(java.lang.String) 133 */ 134 @TestTargetNew( 135 level = TestLevel.COMPLETE, 136 notes = "", 137 method = "Float", 138 args = {java.lang.String.class} 139 ) 140 public void test_ConstructorLjava_lang_String() { 141 // Test for method java.lang.Float(java.lang.String) 142 143 Float f = new Float("900.89"); 144 assertTrue("Created incorrect Float", f.floatValue() == 900.89f); 145 try { 146 new Float("900.89ff"); 147 fail("NumberFormatException is not thrown."); 148 } catch(NumberFormatException nfe) { 149 //expected 150 } 151 } 152 153 /** 154 * @tests java.lang.Float#byteValue() 155 */ 156 @TestTargetNew( 157 level = TestLevel.COMPLETE, 158 notes = "", 159 method = "byteValue", 160 args = {} 161 ) 162 public void test_byteValue() { 163 // Test for method byte java.lang.Float.byteValue() 164 Float f = new Float(0.46874f); 165 Float f2 = new Float(90.8f); 166 assertTrue("Returned incorrect byte value", f.byteValue() == 0 && f2.byteValue() == 90); 167 } 168 169 /** 170 * @tests java.lang.Float#compareTo(java.lang.Float) 171 * @tests java.lang.Float#compare(float, float) 172 */ 173 @TestTargetNew( 174 level = TestLevel.COMPLETE, 175 notes = "", 176 method = "compare", 177 args = {float.class, float.class} 178 ) 179 public void test_compare() { 180 float[] values = new float[] { Float.NEGATIVE_INFINITY, -Float.MAX_VALUE, -2f, 181 -Float.MIN_VALUE, -0f, 0f, Float.MIN_VALUE, 2f, Float.MAX_VALUE, 182 Float.POSITIVE_INFINITY, Float.NaN }; 183 for (int i = 0; i < values.length; i++) { 184 float f1 = values[i]; 185 assertTrue("compare() should be equal: " + f1, Float.compare(f1, f1) == 0); 186 Float F1 = new Float(f1); 187 assertTrue("compareTo() should be equal: " + f1, F1.compareTo(F1) == 0); 188 for (int j = i + 1; j < values.length; j++) { 189 float f2 = values[j]; 190 assertTrue("compare() " + f1 + " should be less " + f2, 191 Float.compare(f1, f2) == -1); 192 assertTrue("compare() " + f2 + " should be greater " + f1, Float 193 .compare(f2, f1) == 1); 194 Float F2 = new Float(f2); 195 assertTrue("compareTo() " + f1 + " should be less " + f2, 196 F1.compareTo(F2) == -1); 197 assertTrue("compareTo() " + f2 + " should be greater " + f1, 198 F2.compareTo(F1) == 1); 199 } 200 } 201 202 try { 203 new Float(0.0F).compareTo(null); 204 fail("No NPE"); 205 } catch (NullPointerException e) { 206 } 207 } 208 209 /** 210 * @tests java.lang.Float#doubleValue() 211 */ 212 @TestTargetNew( 213 level = TestLevel.COMPLETE, 214 notes = "", 215 method = "doubleValue", 216 args = {} 217 ) 218 public void test_doubleValue() { 219 // Test for method double java.lang.Float.doubleValue() 220 assertTrue("Incorrect double value returned", Math.abs(new Float(999999.999f) 221 .doubleValue() - 999999.999d) < 1); 222 } 223 224 /** 225 * @tests java.lang.Float#floatToIntBits(float) 226 */ 227 @TestTargetNew( 228 level = TestLevel.COMPLETE, 229 notes = "", 230 method = "floatToIntBits", 231 args = {float.class} 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 assertEquals(0x7f800000, Float.floatToIntBits(Float.POSITIVE_INFINITY)); 240 assertEquals(0xff800000, Float.floatToIntBits(Float.NEGATIVE_INFINITY)); 241 assertEquals(0x7fc00000, Float.floatToIntBits(Float.NaN)); 242 } 243 244 /** 245 * @tests java.lang.Float#floatToRawIntBits(float) 246 */ 247 @TestTargetNew( 248 level = TestLevel.COMPLETE, 249 notes = "", 250 method = "floatToRawIntBits", 251 args = {float.class} 252 ) 253 public void test_floatToRawIntBitsF() { 254 int i = 0x7fc004d2; 255 float f = Float.intBitsToFloat(i); 256 assertTrue("Wrong raw bits", Float.floatToRawIntBits(f) == i); 257 258 assertEquals(0x7f800000, Float.floatToRawIntBits(Float.POSITIVE_INFINITY)); 259 assertEquals(0xff800000, Float.floatToRawIntBits(Float.NEGATIVE_INFINITY)); 260 assertEquals(0x7fc00000, Float.floatToRawIntBits(Float.NaN)); 261 } 262 263 /** 264 * @tests java.lang.Float#floatValue() 265 */ 266 @TestTargetNew( 267 level = TestLevel.COMPLETE, 268 notes = "", 269 method = "floatValue", 270 args = {} 271 ) 272 public void test_floatValue() { 273 // Test for method float java.lang.Float.floatValue() 274 Float f = new Float(87.657f); 275 Float f2 = new Float(-0.876f); 276 assertTrue("Returned incorrect floatValue", f.floatValue() == 87.657f 277 && (f2.floatValue() == -0.876f)); 278 279 } 280 281 /** 282 * @tests java.lang.Float#hashCode() 283 */ 284 @TestTargetNew( 285 level = TestLevel.COMPLETE, 286 notes = "", 287 method = "hashCode", 288 args = {} 289 ) 290 public void test_hashCode() { 291 // Test for method int java.lang.Float.hashCode() 292 Float f = new Float(1908.8786f); 293 assertTrue("Returned invalid hash code for 1908.8786f", f.hashCode() == Float 294 .floatToIntBits(1908.8786f)); 295 296 f = new Float(-1.112f); 297 assertTrue("Returned invalid hash code for -1.112", f.hashCode() == Float 298 .floatToIntBits(-1.112f)); 299 300 f = new Float(0f); 301 assertTrue("Returned invalid hash code for 0", f.hashCode() == Float.floatToIntBits(0f)); 302 303 assertTrue(new Float(Float.MAX_VALUE).hashCode() != new Float(Float.MIN_VALUE).hashCode()); 304 } 305 306 /** 307 * @tests java.lang.Float#intBitsToFloat(int) 308 */ 309 @TestTargetNew( 310 level = TestLevel.COMPLETE, 311 notes = "", 312 method = "intBitsToFloat", 313 args = {int.class} 314 ) 315 public void test_intBitsToFloatI() { 316 float f = 9876.2345f; 317 int bits = Float.floatToIntBits(f); 318 float r = Float.intBitsToFloat(bits); 319 assertEquals("Incorrect intBits returned", f, r, 0F); 320 321 assertEquals(Float.POSITIVE_INFINITY, Float.intBitsToFloat(0x7f800000)); 322 assertEquals(Float.NEGATIVE_INFINITY, Float.intBitsToFloat(0xff800000)); 323 324 assertEquals(Float.NaN, Float.intBitsToFloat(0x7f800001)); 325 assertEquals(Float.NaN, Float.intBitsToFloat(0x7fffffff)); 326 assertEquals(Float.NaN, Float.intBitsToFloat(0xff800001)); 327 assertEquals(Float.NaN, Float.intBitsToFloat(0xffffffff)); 328 } 329 330 /** 331 * @tests java.lang.Float#intValue() 332 */ 333 @TestTargetNew( 334 level = TestLevel.COMPLETE, 335 notes = "", 336 method = "intValue", 337 args = {} 338 ) 339 public void test_intValue() { 340 // Test for method int java.lang.Float.intValue() 341 Float f = new Float(0.46874f); 342 Float f2 = new Float(90.8f); 343 assertTrue("Returned incorrect int value", f.intValue() == 0 && f2.intValue() == 90); 344 assertEquals(Integer.MAX_VALUE, new Float(Float.MAX_VALUE).intValue()); 345 assertEquals(0, new Float(Float.MIN_VALUE).intValue()); 346 } 347 348 /** 349 * @tests java.lang.Float#isInfinite() 350 */ 351 @TestTargetNew( 352 level = TestLevel.COMPLETE, 353 notes = "", 354 method = "isInfinite", 355 args = {} 356 ) 357 public void test_isInfinite() { 358 // Test for method boolean java.lang.Float.isInfinite() 359 assertTrue("Infinity check failed", 360 (new Float(Float.POSITIVE_INFINITY).isInfinite() && new Float( 361 Float.NEGATIVE_INFINITY).isInfinite()) 362 && !(new Float(0.13131414f).isInfinite())); 363 } 364 365 /** 366 * @tests java.lang.Float#isInfinite(float) 367 */ 368 @TestTargetNew( 369 level = TestLevel.COMPLETE, 370 notes = "", 371 method = "isInfinite", 372 args = {float.class} 373 ) 374 public void test_isInfiniteF() { 375 // Test for method boolean java.lang.Float.isInfinite(float) 376 377 assertTrue("Infinity check failed", Float.isInfinite(Float.POSITIVE_INFINITY) 378 && (Float.isInfinite(Float.NEGATIVE_INFINITY)) && !(Float.isInfinite(1.0f))); 379 } 380 381 /** 382 * @tests java.lang.Float#isNaN() 383 */ 384 @TestTargetNew( 385 level = TestLevel.COMPLETE, 386 notes = "", 387 method = "isNaN", 388 args = {} 389 ) 390 public void test_isNaN() { 391 // Test for method boolean java.lang.Float.isNaN() 392 assertTrue("NAN check failed", new Float(Float.NaN).isNaN() 393 && !(new Float(1.0f).isNaN())); 394 } 395 396 /** 397 * @tests java.lang.Float#isNaN(float) 398 */ 399 @TestTargetNew( 400 level = TestLevel.COMPLETE, 401 notes = "", 402 method = "isNaN", 403 args = {float.class} 404 ) 405 public void test_isNaNF() { 406 // Test for method boolean java.lang.Float.isNaN(float) 407 assertTrue("NaN check failed", Float.isNaN(Float.NaN) && !(Float.isNaN(12.09f)) 408 && !Float.isNaN(Float.MAX_VALUE) && !Float.isNaN(Float.MIN_VALUE)); 409 } 410 411 /** 412 * @tests java.lang.Float#longValue() 413 */ 414 @TestTargetNew( 415 level = TestLevel.COMPLETE, 416 notes = "", 417 method = "longValue", 418 args = {} 419 ) 420 public void test_longValue() { 421 // Test for method long java.lang.Float.longValue() 422 Float f = new Float(0.46874f); 423 Float f2 = new Float(90.8f); 424 assertTrue("Returned incorrect long value", f.longValue() == 0 && f2.longValue() == 90); 425 assertEquals(Long.MAX_VALUE, new Float(Float.MAX_VALUE).longValue()); 426 assertEquals(0, new Float(Float.MIN_VALUE).longValue()); 427 } 428 429 @TestTargetNew( 430 level = TestLevel.PARTIAL_COMPLETE, 431 notes = "Verifies NumberFormatException.", 432 method = "parseFloat", 433 args = {java.lang.String.class} 434 ) 435 public void test_parseFloatLExceptions() { 436 String [] incorrectStrings = {"", ";", "99999999EE999999", "99999l", 437 "0x1.f.ffffep127"}; 438 for(int i = 0; i < incorrectStrings.length; i++) { 439 try { 440 Float.parseFloat(incorrectStrings[i]); 441 fail("NumberFormatException is not thrown for string: " 442 + incorrectStrings[i]); 443 } catch(NumberFormatException nfe) { 444 //expected 445 } 446 } 447 } 448 449 /** 450 * @tests java.lang.Float#parseFloat(java.lang.String) 451 */ 452 @TestTargetNew( 453 level = TestLevel.PARTIAL_COMPLETE, 454 notes = "Verifies boundary cases.", 455 method = "parseFloat", 456 args = {java.lang.String.class} 457 ) 458 public void test_parseFloatLjava_lang_String() { 459 assertEquals("Incorrect float returned, expected zero.", 0.0, Float 460 .parseFloat("7.0064923216240853546186479164495e-46"), 0.0); 461 assertEquals("Incorrect float returned, expected minimum float.", Float.MIN_VALUE, 462 Float.parseFloat("7.0064923216240853546186479164496e-46"), 0.0); 463 464 doTestCompareRawBits( 465 "0.000000000000000000000000000000000000011754942807573642917278829910357665133228589927589904276829631184250030649651730385585324256680905818939208984375", 466 0x800000, "1.17549435E-38"); 467 doTestCompareRawBits( 468 "0.00000000000000000000000000000000000001175494280757364291727882991035766513322858992758990427682963118425003064965173038558532425668090581893920898437499999f", 469 0x7fffff, "1.1754942E-38"); 470 471 /* Test a set of regular floats with exponents from -38 to +38 */ 472 for (int i = 38; i > 3; i--) { 473 String testString; 474 testString = "3.4028234663852886e-" + i; 475 doTestCompareRawBits(testString, rawBitsFor3_4eN38To38[38 - i], 476 expectedStringFor3_4eN38To38[38 - i]); 477 } 478 doTestCompareRawBits("3.4028234663852886e-3", rawBitsFor3_4eN38To38[38 - 3], 479 expectedStringFor3_4eN38To38[38 - 3]); 480 doTestCompareRawBits("3.4028234663852886e-2", rawBitsFor3_4eN38To38[38 - 2], 481 expectedStringFor3_4eN38To38[38 - 2]); 482 doTestCompareRawBits("3.4028234663852886e-1", rawBitsFor3_4eN38To38[38 - 1], 483 expectedStringFor3_4eN38To38[38 - 1]); 484 doTestCompareRawBits("3.4028234663852886e-0", rawBitsFor3_4eN38To38[38 - 0], 485 expectedStringFor3_4eN38To38[38 - 0]); 486 doTestCompareRawBits("3.4028234663852886e+1", rawBitsFor3_4eN38To38[38 + 1], 487 expectedStringFor3_4eN38To38[38 + 1]); 488 doTestCompareRawBits("3.4028234663852886e+2", rawBitsFor3_4eN38To38[38 + 2], 489 expectedStringFor3_4eN38To38[38 + 2]); 490 doTestCompareRawBits("3.4028234663852886e+3", rawBitsFor3_4eN38To38[38 + 3], 491 expectedStringFor3_4eN38To38[38 + 3]); 492 doTestCompareRawBits("3.4028234663852886e+4", rawBitsFor3_4eN38To38[38 + 4], 493 expectedStringFor3_4eN38To38[38 + 4]); 494 doTestCompareRawBits("3.4028234663852886e+5", rawBitsFor3_4eN38To38[38 + 5], 495 expectedStringFor3_4eN38To38[38 + 5]); 496 doTestCompareRawBits("3.4028234663852886e+6", rawBitsFor3_4eN38To38[38 + 6], 497 expectedStringFor3_4eN38To38[38 + 6]); 498 499 for (int i = 7; i < 39; i++) { 500 String testString; 501 testString = "3.4028234663852886e+" + i; 502 doTestCompareRawBits(testString, rawBitsFor3_4eN38To38[38 + i], 503 expectedStringFor3_4eN38To38[38 + i]); 504 } 505 506 /* Test another set of regular floats with exponents from -38 to +38 */ 507 for (int i = 38; i > 3; i--) { 508 String testString; 509 testString = "-1.1754943508222875e-" + i; 510 doTestCompareRawBits(testString, rawBitsFor1_17eN38To38[38 - i], 511 expectedStringFor1_17eN38To38[38 - i]); 512 } 513 doTestCompareRawBits("-1.1754943508222875e-3", rawBitsFor1_17eN38To38[38 - 3], 514 expectedStringFor1_17eN38To38[38 - 3]); 515 doTestCompareRawBits("-1.1754943508222875e-2", rawBitsFor1_17eN38To38[38 - 2], 516 expectedStringFor1_17eN38To38[38 - 2]); 517 doTestCompareRawBits("-1.1754943508222875e-1", rawBitsFor1_17eN38To38[38 - 1], 518 expectedStringFor1_17eN38To38[38 - 1]); 519 doTestCompareRawBits("-1.1754943508222875e-0", rawBitsFor1_17eN38To38[38 - 0], 520 expectedStringFor1_17eN38To38[38 - 0]); 521 doTestCompareRawBits("-1.1754943508222875e+1", rawBitsFor1_17eN38To38[38 + 1], 522 expectedStringFor1_17eN38To38[38 + 1]); 523 doTestCompareRawBits("-1.1754943508222875e+2", rawBitsFor1_17eN38To38[38 + 2], 524 expectedStringFor1_17eN38To38[38 + 2]); 525 doTestCompareRawBits("-1.1754943508222875e+3", rawBitsFor1_17eN38To38[38 + 3], 526 expectedStringFor1_17eN38To38[38 + 3]); 527 doTestCompareRawBits("-1.1754943508222875e+4", rawBitsFor1_17eN38To38[38 + 4], 528 expectedStringFor1_17eN38To38[38 + 4]); 529 doTestCompareRawBits("-1.1754943508222875e+5", rawBitsFor1_17eN38To38[38 + 5], 530 expectedStringFor1_17eN38To38[38 + 5]); 531 doTestCompareRawBits("-1.1754943508222875e+6", rawBitsFor1_17eN38To38[38 + 6], 532 expectedStringFor1_17eN38To38[38 + 6]); 533 534 for (int i = 7; i < 39; i++) { 535 String testString; 536 testString = "-1.1754943508222875e+" + i; 537 doTestCompareRawBits(testString, rawBitsFor1_17eN38To38[38 + i], 538 expectedStringFor1_17eN38To38[38 + i]); 539 } 540 541 /* Test denormalized floats (floats with exponents <= -38 */ 542 doTestCompareRawBits("1.1012984643248170E-45", 1, "1.4E-45"); 543 doTestCompareRawBits("-1.1012984643248170E-45", 0x80000001, "-1.4E-45"); 544 doTestCompareRawBits("1.0E-45", 1, "1.4E-45"); 545 doTestCompareRawBits("-1.0E-45", 0x80000001, "-1.4E-45"); 546 doTestCompareRawBits("0.9E-45", 1, "1.4E-45"); 547 doTestCompareRawBits("-0.9E-45", 0x80000001, "-1.4E-45"); 548 doTestCompareRawBits("4.203895392974451e-45", 3, "4.2E-45"); 549 doTestCompareRawBits("-4.203895392974451e-45", 0x80000003, "-4.2E-45"); 550 doTestCompareRawBits("0.004E-45", 0, "0.0"); 551 doTestCompareRawBits("-0.004E-45", 0x80000000, "-0.0"); 552 553 /* 554 * Test for large floats close to and greater than 3.4028235E38 and 555 * -3.4028235E38 556 */ 557 doTestCompareRawBits("1.2E+38", 0x7eb48e52, "1.2E38"); 558 doTestCompareRawBits("-1.2E+38", 0xfeb48e52, "-1.2E38"); 559 doTestCompareRawBits("3.2E+38", 0x7f70bdc2, "3.2E38"); 560 doTestCompareRawBits("-3.2E+38", 0xff70bdc2, "-3.2E38"); 561 doTestCompareRawBits("3.4E+38", 0x7f7fc99e, "3.4E38"); 562 doTestCompareRawBits("-3.4E+38", 0xff7fc99e, "-3.4E38"); 563 doTestCompareRawBits("3.4028234663852886E+38", 0x7f7fffff, "3.4028235E38"); 564 doTestCompareRawBits("-3.4028234663852886E+38", 0xff7fffff, "-3.4028235E38"); 565 doTestCompareRawBits("3.405E+38", 0x7f800000, "Infinity"); 566 doTestCompareRawBits("-3.405E+38", 0xff800000, "-Infinity"); 567 doTestCompareRawBits("3.41E+38", 0x7f800000, "Infinity"); 568 doTestCompareRawBits("-3.41E+38", 0xff800000, "-Infinity"); 569 doTestCompareRawBits("3.42E+38", 0x7f800000, "Infinity"); 570 doTestCompareRawBits("-3.42E+38", 0xff800000, "-Infinity"); 571 doTestCompareRawBits("1.0E+39", 0x7f800000, "Infinity"); 572 doTestCompareRawBits("-1.0E+39", 0xff800000, "-Infinity"); 573 } 574 575 /** 576 * @tests java.lang.Float#parseFloat(java.lang.String) 577 */ 578 @TestTargetNew( 579 level = TestLevel.PARTIAL_COMPLETE, 580 notes = "Verifies boundary values.", 581 method = "parseFloat", 582 args = {java.lang.String.class} 583 ) 584 public void test_parseFloat_LString_Unusual() { 585 float actual; 586 587 actual = Float.parseFloat("0x00000000000000000000000000000000000000000.0000000000000000000000000000000000000p0000000000000000000000000000000000"); 588 assertEquals("Returned incorrect value", 0.0f, actual, 0.0F); 589 590 actual = Float.parseFloat("+0Xfffff.fffffffffffffffffffffffffffffffp+99F"); 591 assertEquals("Returned incorrect value", 6.64614E35f, actual, 0.0F); 592 593 actual = Float.parseFloat("-0X.123456789abcdefp+99f"); 594 assertEquals("Returned incorrect value", -4.5072022E28f, actual, 0.0F); 595 596 actual = Float.parseFloat("-0X123456789abcdef.p+1f"); 597 assertEquals("Returned incorrect value", -1.63971062E17f, actual, 0.0F); 598 599 actual = Float.parseFloat("-0X000000000000000000000000000001abcdef.0000000000000000000000000001abefp00000000000000000000000000000000000000000004f"); 600 assertEquals("Returned incorrect value", -4.48585472E8f, actual, 0.0F); 601 602 actual = Float.parseFloat("0X0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001234p600f"); 603 assertEquals("Returned incorrect value", 5.907252E33f, actual, 0.0F); 604 605 actual = Float.parseFloat("0x1.p9223372036854775807"); 606 assertEquals("Returned incorrect value", Float.POSITIVE_INFINITY, actual, 0.0F); 607 608 actual = Float.parseFloat("0x1.p9223372036854775808"); 609 assertEquals("Returned incorrect value", Float.POSITIVE_INFINITY, actual, 0.0F); 610 611 actual = Float.parseFloat("0x10.p9223372036854775808"); 612 assertEquals("Returned incorrect value", Float.POSITIVE_INFINITY, actual, 0.0F); 613 614 actual = Float.parseFloat("0xabcd.ffffffffp+2000"); 615 assertEquals("Returned incorrect value", Float.POSITIVE_INFINITY, actual, 0.0F); 616 617 actual = Float.parseFloat("0x1.p-9223372036854775808"); 618 assertEquals("Returned incorrect value", 0.0f, actual, 0.0F); 619 620 actual = Float.parseFloat("0x1.p-9223372036854775809"); 621 assertEquals("Returned incorrect value", 0.0f, actual, 0.0F); 622 623 actual = Float.parseFloat("0x.1p-9223372036854775809"); 624 assertEquals("Returned incorrect value", 0.0f, actual, 0.0F); 625 } 626 627 /** 628 * @tests java.lang.Float#parseFloat(java.lang.String) 629 */ 630 @TestTargetNew( 631 level = TestLevel.PARTIAL_COMPLETE, 632 notes = "Verifies positive exponent.", 633 method = "parseFloat", 634 args = {java.lang.String.class} 635 ) 636 public void test_parseFloat_LString_NormalPositiveExponent() { 637 int[] expecteds = { 638 0x3991a2b4, 0x43cc0247, 0x47909009, 639 0x4ac0c009, 0x4e109005, 0x5140c005, 640 0x5458d805, 0x57848402, 0x5a909002, 641 0x5da8a802, 0x60c0c002, 0x63cccc02, 642 0x66e4e402, 0x69f0f002, 0x6d048401, 643 0x70109001, 0x73169601, 0x76810810, 644 0x79840840, 0x7c8a08a0, 0x7f800000, 645 0x7f800000, 0x7f800000, 0x7f800000, 646 0x7f800000, 647 }; 648 649 for (int i = 0; i < expecteds.length; i++) { 650 int part = i * 6; 651 String inputString = "0x" + part + "." + part + "0123456789abcdefp" + part; 652 653 float actual = Float.parseFloat(inputString); 654 float expected = Float.intBitsToFloat(expecteds[i]); 655 656 String expectedString = Integer.toHexString(Float.floatToIntBits(expected)); 657 String actualString = Integer.toHexString(Float.floatToIntBits(actual)); 658 String errorMsg = i + "th input string is:<" + inputString 659 + ">.The expected result should be:<" + expectedString 660 + ">, but was: <" + actualString + ">. "; 661 662 assertEquals(errorMsg, expected, actual, 0.0F); 663 } 664 } 665 666 /** 667 * @tests java.lang.Float#parseFloat(java.lang.String) 668 */ 669 @TestTargetNew( 670 level = TestLevel.PARTIAL_COMPLETE, 671 notes = "Verifies negative exponent.", 672 method = "parseFloat", 673 args = {java.lang.String.class} 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 @TestTargetNew( 725 level = TestLevel.PARTIAL_COMPLETE, 726 notes = "Verifies max boundary value. Doesn't verify NumberFormatException.", 727 method = "parseFloat", 728 args = {java.lang.String.class} 729 ) 730 public void test_parseFloat_LString_MaxNormalBoundary() { 731 int[] expecteds ={ 732 0x7f7fffff, 733 0x7f7fffff, 734 0x7f7fffff, 735 0x7f800000, 736 0x7f800000, 737 0x7f800000, 738 739 0xff7fffff, 740 0xff7fffff, 741 0xff7fffff, 742 0xff800000, 743 0xff800000, 744 0xff800000, 745 }; 746 747 String[] inputs = { 748 "0x1.fffffep127", 749 "0x1.fffffe000000000000000000000000000000000000000000000001p127", 750 "0x1.fffffeffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp127", 751 "0x1.ffffffp127", 752 "0x1.ffffff000000000000000000000000000000000000000000000001p127", 753 "0x1.ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp127", 754 755 "-0x1.fffffep127", 756 "-0x1.fffffe000000000000000000000000000000000000000000000001p127", 757 "-0x1.fffffeffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp127", 758 "-0x1.ffffffp127", 759 "-0x1.ffffff000000000000000000000000000000000000000000000001p127", 760 "-0x1.ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp127", 761 }; 762 763 for (int i = 0; i < inputs.length; i++) { 764 float actual = Float.parseFloat(inputs[i]); 765 float expected = Float.intBitsToFloat(expecteds[i]); 766 767 String expectedString = Integer.toHexString(Float.floatToIntBits(expected)); 768 String actualString = Integer.toHexString(Float.floatToIntBits(actual)); 769 String errorMsg = i + "th input string is:<" + inputs[i] 770 + ">.The expected result should be:<" + expectedString 771 + ">, but was: <" + actualString + ">. "; 772 773 assertEquals(errorMsg, expected, actual, 0.0F); 774 } 775 } 776 777 /** 778 * @tests java.lang.Float#parseFloat(java.lang.String) 779 */ 780 @TestTargetNew( 781 level = TestLevel.PARTIAL_COMPLETE, 782 notes = "Verifies min boundary value.", 783 method = "parseFloat", 784 args = {java.lang.String.class} 785 ) 786 public void test_parseFloat_LString_MinNormalBoundary() { 787 int expecteds[] = { 788 0x800000, 789 0x800000, 790 0x800000, 791 0x800000, 792 0x800001, 793 0x800001, 794 795 0x80800000, 796 0x80800000, 797 0x80800000, 798 0x80800000, 799 0x80800001, 800 0x80800001, 801 }; 802 803 String inputs[] = { 804 "0x1.0p-126", 805 "0x1.00000000000000000000000000000000000000000000001p-126", 806 "0x1.000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 807 "0x1.000001p-126", 808 "0x1.000001000000000000000000000000000000000000000001p-126", 809 "0x1.000001fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 810 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 819 for (int i = 0; i < inputs.length; i++) { 820 float actual = Float.parseFloat(inputs[i]); 821 float expected = Float.intBitsToFloat(expecteds[i]); 822 823 String expectedString = Integer.toHexString(Float.floatToIntBits(expected)); 824 String actualString = Integer.toHexString(Float.floatToIntBits(actual)); 825 String errorMsg = i + "th input string is:<" + inputs[i] 826 + ">.The expected result should be:<" + expectedString 827 + ">, but was: <" + actualString + ">. "; 828 829 assertEquals(errorMsg, expected, actual, 0.0F); 830 } 831 } 832 833 /** 834 * @tests java.lang.Float#parseFloat(java.lang.String) 835 */ 836 @TestTargetNew( 837 level = TestLevel.PARTIAL_COMPLETE, 838 notes = "Verifies max boundary value.", 839 method = "parseFloat", 840 args = {java.lang.String.class} 841 ) 842 public void test_parseFloat_LString_MaxSubNormalBoundary() { 843 int expecteds[] = { 844 0x7fffff, 845 0x7fffff, 846 0x7fffff, 847 0x800000, 848 0x800000, 849 0x800000, 850 851 0x807fffff, 852 0x807fffff, 853 0x807fffff, 854 0x80800000, 855 0x80800000, 856 0x80800000, 857 }; 858 859 String inputs[] = { 860 "0x0.fffffep-126", 861 "0x0.fffffe000000000000000000000000000000000000000000000000000001p-126", 862 "0x0.fffffefffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 863 "0x0.ffffffp-126", 864 "0x0.ffffff0000000000000000000000000000000000000000000000000000001p-126", 865 "0x0.ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 866 867 "-0x0.fffffep-126", 868 "-0x0.fffffe000000000000000000000000000000000000000000000000000001p-126", 869 "-0x0.fffffefffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 870 "-0x0.ffffffp-126", 871 "-0x0.ffffff0000000000000000000000000000000000000000000000000000001p-126", 872 "-0x0.ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 873 }; 874 875 for (int i = 0; i < inputs.length; i++) { 876 float actual = Float.parseFloat(inputs[i]); 877 float expected = Float.intBitsToFloat(expecteds[i]); 878 879 String expectedString = Integer.toHexString(Float.floatToIntBits(expected)); 880 String actualString = Integer.toHexString(Float.floatToIntBits(actual)); 881 String errorMsg = i + "th input string is:<" + inputs[i] 882 + ">.The expected result should be:<" + expectedString 883 + ">, but was: <" + actualString + ">. "; 884 885 assertEquals(errorMsg, expected, actual, 0.0F); 886 } 887 } 888 889 /** 890 * @tests java.lang.Float#parseFloat(java.lang.String) 891 */ 892 @TestTargetNew( 893 level = TestLevel.PARTIAL_COMPLETE, 894 notes = "Verifies min boundary value.", 895 method = "parseFloat", 896 args = {java.lang.String.class} 897 ) 898 public void test_parseFloat_LString_MinSubNormalBoundary() { 899 int expecteds[] = { 900 0x1, 901 0x1, 902 0x1, 903 0x2, 904 0x2, 905 0x2, 906 907 0x80000001, 908 0x80000001, 909 0x80000001, 910 0x80000002, 911 0x80000002, 912 0x80000002, 913 }; 914 915 String inputs[] = { 916 "0x0.000002p-126", 917 "0x0.00000200000000000000000000000000000000000001p-126", 918 "0x0.000002ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 919 "0x0.000003p-126", 920 "0x0.000003000000000000000000000000000000000000001p-126", 921 "0x0.000003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 922 923 "-0x0.000002p-126", 924 "-0x0.00000200000000000000000000000000000000000001p-126", 925 "-0x0.000002ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 926 "-0x0.000003p-126", 927 "-0x0.000003000000000000000000000000000000000000001p-126", 928 "-0x0.000003ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 929 }; 930 931 for (int i = 0; i < inputs.length; i++) { 932 float actual = Float.parseFloat(inputs[i]); 933 float expected = Float.intBitsToFloat(expecteds[i]); 934 935 String expectedString = Integer.toHexString(Float.floatToIntBits(expected)); 936 String actualString = Integer.toHexString(Float.floatToIntBits(actual)); 937 String errorMsg = i + "th input string is:<" + inputs[i] 938 + ">.The expected result should be:<" + expectedString 939 + ">, but was: <" + actualString + ">. "; 940 941 assertEquals(errorMsg, expected, actual, 0.0F); 942 } 943 } 944 945 /** 946 * @tests java.lang.Float#parseFloat(java.lang.String) 947 */ 948 @TestTargetNew( 949 level = TestLevel.PARTIAL_COMPLETE, 950 notes = "Verifies zero boundary value.", 951 method = "parseFloat", 952 args = {java.lang.String.class} 953 ) 954 public void test_parseFloat_LString_ZeroBoundary() { 955 int expecteds[] = { 956 0x0, 957 0x0, 958 0x0, 959 0x0, 960 0x1, 961 0x1, 962 963 0x80000000, 964 0x80000000, 965 0x80000000, 966 0x80000000, 967 0x80000001, 968 0x80000001, 969 }; 970 971 String inputs[] = { 972 "0x0.000000000000000p-126", 973 "0x0.000000000000000000000000000000000000000000000001p-126", 974 "0x0.000000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 975 "0x0.000001p-126", 976 "0x0.000001000000000000000000000000000000000000000001p-126", 977 "0x0.000001fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 978 979 "-0x0.000000000000000p-126", 980 "-0x0.000000000000000000000000000000000000000000000001p-126", 981 "-0x0.000000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 982 "-0x0.000001p-126", 983 "-0x0.000001000000000000000000000000000000000000000001p-126", 984 "-0x0.000001fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffp-126", 985 }; 986 987 for (int i = 0; i < inputs.length; i++) { 988 float actual = Float.parseFloat(inputs[i]); 989 float expected = Float.intBitsToFloat(expecteds[i]); 990 991 String expectedString = Integer.toHexString(Float.floatToIntBits(expected)); 992 String actualString = Integer.toHexString(Float.floatToIntBits(actual)); 993 String errorMsg = i + "th input string is:<" + inputs[i] 994 + ">.The expected result should be:<" + expectedString 995 + ">, but was: <" + actualString + ">. "; 996 997 assertEquals(errorMsg, expected, actual, 0.0F); 998 } 999 } 1000 1001 /** 1002 * @tests java.lang.Float#parseFloat(java.lang.String) 1003 */ 1004 public void test_parseFloat_LString_Harmony6261() { 1005 // Regression test for HARMONY-6261 1006 float f = new Float("2147483648"); 1007 assertEquals("2.1474836E9", Float.toString(f)); 1008 1009 doTestCompareRawBits("123456790528.000000000000000f", 0x51e5f4c9, "1.2345679E11"); 1010 doTestCompareRawBits("8589934592", 0x50000000, "8.5899346E9"); 1011 doTestCompareRawBits("8606711808", 0x50004000, "8.606712E9"); 1012 } 1013 1014 /** 1015 * @tests java.lang.Float#shortValue() 1016 */ 1017 @TestTargetNew( 1018 level = TestLevel.COMPLETE, 1019 notes = "", 1020 method = "shortValue", 1021 args = {} 1022 ) 1023 public void test_shortValue() { 1024 // Test for method short java.lang.Float.shortValue() 1025 Float f = new Float(0.46874f); 1026 Float f2 = new Float(90.8f); 1027 assertTrue("Returned incorrect short value", f.shortValue() == 0 1028 && f2.shortValue() == 90); 1029 } 1030 1031 /** 1032 * @tests java.lang.Float#toString() 1033 */ 1034 @TestTargetNew( 1035 level = TestLevel.COMPLETE, 1036 notes = "", 1037 method = "toString", 1038 args = {} 1039 ) 1040 public void test_toString() { 1041 // Test for method java.lang.String java.lang.Float.toString() 1042 1043 test_toString(12.90898f, "12.90898"); 1044 1045 test_toString(1.7014118346046924e+38F, "1.7014118E38"); 1046 } 1047 1048 /** 1049 * @tests java.lang.Float#toString(float) 1050 */ 1051 @TestTargetNew( 1052 level = TestLevel.COMPLETE, 1053 notes = "", 1054 method = "toString", 1055 args = {float.class} 1056 ) 1057 public void test_toStringF() { 1058 // Test for method java.lang.String java.lang.Float.toString(float) 1059 1060 float ff; 1061 String answer; 1062 1063 ff = 12.90898f; 1064 answer = "12.90898"; 1065 assertTrue("Incorrect String representation want " + answer + ", got " 1066 + Float.toString(ff), Float.toString(ff).equals(answer)); 1067 1068 ff = Float.MAX_VALUE; 1069 answer = "3.4028235E38"; 1070 assertTrue("Incorrect String representation want " + answer + ", got " 1071 + Float.toString(ff), Float.toString(ff).equals(answer)); 1072 } 1073 1074 /** 1075 * @tests java.lang.Float#valueOf(java.lang.String) 1076 */ 1077 @TestTargetNew( 1078 level = TestLevel.COMPLETE, 1079 notes = "", 1080 method = "valueOf", 1081 args = {java.lang.String.class} 1082 ) 1083 public void test_valueOfLjava_lang_String() { 1084 // Test for method java.lang.Float 1085 // java.lang.Float.valueOf(java.lang.String) 1086 1087 Float wanted = new Float(432.1235f); 1088 Float got = Float.valueOf("432.1235"); 1089 assertTrue("Incorrect float returned--wanted: " + wanted + " but got: " + got, got 1090 .equals(wanted)); 1091 1092 wanted = new Float(0f); 1093 got = Float.valueOf("0"); 1094 assertTrue("Incorrect float returned--wanted: " + wanted + " but got: " + got, got 1095 .equals(wanted)); 1096 1097 wanted = new Float(-1212.3232f); 1098 got = Float.valueOf("-1212.3232"); 1099 assertTrue("Incorrect float returned--wanted: " + wanted + " but got: " + got, got 1100 .equals(wanted)); 1101 1102 try { 1103 Float.valueOf(null); 1104 fail("Expected Float.valueOf(null) to throw NPE."); 1105 } catch (NullPointerException ex) { 1106 // expected 1107 } 1108 1109 try { 1110 Float.valueOf(""); 1111 fail("Expected Float.valueOf(\"\") to throw NFE"); 1112 } catch (NumberFormatException e) { 1113 // expected 1114 } 1115 1116 Float posZero = Float.valueOf("+0.0"); 1117 Float negZero = Float.valueOf("-0.0"); 1118 assertFalse("Floattest0", posZero.equals(negZero)); 1119 assertTrue("Floattest1", 0.0f == -0.0f); 1120 1121 // Tests for float values by name. 1122 Float expectedNaN = new Float(Float.NaN); 1123 1124 Float posNaN = Float.valueOf("NaN"); 1125 assertTrue("Floattest2", posNaN.equals(expectedNaN)); 1126 1127 Float posNaNSigned = Float.valueOf("+NaN"); 1128 assertTrue("Floattest3", posNaNSigned.equals(expectedNaN)); 1129 1130 Float negNaNSigned = Float.valueOf("-NaN"); 1131 assertTrue("Floattest4", negNaNSigned.equals(expectedNaN)); 1132 1133 Float posInfinite = Float.valueOf("Infinity"); 1134 assertTrue("Floattest5", posInfinite.equals(new Float(Float.POSITIVE_INFINITY))); 1135 1136 Float posInfiniteSigned = Float.valueOf("+Infinity"); 1137 assertTrue("Floattest6", posInfiniteSigned.equals(new Float(Float.POSITIVE_INFINITY))); 1138 1139 Float negInfiniteSigned = Float.valueOf("-Infinity"); 1140 assertTrue("Floattest7", negInfiniteSigned.equals(new Float(Float.NEGATIVE_INFINITY))); 1141 } 1142 1143 private void test_toString(float ff, String answer) { 1144 // Test for method java.lang.String java.lang.Double.toString(double) 1145 assertTrue("Incorrect String representation want " + answer + ", got (" 1146 + Float.toString(ff) + ")", Float.toString(ff).equals(answer)); 1147 Float f = new Float(ff); 1148 assertTrue("Incorrect String representation want " + answer + ", got (" 1149 + Float.toString(f.floatValue()) + ")", Float.toString(f.floatValue()).equals( 1150 answer)); 1151 assertTrue("Incorrect String representation want " + answer + ", got (" + f.toString() 1152 + ")", f.toString().equals(answer)); 1153 } 1154 1155 /** 1156 * @tests java.lang.Float#compareTo(java.lang.Float) 1157 * @tests java.lang.Float#compare(float, float) 1158 */ 1159 @TestTargetNew( 1160 level = TestLevel.COMPLETE, 1161 notes = "", 1162 method = "compareTo", 1163 args = {java.lang.Float.class} 1164 ) 1165 public void test_compareToLjava_lang_Float() { 1166 // A selection of float values in ascending order. 1167 float[] values = new float[] { Float.NEGATIVE_INFINITY, -Float.MAX_VALUE, -2f, 1168 -Float.MIN_VALUE, -0f, 0f, Float.MIN_VALUE, 2f, Float.MAX_VALUE, 1169 Float.POSITIVE_INFINITY, Float.NaN }; 1170 1171 for (int i = 0; i < values.length; i++) { 1172 float f1 = values[i]; 1173 1174 // Test that each value compares equal to itself; and each object is 1175 // equal to another object 1176 // like itself 1177 assertTrue("Assert 0: compare() should be equal: " + f1, Float.compare(f1, f1) == 0); 1178 Float objFloat = new Float(f1); 1179 assertTrue("Assert 1: compareTo() should be equal: " + objFloat, objFloat 1180 .compareTo(objFloat) == 0); 1181 1182 // Test that the Float-defined order is respected 1183 for (int j = i + 1; j < values.length; j++) { 1184 float f2 = values[j]; 1185 assertTrue("Assert 2: compare() " + f1 + " should be less " + f2, Float 1186 .compare(f1, f2) == -1); 1187 assertTrue("Assert 3: compare() " + f2 + " should be greater " + f1, Float 1188 .compare(f2, f1) == 1); 1189 1190 Float F2 = new Float(f2); 1191 assertTrue("Assert 4: compareTo() " + f1 + " should be less " + f2, objFloat 1192 .compareTo(F2) == -1); 1193 assertTrue("Assert 5: compareTo() " + f2 + " should be greater " + f1, F2 1194 .compareTo(objFloat) == 1); 1195 } 1196 } 1197 } 1198 1199 /** 1200 * @tests java.lang.Float#equals(java.lang.Object) 1201 */ 1202 @TestTargetNew( 1203 level = TestLevel.COMPLETE, 1204 notes = "", 1205 method = "equals", 1206 args = {java.lang.Object.class} 1207 ) 1208 public void test_equalsLjava_lang_Object() { 1209 Float f1 = new Float(8765.4321f); 1210 Float f2 = new Float(8765.4321f); 1211 Float f3 = new Float(-1.0f); 1212 assertTrue("Assert 0: Equality test failed", f1.equals(f2) && !(f1.equals(f3))); 1213 1214 assertTrue("Assert 1: NaN should not be == Nan", Float.NaN != Float.NaN); 1215 assertTrue("Assert 2: NaN should not be == Nan", new Float(Float.NaN).equals(new Float( 1216 Float.NaN))); 1217 assertTrue("Assert 3: -0f should be == 0f", 0f == -0f); 1218 assertTrue("Assert 4: -0f should not be equals() 0f", !new Float(0f).equals(new Float( 1219 -0f))); 1220 1221 f1 = new Float(1098.576f); 1222 f2 = new Float(1098.576f); 1223 f3 = new Float(1.0f); 1224 assertTrue("Equality test failed", f1.equals(f2) && !(f1.equals(f3))); 1225 1226 assertTrue("NaN should not be == Nan", Float.NaN != Float.NaN); 1227 assertTrue("NaN should not be == Nan", new Float(Float.NaN) 1228 .equals(new Float(Float.NaN))); 1229 assertTrue("-0f should be == 0f", 0f == -0f); 1230 assertTrue("-0f should not be equals() 0f", !new Float(0f).equals(new Float(-0f))); 1231 } 1232 1233 /** 1234 * @tests java.lang.Float#toHexString(float) 1235 */ 1236 @TestTargetNew( 1237 level = TestLevel.COMPLETE, 1238 notes = "", 1239 method = "toHexString", 1240 args = {float.class} 1241 ) 1242 public void test_toHexStringF() { 1243 // the follow values comes from the Float Javadoc/Spec 1244 assertEquals("0x0.0p0", Float.toHexString(0.0F)); 1245 assertEquals("-0x0.0p0", Float.toHexString(-0.0F)); 1246 assertEquals("0x1.0p0", Float.toHexString(1.0F)); 1247 assertEquals("-0x1.0p0", Float.toHexString(-1.0F)); 1248 assertEquals("0x1.0p1", Float.toHexString(2.0F)); 1249 assertEquals("0x1.8p1", Float.toHexString(3.0F)); 1250 assertEquals("0x1.0p-1", Float.toHexString(0.5F)); 1251 assertEquals("0x1.0p-2", Float.toHexString(0.25F)); 1252 assertEquals("0x1.fffffep127", Float.toHexString(Float.MAX_VALUE)); 1253 assertEquals("0x0.000002p-126", Float.toHexString(Float.MIN_VALUE)); 1254 1255 // test edge cases 1256 assertEquals("NaN", Float.toHexString(Float.NaN)); 1257 assertEquals("-Infinity", Float.toHexString(Float.NEGATIVE_INFINITY)); 1258 assertEquals("Infinity", Float.toHexString(Float.POSITIVE_INFINITY)); 1259 1260 // test various numbers 1261 assertEquals("-0x1.da8p6", Float.toHexString(-118.625F)); 1262 assertEquals("0x1.295788p23", Float.toHexString(9743299.65F)); 1263 assertEquals("0x1.295788p23", Float.toHexString(9743299.65000F)); 1264 assertEquals("0x1.295788p23", Float.toHexString(9743299.650001234F)); 1265 assertEquals("0x1.700d1p33", Float.toHexString(12349743299.65000F)); 1266 1267 // test HARMONY-2132 1268 assertEquals("0x1.01p10", Float.toHexString(0x1.01p10f)); 1269 } 1270 1271 /** 1272 * @tests java.lang.Float#valueOf(float) 1273 */ 1274 @TestTargetNew( 1275 level = TestLevel.COMPLETE, 1276 notes = "", 1277 method = "valueOf", 1278 args = {float.class} 1279 ) 1280 public void test_valueOfF() { 1281 assertEquals(new Float(Float.MIN_VALUE), Float.valueOf(Float.MIN_VALUE)); 1282 assertEquals(new Float(Float.MAX_VALUE), Float.valueOf(Float.MAX_VALUE)); 1283 assertEquals(new Float(0), Float.valueOf(0)); 1284 1285 int s = -128; 1286 while (s < 128) { 1287 assertEquals(new Float(s), Float.valueOf(s)); 1288 assertEquals(new Float(s + 0.1F), Float.valueOf(s + 0.1F)); 1289 assertEquals(Float.valueOf(s + 0.1F), Float.valueOf(s + 0.1F)); 1290 s++; 1291 } 1292 } 1293} 1294