tests/libjava-mauve/src/gnu/testlet/java/util/Scanner/Inputs.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.io.ByteArrayInputStream;
       
    27 import java.io.FileInputStream;
       
    28 import java.io.FileNotFoundException;
       
    29 import java.io.FileOutputStream;
       
    30 import java.io.FileReader;
       
    31 import java.io.IOException;
       
    32 
       
    33 import java.util.Random;
       
    34 import java.util.Scanner;
       
    35 
       
    36 /**
       
    37  * @author E0327023 Hernadi Laszlo
       
    38  * @ 26.02.2007 - 04:15:47
       
    39  *
       
    40  */
       
    41 public class Inputs extends Base
       
    42 {
       
    43   /**
       
    44    * The amount of long numbers to generate and test.
       
    45    */
       
    46   private final static int AMOUNT = 20000;
       
    47 
       
    48   public Inputs ()
       
    49   {
       
    50     fileName = getClass().getName () + ".txt";
       
    51     this.isEnabled = false;
       
    52   }
       
    53 
       
    54   /* (non-Javadoc)
       
    55    * @see gnu.testlet.java.util.Scanner.TestBase#doTest()
       
    56    */
       
    57   @Override protected void doTest ()
       
    58   {
       
    59     long[] numbers = new long[AMOUNT];
       
    60     Random r = new Random (System.currentTimeMillis ());
       
    61     int i;
       
    62     long tmp;
       
    63     int errors;
       
    64     final long max = 20000000000000L, mean = max >> 1;
       
    65     StringBuffer tmpBuff = new StringBuffer (10000);
       
    66     FileOutputStream fos = null;
       
    67     Scanner s = null;
       
    68 
       
    69     String[]charSets = {"windows-1252"};
       
    70     String aktName;
       
    71 
       
    72     int aktCS;
       
    73 
       
    74     for (i = 0; i < numbers.length; i++)
       
    75       {
       
    76 	tmp = ((long) (r.nextFloat () * max) - mean);
       
    77 	numbers[i] = tmp;
       
    78       }
       
    79     tmpBuff.insert (0, "" + numbers[0]);
       
    80 
       
    81     for (i = 1; i < numbers.length; i++)
       
    82       {
       
    83 	tmpBuff.append (" " + numbers[i]);
       
    84       }
       
    85 
       
    86     try
       
    87     {
       
    88       fos = new FileOutputStream (this.aktFile);
       
    89       fos.write (tmpBuff.toString ().getBytes ());
       
    90       fos.flush ();
       
    91       fos.close ();
       
    92 
       
    93 //                      Scanner s = new Scanner (aktFile);
       
    94 
       
    95       myHarness.debug ("Testing Readable input...");
       
    96       errors = 0;
       
    97       s = new Scanner (new FileReader (this.aktFile));
       
    98       i = 0;
       
    99       while (s.hasNextLong ())
       
   100 	{
       
   101 	  tmp = s.nextLong ();
       
   102 	  if (tmp != numbers[i])
       
   103 	    {
       
   104 	      errors++;
       
   105 	      this.myHarness.fail ("Readable : nextLong() -> " + tmp +
       
   106 				   " != " + numbers[i]);
       
   107 	    }
       
   108 	  i++;
       
   109 	}
       
   110       if (errors == 0)
       
   111 	myHarness.debug ("all OK");
       
   112       else
       
   113 	this.myHarness.fail (errors + " errors..");
       
   114 
       
   115       myHarness.debug ("Testing ReadableByteChanel input...");
       
   116       errors = 0;
       
   117       s = new Scanner ((new FileInputStream (this.aktFile)).getChannel ());
       
   118       i = 0;
       
   119       while (s.hasNextLong ())
       
   120 	{
       
   121 	  tmp = s.nextLong ();
       
   122 	  if (tmp != numbers[i])
       
   123 	    {
       
   124 	      errors++;
       
   125 	      this.myHarness.fail ("ReadableByteChanel : nextLong() -> " +
       
   126 				   tmp + " != " + numbers[i]);
       
   127 	    }
       
   128 	  i++;
       
   129 	}
       
   130       if (errors == 0)
       
   131 	myHarness.debug ("all OK");
       
   132       else
       
   133 	this.myHarness.fail (errors + " errors..");
       
   134 
       
   135       for (aktCS = 0; aktCS < charSets.length; aktCS++)
       
   136 	{
       
   137 	  aktName = "Testing File + CharSet \"" + charSets[aktCS] + "\"";
       
   138 	  myHarness.debug (aktName);
       
   139 	  errors = 0;
       
   140 	  s = new Scanner (this.aktFile, charSets[aktCS]);
       
   141 	  i = 0;
       
   142 	  while (s.hasNextLong ())
       
   143 	    {
       
   144 	      tmp = s.nextLong ();
       
   145 	      if (tmp != numbers[i])
       
   146 		{
       
   147 		  errors++;
       
   148 		  this.myHarness.fail (aktName + " : nextLong() -> " + tmp +
       
   149 				       " != " + numbers[i]);
       
   150 		}
       
   151 	      i++;
       
   152 	    }
       
   153 	  if (errors == 0)
       
   154 	    myHarness.debug ("all OK");
       
   155 	  else
       
   156 	    this.myHarness.fail (errors + " errors..");
       
   157 
       
   158 	  aktName =
       
   159 	    "Testing InputStream + CharSet \"" + charSets[aktCS] + "\"";
       
   160 	  myHarness.debug (aktName);
       
   161 	  errors = 0;
       
   162 	  s =
       
   163 	    new Scanner (new
       
   164 			   ByteArrayInputStream (tmpBuff.toString ().
       
   165 						 getBytes ()),
       
   166 			   charSets[aktCS]);
       
   167 	  i = 0;
       
   168 	  while (s.hasNextLong ())
       
   169 	    {
       
   170 	      tmp = s.nextLong ();
       
   171 	      if (tmp != numbers[i])
       
   172 		{
       
   173 		  errors++;
       
   174 		  this.myHarness.fail (aktName + " : nextLong() -> " + tmp +
       
   175 				       " != " + numbers[i]);
       
   176 		}
       
   177 	      i++;
       
   178 	    }
       
   179 	  if (errors == 0)
       
   180 	    myHarness.debug ("all OK");
       
   181 	  else
       
   182 	    this.myHarness.fail (errors + " errors..");
       
   183 
       
   184 	  aktName =
       
   185 	    "Testing ReadableByteChanel + CharSet \"" + charSets[aktCS] +
       
   186 	    "\"";
       
   187 	  myHarness.debug (aktName);
       
   188 	  errors = 0;
       
   189 	  s =
       
   190 	    new Scanner ((new FileInputStream (this.aktFile)).getChannel (),
       
   191 			   charSets[aktCS]);
       
   192 	  i = 0;
       
   193 	  while (s.hasNextLong ())
       
   194 	    {
       
   195 	      tmp = s.nextLong ();
       
   196 	      if (tmp != numbers[i])
       
   197 		{
       
   198 		  errors++;
       
   199 		  this.myHarness.fail (aktName + " : nextLong() -> " + tmp +
       
   200 				       " != " + numbers[i]);
       
   201 		}
       
   202 	      i++;
       
   203 	    }
       
   204 	  if (errors == 0)
       
   205 	    myHarness.debug ("all OK");
       
   206 	  else
       
   207 	    this.myHarness.fail (errors + " errors..");
       
   208 
       
   209 	}
       
   210 
       
   211       this.myHarness.check (i, numbers.length,
       
   212 			    "Incomplete read... (" + i + " / " +
       
   213 			    numbers.length + ")");
       
   214       s.close ();
       
   215     }
       
   216     catch (FileNotFoundException e)
       
   217     {
       
   218       this.myHarness.fail ("Could not create file");
       
   219     }
       
   220     catch (IOException e)
       
   221     {
       
   222       this.myHarness.fail ("Could not write to File \"" +
       
   223 			   this.aktFile.getName () + "\"");
       
   224     }
       
   225   }
       
   226 
       
   227 }