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