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