1 /* 2 * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package test.java.math.BigInteger; 24 25 import static java.math.BigInteger.ONE; 26 27 import java.math.BigInteger; 28 import java.util.ArrayList; 29 import java.util.Arrays; 30 import java.util.Collections; 31 import java.util.List; 32 import java.util.Random; 33 34 import org.testng.Assert; 35 import org.testng.annotations.Test; 36 37 /** 38 * @test 39 * @bug 7131192 40 * @summary This test ensures that BigInteger.floatValue() and 41 * BigInteger.doubleValue() behave correctly. 42 * @author Louis Wasserman 43 */ 44 45 // Android-changed: Replace error printing with asserts. 46 public class PrimitiveConversionTests { 47 static final List<BigInteger> ALL_BIGINTEGER_CANDIDATES; 48 49 static { 50 List<BigInteger> samples = new ArrayList<>(); 51 // Now add values near 2^N for lots of values of N. 52 for (int exponent : Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 31, 32, 33, 53 34, 62, 63, 64, 65, 71, 72, 73, 79, 80, 81, 255, 256, 257, 511, 54 512, 513, Double.MAX_EXPONENT - 1, Double.MAX_EXPONENT, 55 Double.MAX_EXPONENT + 1, 2000, 2001, 2002)) { 56 BigInteger x = ONE.shiftLeft(exponent); 57 for (BigInteger y : Arrays.asList(x, x.add(ONE), x.subtract(ONE))) { 58 samples.add(y); y.negate()59 samples.add(y.negate()); 60 } 61 } 62 63 Random rng = new Random(1234567); 64 for (int i = 0; i < 2000; i++) { samples.add(new BigInteger(rng.nextInt(2000), rng))65 samples.add(new BigInteger(rng.nextInt(2000), rng)); 66 } 67 68 ALL_BIGINTEGER_CANDIDATES = Collections.unmodifiableList(samples); 69 } 70 71 @Test testDoubleValue()72 public void testDoubleValue() { 73 for (BigInteger big : ALL_BIGINTEGER_CANDIDATES) { 74 double expected = Double.parseDouble(big.toString()); 75 double actual = big.doubleValue(); 76 77 // should be bitwise identical 78 Assert.assertEquals(Double.doubleToRawLongBits(actual), 79 Double.doubleToRawLongBits(expected)); 80 } 81 } 82 83 @Test testFloatValue()84 public void testFloatValue() { 85 for (BigInteger big : ALL_BIGINTEGER_CANDIDATES) { 86 float expected = Float.parseFloat(big.toString()); 87 float actual = big.floatValue(); 88 89 // should be bitwise identical 90 Assert.assertEquals(Float.floatToRawIntBits(actual), 91 Float.floatToRawIntBits(expected)); 92 } 93 } 94 95 } 96