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 48 public class ParsingTest { 49 main(String... argv)50 public static void main(String... argv) { 51 check(+100L, "+100"); 52 check(-100L, "-100"); 53 54 check(0L, "+0"); 55 check(0L, "-0"); 56 check(0L, "+00000"); 57 check(0L, "-00000"); 58 59 check(0L, "0"); 60 check(1L, "1"); 61 check(9L, "9"); 62 63 checkFailure(""); 64 checkFailure("\u0000"); 65 checkFailure("\u002f"); 66 checkFailure("+"); 67 checkFailure("-"); 68 checkFailure("++"); 69 checkFailure("+-"); 70 checkFailure("-+"); 71 checkFailure("--"); 72 checkFailure("++100"); 73 checkFailure("--100"); 74 checkFailure("+-6"); 75 checkFailure("-+6"); 76 checkFailure("*100"); 77 78 check(0L, "test-00000", 4, 10, 10); 79 check(-12345L, "test-12345", 4, 10, 10); 80 check(12345L, "xx12345yy", 2, 7, 10); 81 check(123456789012345L, "xx123456789012345yy", 2, 17, 10); 82 check(15L, "xxFyy", 2, 3, 16); 83 84 checkNumberFormatException("", 0, 0, 10); 85 checkNumberFormatException("+-6", 0, 3, 10); 86 checkNumberFormatException("1000000", 7, 7, 10); 87 checkNumberFormatException("1000000", 0, 2, Character.MAX_RADIX + 1); 88 checkNumberFormatException("1000000", 0, 2, Character.MIN_RADIX - 1); 89 90 checkIndexOutOfBoundsException("", 1, 1, 10); 91 checkIndexOutOfBoundsException("1000000", 10, 4, 10); 92 checkIndexOutOfBoundsException("1000000", 10, 2, Character.MAX_RADIX + 1); 93 checkIndexOutOfBoundsException("1000000", 10, 2, Character.MIN_RADIX - 1); 94 checkIndexOutOfBoundsException("1000000", -1, 2, Character.MAX_RADIX + 1); 95 checkIndexOutOfBoundsException("1000000", -1, 2, Character.MIN_RADIX - 1); 96 checkIndexOutOfBoundsException("-1", 0, 3, 10); 97 checkIndexOutOfBoundsException("-1", 2, 3, 10); 98 checkIndexOutOfBoundsException("-1", -1, 2, 10); 99 100 checkNull(0, 1, 10); 101 checkNull(-1, 0, 10); 102 checkNull(0, 0, 10); 103 checkNull(0, -1, 10); 104 checkNull(-1, -1, -1); 105 } 106 check(long expected, String val)107 private static void check(long expected, String val) { 108 long n = Long.parseLong(val); 109 if (n != expected) 110 throw new RuntimeException("Long.parseLong failed. String:" + 111 val + " Result:" + n); 112 } 113 checkFailure(String val)114 private static void checkFailure(String val) { 115 long n = 0L; 116 try { 117 n = Long.parseLong(val); 118 System.err.println("parseLong(" + val + ") incorrectly returned " + n); 119 throw new RuntimeException(); 120 } catch (NumberFormatException nfe) { 121 ; // Expected 122 } 123 } 124 checkNumberFormatException(String val, int start, int end, int radix)125 private static void checkNumberFormatException(String val, int start, int end, int radix) { 126 long n = 0; 127 try { 128 n = Long.parseLong(val, start, end, radix); 129 System.err.println("parseLong(" + val + ", " + start + ", " + end + ", " + radix + 130 ") incorrectly returned " + n); 131 throw new RuntimeException(); 132 } catch (NumberFormatException nfe) { 133 ; // Expected 134 } 135 } 136 checkIndexOutOfBoundsException(String val, int start, int end, int radix)137 private static void checkIndexOutOfBoundsException(String val, int start, int end, int radix) { 138 long n = 0; 139 try { 140 n = Long.parseLong(val, start, end, radix); 141 System.err.println("parseLong(" + val + ", " + start + ", " + end + ", " + radix + 142 ") incorrectly returned " + n); 143 throw new RuntimeException(); 144 } catch (IndexOutOfBoundsException ioob) { 145 ; // Expected 146 } 147 } 148 checkNull(int start, int end, int radix)149 private static void checkNull(int start, int end, int radix) { 150 long n = 0; 151 try { 152 n = Long.parseLong(null, start, end, radix); 153 System.err.println("parseLong(null, " + start + ", " + end + ", " + radix + 154 ") incorrectly returned " + n); 155 throw new RuntimeException(); 156 } catch (NullPointerException npe) { 157 ; // Expected 158 } 159 } 160 check(long expected, String val, int start, int end, int radix)161 private static void check(long expected, String val, int start, int end, int radix) { 162 long n = Long.parseLong(val, start, end, radix); 163 if (n != expected) 164 throw new RuntimeException("Long.parseLong failed. Expexted: " + expected + " String: \"" + 165 val + "\", start: " + start + ", end: " + end + " radix: " + radix + " Result: " + n); 166 } 167 } 168