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