1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
// Copyright (c) 2007 Hernadi Laszlo
// This file is part of Mauve.
// Mauve is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
// Mauve is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Mauve; see the file COPYING. If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
// Tags: JDK1.5
/**
*
*/
package gnu.testlet.java.util.Scanner;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
/**
* @author E0327023 Hernadi Laszlo
* @ 26.02.2007 - 04:15:47
*
*/
public class Inputs extends Base
{
/**
* The amount of long numbers to generate and test.
*/
private final static int AMOUNT = 20000;
public Inputs ()
{
fileName = getClass().getName () + ".txt";
this.isEnabled = false;
}
/* (non-Javadoc)
* @see gnu.testlet.java.util.Scanner.TestBase#doTest()
*/
@Override protected void doTest ()
{
long[] numbers = new long[AMOUNT];
Random r = new Random (System.currentTimeMillis ());
int i;
long tmp;
int errors;
final long max = 20000000000000L, mean = max >> 1;
StringBuffer tmpBuff = new StringBuffer (10000);
FileOutputStream fos = null;
Scanner s = null;
String[]charSets = {"windows-1252"};
String aktName;
int aktCS;
for (i = 0; i < numbers.length; i++)
{
tmp = ((long) (r.nextFloat () * max) - mean);
numbers[i] = tmp;
}
tmpBuff.insert (0, "" + numbers[0]);
for (i = 1; i < numbers.length; i++)
{
tmpBuff.append (" " + numbers[i]);
}
try
{
fos = new FileOutputStream (this.aktFile);
fos.write (tmpBuff.toString ().getBytes ());
fos.flush ();
fos.close ();
// Scanner s = new Scanner (aktFile);
myHarness.debug ("Testing Readable input...");
errors = 0;
s = new Scanner (new FileReader (this.aktFile));
i = 0;
while (s.hasNextLong ())
{
tmp = s.nextLong ();
if (tmp != numbers[i])
{
errors++;
this.myHarness.fail ("Readable : nextLong() -> " + tmp +
" != " + numbers[i]);
}
i++;
}
if (errors == 0)
myHarness.debug ("all OK");
else
this.myHarness.fail (errors + " errors..");
myHarness.debug ("Testing ReadableByteChanel input...");
errors = 0;
s = new Scanner ((new FileInputStream (this.aktFile)).getChannel ());
i = 0;
while (s.hasNextLong ())
{
tmp = s.nextLong ();
if (tmp != numbers[i])
{
errors++;
this.myHarness.fail ("ReadableByteChanel : nextLong() -> " +
tmp + " != " + numbers[i]);
}
i++;
}
if (errors == 0)
myHarness.debug ("all OK");
else
this.myHarness.fail (errors + " errors..");
for (aktCS = 0; aktCS < charSets.length; aktCS++)
{
aktName = "Testing File + CharSet \"" + charSets[aktCS] + "\"";
myHarness.debug (aktName);
errors = 0;
s = new Scanner (this.aktFile, charSets[aktCS]);
i = 0;
while (s.hasNextLong ())
{
tmp = s.nextLong ();
if (tmp != numbers[i])
{
errors++;
this.myHarness.fail (aktName + " : nextLong() -> " + tmp +
" != " + numbers[i]);
}
i++;
}
if (errors == 0)
myHarness.debug ("all OK");
else
this.myHarness.fail (errors + " errors..");
aktName =
"Testing InputStream + CharSet \"" + charSets[aktCS] + "\"";
myHarness.debug (aktName);
errors = 0;
s =
new Scanner (new
ByteArrayInputStream (tmpBuff.toString ().
getBytes ()),
charSets[aktCS]);
i = 0;
while (s.hasNextLong ())
{
tmp = s.nextLong ();
if (tmp != numbers[i])
{
errors++;
this.myHarness.fail (aktName + " : nextLong() -> " + tmp +
" != " + numbers[i]);
}
i++;
}
if (errors == 0)
myHarness.debug ("all OK");
else
this.myHarness.fail (errors + " errors..");
aktName =
"Testing ReadableByteChanel + CharSet \"" + charSets[aktCS] +
"\"";
myHarness.debug (aktName);
errors = 0;
s =
new Scanner ((new FileInputStream (this.aktFile)).getChannel (),
charSets[aktCS]);
i = 0;
while (s.hasNextLong ())
{
tmp = s.nextLong ();
if (tmp != numbers[i])
{
errors++;
this.myHarness.fail (aktName + " : nextLong() -> " + tmp +
" != " + numbers[i]);
}
i++;
}
if (errors == 0)
myHarness.debug ("all OK");
else
this.myHarness.fail (errors + " errors..");
}
this.myHarness.check (i, numbers.length,
"Incomplete read... (" + i + " / " +
numbers.length + ")");
s.close ();
}
catch (FileNotFoundException e)
{
this.myHarness.fail ("Could not create file");
}
catch (IOException e)
{
this.myHarness.fail ("Could not write to File \"" +
this.aktFile.getName () + "\"");
}
}
}
|