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