tests/libjava-mauve/src/gnu/testlet/java/lang/Integer/parseInt.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 /* Copyright (C) 2002 Free Software Foundation, Inc.
       
     2  * Written by Mark Wielaard <mark@klomp.org>
       
     3  *
       
     4  * This file is part of Mauve.
       
     5  *
       
     6  * Mauve is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License as published by
       
     8  * the Free Software Foundation; either version 2, or (at your option)
       
     9  * any later version.
       
    10  *
       
    11  * Mauve is distributed in the hope that it will be useful,
       
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14  * GNU General Public License for more details.
       
    15  *
       
    16  * You should have received a copy of the GNU General Public License
       
    17  * along with Mauve; see the file COPYING.  If not, write to
       
    18  * the Free Software Foundation, 59 Temple Place - Suite 330,
       
    19  * Boston, MA 02111-1307, USA.  */
       
    20 
       
    21 // Tags: JDK1.1
       
    22 
       
    23 package gnu.testlet.java.lang.Integer;
       
    24 
       
    25 import gnu.testlet.Testlet;
       
    26 import gnu.testlet.TestHarness;
       
    27 
       
    28 public class parseInt implements Testlet
       
    29 {
       
    30   public void test(TestHarness harness)
       
    31   {
       
    32     int i;
       
    33 
       
    34     i = Integer.parseInt("0");
       
    35     harness.check(i, 0);
       
    36 
       
    37     i = Integer.parseInt("1");
       
    38     harness.check(i, 1);
       
    39 
       
    40     i = Integer.parseInt("000");
       
    41     harness.check(i, 0);
       
    42 
       
    43     i = Integer.parseInt("007");
       
    44     harness.check(i, 7);
       
    45 
       
    46     i = Integer.parseInt("-0");
       
    47     harness.check(i, 0);
       
    48 
       
    49     i = Integer.parseInt("-1");
       
    50     harness.check(i, -1);
       
    51 
       
    52     i = Integer.parseInt("-2147483648");
       
    53     harness.check(i, Integer.MIN_VALUE);
       
    54 
       
    55     i = Integer.parseInt("2147483647");
       
    56     harness.check(i, Integer.MAX_VALUE);
       
    57 
       
    58     try
       
    59       {
       
    60 	i = Integer.parseInt("-2147483649");
       
    61 	harness.fail("-2147483649 is to small for an int");
       
    62       }
       
    63     catch (NumberFormatException nfe)
       
    64       {
       
    65 	harness.check(true);
       
    66       }
       
    67 
       
    68     try
       
    69       {
       
    70 	i = Integer.parseInt("2147483648");
       
    71 	harness.fail("2147483648 is to big for an int");
       
    72       }
       
    73     catch (NumberFormatException nfe)
       
    74       {
       
    75 	harness.check(true);
       
    76       }
       
    77 
       
    78     try
       
    79       {
       
    80         i = Integer.parseInt("abc");
       
    81 	harness.fail("Illegal input (abc) must throw NumberFormatException");
       
    82       }
       
    83     catch (NumberFormatException nfe)
       
    84       {
       
    85 	harness.check(true);
       
    86       }
       
    87 
       
    88     try
       
    89       {
       
    90         i = Integer.parseInt("-");
       
    91 	harness.fail("Single '-' must throw NumberFormatException");
       
    92       }
       
    93     catch (NumberFormatException nfe)
       
    94       {
       
    95 	harness.check(true);
       
    96       }
       
    97 
       
    98     try
       
    99     {
       
   100       i = Integer.parseInt("+");
       
   101 	harness.fail("Single '+' must throw NumberFormatException");
       
   102     }
       
   103   catch (NumberFormatException nfe)
       
   104     {
       
   105 	harness.check(true);
       
   106     }
       
   107   
       
   108     // In JDK1.7, '+' is considered a valid character.
       
   109     try
       
   110       {
       
   111         i = Integer.parseInt("+10");
       
   112         harness.check(true);
       
   113 	harness.check(i, 10);
       
   114       }
       
   115     catch (NumberFormatException nfe)
       
   116       {
       
   117     	harness.fail("Leading '+' does not throw NumberFormatException");
       
   118       }
       
   119 
       
   120     try
       
   121       {
       
   122         i = Integer.parseInt(null);
       
   123 	harness.fail("null input must throw NumberFormatException");
       
   124       }
       
   125     catch (NullPointerException npe)
       
   126       {
       
   127 	harness.fail("null input must throw NumberFormatException, not NullPointerException");
       
   128       }
       
   129     catch (NumberFormatException nfe)
       
   130       {
       
   131 	harness.check(true);
       
   132       }
       
   133     
       
   134     try
       
   135       {
       
   136         i = Integer.parseInt("");
       
   137 	harness.fail("empty input must throw NumberFormatException");
       
   138       }
       
   139     catch (IndexOutOfBoundsException ioobe)
       
   140       {
       
   141 	harness.fail("empty input must throw NumberFormatException, not IndexOutOfBoundsException");
       
   142       }
       
   143     catch (NumberFormatException nfe)
       
   144       {
       
   145 	harness.check(true);
       
   146       }
       
   147   }
       
   148 }