Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/math/BigInteger/PrimitiveConversionTests.java
38812 views
/*1* Copyright (c) 1998, 2013, 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() {65int failures = 0;66for (BigInteger big : ALL_BIGINTEGER_CANDIDATES) {67double expected = Double.parseDouble(big.toString());68double actual = big.doubleValue();6970// should be bitwise identical71if (Double.doubleToRawLongBits(expected) != Double72.doubleToRawLongBits(actual)) {73System.out.println(big);74failures++;75}76}77return failures;78}7980public static int testFloatValue() {81int failures = 0;82for (BigInteger big : ALL_BIGINTEGER_CANDIDATES) {83float expected = Float.parseFloat(big.toString());84float actual = big.floatValue();8586// should be bitwise identical87if (Float.floatToRawIntBits(expected) != Float88.floatToRawIntBits(actual)) {89System.out.println(big + " " + expected + " " + actual);90failures++;91}92}93return failures;94}9596public static void main(String[] args) {97int failures = testDoubleValue();98failures += testFloatValue();99if (failures > 0) {100throw new RuntimeException("Incurred " + failures101+ " failures while testing primitive conversions.");102}103}104}105106107