Path: blob/master/test/jdk/java/lang/Integer/Decode.java
41149 views
/*1* Copyright (c) 2006, 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 4136371 5017980 657605526* @summary Test Integer.decode method27* @author madbot28* @author Joseph D. Darcy29*/3031/**32* There are six methods in java.lang.Integer which transform strings33* into an int or Integer value:34*35* public Integer(String s)36* public static Integer decode(String nm)37* public static int parseInteger(String s, int radix)38* public static int parseInteger(String s)39* public static Integer valueOf(String s, int radix)40* public static Integer valueOf(String s)41*42* The other five methods are tested elsewhere.43*/44public class Decode {4546private static void check(String val, int expected) {47int n = (Integer.decode(val)).intValue();48if (n != expected)49throw new RuntimeException("Integer.decode failed. String:" +50val + " Result:" + n);51}5253private static void checkFailure(String val, String message) {54try {55int n = (Integer.decode(val)).intValue();56throw new RuntimeException(message);57} catch (NumberFormatException e) { /* Okay */}58}5960public static void main(String[] args) throws Exception {61check(new String(""+Integer.MIN_VALUE), Integer.MIN_VALUE);62check(new String(""+Integer.MAX_VALUE), Integer.MAX_VALUE);6364check("10", 10);65check("0x10", 16);66check("0X10", 16);67check("010", 8);68check("#10", 16);6970check("+10", 10);71check("+0x10", 16);72check("+0X10", 16);73check("+010", 8);74check("+#10", 16);7576check("-10", -10);77check("-0x10", -16);78check("-0X10", -16);79check("-010", -8);80check("-#10", -16);8182check(Long.toString(Integer.MIN_VALUE), Integer.MIN_VALUE);83check(Long.toString(Integer.MAX_VALUE), Integer.MAX_VALUE);8485checkFailure("0x-10", "Integer.decode allows negative sign in wrong position.");86checkFailure("0x+10", "Integer.decode allows positive sign in wrong position.");8788checkFailure("+", "Raw plus sign allowed.");89checkFailure("-", "Raw minus sign allowed.");9091checkFailure(Long.toString((long)Integer.MIN_VALUE - 1L), "Out of range");92checkFailure(Long.toString((long)Integer.MAX_VALUE + 1L), "Out of range");9394checkFailure("", "Empty String");9596try {97Integer.decode(null);98throw new RuntimeException("Integer.decode(null) expected to throw NPE");99} catch (NullPointerException npe) {/* Okay */}100}101}102103104