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 27 * @summary Test parsing methods 28 * @author Joseph D. Darcy 29 */ 30 31 /** 32 * There are eight 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(""); 65 checkFailure("\u0000"); 66 checkFailure("\u002f"); 67 checkFailure("+"); 68 checkFailure("-"); 69 checkFailure("++"); 70 checkFailure("+-"); 71 checkFailure("-+"); 72 checkFailure("--"); 73 checkFailure("++100"); 74 checkFailure("--100"); 75 checkFailure("+-6"); 76 checkFailure("-+6"); 77 checkFailure("*100"); 78 79 check("test-00000", 0L, 4, 10); 80 check("test-12345", -12345L, 4, 10); 81 check("xx12345yy", 12345L, 2, 7); 82 check("xx123456789012345yy", 123456789012345L, 2, 17); 83 84 checkNumberFormatException("100", 10, 3); 85 checkNumberFormatException("", 10, 0); 86 checkNumberFormatException("+1000000", 10, 8); 87 checkNumberFormatException("-1000000", 10, 8); 88 89 checkNumberFormatException("", 10, 0, 0); 90 checkNumberFormatException("+-6", 10, 0, 3); 91 checkNumberFormatException("1000000", 10, 7, 7); 92 checkNumberFormatException("1000000", Character.MAX_RADIX + 1, 0, 2); 93 checkNumberFormatException("1000000", Character.MIN_RADIX - 1, 0, 2); 94 95 checkIndexOutOfBoundsException("", 10, 1, 1); 96 checkIndexOutOfBoundsException("1000000", 10, 10, 4); 97 checkIndexOutOfBoundsException("1000000", Character.MAX_RADIX + 1, 10, 2); 98 checkIndexOutOfBoundsException("1000000", Character.MIN_RADIX - 1, 10, 2); 99 checkIndexOutOfBoundsException("1000000", Character.MAX_RADIX + 1, -1, 2); 100 checkIndexOutOfBoundsException("1000000", Character.MIN_RADIX - 1, -1, 2); 101 checkIndexOutOfBoundsException("-1", 10, 0, 3); 102 checkIndexOutOfBoundsException("-1", 10, 2, 3); 103 checkIndexOutOfBoundsException("-1", 10, -1, 2); 104 105 checkNull(10, 0, 1); 106 checkNull(10, -1, 0); 107 checkNull(10, 0, 0); 108 checkNull(10, 0, -1); 109 checkNull(-1, -1, -1); 110 } 111 112 private static void check(String val, long expected) { 113 long n = Long.parseLong(val); 114 if (n != expected) 115 throw new RuntimeException("Long.parseLong failed. String:" + 116 val + " Result:" + n); 117 } 118 119 private static void checkFailure(String val) { 120 long n = 0L; 121 try { 122 n = Long.parseLong(val); 123 System.err.println("parseLong(" + val + ") incorrectly returned " + n); 124 throw new RuntimeException(); 125 } catch (NumberFormatException nfe) { 126 ; // Expected 127 } 128 } 129 130 private static void checkNumberFormatException(String val, int radix, int start) { 131 int n = 0; 132 try { 133 n = Integer.parseInt(val, radix, start); 134 System.err.println("parseInt(" + val + ", " + radix + ", " + start + 135 ") incorrectly returned " + n); 136 throw new RuntimeException(); 137 } catch (NumberFormatException nfe) { 138 ; // Expected 139 } 140 } 141 142 private static void checkNumberFormatException(String val, int radix, int start, int end) { 143 long n = 0; 144 try { 145 n = Long.parseLong(val, radix, start, end); 146 System.err.println("parseInt(" + val + ", " + radix + ", " + start + ", " + end + 147 ") incorrectly returned " + n); 148 throw new RuntimeException(); 149 } catch (NumberFormatException nfe) { 150 ; // Expected 151 } 152 } 153 154 private static void checkIndexOutOfBoundsException(String val, int radix, int start) { 155 int n = 0; 156 try { 157 n = Integer.parseInt(val, radix, start); 158 System.err.println("parseInt(" + val + ", " + radix + ", " + start + 159 ") incorrectly returned " + n); 160 throw new RuntimeException(); 161 } catch (IndexOutOfBoundsException ioob) { 162 ; // Expected 163 } 164 } 165 166 private static void checkIndexOutOfBoundsException(String val, int radix, int start, int end) { 167 long n = 0; 168 try { 169 n = Long.parseLong(val, radix, start, end); 170 System.err.println("parseInt(" + val + ", " + radix + ", " + start + ", " + end + 171 ") incorrectly returned " + n); 172 throw new RuntimeException(); 173 } catch (IndexOutOfBoundsException ioob) { 174 ; // Expected 175 } 176 } 177 178 private static void checkNull(int radix, int start, int end) { 179 long n = 0; 180 try { 181 n = Long.parseLong(null, 10, start, end); 182 System.err.println("parseInt(null, " + radix + ", " + start + ", " + end + 183 ") incorrectly returned " + n); 184 throw new RuntimeException(); 185 } catch (NullPointerException npe) { 186 ; // Expected 187 } 188 } 189 190 private static void check(String val, long expected, int start, int end) { 191 long n = Long.parseLong(val, 10, start, end); 192 if (n != expected) 193 throw new RuntimeException("Long.parseLong failed. String:" + 194 val + ", start: " + start + ", end: " + end + " Result:" + n); 195 } 196 }