• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2006, 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 5017980 6576055 8041972 8055251
27  * @summary Test parsing methods
28  * @author Joseph D. Darcy
29  */
30 package test.java.lang.Integer;
31 
32 import java.lang.IndexOutOfBoundsException;
33 import java.lang.NullPointerException;
34 import java.lang.RuntimeException;
35 
36 // Android-added: support for wrapper to avoid d8 backporting of Integer.parseInt (b/215435866).
37 import java.lang.invoke.MethodHandle;
38 import java.lang.invoke.MethodHandles;
39 import java.lang.invoke.MethodType;
40 
41 /**
42  * There are seven methods in java.lang.Integer which transform strings
43  * into an int or Integer value:
44  *
45  * public Integer(String s)
46  * public static Integer decode(String nm)
47  * public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix)
48  * public static int parseInt(String s, int radix)
49  * public static int parseInt(String s)
50  * public static Integer valueOf(String s, int radix)
51  * public static Integer valueOf(String s)
52  *
53  * Besides decode, all the methods and constructor call down into
54  * parseInt(CharSequence, int, int, int) to do the actual work.  Therefore, the
55  * behavior of parseInt(CharSequence, int, int, int) will be tested here.
56  *
57  */
58 
59 public class ParsingTest {
60 
main(String... argv)61     public static void main(String... argv) {
62         check(+100, "+100");
63         check(-100, "-100");
64 
65         check(0, "+0");
66         check(0, "-0");
67         check(0, "+00000");
68         check(0, "-00000");
69 
70         check(0, "0");
71         check(1, "1");
72         check(9, "9");
73 
74         checkFailure("");
75         checkFailure("\u0000");
76         checkFailure("\u002f");
77         checkFailure("+");
78         checkFailure("-");
79         checkFailure("++");
80         checkFailure("+-");
81         checkFailure("-+");
82         checkFailure("--");
83         checkFailure("++100");
84         checkFailure("--100");
85         checkFailure("+-6");
86         checkFailure("-+6");
87         checkFailure("*100");
88 
89         // check offset based methods
90         check(0, "+00000", 0, 6, 10);
91         check(0, "-00000", 0, 6, 10);
92         check(0, "test-00000", 4, 10, 10);
93         check(-12345, "test-12345", 4, 10, 10);
94         check(12345, "xx12345yy", 2, 7, 10);
95         check(15, "xxFyy", 2, 3, 16);
96 
97         checkNumberFormatException("", 0, 0, 10);
98         checkNumberFormatException("+-6", 0, 3, 10);
99         checkNumberFormatException("1000000", 7, 7, 10);
100         checkNumberFormatException("1000000", 0, 2, Character.MAX_RADIX + 1);
101         checkNumberFormatException("1000000", 0, 2, Character.MIN_RADIX - 1);
102 
103         checkIndexOutOfBoundsException("1000000", 10, 4, 10);
104         checkIndexOutOfBoundsException("1000000", -1, 2, Character.MAX_RADIX + 1);
105         checkIndexOutOfBoundsException("1000000", -1, 2, Character.MIN_RADIX - 1);
106         checkIndexOutOfBoundsException("1000000", 10, 2, Character.MAX_RADIX + 1);
107         checkIndexOutOfBoundsException("1000000", 10, 2, Character.MIN_RADIX - 1);
108         checkIndexOutOfBoundsException("-1", 0, 3, 10);
109         checkIndexOutOfBoundsException("-1", 2, 3, 10);
110         checkIndexOutOfBoundsException("-1", -1, 2, 10);
111 
112         checkNull(0, 1, 10);
113         checkNull(-1, 0, 10);
114         checkNull(0, 0, 10);
115         checkNull(0, -1, 10);
116         checkNull(-1, -1, -1);
117     }
118 
check(int expected, String val)119     private static void check(int expected, String val) {
120         int n = Integer.parseInt(val);
121         if (n != expected)
122             throw new RuntimeException("Integer.parseInt failed. String:" +
123                                                 val + " Result:" + n);
124     }
125 
checkFailure(String val)126     private static void checkFailure(String val) {
127         int n = 0;
128         try {
129             n = Integer.parseInt(val);
130             System.err.println("parseInt(" + val + ") incorrectly returned " + n);
131             throw new RuntimeException();
132         } catch (NumberFormatException nfe) {
133             ; // Expected
134         }
135     }
136 
checkNumberFormatException(String val, int start, int end, int radix)137     private static void checkNumberFormatException(String val, int start, int end, int radix) {
138         int n = 0;
139         try {
140             // Android-changed: call wrapper to avoid d8 backports (b/215435866).
141             // n = Integer.parseInt(val, start, end, radix);
142             n = Integer_parseInt(val, start, end, radix);
143             System.err.println("parseInt(" + val + ", " + start + ", " + end + ", " + radix +
144                     ") incorrectly returned " + n);
145             throw new RuntimeException();
146         } catch (NumberFormatException nfe) {
147             ; // Expected
148         }
149     }
150 
checkIndexOutOfBoundsException(String val, int start, int end, int radix)151     private static void checkIndexOutOfBoundsException(String val, int start, int end, int radix) {
152         int n = 0;
153         try {
154             // Android-changed: call wrapper to avoid d8 backports (b/215435866).
155             // n = Integer.parseInt(val, start, end, radix);
156             n = Integer_parseInt(val, start, end, radix);
157             System.err.println("parseInt(" + val + ", " + start + ", " + end + ", " + radix  +
158                     ") incorrectly returned " + n);
159             throw new RuntimeException();
160         } catch (IndexOutOfBoundsException ioob) {
161             ; // Expected
162         }
163     }
164 
checkNull(int start, int end, int radix)165     private static void checkNull(int start, int end, int radix) {
166         int n = 0;
167         try {
168             // Android-changed: call wrapper to avoid d8 backports (b/215435866).
169             // n = Integer.parseInt(null, start, end, radix);
170             n = Integer_parseInt(null, start, end, radix);
171             System.err.println("parseInt(null, " + start + ", " + end + ", " + radix +
172                     ") incorrectly returned " + n);
173             throw new RuntimeException();
174         } catch (NullPointerException npe) {
175             ; // Expected
176         }
177     }
178 
check(int expected, String val, int start, int end, int radix)179     private static void check(int expected, String val, int start, int end, int radix) {
180         // Android-changed: call wrapper to avoid d8 backports (b/215435866).
181         // int n = Integer.parseInt(val, start, end, radix);
182         int n = Integer_parseInt(val, start, end, radix);
183         if (n != expected)
184             throw new RuntimeException("Integer.parsedInt failed. Expected: " + expected + " String: \"" +
185                     val + "\", start: " + start + ", end: " + end + ", radix: " + radix + " Result:" + n);
186     }
187 
188     // Android-added: wrapper to avoid d8 backporting of Integer.parseInt (b/215435866).
Integer_parseInt(String val, int start, int end, int radix)189     private static int Integer_parseInt(String val, int start, int end, int radix) {
190         try {
191             MethodType parseIntType = MethodType.methodType(int.class,
192                                                             CharSequence.class,
193                                                             int.class,
194                                                             int.class,
195                                                             int.class);
196             MethodHandle parseInt =
197                     MethodHandles.lookup().findStatic(Integer.class, "parseInt", parseIntType);
198             return (int) parseInt.invokeExact((CharSequence) val, start, end, radix);
199         } catch (IndexOutOfBoundsException | NullPointerException | NumberFormatException e) {
200             // Expected exceptions from the target method during the tests here.
201             throw e;
202         } catch (Throwable t) {
203             // Everything else.
204             throw new RuntimeException(t);
205         }
206     }
207 }
208