test/java/lang/Integer/ParsingTest.java

Print this page
rev 10062 : 8041972: Add improved parse/format methods for Long/Integer
Contributed-by: redestad


   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
  27  * @summary Test parsing methods
  28  * @author Joseph D. Darcy
  29  */
  30 



  31 
  32 /**
  33  * There are six methods in java.lang.Integer which transform strings
  34  * into an int or Integer value:
  35  *
  36  * public Integer(String s)
  37  * public static Integer decode(String nm)


  38  * public static int parseInt(String s, int radix)
  39  * public static int parseInt(String s)
  40  * public static Integer valueOf(String s, int radix)
  41  * public static Integer valueOf(String s)
  42  *
  43  * Besides decode, all the methods and constructor call down into
  44  * parseInt(String, int) to do the actual work.  Therefore, the
  45  * behavior of parseInt(String, int) will be tested here.

  46  */
  47 
  48 public class ParsingTest {

  49     public static void main(String... argv) {
  50         check("+100", +100);
  51         check("-100", -100);
  52 
  53         check("+0", 0);
  54         check("-0", 0);
  55         check("+00000", 0);
  56         check("-00000", 0);
  57 



  58         check("0", 0);
  59         check("1", 1);
  60         check("9", 9);
  61 
  62         checkFailure("\u0000");
  63         checkFailure("\u002f");
  64         checkFailure("+");
  65         checkFailure("-");
  66         checkFailure("++");
  67         checkFailure("+-");
  68         checkFailure("-+");
  69         checkFailure("--");
  70         checkFailure("++100");
  71         checkFailure("--100");
  72         checkFailure("+-6");
  73         checkFailure("-+6");
  74         checkFailure("*100");

























  75     }
  76 
  77     private static void check(String val, int expected) {
  78         int n = Integer.parseInt(val);
  79         if (n != expected)
  80             throw new RuntimeException("Integer.parsedInt failed. String:" +
  81                                                 val + " Result:" + n);
  82     }
  83 
  84     private static void checkFailure(String val) {
  85         int n = 0;
  86         try {
  87             n = Integer.parseInt(val);
  88             System.err.println("parseInt(" + val + ") incorrectly returned " + n);
  89             throw new RuntimeException();
  90         } catch (NumberFormatException nfe) {
  91             ; // Expected
  92         }
























































  93     }
  94 }


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