Path: blob/master/test/jdk/java/lang/Short/Decode.java
41149 views
/*1* Copyright (c) 1998, 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 4136371 5017980 657605526* @summary Test Short.decode method27* @author madbot28* @author Joseph D. Darcy29*/3031/**32* There are six methods in java.lang.Short which transform strings33* into a short or Short value:34*35* public Short(String s)36* public static Short decode(String nm)37* public static short parseShort(String s, int radix)38* public static short parseShort(String s)39* public static Short valueOf(String s, int radix)40* public static Short valueOf(String s)41*42* However, of these only decode has a nontrivial implementation43* in that class.44*/45public class Decode {4647private static void check(String ashort, short expected) {48short sh = (Short.decode(ashort)).shortValue();49if (sh != expected)50throw new RuntimeException("Short.decode failed. String:" +51ashort + " Result:" + sh);52}5354private static void checkFailure(String val, String message) {55try {56short n = (Short.decode(val)).shortValue();57throw new RuntimeException(message);58} catch (NumberFormatException e) { /* Okay */}59}6061public static void main(String[] args) throws Exception {62check(new String(""+Short.MIN_VALUE), Short.MIN_VALUE);63check(new String(""+Short.MAX_VALUE), Short.MAX_VALUE);6465check("10", (short)10);66check("0x10", (short)16);67check("0X10", (short)16);68check("010", (short)8);69check("#10", (short)16);7071check("+10", (short)10);72check("+0x10", (short)16);73check("+0X10", (short)16);74check("+010", (short)8);75check("+#10", (short)16);7677check("-10", (short)-10);78check("-0x10", (short)-16);79check("-0X10", (short)-16);80check("-010", (short)-8);81check("-#10", (short)-16);8283check(Integer.toString((int)Short.MIN_VALUE), Short.MIN_VALUE);84check(Integer.toString((int)Short.MAX_VALUE), Short.MAX_VALUE);8586checkFailure("0x-10", "Short.decode allows negative sign in wrong position.");87checkFailure("0x+10", "Short.decode allows positive sign in wrong position.");8889checkFailure("+", "Raw plus sign allowed.");90checkFailure("-", "Raw minus sign allowed.");9192checkFailure(Integer.toString((int)Short.MIN_VALUE - 1), "Out of range");93checkFailure(Integer.toString((int)Short.MAX_VALUE + 1), "Out of range");9495checkFailure("", "Empty String");96}97}9899100