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
27 * @summary Test parsing methods
28 * @author Joseph D. Darcy
29 */
30
31
32 /**
33 * There are six methods in java.lang.Long which transform strings
34 * into a long or Long value:
35 *
36 * public Long(String s)
37 * public static Long decode(String nm)
38 * public static long parseLong(String s, int radix)
39 * public static long parseLong(String s)
40 * public static Long valueOf(String s, int radix)
41 * public static Long valueOf(String s)
42 *
43 * Besides decode, all the methods and constructor call down into
44 * parseLong(String, int) to do the actual work. Therefore, the
45 * behavior of parseLong(String, int) will be tested here.
46 */
47
48 public class ParsingTest {
49 public static void main(String... argv) {
50 check("+100", +100L);
51 check("-100", -100L);
52
53 check("+0", 0L);
54 check("-0", 0L);
55 check("+00000", 0L);
56 check("-00000", 0L);
57
58 check("0", 0L);
59 check("1", 1L);
60 check("9", 9L);
61
62 checkFailure("\u0000");
63 checkFailure("\u002f");
64 checkFailure("+");
65 checkFailure("-");
66 checkFailure("++");
67 checkFailure("+-");
68 checkFailure("-+");
69 checkFailure("--");
70 checkFailure("++100");
71 checkFailure("--100");
72 checkFailure("+-6");
73 checkFailure("-+6");
74 checkFailure("*100");
75 }
76
77 private static void check(String val, long expected) {
78 long n = Long.parseLong(val);
79 if (n != expected)
80 throw new RuntimeException("Long.parsedLong failed. String:" +
81 val + " Result:" + n);
82 }
83
84 private static void checkFailure(String val) {
85 long n = 0L;
86 try {
87 n = Long.parseLong(val);
88 System.err.println("parseLong(" + val + ") incorrectly returned " + n);
89 throw new RuntimeException();
90 } catch (NumberFormatException nfe) {
91 ; // Expected
92 }
93 }
94 }
|
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
27 * @summary Test parsing methods
28 * @author Joseph D. Darcy
29 */
30
31 /**
32 * There are six methods in java.lang.Long which transform strings
33 * into a long or Long value:
34 *
35 * public Long(String s)
36 * public static Long decode(String nm)
37 * public static long parseLong(CharSequence s, int radix, int beginIndex, int endIndex)
38 * public static long parseLong(CharSequence s, int radix, int beginIndex)
39 * public static long parseLong(String s, int radix)
40 * public static long parseLong(String s)
41 * public static Long valueOf(String s, int radix)
42 * public static Long valueOf(String s)
43 *
44 * Besides decode, all the methods and constructor call down into
45 * parseLong(CharSequence, int, int, int) to do the actual work. Therefore, the
46 * behavior of parseLong(CharSequence, int, int, int) will be tested here.
47 */
48
49 public class ParsingTest {
50
51 public static void main(String... argv) {
52 check("+100", +100L);
53 check("-100", -100L);
54
55 check("+0", 0L);
56 check("-0", 0L);
57 check("+00000", 0L);
58 check("-00000", 0L);
59
60 check("0", 0L);
61 check("1", 1L);
62 check("9", 9L);
63
64 checkFailure("\u0000");
65 checkFailure("\u002f");
66 checkFailure("+");
67 checkFailure("-");
68 checkFailure("++");
69 checkFailure("+-");
70 checkFailure("-+");
71 checkFailure("--");
72 checkFailure("++100");
73 checkFailure("--100");
74 checkFailure("+-6");
75 checkFailure("-+6");
76 checkFailure("*100");
77
78 check("test-00000", 0L, 4, 10);
79 check("test-12345", -12345L, 4, 10);
80 check("xx12345yy", 12345L, 2, 7);
81 check("xx123456789012345yy", 123456789012345L, 2, 17);
82
83 checkNumberFormatException("+-6", 10, 0, 3);
84 checkNumberFormatException("1000000", Character.MAX_RADIX + 1, 0, 2);
85 checkNumberFormatException("1000000", Character.MIN_RADIX - 1, 0, 2);
86 checkNumberFormatException("1000000", Character.MAX_RADIX + 1, -1, 2);
87 checkNumberFormatException("1000000", Character.MIN_RADIX - 1, -1, 2);
88
89 checkIllegalArgumentException("1000000", 10, 7, 7);
90 checkIllegalArgumentException("1000000", 10, 10, 4);
91 checkIllegalArgumentException("1000000", Character.MAX_RADIX + 1, 10, 2);
92 checkIllegalArgumentException("1000000", Character.MIN_RADIX - 1, 10, 2);
93
94 checkIndexOutOfBoundsException("-1", 10, 0, 3);
95 checkIndexOutOfBoundsException("-1", 10, 2, 3);
96 checkIndexOutOfBoundsException("-1", 10, -1, 2);
97
98 checkNull(10, 0, 1);
99 checkNull(10, -1, 0);
100 checkNull(10, 0, 0);
101 checkNull(10, 0, -1);
102 checkNull(-1, -1, -1);
103 }
104
105 private static void check(String val, long expected) {
106 long n = Long.parseLong(val);
107 if (n != expected)
108 throw new RuntimeException("Long.parseLong failed. String:" +
109 val + " Result:" + n);
110 }
111
112 private static void checkFailure(String val) {
113 long n = 0L;
114 try {
115 n = Long.parseLong(val);
116 System.err.println("parseLong(" + val + ") incorrectly returned " + n);
117 throw new RuntimeException();
118 } catch (NumberFormatException nfe) {
119 ; // Expected
120 }
121 }
122
123 private static void checkNumberFormatException(String val, int radix, int start, int end) {
124 long n = 0;
125 try {
126 n = Long.parseLong(val, radix, start, end);
127 System.err.println("parseInt(" + val + ", " + radix + ", " + start + ", " + end +
128 ") incorrectly returned " + n);
129 throw new RuntimeException();
130 } catch (NumberFormatException nfe) {
131 ; // Expected
132 }
133 }
134
135 private static void checkIndexOutOfBoundsException(String val, int radix, int start, int end) {
136 long n = 0;
137 try {
138 n = Long.parseLong(val, radix, start, end);
139 System.err.println("parseInt(" + val + ", " + radix + ", " + start + ", " + end +
140 ") incorrectly returned " + n);
141 throw new RuntimeException();
142 } catch (IndexOutOfBoundsException ioob) {
143 ; // Expected
144 }
145 }
146
147 private static void checkIllegalArgumentException(String val, int radix, int start, int end) {
148 long n = 0;
149 try {
150 n = Long.parseLong(val, radix, start, end);
151 System.err.println("parseInt(" + val + ", " + radix + ", " + start + ", " + end +
152 ") incorrectly returned " + n);
153 throw new RuntimeException();
154 } catch (IllegalArgumentException nfe) {
155 ; // Expected
156 }
157 }
158
159 private static void checkNull(int radix, int start, int end) {
160 long n = 0;
161 try {
162 n = Long.parseLong(null, 10, start, end);
163 System.err.println("parseInt(null, " + radix + ", " + start + ", " + end +
164 ") incorrectly returned " + n);
165 throw new RuntimeException();
166 } catch (NullPointerException npe) {
167 ; // Expected
168 }
169 }
170
171 private static void check(String val, long expected, int start, int end) {
172 long n = Long.parseLong(val, 10, start, end);
173 if (n != expected)
174 throw new RuntimeException("Long.parseLong failed. String:" +
175 val + ", start: " + start + ", end: " + end + " Result:" + n);
176 }
177 }
|