1826
|
1 |
/*
|
|
2 |
* Copyright 1998-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 4136371 5017980 6576055
|
|
27 |
* @summary Test Short.decode method
|
|
28 |
* @author madbot
|
|
29 |
* @author Joseph D. Darcy
|
|
30 |
*/
|
|
31 |
|
|
32 |
/**
|
|
33 |
* There are six methods in java.lang.Short which transform strings
|
|
34 |
* into a short or Short value:
|
|
35 |
*
|
|
36 |
* public Short(String s)
|
|
37 |
* public static Short decode(String nm)
|
|
38 |
* public static short parseShort(String s, int radix)
|
|
39 |
* public static short parseShort(String s)
|
|
40 |
* public static Short valueOf(String s, int radix)
|
|
41 |
* public static Short valueOf(String s)
|
|
42 |
*
|
|
43 |
* However, of these only decode has a nontrivial implementation
|
|
44 |
* in that class.
|
|
45 |
*/
|
|
46 |
public class Decode {
|
|
47 |
|
|
48 |
private static void check(String ashort, short expected) {
|
|
49 |
short sh = (Short.decode(ashort)).shortValue();
|
|
50 |
if (sh != expected)
|
|
51 |
throw new RuntimeException("Short.decode failed. String:" +
|
|
52 |
ashort + " Result:" + sh);
|
|
53 |
}
|
|
54 |
|
|
55 |
private static void checkFailure(String val, String message) {
|
|
56 |
try {
|
|
57 |
short n = (Short.decode(val)).shortValue();
|
|
58 |
throw new RuntimeException(message);
|
|
59 |
} catch (NumberFormatException e) { /* Okay */}
|
|
60 |
}
|
|
61 |
|
|
62 |
public static void main(String[] args) throws Exception {
|
|
63 |
check(new String(""+Short.MIN_VALUE), Short.MIN_VALUE);
|
|
64 |
check(new String(""+Short.MAX_VALUE), Short.MAX_VALUE);
|
|
65 |
|
|
66 |
check("10", (short)10);
|
|
67 |
check("0x10", (short)16);
|
|
68 |
check("0X10", (short)16);
|
|
69 |
check("010", (short)8);
|
|
70 |
check("#10", (short)16);
|
|
71 |
|
|
72 |
check("+10", (short)10);
|
|
73 |
check("+0x10", (short)16);
|
|
74 |
check("+0X10", (short)16);
|
|
75 |
check("+010", (short)8);
|
|
76 |
check("+#10", (short)16);
|
|
77 |
|
|
78 |
check("-10", (short)-10);
|
|
79 |
check("-0x10", (short)-16);
|
|
80 |
check("-0X10", (short)-16);
|
|
81 |
check("-010", (short)-8);
|
|
82 |
check("-#10", (short)-16);
|
|
83 |
|
|
84 |
check(Integer.toString((int)Short.MIN_VALUE), Short.MIN_VALUE);
|
|
85 |
check(Integer.toString((int)Short.MAX_VALUE), Short.MAX_VALUE);
|
|
86 |
|
|
87 |
checkFailure("0x-10", "Short.decode allows negative sign in wrong position.");
|
|
88 |
checkFailure("0x+10", "Short.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)Short.MIN_VALUE - 1), "Out of range");
|
|
94 |
checkFailure(Integer.toString((int)Short.MAX_VALUE + 1), "Out of range");
|
|
95 |
|
|
96 |
checkFailure("", "Empty String");
|
|
97 |
}
|
|
98 |
}
|