test/java/lang/Long/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 /**
  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         long n = 0;
 132         try {
 133             n = Long.parseLong(val, radix, start);
 134             System.err.println("parseLong(" + 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("parseLong(" + 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         long n = 0;
 156         try {
 157             n = Long.parseLong(val, radix, start);
 158             System.err.println("parseLong(" + 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("parseLong(" + 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("parseLong(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 }


   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 
  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 
 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 
 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 
 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 
 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 
 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 
 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 }