test/java/lang/Integer/ParsingTest.java

Print this page
rev 10594 : 8055251: Re-examine Integer.parseInt and Long.parseLong methods


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


   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 import java.lang.IndexOutOfBoundsException;
  32 import java.lang.NullPointerException;
  33 import java.lang.RuntimeException;
  34 
  35 /**
  36  * There are seven methods in java.lang.Integer which transform strings
  37  * into an int or Integer value:
  38  *
  39  * public Integer(String s)
  40  * public static Integer decode(String nm)
  41  * public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix)

  42  * public static int parseInt(String s, int radix)
  43  * public static int parseInt(String s)
  44  * public static Integer valueOf(String s, int radix)
  45  * public static Integer valueOf(String s)
  46  *
  47  * Besides decode, all the methods and constructor call down into
  48  * parseInt(CharSequence, int, int, int) to do the actual work.  Therefore, the
  49  * behavior of parseInt(CharSequence, int, int, int) will be tested here.
  50  *
  51  */
  52 
  53 public class ParsingTest {
  54 
  55     public static void main(String... argv) {
  56         check(+100, "+100");
  57         check(-100, "-100");
  58 
  59         check(0, "+0");
  60         check(0, "-0");
  61         check(0, "+00000");
  62         check(0, "-00000");
  63 
  64         check(0, "0");
  65         check(1, "1");
  66         check(9, "9");



  67 
  68         checkFailure("");
  69         checkFailure("\u0000");
  70         checkFailure("\u002f");
  71         checkFailure("+");
  72         checkFailure("-");
  73         checkFailure("++");
  74         checkFailure("+-");
  75         checkFailure("-+");
  76         checkFailure("--");
  77         checkFailure("++100");
  78         checkFailure("--100");
  79         checkFailure("+-6");
  80         checkFailure("-+6");
  81         checkFailure("*100");
  82 
  83         // check offset based methods
  84         check(0, "+00000", 0, 6, 10);
  85         check(0, "-00000", 0, 6, 10);
  86         check(0, "test-00000", 4, 10, 10);
  87         check(-12345, "test-12345", 4, 10, 10);
  88         check(12345, "xx12345yy", 2, 7, 10);
  89         check(15, "xxFyy", 2, 3, 16);
  90 
  91         checkNumberFormatException("", 0, 0, 10);
  92         checkNumberFormatException("+-6", 0, 3, 10);
  93         checkNumberFormatException("1000000", 7, 7, 10);
  94         checkNumberFormatException("1000000", 0, 2, Character.MAX_RADIX + 1);
  95         checkNumberFormatException("1000000", 0, 2, Character.MIN_RADIX - 1);
  96 
  97         checkIndexOutOfBoundsException("1000000", 10, 4, 10);
  98         checkIndexOutOfBoundsException("1000000", -1, 2, Character.MAX_RADIX + 1);
  99         checkIndexOutOfBoundsException("1000000", -1, 2, Character.MIN_RADIX - 1);
 100         checkIndexOutOfBoundsException("1000000", 10, 2, Character.MAX_RADIX + 1);
 101         checkIndexOutOfBoundsException("1000000", 10, 2, Character.MIN_RADIX - 1);
 102         checkIndexOutOfBoundsException("-1", 0, 3, 10);
 103         checkIndexOutOfBoundsException("-1", 2, 3, 10);
 104         checkIndexOutOfBoundsException("-1", -1, 2, 10);
 105 
 106         checkNull(0, 1, 10);
 107         checkNull(-1, 0, 10);
 108         checkNull(0, 0, 10);
 109         checkNull(0, -1, 10);




 110         checkNull(-1, -1, -1);
 111     }
 112 
 113     private static void check(int expected, String val) {
 114         int n = Integer.parseInt(val);
 115         if (n != expected)
 116             throw new RuntimeException("Integer.parseInt failed. String:" +
 117                                                 val + " Result:" + n);
 118     }
 119 
 120     private static void checkFailure(String val) {
 121         int n = 0;
 122         try {
 123             n = Integer.parseInt(val);
 124             System.err.println("parseInt(" + val + ") incorrectly returned " + n);
 125             throw new RuntimeException();
 126         } catch (NumberFormatException nfe) {
 127             ; // Expected
 128         }
 129     }
 130 
 131     private static void checkNumberFormatException(String val, int start, int end, int radix) {
 132         int n = 0;
 133         try {
 134             n = Integer.parseInt(val, start, end, radix);
 135             System.err.println("parseInt(" + val + ", " + start + ", " + end + ", " + radix +
 136                     ") incorrectly returned " + n);
 137             throw new RuntimeException();
 138         } catch (NumberFormatException nfe) {
 139             ; // Expected
 140         }
 141     }
 142 
 143     private static void checkIndexOutOfBoundsException(String val, int start, int end, int radix) {
 144         int n = 0;
 145         try {
 146             n = Integer.parseInt(val, start, end, radix);
 147             System.err.println("parseInt(" + val + ", " + start + ", " + end + ", " + radix  +
























 148                     ") incorrectly returned " + n);
 149             throw new RuntimeException();
 150         } catch (IndexOutOfBoundsException ioob) {
 151             ; // Expected
 152         }
 153     }
 154 
 155     private static void checkNull(int start, int end, int radix) {
 156         int n = 0;
 157         try {
 158             n = Integer.parseInt(null, start, end, radix);
 159             System.err.println("parseInt(null, " + start + ", " + end + ", " + radix +
 160                     ") incorrectly returned " + n);
 161             throw new RuntimeException();
 162         } catch (NullPointerException npe) {
 163             ; // Expected
 164         }
 165     }
 166 
 167     private static void check(int expected, String val, int start, int end, int radix) {
 168         int n = Integer.parseInt(val, start, end, radix);
 169         if (n != expected)
 170             throw new RuntimeException("Integer.parsedInt failed. Expected: " + expected + " String: \"" +
 171                     val + "\", start: " + start + ", end: " + end + ", radix: " + radix + " Result:" + n);
 172     }
 173 }