Path: blob/master/test/jdk/java/math/BigInteger/TestValueExact.java
41149 views
/*1* Copyright (c) 2011, 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*/2223/*24* @test25* @bug 637140126* @summary Tests of fooValueExact methods27* @author Joseph D. Darcy28*/29import java.math.BigInteger;3031public class TestValueExact {32public static void main(String... args) {33int errors = 0;3435errors += testLongValueExact();36errors += testIntValueExact();37errors += testShortValueExact();38errors += testByteValueExact();3940if (errors > 0)41throw new RuntimeException();42}4344private static int testLongValueExact() {45int errors = 0;46BigInteger[] inRange = {47BigInteger.valueOf(Long.MIN_VALUE),48BigInteger.ZERO,49BigInteger.valueOf(Long.MAX_VALUE)50};5152BigInteger[] outOfRange = {53BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE),54BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE)55};5657for (BigInteger bi : inRange) {58if (bi.longValueExact() != bi.longValue()) {59System.err.println("Mismatching int conversion for " + bi);60errors++;61}62}6364for (BigInteger bi : outOfRange) {65try {66long value = bi.longValueExact();67System.err.println("Failed to get expected exception on " +68bi + " got " + value);69errors++;70} catch(ArithmeticException ae) {71; // Expected72}73}74return errors;75}7677private static int testIntValueExact() {78int errors = 0;79BigInteger[] inRange = {80BigInteger.valueOf(Integer.MIN_VALUE),81BigInteger.ZERO,82BigInteger.ONE,83BigInteger.TEN,84BigInteger.valueOf(Integer.MAX_VALUE)85};8687BigInteger[] outOfRange = {88BigInteger.valueOf((long)Integer.MIN_VALUE - 1),89BigInteger.valueOf((long)Integer.MAX_VALUE + 1)90};9192for (BigInteger bi : inRange) {93if (bi.intValueExact() != bi.intValue()) {94System.err.println("Mismatching int conversion for " + bi);95errors++;96}97}9899for (BigInteger bi : outOfRange) {100try {101int value = bi.intValueExact();102System.err.println("Failed to get expected exception on " +103bi + " got " + value);104errors++;105} catch(ArithmeticException ae) {106; // Expected107}108}109return errors;110}111112private static int testShortValueExact() {113int errors = 0;114BigInteger[] inRange = {115BigInteger.valueOf(Short.MIN_VALUE),116BigInteger.ZERO,117BigInteger.ONE,118BigInteger.TEN,119BigInteger.valueOf(Short.MAX_VALUE)120};121122BigInteger[] outOfRange = {123BigInteger.valueOf((long)Integer.MIN_VALUE - 1),124BigInteger.valueOf((long)Integer.MIN_VALUE),125BigInteger.valueOf( (int)Short.MIN_VALUE - 1),126BigInteger.valueOf( (int)Short.MAX_VALUE + 1),127BigInteger.valueOf((long)Integer.MAX_VALUE),128BigInteger.valueOf((long)Integer.MAX_VALUE + 1)129};130131for (BigInteger bi : inRange) {132if (bi.shortValueExact() != bi.shortValue()) {133System.err.println("Mismatching short conversion for " + bi);134errors++;135}136}137138for (BigInteger bi : outOfRange) {139try {140int value = bi.shortValueExact();141System.err.println("Failed to get expected exception on " +142bi + " got " + value);143errors++;144} catch(ArithmeticException ae) {145; // Expected146}147}148return errors;149}150151private static int testByteValueExact() {152int errors = 0;153BigInteger[] inRange = {154BigInteger.valueOf(Byte.MIN_VALUE),155BigInteger.valueOf(0),156BigInteger.ONE,157BigInteger.TEN,158BigInteger.valueOf(Byte.MAX_VALUE)159};160161BigInteger[] outOfRange = {162BigInteger.valueOf((long)Integer.MIN_VALUE - 1),163BigInteger.valueOf((long)Integer.MIN_VALUE),164BigInteger.valueOf( (int)Short.MIN_VALUE - 1),165BigInteger.valueOf( (int)Short.MIN_VALUE),166BigInteger.valueOf( (int)Byte.MIN_VALUE - 1),167BigInteger.valueOf( (int)Byte.MAX_VALUE + 1),168BigInteger.valueOf( (int)Short.MAX_VALUE + 1),169BigInteger.valueOf( (int)Short.MAX_VALUE),170BigInteger.valueOf((long)Integer.MAX_VALUE),171BigInteger.valueOf((long)Integer.MAX_VALUE + 1)172};173174for (BigInteger bi : inRange) {175if (bi.byteValueExact() != bi.byteValue()) {176System.err.println("Mismatching byte conversion for " + bi);177errors++;178}179}180181for (BigInteger bi : outOfRange) {182try {183int value = bi.byteValueExact();184System.err.println("Failed to get expected exception on " +185bi + " got " + value);186errors++;187} catch(ArithmeticException ae) {188; // Expected189}190}191return errors;192}193}194195196