tests/libjava-mauve/src/gnu/testlet/java/text/DecimalFormat/parse.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 // Test simple forms of DecimalFormat.parse.
       
     2 
       
     3 // Copyright (c) 1999  Cygnus Solutions
       
     4 // Written by Tom Tromey <tromey@cygnus.com>
       
     5 
       
     6 // This file is part of Mauve.
       
     7 
       
     8 // Mauve is free software; you can redistribute it and/or modify
       
     9 // it under the terms of the GNU General Public License as published by
       
    10 // the Free Software Foundation; either version 2, or (at your option)
       
    11 // any later version.
       
    12 
       
    13 // Mauve is distributed in the hope that it will be useful,
       
    14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    16 // GNU General Public License for more details.
       
    17 
       
    18 // You should have received a copy of the GNU General Public License
       
    19 // along with Mauve; see the file COPYING.  If not, write to
       
    20 // the Free Software Foundation, 59 Temple Place - Suite 330,
       
    21 // Boston, MA 02111-1307, USA.
       
    22 
       
    23 // Tags: JDK1.1
       
    24 
       
    25 package gnu.testlet.java.text.DecimalFormat;
       
    26 
       
    27 import gnu.testlet.Testlet;
       
    28 import gnu.testlet.TestHarness;
       
    29 
       
    30 import java.text.DecimalFormat;
       
    31 import java.text.ParsePosition;
       
    32 import java.util.Locale;
       
    33 
       
    34 public class parse implements Testlet
       
    35 {
       
    36   public void apply (TestHarness harness, DecimalFormat df, String pattern)
       
    37     {
       
    38       harness.checkPoint("pattern " + pattern);
       
    39       boolean ok = true;
       
    40       try
       
    41 	{
       
    42 	  df.applyPattern(pattern);
       
    43 	}
       
    44       catch (IllegalArgumentException x)
       
    45 	{
       
    46 	  ok = false;
       
    47 	}
       
    48       harness.check (ok);
       
    49     }
       
    50 
       
    51   public Number parseIt (DecimalFormat df, String string, ParsePosition pos)
       
    52     {
       
    53       pos.setIndex (0);
       
    54       return df.parse (string, pos);
       
    55     }
       
    56 
       
    57   public void test (TestHarness harness)
       
    58     {
       
    59       // Just to be explicit: we're only testing the US locale here.
       
    60       Locale loc = Locale.US;
       
    61       Locale.setDefault (loc);
       
    62 
       
    63       Number num;
       
    64       ParsePosition pp = new ParsePosition (0);
       
    65 
       
    66       // Some tests taken from JCL book.
       
    67       DecimalFormat df = new DecimalFormat ("0.##;-0.##");
       
    68       num = parseIt (df, "-1234.56", pp);
       
    69       harness.check (num instanceof Double);
       
    70       harness.check (num.doubleValue(), -1234.56);
       
    71 
       
    72       num = parseIt (df, "-0", pp);
       
    73       harness.check (num instanceof Double);
       
    74       harness.check (num.doubleValue(), -0.0);
       
    75       
       
    76       num = parseIt (df, "-0.0", pp);
       
    77       harness.check (num instanceof Double);
       
    78       harness.check (num.doubleValue(), -0.0);
       
    79       
       
    80       apply (harness, df, "0.#");
       
    81       num = parseIt (df, "1234.6", pp);
       
    82       harness.check (num instanceof Double);
       
    83       harness.check (num.doubleValue(), 1234.6);
       
    84 
       
    85       apply (harness, df, "0");
       
    86       num = parseIt (df, "-1235", pp);
       
    87       harness.check (num instanceof Long);
       
    88       harness.check (num.longValue (), -1235);
       
    89 
       
    90       num = parseIt (df, Long.toString (Long.MIN_VALUE), pp);
       
    91       harness.check (num instanceof Long);
       
    92       harness.check (num.longValue(), Long.MIN_VALUE);
       
    93 
       
    94       apply (harness, df, "'#'#.#");
       
    95       num = parseIt (df, "#30", pp);
       
    96       harness.check (num instanceof Long);
       
    97       harness.check (num.longValue (), 30);
       
    98 
       
    99       num = parseIt (df, "xx30", pp);
       
   100       harness.check (num, null);
       
   101 
       
   102       apply (harness, df, "0.0000E0");
       
   103       num = parseIt (df, "2.000E5", pp);
       
   104       harness.check (num instanceof Long);
       
   105       harness.check (num.longValue (), 200000);
       
   106 
       
   107       num = parseIt (df, "2.0000E-5", pp);
       
   108       harness.check (num instanceof Double);
       
   109       harness.check (num.doubleValue(), 2.0E-5);
       
   110       
       
   111       // this one is tricky... -E5 is considered part of the suffix
       
   112       num = parseIt (df, "2.000-E5", pp);
       
   113       harness.check (num instanceof Long);
       
   114       harness.check (num.doubleValue(), 2);
       
   115       
       
   116       apply (harness, df, "0.000");
       
   117       num = parseIt (df, "2.000", pp);
       
   118       harness.check (num instanceof Long);
       
   119       harness.check (num.longValue (), 2);
       
   120 
       
   121       apply (harness, df, "###0.#;(###0.#)");
       
   122       num = parseIt (df, "201.2", pp);
       
   123       harness.check (num instanceof Double);
       
   124       harness.check (num.doubleValue(), 201.2);
       
   125       num = parseIt (df, "(201.2)", pp);
       
   126       harness.check (num instanceof Double);
       
   127       harness.check (num.doubleValue(), -201.2);
       
   128 
       
   129       apply (harness, df, "0.#;0.#-");
       
   130       num = parseIt (df, "303", pp);
       
   131       harness.check (num instanceof Long);
       
   132       harness.check (num.longValue (), 303);
       
   133 
       
   134       num = parseIt (df, "303-", pp);
       
   135       harness.check (num instanceof Long);
       
   136       harness.check (num.longValue (), -303);
       
   137 
       
   138       num = parseIt (df, "1.", pp);
       
   139       harness.check (num instanceof Long);
       
   140       harness.check (num.longValue (), 1);
       
   141       
       
   142       num = parseIt (df, "1.0", pp);
       
   143       harness.check (num instanceof Long);
       
   144       harness.check (num.longValue (), 1);
       
   145       
       
   146       num = parseIt (df, ".01", pp);
       
   147       harness.check (num instanceof Double);
       
   148       harness.check (num.longValue (), 0);
       
   149       
       
   150       num = parseIt (df, "9223372036854775808-", pp);
       
   151       harness.check (num instanceof Long);
       
   152       harness.check (num.longValue(), Long.MIN_VALUE);
       
   153 
       
   154       apply (harness, df, "0.###;0.###-");
       
   155       num = parseIt (df, ".01", pp);
       
   156       harness.check (num instanceof Double);
       
   157       harness.check (num.doubleValue(), 0.01);
       
   158       
       
   159       num = parseIt (df, ".05", pp);
       
   160       harness.check (num instanceof Double);
       
   161       harness.check (num.doubleValue(), 0.05);
       
   162       
       
   163       num = parseIt (df, ".5", pp);
       
   164       harness.check (num instanceof Double);
       
   165       harness.check (num.doubleValue(), 0.5);
       
   166       
       
   167       apply (harness, df, "#,##0.00");
       
   168       num = parseIt (df, "3,110.00", pp);
       
   169       harness.check (num instanceof Long);
       
   170       harness.check (num.longValue(), 3110);
       
   171 
       
   172       apply (harness, df, "#,##0.00");
       
   173       num = parseIt (df, "31,10.00", pp);
       
   174       harness.check (num instanceof Long);
       
   175       harness.check (num.longValue(), 3110);
       
   176       
       
   177       apply (harness, df, "#,##0.00");
       
   178       num = parseIt (df, "3110", pp);
       
   179       harness.check (num instanceof Long);
       
   180       harness.check (num.longValue(), 3110);
       
   181       
       
   182       apply (harness, df, "#,##0X");
       
   183       num = parseIt (df, "3,110X", pp);
       
   184       harness.check (num instanceof Long);
       
   185       harness.check (num.longValue(), 3110);
       
   186 
       
   187       apply (harness, df, "#,##0X");
       
   188       num = parseIt (df, "3,110", pp);
       
   189       harness.check (num == null);
       
   190       harness.check (pp.getErrorIndex() == 5);
       
   191 
       
   192       apply (harness, df, "#,##0X");
       
   193       num = parseIt (df, "3,110Y", pp);
       
   194       harness.check (num == null);
       
   195       harness.check (pp.getErrorIndex(), 5);
       
   196     }
       
   197 }