1 /*
2  * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 4242173 5017980 6576055
27  * @summary Test Byte.decode method
28  * @author madbot
29  * @author Joseph D. Darcy
30  */
31 
32 /**
33  * There are six methods in java.lang.Byte which transform strings
34  * into a byte or Byte value:
35  *
36  * public Byte(String s)
37  * public static Byte decode(String nm)
38  * public static byte parseByte(String s, int radix)
39  * public static byte parseByte(String s)
40  * public static Byte valueOf(String s, int radix)
41  * public static Byte valueOf(String s)
42  *
43  * However, of these only decode has a nontrivial implementation
44  * in that class.
45  */
46 public class Decode {
47 
check(String val, byte expected)48     private static void check(String val, byte expected) {
49         byte n = (Byte.decode(val)).byteValue();
50         if (n != expected)
51             throw new RuntimeException("Byte.decode failed. String:" +
52                                                 val + " Result:" + n);
53     }
54 
checkFailure(String val, String message)55     private static void checkFailure(String val, String message) {
56         try {
57             byte n = (Byte.decode(val)).byteValue();
58             throw new RuntimeException(message);
59         } catch (NumberFormatException e) { /* Okay */}
60     }
61 
main(String[] args)62     public static void main(String[] args) throws Exception {
63         check(new String(""+Byte.MIN_VALUE), Byte.MIN_VALUE);
64         check(new String(""+Byte.MAX_VALUE), Byte.MAX_VALUE);
65 
66         check("10",   (byte)10);
67         check("0x10", (byte)16);
68         check("0X10", (byte)16);
69         check("010",  (byte)8);
70         check("#10",  (byte)16);
71 
72         check("+10",   (byte)10);
73         check("+0x10", (byte)16);
74         check("+0X10", (byte)16);
75         check("+010",  (byte)8);
76         check("+#10",  (byte)16);
77 
78         check("-10",   (byte)-10);
79         check("-0x10", (byte)-16);
80         check("-0X10", (byte)-16);
81         check("-010",  (byte)-8);
82         check("-#10",  (byte)-16);
83 
84         check(Integer.toString((int)Byte.MIN_VALUE), Byte.MIN_VALUE);
85         check(Integer.toString((int)Byte.MAX_VALUE), Byte.MAX_VALUE);
86 
87         checkFailure("0x-10",   "Byte.decode allows negative sign in wrong position.");
88         checkFailure("0x+10",   "Byte.decode allows positive sign in wrong position.");
89 
90         checkFailure("+",       "Raw plus sign allowed.");
91         checkFailure("-",       "Raw minus sign allowed.");
92 
93         checkFailure(Integer.toString((int)Byte.MIN_VALUE - 1), "Out of range");
94         checkFailure(Integer.toString((int)Byte.MAX_VALUE + 1), "Out of range");
95 
96         checkFailure("", "Empty String");
97     }
98 }
99