Advertisement
Guest User

Testing framework

a guest
Oct 18th, 2013
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.09 KB | None | 0 0
  1. /**
  2. * @author egor@egork.net
  3. */
  4. import java.io.InputStream
  5. import java.util.InputMismatchException
  6. import java.io.IOException
  7. import java.math.BigInteger
  8. import java.io.PrintWriter
  9. import java.io.Writer
  10. import java.io.BufferedWriter
  11. import java.io.OutputStreamWriter
  12. import java.io.OutputStream
  13. import java.io.FileInputStream
  14. import java.io.FileOutputStream
  15. import java.io.StringBufferInputStream
  16. import java.io.StringWriter
  17.  
  18. val inputFile : String? = null
  19. val outputFile : String? = null
  20. val inputType : Int = 0
  21. val tests = array(
  22. "2 3", "5",
  23. "17 -18", "-1"
  24. )
  25.  
  26. fun solve(inp: InputReader, out: OutputWriter, testNumber: Int) {
  27. out.printLine(inp.readInt() + inp.readInt())
  28. }
  29.  
  30. fun check(input: String, answer: String, output: String): String {
  31. var expected = InputReader(StringBufferInputStream(answer))
  32. var actual = InputReader(StringBufferInputStream(output))
  33.  
  34. while (true) {
  35. if (expected.isExhausted()) {
  36. if (actual.isExhausted())
  37. return "OK"
  38. return "PE"
  39. }
  40. if (actual.isExhausted())
  41. return "PE"
  42. val expectedToken = expected.readString()
  43. val actualToken = actual.readString()
  44. if (!expectedToken.equals(actualToken))
  45. return "WA, " + expectedToken + " != " + actualToken
  46. }
  47. }
  48.  
  49. fun main(args: Array<String>) {
  50. if (args.size != 0 && "Test".equals(args[0])) {
  51. test()
  52. } else {
  53. var inp: InputReader
  54. if (inputFile == null)
  55. inp = InputReader(System.`in`)
  56. else
  57. inp = InputReader(FileInputStream(inputFile))
  58. var out: OutputWriter
  59. if (outputFile == null)
  60. out = OutputWriter(PrintWriter(System.out))
  61. else
  62. out = OutputWriter.init(FileOutputStream(outputFile))
  63. run(inp, out)
  64. }
  65. }
  66.  
  67. fun test() {
  68. var i = 0
  69. var maxTime: Long = 0
  70. var ok = -1
  71. while (i < tests.size) {
  72. var input = tests[i]
  73. var answer = tests[i + 1]
  74. i += 2
  75. var testNumber = i / 2
  76. var inp = InputReader(StringBufferInputStream(input))
  77. var out = OutputWriter(PrintWriter(StringWriter()))
  78. println("Test #" + testNumber)
  79. println("Input:")
  80. println(input)
  81. println()
  82. println("Answer:")
  83. println(answer)
  84. println()
  85. var time = System.currentTimeMillis()
  86. var verdict: String
  87. try {
  88. run(inp, out)
  89. time = System.currentTimeMillis() - time
  90. var output = out.toString()
  91. println(output)
  92. verdict = check(input, answer, output)
  93. } catch (e: Throwable) {
  94. verdict = "Runtime Error (" + e.javaClass.getSimpleName() + ")"
  95. println(e.getMessage())
  96. e.printStackTrace()
  97. }
  98. println("Verdict = " + verdict)
  99. if (!verdict.equals("OK"))
  100. ok = testNumber
  101. println(java.lang.String.format("Time = %.3f\n", time / 1000.0))
  102. maxTime = Math.max(maxTime, time)
  103. println("----------------------------------------------------------")
  104. }
  105. if (ok == -1)
  106. println("All test passed")
  107. else
  108. println("First failed test = " + ok)
  109. println(java.lang.String.format("Maximal time = %.3f\n", maxTime / 1000.0))
  110. }
  111.  
  112. fun run(inp: InputReader, out: OutputWriter) {
  113. if (inputType == 0)
  114. solve(inp, out, 1)
  115. else if (inputType == 1) {
  116. var testCount = inp.readInt()
  117. for (i in 1..testCount) {
  118. solve(inp, out, i)
  119. }
  120. } else if (inputType == 2) {
  121. try {
  122. var test = 1
  123. while (true) {
  124. solve(inp, out, test++)
  125. }
  126. } catch (e: UnknownError) {
  127. }
  128. }
  129. }
  130.  
  131. open class InputReader(stream: InputStream) {
  132. private var finished: Boolean = false
  133. private var stream: InputStream
  134. private var buf: ByteArray = ByteArray(1024)
  135. private var curChar: Int = 0
  136. private var numChars: Int = 0
  137. private var filter: SpaceCharFilter? = null
  138.  
  139. public open fun read(): Int {
  140. if (numChars == -1)
  141. throw InputMismatchException()
  142.  
  143. if (curChar >= numChars)
  144. {
  145. curChar = 0
  146. try
  147. {
  148. numChars = stream.read(buf)
  149. }
  150. catch (e: IOException) {
  151. throw InputMismatchException()
  152. }
  153.  
  154. if (numChars <= 0)
  155. return -1
  156.  
  157. }
  158.  
  159. return buf[curChar++].toInt()
  160. }
  161. public open fun peek(): Int {
  162. if (numChars == -1)
  163. return -1
  164.  
  165. if (curChar >= numChars)
  166. {
  167. curChar = 0
  168. try
  169. {
  170. numChars = stream.read(buf)
  171. }
  172. catch (e: IOException) {
  173. return -1
  174. }
  175.  
  176. if (numChars <= 0)
  177. return -1
  178.  
  179. }
  180.  
  181. return buf[curChar].toInt()
  182. }
  183. public open fun readInt(): Int {
  184. var c: Int = read()
  185. while (isSpaceChar(c))
  186. c = read()
  187. var sgn: Int = 1
  188. if (c == '-'.toInt())
  189. {
  190. sgn = -1
  191. c = read()
  192. }
  193.  
  194. var res: Int = 0
  195. do
  196. {
  197. if (c < '0' || c > '9')
  198. throw InputMismatchException()
  199.  
  200. res *= 10
  201. res += c - '0'
  202. c = read()
  203. }
  204. while (!isSpaceChar(c))
  205. return res * sgn
  206. }
  207. public open fun readLong(): Long {
  208. var c: Int = read()
  209. while (isSpaceChar(c))
  210. c = read()
  211. var sgn: Int = 1
  212. if (c == '-'.toInt())
  213. {
  214. sgn = -1
  215. c = read()
  216. }
  217.  
  218. var res: Long = 0
  219. do
  220. {
  221. if (c < '0' || c > '9')
  222. throw InputMismatchException()
  223.  
  224. res *= 10
  225. res += c - '0'
  226. c = read()
  227. }
  228. while (!isSpaceChar(c))
  229. return res * sgn
  230. }
  231. public open fun readString(): String {
  232. var c: Int = read()
  233. while (isSpaceChar(c))
  234. c = read()
  235. var res: StringBuilder = StringBuilder()
  236. do
  237. {
  238. res.appendCodePoint(c)
  239. c = read()
  240. }
  241. while (!isSpaceChar(c))
  242. return res.toString()
  243. }
  244. public open fun isSpaceChar(c: Int): Boolean {
  245. if (filter != null)
  246. return filter?.isSpaceChar(c)!!
  247.  
  248. return isWhitespace(c)
  249. }
  250. private open fun readLine0(): String {
  251. var buf: StringBuilder = StringBuilder()
  252. var c: Int = read()
  253. while (c != '\n'.toInt() && c != -1)
  254. {
  255. if (c != '\r'.toInt())
  256. buf.appendCodePoint(c)
  257.  
  258. c = read()
  259. }
  260. return buf.toString()
  261. }
  262. public open fun readLine(): String {
  263. var s: String = readLine0()
  264. while ((s.trim().length()) == 0)
  265. s = readLine0()
  266. return s
  267. }
  268. public open fun readLine(ignoreEmptyLines: Boolean): String {
  269. if (ignoreEmptyLines)
  270. return readLine()
  271. else
  272. return readLine0()
  273. }
  274. public open fun readBigInteger(): BigInteger {
  275. try
  276. {
  277. return BigInteger(readString())
  278. }
  279. catch (e: NumberFormatException) {
  280. throw InputMismatchException()
  281. }
  282.  
  283. }
  284. public open fun readCharacter(): Char {
  285. var c: Int = read()
  286. while (isSpaceChar(c))
  287. c = read()
  288. return c.toChar()
  289. }
  290. public open fun readDouble(): Double {
  291. var c: Int = read()
  292. while (isSpaceChar(c))
  293. c = read()
  294. var sgn: Int = 1
  295. if (c == '-'.toInt())
  296. {
  297. sgn = -1
  298. c = read()
  299. }
  300.  
  301. var res: Double = 0.0
  302. while (!isSpaceChar(c) && c != '.'.toInt())
  303. {
  304. if (c == 'e'.toInt() || c == 'E'.toInt())
  305. return res * Math.pow((10).toDouble(), (readInt()).toDouble())
  306.  
  307. if (c < '0' || c > '9')
  308. throw InputMismatchException()
  309.  
  310. res *= 10
  311. res += c - '0'
  312. c = read()
  313. }
  314. if (c == '.'.toInt())
  315. {
  316. c = read()
  317. var m: Double = 1.0
  318. while (!isSpaceChar(c))
  319. {
  320. if (c == 'e'.toInt() || c == 'E'.toInt())
  321. return res * Math.pow((10).toDouble(), (readInt()).toDouble())
  322.  
  323. if (c < '0' || c > '9')
  324. throw InputMismatchException()
  325.  
  326. m /= 10
  327. res += (c - '0') * m
  328. c = read()
  329. }
  330. }
  331.  
  332. return res * sgn
  333. }
  334. public open fun isExhausted(): Boolean {
  335. var value: Int
  336. while (true) {
  337. value = peek()
  338. if (!isSpaceChar(value) || value == -1)
  339. break
  340. read()
  341. }
  342. return value == -1
  343. }
  344. public open fun next(): String {
  345. return readString()
  346. }
  347. public open fun getFilter(): SpaceCharFilter? {
  348. return filter
  349. }
  350. public open fun setFilter(filter: SpaceCharFilter?): Unit {
  351. this.filter = filter
  352. }
  353. {
  354. this.stream = stream
  355. }
  356. class object {
  357. public open fun isWhitespace(c: Int): Boolean {
  358. return c == ' '.toInt() || c == '\n'.toInt() || c == '\r'.toInt() || c == '\t'.toInt() || c == -1
  359. }
  360. public trait SpaceCharFilter {
  361. public open fun isSpaceChar(ch: Int): Boolean
  362. }
  363. }
  364. }
  365.  
  366. public open class OutputWriter(_writer: PrintWriter) {
  367. private val writer: PrintWriter
  368. public open fun print(array: CharArray): Unit {
  369. writer.print(array)
  370. }
  371. public open fun print(vararg objects: Any): Unit {
  372. for (i in 0..objects.size - 1) {
  373. if (i != 0)
  374. writer.print(' ')
  375.  
  376. writer.print(objects[i])
  377. }
  378. }
  379. public open fun print(array: IntArray): Unit {
  380. for (i in 0..array.size - 1) {
  381. if (i != 0)
  382. writer.print(' ')
  383.  
  384. writer.print(array[i])
  385. }
  386. }
  387. public open fun print(array: LongArray): Unit {
  388. for (i in 0..array.size - 1) {
  389. if (i != 0)
  390. writer.print(' ')
  391.  
  392. writer.print(array[i])
  393. }
  394. }
  395. public open fun printLine(array: IntArray): Unit {
  396. print(array)
  397. writer.println()
  398. }
  399. public open fun printLine(array: LongArray): Unit {
  400. print(array)
  401. writer.println()
  402. }
  403. public open fun printLine(): Unit {
  404. writer.println()
  405. }
  406. public open fun printLine(vararg objects: Any): Unit {
  407. print(objects)
  408. writer.println()
  409. }
  410. public open fun print(i: Char): Unit {
  411. writer.print(i)
  412. }
  413. public open fun printLine(i: Char): Unit {
  414. writer.println(i)
  415. }
  416. public open fun printLine(array: CharArray): Unit {
  417. writer.println(array)
  418. }
  419. public open fun printFormat(format: String, vararg objects: Any): Unit {
  420. writer.printf(format, objects)
  421. }
  422. public open fun close(): Unit {
  423. writer.close()
  424. }
  425. public open fun flush(): Unit {
  426. writer.flush()
  427. }
  428. public open fun print(i: Long): Unit {
  429. writer.print(i)
  430. }
  431. public open fun printLine(i: Long): Unit {
  432. writer.println(i)
  433. }
  434. public open fun print(i: Int): Unit {
  435. writer.print(i)
  436. }
  437. public open fun printLine(i: Int): Unit {
  438. writer.println(i)
  439. }
  440. {
  441. writer = _writer
  442. }
  443. class object {
  444. public open fun init(outputStream: OutputStream): OutputWriter {
  445. val __ = OutputWriter(PrintWriter(BufferedWriter(OutputStreamWriter(outputStream))))
  446. return __
  447. }
  448. public open fun init(writer: Writer): OutputWriter {
  449. val __ = OutputWriter(PrintWriter(writer))
  450. return __
  451. }
  452. }
  453. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement