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 
31 /**
32  * There are seven 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(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(CharSequence, int, int, int) to do the actual work.  Therefore, the
45  * behavior of parseLong(CharSequence, int, int, int) will be tested here.
46  */
47 package test.java.lang.Long;
48 
49 // Android-added: support for wrapper to avoid d8 backporting of Integer.parseInt (b/215435867).
50 import java.lang.invoke.MethodHandle;
51 import java.lang.invoke.MethodHandles;
52 import java.lang.invoke.MethodType;
53 
54 import org.testng.annotations.Test;
55 
56 public class ParsingTest {
57 
58     @Test
testParsing()59     public void testParsing() {
60         check(+100L, "+100");
61         check(-100L, "-100");
62 
63         check(0L, "+0");
64         check(0L, "-0");
65         check(0L, "+00000");
66         check(0L, "-00000");
67 
68         check(0L, "0");
69         check(1L, "1");
70         check(9L, "9");
71 
72         checkFailure("");
73         checkFailure("\u0000");
74         checkFailure("\u002f");
75         checkFailure("+");
76         checkFailure("-");
77         checkFailure("++");
78         checkFailure("+-");
79         checkFailure("-+");
80         checkFailure("--");
81         checkFailure("++100");
82         checkFailure("--100");
83         checkFailure("+-6");
84         checkFailure("-+6");
85         checkFailure("*100");
86 
87         check(0L, "test-00000", 4, 10, 10);
88         check(-12345L, "test-12345", 4, 10, 10);
89         check(12345L, "xx12345yy", 2, 7, 10);
90         check(123456789012345L, "xx123456789012345yy", 2, 17, 10);
91         check(15L, "xxFyy", 2, 3, 16);
92 
93         checkNumberFormatException("", 0, 0, 10);
94         checkNumberFormatException("+-6", 0, 3, 10);
95         checkNumberFormatException("1000000", 7, 7, 10);
96         checkNumberFormatException("1000000", 0, 2, Character.MAX_RADIX + 1);
97         checkNumberFormatException("1000000", 0, 2, Character.MIN_RADIX - 1);
98 
99         checkIndexOutOfBoundsException("", 1, 1, 10);
100         checkIndexOutOfBoundsException("1000000", 10, 4, 10);
101         checkIndexOutOfBoundsException("1000000", 10, 2, Character.MAX_RADIX + 1);
102         checkIndexOutOfBoundsException("1000000", 10, 2, Character.MIN_RADIX - 1);
103         checkIndexOutOfBoundsException("1000000", -1, 2, Character.MAX_RADIX + 1);
104         checkIndexOutOfBoundsException("1000000", -1, 2, Character.MIN_RADIX - 1);
105         checkIndexOutOfBoundsException("-1", 0, 3, 10);
106         checkIndexOutOfBoundsException("-1", 2, 3, 10);
107         checkIndexOutOfBoundsException("-1", -1, 2, 10);
108 
109         checkNull(0, 1, 10);
110         checkNull(-1, 0, 10);
111         checkNull(0, 0, 10);
112         checkNull(0, -1, 10);
113         checkNull(-1, -1, -1);
114     }
115 
check(long expected, String val)116     private static void check(long expected, String val) {
117         long n = Long.parseLong(val);
118         if (n != expected)
119             throw new RuntimeException("Long.parseLong failed. String:" +
120                                        val + " Result:" + n);
121     }
122 
checkFailure(String val)123     private static void checkFailure(String val) {
124         long n = 0L;
125         try {
126             n = Long.parseLong(val);
127             System.err.println("parseLong(" + val + ") incorrectly returned " + n);
128             throw new RuntimeException();
129         } catch (NumberFormatException nfe) {
130             ; // Expected
131         }
132     }
133 
checkNumberFormatException(String val, int start, int end, int radix)134     private static void checkNumberFormatException(String val, int start, int end, int radix) {
135         long n = 0;
136         try {
137             // Android-changed: call wrapper to avoid d8 backporting Long.parseLong (b/215435867).
138             // n = Long.parseLong(val, start, end, radix);
139             n = Long_parseLong(val, start, end, radix);
140             System.err.println("parseLong(" + val + ", " + start + ", " + end + ", " + radix +
141                     ") incorrectly returned " + n);
142             throw new RuntimeException();
143         } catch (NumberFormatException nfe) {
144             ; // Expected
145         }
146     }
147 
checkIndexOutOfBoundsException(String val, int start, int end, int radix)148     private static void checkIndexOutOfBoundsException(String val, int start, int end, int radix) {
149         long n = 0;
150         try {
151             // Android-changed: call wrapper to avoid d8 backporting Long.parseLong (b/215435867).
152             // n = Long.parseLong(val, start, end, radix);
153             n = Long_parseLong(val, start, end, radix);
154             System.err.println("parseLong(" + val + ", " + start + ", " + end + ", " + radix +
155                     ") incorrectly returned " + n);
156             throw new RuntimeException();
157         } catch (IndexOutOfBoundsException ioob) {
158             ; // Expected
159         }
160     }
161 
checkNull(int start, int end, int radix)162     private static void checkNull(int start, int end, int radix) {
163         long n = 0;
164         try {
165             // Android-changed: call wrapper to avoid d8 backporting Long.parseLong (b/215435867).
166             // n = Long.parseLong(null, start, end, radix);
167             n = Long_parseLong(null, start, end, radix);
168             System.err.println("parseLong(null, " + start + ", " + end + ", " + radix +
169                     ") incorrectly returned " + n);
170             throw new RuntimeException();
171         } catch (NullPointerException npe) {
172             ; // Expected
173         }
174     }
175 
check(long expected, String val, int start, int end, int radix)176     private static void check(long expected, String val, int start, int end, int radix) {
177         // Android-changed: call wrapper to avoid d8 backporting Long.parseLong (b/215435867).
178         // long n = Long.parseLong(val, start, end, radix);
179         long n = Long_parseLong(val, start, end, radix);
180         if (n != expected)
181             throw new RuntimeException("Long.parseLong failed. Expexted: " + expected + " String: \"" +
182                     val + "\", start: " + start + ", end: " + end + " radix: " + radix + " Result: " + n);
183     }
184 
185     // Android-added: wrapper to avoid d8 backporting of Long.parseLong (b/215435867).
Long_parseLong(String val, int start, int end, int radix)186     private static long Long_parseLong(String val, int start, int end, int radix) {
187         try {
188             MethodType parseType = MethodType.methodType(long.class,
189                                                          CharSequence.class,
190                                                          int.class,
191                                                          int.class,
192                                                          int.class);
193             MethodHandle parse =
194                     MethodHandles.lookup().findStatic(Long.class, "parseLong", parseType);
195             return (long) parse.invokeExact((CharSequence) val, start, end, radix);
196         } catch (IndexOutOfBoundsException | NullPointerException | NumberFormatException e) {
197             // Expected exceptions from the target method during the tests here.
198             throw e;
199         } catch (Throwable t) {
200             // Everything else.
201             throw new RuntimeException(t);
202         }
203     }
204 }
205