Path: blob/master/test/jdk/java/math/BigInteger/StringConstructor.java
41149 views
/*1* Copyright (c) 2001, 2007, 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 4489146 501798026* @summary tests String constructors of BigInteger27* @author Joseph D. Darcy28*/29import java.math.*;3031public class StringConstructor {323334public static void main(String [] argv) {35// Good strings36constructWithoutError("0", 0L);37constructWithoutError("000000000000000000", 0L);38constructWithoutError("1", 1L);39constructWithoutError("-1", -1L);40constructWithoutError("+1", +1L);41constructWithoutError( "123456789123456789", 123456789123456789L);42constructWithoutError("+123456789123456789", 123456789123456789L);43constructWithoutError("-123456789123456789", -123456789123456789L);44constructWithoutError(Integer.toString(Integer.MIN_VALUE),45(long)Integer.MIN_VALUE);46constructWithoutError(Integer.toString(Integer.MAX_VALUE),47(long)Integer.MAX_VALUE);48constructWithoutError(Long.toString(Long.MIN_VALUE),49Long.MIN_VALUE);50constructWithoutError(Long.toString(Long.MAX_VALUE),51Long.MAX_VALUE);5253// Bad strings54constructWithError("");55constructWithError("-");56constructWithError("+");57constructWithError("--");58constructWithError("++");59constructWithError("-000-0");60constructWithError("+000+0");61constructWithError("+000-0");62constructWithError("--1234567890");63constructWithError("++1234567890");64constructWithError("-0-12345678");65constructWithError("+0+12345678");66constructWithError("--12345678-12345678-12345678");67constructWithError("++12345678+12345678+12345678");68constructWithError("12345-");69constructWithError("12345+");70}7172// this method adapted from ../BigDecimal/StringConstructor.java73private static void constructWithError(String badString) {74try {75BigInteger bi = new BigInteger(badString);76throw new RuntimeException(badString + " accepted");77} catch(NumberFormatException e) {78}79}8081private static void constructWithoutError(String goodString, long value) {82BigInteger bi = new BigInteger(goodString);83if(bi.longValue() != value) {84System.err.printf("From ``%s'' expected %d, got %s.\n", goodString, value, bi);85throw new RuntimeException();86}87}8889}909192