Path: blob/master/test/jdk/java/math/BigInteger/PrimitiveConversionTests.java
41152 views
/*1* Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import static java.math.BigInteger.ONE;2425import java.math.BigInteger;26import java.util.ArrayList;27import java.util.Arrays;28import java.util.Collections;29import java.util.List;30import java.util.Random;3132/**33* @test34* @bug 713119235* @summary This test ensures that BigInteger.floatValue() and36* BigInteger.doubleValue() behave correctly.37* @author Louis Wasserman38*/39public class PrimitiveConversionTests {40static final List<BigInteger> ALL_BIGINTEGER_CANDIDATES;4142static {43List<BigInteger> samples = new ArrayList<>();44// Now add values near 2^N for lots of values of N.45for (int exponent : Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 31, 32, 33,4634, 62, 63, 64, 65, 71, 72, 73, 79, 80, 81, 255, 256, 257, 511,47512, 513, Double.MAX_EXPONENT - 1, Double.MAX_EXPONENT,48Double.MAX_EXPONENT + 1, 2000, 2001, 2002)) {49BigInteger x = ONE.shiftLeft(exponent);50for (BigInteger y : Arrays.asList(x, x.add(ONE), x.subtract(ONE))) {51samples.add(y);52samples.add(y.negate());53}54}5556Random rng = new Random(1234567);57for (int i = 0; i < 2000; i++) {58samples.add(new BigInteger(rng.nextInt(2000), rng));59}6061ALL_BIGINTEGER_CANDIDATES = Collections.unmodifiableList(samples);62}6364public static int testDoubleValue() {65System.out.println("--- testDoubleValue ---");66int failures = 0;67for (BigInteger big : ALL_BIGINTEGER_CANDIDATES) {68double expected = Double.parseDouble(big.toString());69double actual = big.doubleValue();7071// should be bitwise identical72if (Double.doubleToRawLongBits(expected) != Double73.doubleToRawLongBits(actual)) {74System.out.format("big: %s, expected: %f, actual: %f%n",75big, expected, actual);76failures++;77}78}79return failures;80}8182public static int testFloatValue() {83System.out.println("--- testFloatValue ---");84int failures = 0;85for (BigInteger big : ALL_BIGINTEGER_CANDIDATES) {86float expected = Float.parseFloat(big.toString());87float actual = big.floatValue();8889// should be bitwise identical90if (Float.floatToRawIntBits(expected) != Float91.floatToRawIntBits(actual)) {92System.out.format("big: %s, expected: %f, actual: %f%n",93big, expected, actual);94failures++;95}96}97return failures;98}99100public static void main(String[] args) {101int failures = testDoubleValue();102failures += testFloatValue();103if (failures > 0) {104throw new RuntimeException("Incurred " + failures105+ " failures while testing primitive conversions.");106}107}108}109110111