1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 /** 18 * @author Elena Semukhina 19 */ 20 21 package org.apache.harmony.tests.java.math; 22 23 import junit.framework.TestCase; 24 import java.math.BigInteger; 25 26 /** 27 * Class: java.math.BigInteger 28 * Methods: intValue, longValue, toByteArray(), valueOf(long val), 29 * floatValue(), doubleValue() 30 */ 31 public class BigIntegerConvertTest extends TestCase { 32 /** 33 * Return the double value of ZERO. 34 */ 35 public void testDoubleValueZero() { 36 String a = "0"; 37 double result = 0.0; 38 double aNumber = new BigInteger(a).doubleValue(); 39 assertTrue(aNumber == result); 40 } 41 42 /** 43 * Convert a positive number to a double value. 44 * The number's length is less than 64 bits. 45 */ 46 public void testDoubleValuePositive1() { 47 String a = "27467238945"; 48 double result = 2.7467238945E10; 49 double aNumber = new BigInteger(a).doubleValue(); 50 assertTrue(aNumber == result); 51 } 52 53 /** 54 * Convert a positive number to a double value. 55 * The number's bit length is inside [63, 1024]. 56 */ 57 public void testDoubleValuePositive2() { 58 String a = "2746723894572364578265426346273456972"; 59 double result = 2.7467238945723645E36; 60 double aNumber = new BigInteger(a).doubleValue(); 61 assertTrue(aNumber == result); 62 } 63 64 /** 65 * Convert a negative number to a double value. 66 * The number's bit length is less than 64 bits. 67 */ 68 public void testDoubleValueNegative1() { 69 String a = "-27467238945"; 70 double result = -2.7467238945E10; 71 double aNumber = new BigInteger(a).doubleValue(); 72 assertTrue(aNumber == result); 73 } 74 75 /** 76 * Convert a negative number to a double value. 77 * The number's bit length is inside [63, 1024]. 78 */ 79 public void testDoubleValueNegative2() { 80 String a = "-2746723894572364578265426346273456972"; 81 double result = -2.7467238945723645E36; 82 double aNumber = new BigInteger(a).doubleValue(); 83 assertTrue(aNumber == result); 84 } 85 86 /** 87 * Convert a positive number to a double value. 88 * Rounding is needed. 89 * The rounding bit is 1 and the next bit to the left is 1. 90 */ 91 public void testDoubleValuePosRounded1() { 92 byte[] a = {-128, 1, 2, 3, 4, 5, 60, 23, 1, -3, -5}; 93 int aSign = 1; 94 double result = 1.54747264387948E26; 95 double aNumber = new BigInteger(aSign, a).doubleValue(); 96 assertTrue(aNumber == result); 97 } 98 99 /** 100 * Convert a positive number to a double value. 101 * Rounding is needed. 102 * The rounding bit is 1 and the next bit to the left is 0 103 * but some of dropped bits are 1s. 104 */ 105 public void testDoubleValuePosRounded2() { 106 byte[] a = {-128, 1, 2, 3, 4, 5, 36, 23, 1, -3, -5}; 107 int aSign = 1; 108 double result = 1.547472643879479E26; 109 double aNumber = new BigInteger(aSign, a).doubleValue(); 110 assertTrue(aNumber == result); 111 } 112 /** 113 * Convert a positive number to a double value. 114 * Rounding is NOT needed. 115 */ 116 public void testDoubleValuePosNotRounded() { 117 byte[] a = {-128, 1, 2, 3, 4, 5, -128, 23, 1, -3, -5}; 118 int aSign = 1; 119 double result = 1.5474726438794828E26; 120 double aNumber = new BigInteger(aSign, a).doubleValue(); 121 assertTrue(aNumber == result); 122 } 123 124 /** 125 * Convert a positive number to a double value. 126 * Rounding is needed. 127 */ 128 public void testDoubleValueNegRounded1() { 129 byte[] a = {-128, 1, 2, 3, 4, 5, 60, 23, 1, -3, -5}; 130 int aSign = -1; 131 double result = -1.54747264387948E26; 132 double aNumber = new BigInteger(aSign, a).doubleValue(); 133 assertTrue(aNumber == result); 134 } 135 136 /** 137 * Convert a positive number to a double value. 138 * Rounding is needed. 139 * The rounding bit is 1 and the next bit to the left is 0 140 * but some of dropped bits are 1s. 141 */ 142 public void testDoubleValueNegRounded2() { 143 byte[] a = {-128, 1, 2, 3, 4, 5, 36, 23, 1, -3, -5}; 144 int aSign = -1; 145 double result = -1.547472643879479E26; 146 double aNumber = new BigInteger(aSign, a).doubleValue(); 147 assertTrue(aNumber == result); 148 } 149 150 /** 151 * Convert a positive number to a double value. 152 * Rounding is NOT needed. 153 */ 154 public void testDoubleValueNegNotRounded() { 155 byte[] a = {-128, 1, 2, 3, 4, 5, -128, 23, 1, -3, -5}; 156 int aSign = -1; 157 double result = -1.5474726438794828E26; 158 double aNumber = new BigInteger(aSign, a).doubleValue(); 159 assertTrue(aNumber == result); 160 } 161 162 /** 163 * Convert a positive number to a double value. 164 * The exponent is 1023 and the mantissa is all 1s. 165 * The rounding bit is 0. 166 * The result is Double.MAX_VALUE. 167 */ 168 public void testDoubleValuePosMaxValue() { 169 byte[] a = {0, -1, -1, -1, -1, -1, -1, -8, -1, -1, -1, -1, -1, -1, -1, -1, -1, 170 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 171 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 172 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 173 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 174 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 175 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 176 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 177 }; 178 int aSign = 1; 179 double aNumber = new BigInteger(aSign, a).doubleValue(); 180 assertTrue(aNumber == Double.MAX_VALUE); 181 } 182 183 /** 184 * Convert a negative number to a double value. 185 * The exponent is 1023 and the mantissa is all 1s. 186 * The result is -Double.MAX_VALUE. 187 */ 188 public void testDoubleValueNegMaxValue() { 189 byte[] a = {0, -1, -1, -1, -1, -1, -1, -8, -1, -1, -1, -1, -1, -1, -1, -1, -1, 190 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 191 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 192 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 193 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 194 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 195 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 196 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 197 }; 198 int aSign = -1; 199 double aNumber = new BigInteger(aSign, a).doubleValue(); 200 assertTrue(aNumber == -Double.MAX_VALUE); 201 } 202 203 /** 204 * Convert a positive number to a double value. 205 * The exponent is 1023 and the mantissa is all 1s. 206 * The rounding bit is 1. 207 * The result is Double.POSITIVE_INFINITY. 208 */ 209 public void testDoubleValuePositiveInfinity1() { 210 byte[] a = {-1, -1, -1, -1, -1, -1, -1, -8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 211 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 212 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 213 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 214 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 215 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 216 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 217 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 218 }; 219 int aSign = 1; 220 double aNumber = new BigInteger(aSign, a).doubleValue(); 221 assertTrue(aNumber == Double.POSITIVE_INFINITY); 222 } 223 224 /** 225 * Convert a positive number to a double value. 226 * The number's bit length is greater than 1024. 227 */ 228 public void testDoubleValuePositiveInfinity2() { 229 String a = "2746723894572364578265426346273456972283746872364768676747462342342342342342342342323423423423423423426767456345745293762384756238475634563456845634568934568347586346578648576478568456457634875673845678456786587345873645767456834756745763457863485768475678465783456702897830296720476846578634576384567845678346573465786457863"; 230 double aNumber = new BigInteger(a).doubleValue(); 231 assertTrue(aNumber == Double.POSITIVE_INFINITY); 232 } 233 234 /** 235 * Convert a negative number to a double value. 236 * The number's bit length is greater than 1024. 237 */ 238 public void testDoubleValueNegativeInfinity1() { 239 String a = "-2746723894572364578265426346273456972283746872364768676747462342342342342342342342323423423423423423426767456345745293762384756238475634563456845634568934568347586346578648576478568456457634875673845678456786587345873645767456834756745763457863485768475678465783456702897830296720476846578634576384567845678346573465786457863"; 240 double aNumber = new BigInteger(a).doubleValue(); 241 assertTrue(aNumber == Double.NEGATIVE_INFINITY); 242 } 243 244 /** 245 * Convert a negative number to a double value. 246 * The exponent is 1023 and the mantissa is all 0s. 247 * The rounding bit is 0. 248 * The result is Double.NEGATIVE_INFINITY. 249 */ 250 public void testDoubleValueNegativeInfinity2() { 251 byte[] a = {-1, -1, -1, -1, -1, -1, -1, -8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 252 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 253 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 254 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 256 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 257 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 258 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 259 }; 260 int aSign = -1; 261 double aNumber = new BigInteger(aSign, a).doubleValue(); 262 assertTrue(aNumber == Double.NEGATIVE_INFINITY); 263 } 264 265 /** 266 * Convert a positive number to a double value. 267 * The exponent is 1023 and the mantissa is all 0s 268 * but the 54th bit (implicit) is 1. 269 */ 270 public void testDoubleValuePosMantissaIsZero() { 271 byte[] a = {-128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 272 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 273 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 274 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 275 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 276 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 277 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 278 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 279 }; 280 int aSign = 1; 281 double result = 8.98846567431158E307; 282 double aNumber = new BigInteger(aSign, a).doubleValue(); 283 assertTrue(aNumber == result); 284 } 285 286 /** 287 * Convert a positive number to a double value. 288 * The exponent is 1023 and the mantissa is all 0s 289 * but the 54th bit (implicit) is 1. 290 */ 291 public void testDoubleValueNegMantissaIsZero() { 292 byte[] a = {-128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 293 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 294 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 295 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 296 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 297 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 298 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 299 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 300 }; 301 int aSign = -1; 302 double aNumber = new BigInteger(aSign, a).doubleValue(); 303 assertTrue(aNumber == -8.98846567431158E307); 304 } 305 306 /** 307 * Return the float value of ZERO. 308 */ 309 public void testFloatValueZero() { 310 String a = "0"; 311 float result = 0.0f; 312 float aNumber = new BigInteger(a).floatValue(); 313 assertTrue(aNumber == result); 314 } 315 316 /** 317 * Convert a positive number to a float value. 318 * The number's length is less than 32 bits. 319 */ 320 public void testFloatValuePositive1() { 321 String a = "27467238"; 322 float result = 2.7467238E7f; 323 float aNumber = new BigInteger(a).floatValue(); 324 assertTrue(aNumber == result); 325 } 326 327 /** 328 * Convert a positive number to a float value. 329 * The number's bit length is inside [32, 127]. 330 */ 331 public void testFloatValuePositive2() { 332 String a = "27467238945723645782"; 333 float result = 2.7467239E19f; 334 float aNumber = new BigInteger(a).floatValue(); 335 assertTrue(aNumber == result); 336 } 337 338 /** 339 * Convert a negative number to a float value. 340 * The number's bit length is less than 32 bits. 341 */ 342 public void testFloatValueNegative1() { 343 String a = "-27467238"; 344 float result = -2.7467238E7f; 345 float aNumber = new BigInteger(a).floatValue(); 346 assertTrue(aNumber == result); 347 } 348 349 /** 350 * Convert a negative number to a doufloatble value. 351 * The number's bit length is inside [63, 1024]. 352 */ 353 public void testFloatValueNegative2() { 354 String a = "-27467238945723645782"; 355 float result = -2.7467239E19f; 356 float aNumber = new BigInteger(a).floatValue(); 357 assertTrue(aNumber == result); 358 } 359 360 /** 361 * Convert a positive number to a float value. 362 * Rounding is needed. 363 * The rounding bit is 1 and the next bit to the left is 1. 364 */ 365 public void testFloatValuePosRounded1() { 366 byte[] a = {-128, 1, -1, -4, 4, 5, 60, 23, 1, -3, -5}; 367 int aSign = 1; 368 float result = 1.5475195E26f; 369 float aNumber = new BigInteger(aSign, a).floatValue(); 370 assertTrue(aNumber == result); 371 } 372 373 /** 374 * Convert a positive number to a float value. 375 * Rounding is needed. 376 * The rounding bit is 1 and the next bit to the left is 0 377 * but some of dropped bits are 1s. 378 */ 379 public void testFloatValuePosRounded2() { 380 byte[] a = {-128, 1, 2, -128, 4, 5, 60, 23, 1, -3, -5}; 381 int aSign = 1; 382 float result = 1.5474728E26f; 383 float aNumber = new BigInteger(aSign, a).floatValue(); 384 assertTrue(aNumber == result); 385 } 386 /** 387 * Convert a positive number to a float value. 388 * Rounding is NOT needed. 389 */ 390 public void testFloatValuePosNotRounded() { 391 byte[] a = {-128, 1, 2, 3, 4, 5, 60, 23, 1, -3, -5}; 392 int aSign = 1; 393 float result = 1.5474726E26f; 394 float aNumber = new BigInteger(aSign, a).floatValue(); 395 assertTrue(aNumber == result); 396 } 397 398 /** 399 * Convert a positive number to a float value. 400 * Rounding is needed. 401 */ 402 public void testFloatValueNegRounded1() { 403 byte[] a = {-128, 1, -1, -4, 4, 5, 60, 23, 1, -3, -5}; 404 int aSign = -1; 405 float result = -1.5475195E26f; 406 float aNumber = new BigInteger(aSign, a).floatValue(); 407 assertTrue(aNumber == result); 408 } 409 410 /** 411 * Convert a positive number to a float value. 412 * Rounding is needed. 413 * The rounding bit is 1 and the next bit to the left is 0 414 * but some of dropped bits are 1s. 415 */ 416 public void testFloatValueNegRounded2() { 417 byte[] a = {-128, 1, 2, -128, 4, 5, 60, 23, 1, -3, -5}; 418 int aSign = -1; 419 float result = -1.5474728E26f; 420 float aNumber = new BigInteger(aSign, a).floatValue(); 421 assertTrue(aNumber == result); 422 } 423 424 /** 425 * Convert a positive number to a float value. 426 * Rounding is NOT needed. 427 */ 428 public void testFloatValueNegNotRounded() { 429 byte[] a = {-128, 1, 2, 3, 4, 5, 60, 23, 1, -3, -5}; 430 int aSign = -1; 431 float result = -1.5474726E26f; 432 float aNumber = new BigInteger(aSign, a).floatValue(); 433 assertTrue(aNumber == result); 434 } 435 436 /** 437 * Convert a positive number to a float value. 438 * The exponent is 1023 and the mantissa is all 1s. 439 * The rounding bit is 0. 440 * The result is Float.MAX_VALUE. 441 */ 442 public void testFloatValuePosMaxValue() { 443 byte[] a = {0, -1, -1, -1, 0, -1, -1, -8, -1, -1, -1, -1, -1, -1, -1, -1, -1}; 444 int aSign = 1; 445 float aNumber = new BigInteger(aSign, a).floatValue(); 446 assertTrue(aNumber == Float.MAX_VALUE); 447 } 448 449 /** 450 * Convert a negative number to a float value. 451 * The exponent is 1023 and the mantissa is all 1s. 452 * The rounding bit is 0. 453 * The result is -Float.MAX_VALUE. 454 */ 455 public void testFloatValueNegMaxValue() { 456 byte[] a = {0, -1, -1, -1, 0, -1, -1, -8, -1, -1, -1, -1, -1, -1, -1, -1, -1}; 457 int aSign = -1; 458 float aNumber = new BigInteger(aSign, a).floatValue(); 459 assertTrue(aNumber == -Float.MAX_VALUE); 460 } 461 462 /** 463 * Convert a positive number to a float value. 464 * The exponent is 1023 and the mantissa is all 1s. 465 * The rounding bit is 1. 466 * The result is Float.POSITIVE_INFINITY. 467 */ 468 public void testFloatValuePositiveInfinity1() { 469 byte[] a = {0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; 470 int aSign = 1; 471 float aNumber = new BigInteger(aSign, a).floatValue(); 472 assertTrue(aNumber == Float.POSITIVE_INFINITY); 473 } 474 475 /** 476 * Convert a positive number to a float value. 477 * The number's bit length is greater than 127. 478 */ 479 public void testFloatValuePositiveInfinity2() { 480 String a = "2746723894572364578265426346273456972283746872364768676747462342342342342342342342323423423423423423426767456345745293762384756238475634563456845634568934568347586346578648576478568456457634875673845678456786587345873645767456834756745763457863485768475678465783456702897830296720476846578634576384567845678346573465786457863"; 481 float aNumber = new BigInteger(a).floatValue(); 482 assertTrue(aNumber == Float.POSITIVE_INFINITY); 483 } 484 485 /** 486 * Convert a negative number to a float value. 487 * The number's bit length is greater than 127. 488 */ 489 public void testFloatValueNegativeInfinity1() { 490 String a = "-2746723894572364578265426346273456972283746872364768676747462342342342342342342342323423423423423423426767456345745293762384756238475634563456845634568934568347586346578648576478568456457634875673845678456786587345873645767456834756745763457863485768475678465783456702897830296720476846578634576384567845678346573465786457863"; 491 float aNumber = new BigInteger(a).floatValue(); 492 assertTrue(aNumber == Float.NEGATIVE_INFINITY); 493 } 494 495 /** 496 * Convert a negative number to a float value. 497 * The exponent is 1023 and the mantissa is all 0s. 498 * The rounding bit is 0. 499 * The result is Float.NEGATIVE_INFINITY. 500 */ 501 public void testFloatValueNegativeInfinity2() { 502 byte[] a = {0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; 503 int aSign = -1; 504 float aNumber = new BigInteger(aSign, a).floatValue(); 505 assertTrue(aNumber == Float.NEGATIVE_INFINITY); 506 } 507 508 /** 509 * Convert a positive number to a float value. 510 * The exponent is 1023 and the mantissa is all 0s 511 * but the 54th bit (implicit) is 1. 512 */ 513 public void testFloatValuePosMantissaIsZero() { 514 byte[] a = {-128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 515 int aSign = 1; 516 float result = 1.7014118E38f; 517 float aNumber = new BigInteger(aSign, a).floatValue(); 518 assertTrue(aNumber == result); 519 } 520 521 /** 522 * Convert a positive number to a double value. 523 * The exponent is 1023 and the mantissa is all 0s 524 * but the 54th bit (implicit) is 1. 525 */ 526 public void testFloatValueNegMantissaIsZero() { 527 byte[] a = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 528 int aSign = -1; 529 float aNumber = new BigInteger(aSign, a).floatValue(); 530 assertTrue(aNumber == Float.NEGATIVE_INFINITY); 531 } 532 533 /** 534 * Convert a negative number to a float value. 535 * The number's bit length is less than 32 bits. 536 */ 537 public void testFloatValueBug2482() { 538 String a = "2147483649"; 539 float result = 2.14748365E9f; 540 float aNumber = new BigInteger(a).floatValue(); 541 assertTrue(aNumber == result); 542 } 543 544 /** 545 * Convert a positive BigInteger to an integer value. 546 * The low digit is positive 547 */ 548 public void testIntValuePositive1() { 549 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3}; 550 int resInt = 1496144643; 551 int aNumber = new BigInteger(aBytes).intValue(); 552 assertTrue(aNumber == resInt); 553 } 554 555 /** 556 * Convert a positive BigInteger to an integer value. 557 * The low digit is positive 558 */ 559 public void testIntValuePositive2() { 560 byte aBytes[] = {12, 56, 100}; 561 int resInt = 800868; 562 int aNumber = new BigInteger(aBytes).intValue(); 563 assertTrue(aNumber == resInt); 564 } 565 566 /** 567 * Convert a positive BigInteger to an integer value. 568 * The low digit is negative. 569 */ 570 public void testIntValuePositive3() { 571 byte aBytes[] = {56, 13, 78, -12, -5, 56, 100}; 572 int sign = 1; 573 int resInt = -184862620; 574 int aNumber = new BigInteger(sign, aBytes).intValue(); 575 assertTrue(aNumber == resInt); 576 } 577 578 /** 579 * Convert a negative BigInteger to an integer value. 580 * The low digit is negative. 581 */ 582 public void testIntValueNegative1() { 583 byte aBytes[] = {12, 56, 100, -2, -76, -128, 45, 91, 3}; 584 int sign = -1; 585 int resInt = 2144511229; 586 int aNumber = new BigInteger(sign, aBytes).intValue(); 587 assertTrue(aNumber == resInt); 588 } 589 590 /** 591 * Convert a negative BigInteger to an integer value. 592 * The low digit is negative. 593 */ 594 public void testIntValueNegative2() { 595 byte aBytes[] = {-12, 56, 100}; 596 int result = -771996; 597 int aNumber = new BigInteger(aBytes).intValue(); 598 assertTrue(aNumber == result); 599 } 600 601 /** 602 * Convert a negative BigInteger to an integer value. 603 * The low digit is positive. 604 */ 605 public void testIntValueNegative3() { 606 byte aBytes[] = {12, 56, 100, -2, -76, 127, 45, 91, 3}; 607 int sign = -1; 608 int resInt = -2133678851; 609 int aNumber = new BigInteger(sign, aBytes).intValue(); 610 assertTrue(aNumber == resInt); 611 } 612 613 /** 614 * Convert a BigInteger to a positive long value 615 * The BigInteger is longer than int. 616 */ 617 public void testLongValuePositive1() { 618 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, 120, -34, -12, 45, 98}; 619 long result = 3268209772258930018L; 620 long aNumber = new BigInteger(aBytes).longValue(); 621 assertTrue(aNumber == result); 622 } 623 624 /** 625 * Convert a number to a positive long value 626 * The number fits in a long. 627 */ 628 public void testLongValuePositive2() { 629 byte aBytes[] = {12, 56, 100, 18, -105, 34, -18, 45}; 630 long result = 880563758158769709L; 631 long aNumber = new BigInteger(aBytes).longValue(); 632 assertTrue(aNumber == result); 633 } 634 635 /** 636 * Convert a number to a negative long value 637 * The BigInteger is longer than int. 638 */ 639 public void testLongValueNegative1() { 640 byte aBytes[] = {12, -1, 100, -2, -76, -128, 45, 91, 3}; 641 long result = -43630045168837885L; 642 long aNumber = new BigInteger(aBytes).longValue(); 643 assertTrue(aNumber == result); 644 } 645 646 /** 647 * Convert a number to a negative long value 648 * The number fits in a long. 649 */ 650 public void testLongValueNegative2() { 651 byte aBytes[] = {-12, 56, 100, 45, -101, 45, 98}; 652 long result = -3315696807498398L; 653 long aNumber = new BigInteger(aBytes).longValue(); 654 assertTrue(aNumber == result); 655 } 656 657 /** 658 * valueOf (long val): convert Integer.MAX_VALUE to a BigInteger. 659 */ 660 public void testValueOfIntegerMax() { 661 long longVal = Integer.MAX_VALUE; 662 BigInteger aNumber = BigInteger.valueOf(longVal); 663 byte rBytes[] = {127, -1, -1, -1}; 664 byte resBytes[] = new byte[rBytes.length]; 665 resBytes = aNumber.toByteArray(); 666 for(int i = 0; i < resBytes.length; i++) { 667 assertTrue(resBytes[i] == rBytes[i]); 668 } 669 assertEquals("incorrect sign", 1, aNumber.signum()); 670 } 671 672 /** 673 * valueOf (long val): convert Integer.MIN_VALUE to a BigInteger. 674 */ 675 public void testValueOfIntegerMin() { 676 long longVal = Integer.MIN_VALUE; 677 BigInteger aNumber = BigInteger.valueOf(longVal); 678 byte rBytes[] = {-128, 0, 0, 0}; 679 byte resBytes[] = new byte[rBytes.length]; 680 resBytes = aNumber.toByteArray(); 681 for(int i = 0; i < resBytes.length; i++) { 682 assertTrue(resBytes[i] == rBytes[i]); 683 } 684 assertEquals("incorrect sign", -1, aNumber.signum()); 685 } 686 687 /** 688 * valueOf (long val): convert Long.MAX_VALUE to a BigInteger. 689 */ 690 public void testValueOfLongMax() { 691 long longVal = Long.MAX_VALUE; 692 BigInteger aNumber = BigInteger.valueOf(longVal); 693 byte rBytes[] = {127, -1, -1, -1, -1, -1, -1, -1}; 694 byte resBytes[] = new byte[rBytes.length]; 695 resBytes = aNumber.toByteArray(); 696 for(int i = 0; i < resBytes.length; i++) { 697 assertTrue(resBytes[i] == rBytes[i]); 698 } 699 assertEquals("incorrect sign", 1, aNumber.signum()); 700 } 701 702 /** 703 * valueOf (long val): convert Long.MIN_VALUE to a BigInteger. 704 */ 705 public void testValueOfLongMin() { 706 long longVal = Long.MIN_VALUE; 707 BigInteger aNumber = BigInteger.valueOf(longVal); 708 byte rBytes[] = {-128, 0, 0, 0, 0, 0, 0, 0}; 709 byte resBytes[] = new byte[rBytes.length]; 710 resBytes = aNumber.toByteArray(); 711 for(int i = 0; i < resBytes.length; i++) { 712 assertTrue(resBytes[i] == rBytes[i]); 713 } 714 assertEquals("incorrect sign", -1, aNumber.signum()); 715 } 716 717 /** 718 * valueOf (long val): convert a positive long value to a BigInteger. 719 */ 720 public void testValueOfLongPositive1() { 721 long longVal = 268209772258930018L; 722 BigInteger aNumber = BigInteger.valueOf(longVal); 723 byte rBytes[] = {3, -72, -33, 93, -24, -56, 45, 98}; 724 byte resBytes[] = new byte[rBytes.length]; 725 resBytes = aNumber.toByteArray(); 726 for(int i = 0; i < resBytes.length; i++) { 727 assertTrue(resBytes[i] == rBytes[i]); 728 } 729 assertEquals("incorrect sign", 1, aNumber.signum()); 730 } 731 732 /** 733 * valueOf (long val): convert a positive long value to a BigInteger. 734 * The long value fits in integer. 735 */ 736 public void testValueOfLongPositive2() { 737 long longVal = 58930018L; 738 BigInteger aNumber = BigInteger.valueOf(longVal); 739 byte rBytes[] = {3, -125, 51, 98}; 740 byte resBytes[] = new byte[rBytes.length]; 741 resBytes = aNumber.toByteArray(); 742 for(int i = 0; i < resBytes.length; i++) { 743 assertTrue(resBytes[i] == rBytes[i]); 744 } 745 assertEquals("incorrect sign", 1, aNumber.signum()); 746 } 747 748 /** 749 * valueOf (long val): convert a negative long value to a BigInteger. 750 */ 751 public void testValueOfLongNegative1() { 752 long longVal = -268209772258930018L; 753 BigInteger aNumber = BigInteger.valueOf(longVal); 754 byte rBytes[] = {-4, 71, 32, -94, 23, 55, -46, -98}; 755 byte resBytes[] = new byte[rBytes.length]; 756 resBytes = aNumber.toByteArray(); 757 for(int i = 0; i < resBytes.length; i++) { 758 assertTrue(resBytes[i] == rBytes[i]); 759 } 760 assertEquals("incorrect sign", -1, aNumber.signum()); 761 } 762 763 /** 764 * valueOf (long val): convert a negative long value to a BigInteger. 765 * The long value fits in integer. 766 */ 767 public void testValueOfLongNegative2() { 768 long longVal = -58930018L; 769 BigInteger aNumber = BigInteger.valueOf(longVal); 770 byte rBytes[] = {-4, 124, -52, -98}; 771 byte resBytes[] = new byte[rBytes.length]; 772 resBytes = aNumber.toByteArray(); 773 for(int i = 0; i < resBytes.length; i++) { 774 assertTrue(resBytes[i] == rBytes[i]); 775 } 776 assertEquals("incorrect sign", -1, aNumber.signum()); 777 } 778 /** 779 * valueOf (long val): convert a zero long value to a BigInteger. 780 */ 781 public void testValueOfLongZero() { 782 long longVal = 0L; 783 BigInteger aNumber = BigInteger.valueOf(longVal); 784 byte rBytes[] = {0}; 785 byte resBytes[] = new byte[rBytes.length]; 786 resBytes = aNumber.toByteArray(); 787 for(int i = 0; i < resBytes.length; i++) { 788 assertTrue(resBytes[i] == rBytes[i]); 789 } 790 assertEquals("incorrect sign", 0, aNumber.signum()); 791 } 792 } 793