PageRenderTime 62ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/java-1.7.0-openjdk/mauve-2008-10-22/gnu/testlet/java/util/Scanner/Inputs.java

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