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
31import java.lang.IndexOutOfBoundsException;
32import java.lang.NullPointerException;
33import java.lang.RuntimeException;
34
35/**
36 * There are seven methods in java.lang.Integer which transform strings
37 * into an int or Integer value:
38 *
39 * public Integer(String s)
40 * public static Integer decode(String nm)
41 * public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix)
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
48 * parseInt(CharSequence, int, int, int) to do the actual work.  Therefore, the
49 * behavior of parseInt(CharSequence, int, int, int) will be tested here.
50 *
51 */
52
53public class ParsingTest {
54
55    public static void main(String... argv) {
56        check(+100, "+100");
57        check(-100, "-100");
58
59        check(0, "+0");
60        check(0, "-0");
61        check(0, "+00000");
62        check(0, "-00000");
63
64        check(0, "0");
65        check(1, "1");
66        check(9, "9");
67
68        checkFailure("");
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");
82
83        // check offset based methods
84        check(0, "+00000", 0, 6, 10);
85        check(0, "-00000", 0, 6, 10);
86        check(0, "test-00000", 4, 10, 10);
87        check(-12345, "test-12345", 4, 10, 10);
88        check(12345, "xx12345yy", 2, 7, 10);
89        check(15, "xxFyy", 2, 3, 16);
90
91        checkNumberFormatException("", 0, 0, 10);
92        checkNumberFormatException("+-6", 0, 3, 10);
93        checkNumberFormatException("1000000", 7, 7, 10);
94        checkNumberFormatException("1000000", 0, 2, Character.MAX_RADIX + 1);
95        checkNumberFormatException("1000000", 0, 2, Character.MIN_RADIX - 1);
96
97        checkIndexOutOfBoundsException("1000000", 10, 4, 10);
98        checkIndexOutOfBoundsException("1000000", -1, 2, Character.MAX_RADIX + 1);
99        checkIndexOutOfBoundsException("1000000", -1, 2, Character.MIN_RADIX - 1);
100        checkIndexOutOfBoundsException("1000000", 10, 2, Character.MAX_RADIX + 1);
101        checkIndexOutOfBoundsException("1000000", 10, 2, Character.MIN_RADIX - 1);
102        checkIndexOutOfBoundsException("-1", 0, 3, 10);
103        checkIndexOutOfBoundsException("-1", 2, 3, 10);
104        checkIndexOutOfBoundsException("-1", -1, 2, 10);
105
106        checkNull(0, 1, 10);
107        checkNull(-1, 0, 10);
108        checkNull(0, 0, 10);
109        checkNull(0, -1, 10);
110        checkNull(-1, -1, -1);
111    }
112
113    private static void check(int expected, String val) {
114        int n = Integer.parseInt(val);
115        if (n != expected)
116            throw new RuntimeException("Integer.parseInt failed. String:" +
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    }
130
131    private static void checkNumberFormatException(String val, int start, int end, int radix) {
132        int n = 0;
133        try {
134            n = Integer.parseInt(val, start, end, radix);
135            System.err.println("parseInt(" + val + ", " + start + ", " + end + ", " + radix +
136                    ") incorrectly returned " + n);
137            throw new RuntimeException();
138        } catch (NumberFormatException nfe) {
139            ; // Expected
140        }
141    }
142
143    private static void checkIndexOutOfBoundsException(String val, int start, int end, int radix) {
144        int n = 0;
145        try {
146            n = Integer.parseInt(val, start, end, radix);
147            System.err.println("parseInt(" + val + ", " + start + ", " + end + ", " + radix  +
148                    ") incorrectly returned " + n);
149            throw new RuntimeException();
150        } catch (IndexOutOfBoundsException ioob) {
151            ; // Expected
152        }
153    }
154
155    private static void checkNull(int start, int end, int radix) {
156        int n = 0;
157        try {
158            n = Integer.parseInt(null, start, end, radix);
159            System.err.println("parseInt(null, " + start + ", " + end + ", " + radix +
160                    ") incorrectly returned " + n);
161            throw new RuntimeException();
162        } catch (NullPointerException npe) {
163            ; // Expected
164        }
165    }
166
167    private static void check(int expected, String val, int start, int end, int radix) {
168        int n = Integer.parseInt(val, start, end, radix);
169        if (n != expected)
170            throw new RuntimeException("Integer.parsedInt failed. Expected: " + expected + " String: \"" +
171                    val + "\", start: " + start + ", end: " + end + ", radix: " + radix + " Result:" + n);
172    }
173}
174