author | mikael |
Fri, 04 Dec 2015 15:08:49 -0800 | |
changeset 35090 | 1f5b6aa795d0 |
parent 26462 | d6d34934be12 |
permissions | -rw-r--r-- |
1826 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved. |
1826 | 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 |
* |
|
5506 | 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. |
|
1826 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
26 |
* @bug 5017980 6576055 8041972 8055251 |
1826 | 27 |
* @summary Test parsing methods |
28 |
* @author Joseph D. Darcy |
|
29 |
*/ |
|
30 |
||
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
31 |
import java.lang.IndexOutOfBoundsException; |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
32 |
import java.lang.NullPointerException; |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
33 |
import java.lang.RuntimeException; |
1826 | 34 |
|
35 |
/** |
|
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
36 |
* There are seven methods in java.lang.Integer which transform strings |
1826 | 37 |
* into an int or Integer value: |
38 |
* |
|
39 |
* public Integer(String s) |
|
40 |
* public static Integer decode(String nm) |
|
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
41 |
* public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix) |
1826 | 42 |
* public static int parseInt(String s, int radix) |
43 |
* public static int parseInt(String s) |
|
44 |
* public static Integer valueOf(String s, int radix) |
|
45 |
* public static Integer valueOf(String s) |
|
46 |
* |
|
47 |
* Besides decode, all the methods and constructor call down into |
|
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
48 |
* parseInt(CharSequence, int, int, int) to do the actual work. Therefore, the |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
49 |
* behavior of parseInt(CharSequence, int, int, int) will be tested here. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
50 |
* |
1826 | 51 |
*/ |
52 |
||
53 |
public class ParsingTest { |
|
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
54 |
|
1826 | 55 |
public static void main(String... argv) { |
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
56 |
check(+100, "+100"); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
57 |
check(-100, "-100"); |
1826 | 58 |
|
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
59 |
check(0, "+0"); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
60 |
check(0, "-0"); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
61 |
check(0, "+00000"); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
62 |
check(0, "-00000"); |
1826 | 63 |
|
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
64 |
check(0, "0"); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
65 |
check(1, "1"); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
66 |
check(9, "9"); |
1826 | 67 |
|
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
68 |
checkFailure(""); |
1826 | 69 |
checkFailure("\u0000"); |
70 |
checkFailure("\u002f"); |
|
71 |
checkFailure("+"); |
|
72 |
checkFailure("-"); |
|
73 |
checkFailure("++"); |
|
74 |
checkFailure("+-"); |
|
75 |
checkFailure("-+"); |
|
76 |
checkFailure("--"); |
|
77 |
checkFailure("++100"); |
|
78 |
checkFailure("--100"); |
|
79 |
checkFailure("+-6"); |
|
80 |
checkFailure("-+6"); |
|
81 |
checkFailure("*100"); |
|
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
82 |
|
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
83 |
// check offset based methods |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
84 |
check(0, "+00000", 0, 6, 10); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
85 |
check(0, "-00000", 0, 6, 10); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
86 |
check(0, "test-00000", 4, 10, 10); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
87 |
check(-12345, "test-12345", 4, 10, 10); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
88 |
check(12345, "xx12345yy", 2, 7, 10); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
89 |
check(15, "xxFyy", 2, 3, 16); |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
90 |
|
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
91 |
checkNumberFormatException("", 0, 0, 10); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
92 |
checkNumberFormatException("+-6", 0, 3, 10); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
93 |
checkNumberFormatException("1000000", 7, 7, 10); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
94 |
checkNumberFormatException("1000000", 0, 2, Character.MAX_RADIX + 1); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
95 |
checkNumberFormatException("1000000", 0, 2, Character.MIN_RADIX - 1); |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
96 |
|
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
97 |
checkIndexOutOfBoundsException("1000000", 10, 4, 10); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
98 |
checkIndexOutOfBoundsException("1000000", -1, 2, Character.MAX_RADIX + 1); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
99 |
checkIndexOutOfBoundsException("1000000", -1, 2, Character.MIN_RADIX - 1); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
100 |
checkIndexOutOfBoundsException("1000000", 10, 2, Character.MAX_RADIX + 1); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
101 |
checkIndexOutOfBoundsException("1000000", 10, 2, Character.MIN_RADIX - 1); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
102 |
checkIndexOutOfBoundsException("-1", 0, 3, 10); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
103 |
checkIndexOutOfBoundsException("-1", 2, 3, 10); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
104 |
checkIndexOutOfBoundsException("-1", -1, 2, 10); |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
105 |
|
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
106 |
checkNull(0, 1, 10); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
107 |
checkNull(-1, 0, 10); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
108 |
checkNull(0, 0, 10); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
109 |
checkNull(0, -1, 10); |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
110 |
checkNull(-1, -1, -1); |
1826 | 111 |
} |
112 |
||
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
113 |
private static void check(int expected, String val) { |
1826 | 114 |
int n = Integer.parseInt(val); |
115 |
if (n != expected) |
|
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
116 |
throw new RuntimeException("Integer.parseInt failed. String:" + |
1826 | 117 |
val + " Result:" + n); |
118 |
} |
|
119 |
||
120 |
private static void checkFailure(String val) { |
|
121 |
int n = 0; |
|
122 |
try { |
|
123 |
n = Integer.parseInt(val); |
|
124 |
System.err.println("parseInt(" + val + ") incorrectly returned " + n); |
|
125 |
throw new RuntimeException(); |
|
126 |
} catch (NumberFormatException nfe) { |
|
127 |
; // Expected |
|
128 |
} |
|
129 |
} |
|
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
130 |
|
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
131 |
private static void checkNumberFormatException(String val, int start, int end, int radix) { |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
132 |
int n = 0; |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
133 |
try { |
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
134 |
n = Integer.parseInt(val, start, end, radix); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
135 |
System.err.println("parseInt(" + val + ", " + start + ", " + end + ", " + radix + |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
136 |
") incorrectly returned " + n); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
137 |
throw new RuntimeException(); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
138 |
} catch (NumberFormatException nfe) { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
139 |
; // Expected |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
140 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
141 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
142 |
|
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
143 |
private static void checkIndexOutOfBoundsException(String val, int start, int end, int radix) { |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
144 |
int n = 0; |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
145 |
try { |
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
146 |
n = Integer.parseInt(val, start, end, radix); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
147 |
System.err.println("parseInt(" + val + ", " + start + ", " + end + ", " + radix + |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
148 |
") incorrectly returned " + n); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
149 |
throw new RuntimeException(); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
150 |
} catch (IndexOutOfBoundsException ioob) { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
151 |
; // Expected |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
152 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
153 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
154 |
|
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
155 |
private static void checkNull(int start, int end, int radix) { |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
156 |
int n = 0; |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
157 |
try { |
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
158 |
n = Integer.parseInt(null, start, end, radix); |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
159 |
System.err.println("parseInt(null, " + start + ", " + end + ", " + radix + |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
160 |
") incorrectly returned " + n); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
161 |
throw new RuntimeException(); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
162 |
} catch (NullPointerException npe) { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
163 |
; // Expected |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
164 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
165 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
166 |
|
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
167 |
private static void check(int expected, String val, int start, int end, int radix) { |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
168 |
int n = Integer.parseInt(val, start, end, radix); |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
169 |
if (n != expected) |
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
170 |
throw new RuntimeException("Integer.parsedInt failed. Expected: " + expected + " String: \"" + |
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25653
diff
changeset
|
171 |
val + "\", start: " + start + ", end: " + end + ", radix: " + radix + " Result:" + n); |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
5506
diff
changeset
|
172 |
} |
1826 | 173 |
} |