tests/libjava-mauve/src/gnu/testlet/java/util/Scanner/BigDecimalInteger.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 // Copyright (c) 2007 Hernadi Laszlo
       
     2 
       
     3 // This file is part of Mauve.
       
     4 
       
     5 // Mauve is free software; you can redistribute it and/or modify
       
     6 // it under the terms of the GNU General Public License as published by
       
     7 // the Free Software Foundation; either version 2, or (at your option)
       
     8 // any later version.
       
     9 
       
    10 // Mauve is distributed in the hope that it will be useful,
       
    11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13 // GNU General Public License for more details.
       
    14 
       
    15 // You should have received a copy of the GNU General Public License
       
    16 // along with Mauve; see the file COPYING.  If not, write to
       
    17 // the Free Software Foundation, 59 Temple Place - Suite 330,
       
    18 // Boston, MA 02111-1307, USA.
       
    19 
       
    20 // Tags: JDK1.5
       
    21 /**
       
    22  * 
       
    23  */
       
    24 package gnu.testlet.java.util.Scanner;
       
    25 
       
    26 import java.math.BigDecimal;
       
    27 import java.math.BigInteger;
       
    28 
       
    29 import java.util.Random;
       
    30 import java.util.Scanner;
       
    31 
       
    32 /**
       
    33  * @author E0327023 Hernadi Laszlo
       
    34  * @ 15.02.2007 - 13:37:48
       
    35  *
       
    36  */
       
    37 public class BigDecimalInteger extends Base
       
    38 {
       
    39 
       
    40   /**
       
    41    * The amount of BigDecimals and BigIntegers to generate and test
       
    42    */
       
    43   private final static int AMOUNT = 5000;
       
    44 
       
    45   public BigDecimalInteger ()
       
    46   {
       
    47     this.isEnabled = false;
       
    48   }
       
    49 
       
    50   /* (non-Javadoc)
       
    51    * @see gnu.testlet.java.util.Scanner.TestBase#doTest()
       
    52    */
       
    53   @Override protected void doTest ()
       
    54   {
       
    55     BigDecimal[]decimals = new BigDecimal[AMOUNT];
       
    56     BigInteger[]integers = new BigInteger[decimals.length];
       
    57     long runID = System.currentTimeMillis ();
       
    58     Random rand = new Random (runID);
       
    59     StringBuilder sBuff = new StringBuilder (1000);
       
    60     String inStr;
       
    61     int i;
       
    62     BigDecimal tmpDec;
       
    63     BigInteger tmpInt;
       
    64     boolean fund;
       
    65     boolean failed;
       
    66     int runsLeft = 10;
       
    67 
       
    68     for (i = 0; i < decimals.length; i++)
       
    69       {
       
    70 	decimals[i] = new BigDecimal (rand.nextDouble () - 0.5);
       
    71 	integers[i] = BigInteger.valueOf (rand.nextInt ());
       
    72       }
       
    73 
       
    74     sBuff.append (decimals[0] + " " + integers[0]);
       
    75     for (i = 1; i < decimals.length; i++)
       
    76       {
       
    77 	sBuff.append (" " + decimals[i] + " " + integers[i]);
       
    78       }
       
    79 
       
    80     inStr = sBuff.toString ();
       
    81     Scanner s = new Scanner (inStr);
       
    82 
       
    83     i = 0;
       
    84     while (s.hasNext () && runsLeft > 0)
       
    85       {
       
    86 	failed = false;
       
    87 	fund = s.hasNextBigDecimal();
       
    88 	myHarness.check(fund, "hasNextBigDecimal()");
       
    89 	tmpDec = s.nextBigDecimal ();
       
    90 	myHarness.check(tmpDec, decimals[i], tmpDec + " == " + decimals[i]);
       
    91 	fund = s.hasNextBigInteger();
       
    92 	myHarness.check(fund, "hasNextBigInteger()");
       
    93 	tmpInt = s.nextBigInteger ();
       
    94 	myHarness.check(tmpInt, integers[i], tmpInt + " == " + integers[i]);
       
    95 	if (!fund)
       
    96 	  {
       
    97 	    this.myHarness.fail ("\"" + s.next () +
       
    98 				 "\" is neither BigDecimal nor BigInteger");
       
    99 	  }
       
   100 	i++;
       
   101 	if (failed)
       
   102 	  {
       
   103 	    runsLeft--;
       
   104 	  }
       
   105       }
       
   106     this.myHarness.check (i, decimals.length,
       
   107 			  "not all read (" + i + " / " + decimals.length +
       
   108 			  ")");
       
   109   }
       
   110 
       
   111 }