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 import sun.misc.SharedSecrets;
32 import sun.misc.JavaLangAccess;
33
34 /**
35 * There are six methods in java.lang.Long which transform strings
36 * into a long or Long value:
37 *
38 * public Long(String s)
39 * public static Long decode(String nm)
40 * public static long parseLong(String s, int radix)
41 * public static long parseLong(String s)
42 * public static Long valueOf(String s, int radix)
43 * public static Long valueOf(String s)
44 *
45 * Besides decode, all the methods and constructor call down into
46 * parseLong(String, int) to do the actual work. Therefore, the
47 * behavior of parseLong(String, int) will be tested here.
48 *
49 * Internally, parseLong(String, int, int, int) has been added to
50 * support parsing subsequences without explicit substrings. These will also
51 * be tested here
52 */
53
54 public class ParsingTest {
55
56 private static JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
57
58 public static void main(String... argv) {
59 check("+100", +100L);
60 check("-100", -100L);
61
62 check("+0", 0L);
63 check("-0", 0L);
64 check("+00000", 0L);
65 check("-00000", 0L);
66
67 check("0", 0L);
68 check("1", 1L);
69 check("9", 9L);
70
71 checkFailure("\u0000");
72 checkFailure("\u002f");
73 checkFailure("+");
74 checkFailure("-");
75 checkFailure("++");
76 checkFailure("+-");
77 checkFailure("-+");
78 checkFailure("--");
79 checkFailure("++100");
80 checkFailure("--100");
81 checkFailure("+-6");
82 checkFailure("-+6");
83 checkFailure("*100");
84
85 check("test-00000", 0L, 4, 10);
86 check("test-12345", -12345L, 4, 10);
87 check("xx12345yy", 12345L, 2, 7);
88 check("xx123456789012345yy", 123456789012345L, 2, 17);
89
90 checkFailure("+00000", 0, 7);
91 checkFailure("-00000", 0, 7);
92 checkFailure("-00000", 6, 6);
93 checkFailure("-00000", 6, 0);
94 checkFailure("+-6", 0, 3);
95 }
96
97 private static void check(String val, long expected) {
98 long n = Long.parseLong(val);
99 if (n != expected)
100 throw new RuntimeException("Long.parseLong failed. String:" +
101 val + " Result:" + n);
102 }
103
104 private static void checkFailure(String val) {
105 long n = 0L;
106 try {
107 n = Long.parseLong(val);
108 System.err.println("parseLong(" + val + ") incorrectly returned " + n);
109 throw new RuntimeException();
110 } catch (NumberFormatException nfe) {
111 ; // Expected
112 }
113 }
114
115 private static void checkFailure(String val, int start, int end) {
116 long n = 0L;
117 try {
118 n = jla.parseLong(val, 10, start, end);
119 System.err.println("parseLong(" + val + ") incorrectly returned " + n);
120 throw new RuntimeException();
121 } catch (NumberFormatException nfe) {
122 ; // Expected
123 }
124 }
125
126 private static void check(String val, long expected, int start, int end) {
127 long n = jla.parseLong(val, 10, start, end);
128 if (n != expected)
129 throw new RuntimeException("Long.parseLong failed. String:" +
130 val + " Result:" + n);
131 }
132 }
|