From dbd3fbcc6edb5444a76cc8ee7b221db0fd4e52b4 Mon Sep 17 00:00:00 2001 From: 22222 <22222@users.noreply.github.com> Date: Sun, 27 Sep 2015 18:16:00 -0500 Subject: [PATCH] Add more tests As part of porting ArnoldC to javascript, I wrote some additional tests to help determine what the correct behaviour is in some cases. These tests are mostly focused around how variable scope works, numeric limits, and other edge cases. The only existing test I modified was in the logical test, and that was just to change its name (the name said it was expecting a result of False but it really expected a result of True). --- .../scala/org/arnoldc/ArithmeticTest.scala | 277 +++++++++++++++++ .../org/arnoldc/BranchStatementTest.scala | 74 +++++ src/test/scala/org/arnoldc/InputTest.scala | 269 +++++++++++++++++ src/test/scala/org/arnoldc/LogicalTest.scala | 2 +- src/test/scala/org/arnoldc/MethodTest.scala | 278 ++++++++++++++++++ 5 files changed, 899 insertions(+), 1 deletion(-) diff --git a/src/test/scala/org/arnoldc/ArithmeticTest.scala b/src/test/scala/org/arnoldc/ArithmeticTest.scala index 43e9955..b147678 100644 --- a/src/test/scala/org/arnoldc/ArithmeticTest.scala +++ b/src/test/scala/org/arnoldc/ArithmeticTest.scala @@ -13,6 +13,24 @@ class ArithmeticTest extends ArnoldGeneratorTest { getOutput(code) should equal("") } + it should "function when a variable is declared with keyword name" in { + val code: String = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE while\n" + + "YOU SET US UP 123\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("") + } + + it should "function when a variable is declared with keyword name vol2" in { + val code: String = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE CHILL\n" + + "YOU SET US UP 123\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("") + } + it should "function when an integer is printed" in { val code: String = "IT'S SHOWTIME\n" + @@ -55,6 +73,14 @@ class ArithmeticTest extends ArnoldGeneratorTest { getOutput(code) should equal("!!! ??? äöäöäöä@#0123=+-,.\n") } + it should "evaluate when a string with newlines is printed" in { + val code: String = + "IT'S SHOWTIME\n" + + "TALK TO THE HAND \"line1\nline2\nline3\n\n\"\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("line1\nline2\nline3\n\n\n") + } + it should "evaluate when an integer is declared and printed" in { val code: String = "IT'S SHOWTIME\n" + @@ -290,6 +316,21 @@ class ArithmeticTest extends ArnoldGeneratorTest { "YOU HAVE BEEN TERMINATED\n" getOutput(code) should equal("2\n") } + it should "evaluate when dividing to fraction and then multiplying back" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE var\n" + + "YOU SET US UP 1\n" + + "TALK TO THE HAND var\n" + + "GET TO THE CHOPPER var\n" + + "HERE IS MY INVITATION var\n" + + "HE HAD TO SPLIT 2\n" + + "YOU'RE FIRED 2\n" + + "ENOUGH TALK\n" + + "TALK TO THE HAND var\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("1\n0\n") + } it should "evaluate when calculating modulo variables vol1" in { val code = @@ -319,6 +360,20 @@ class ArithmeticTest extends ArnoldGeneratorTest { getOutput(code) should equal("0\n") } + it should "evaluate when calculating modulo variables with negative value" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE var\n" + + "YOU SET US UP -3\n" + + "GET TO THE CHOPPER var\n" + + "HERE IS MY INVITATION var\n" + + "I LET HIM GO 2\n" + + "ENOUGH TALK\n" + + "TALK TO THE HAND var\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("-1\n") + } + it should "evaluate using different arithmetic operations" in { val code = "IT'S SHOWTIME\n" + @@ -374,6 +429,212 @@ class ArithmeticTest extends ArnoldGeneratorTest { getOutput(code) should equal("12\n") } + it should "evaluate short max value on declaration" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE var\n" + + "YOU SET US UP 32767\n" + + "TALK TO THE HAND var\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("32767\n") + } + + it should "evaluate short min value on declaration" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE var\n" + + "YOU SET US UP -32768\n" + + "TALK TO THE HAND var\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("-32768\n") + } + + it should "evaluate short overflow on declaration" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE var\n" + + "YOU SET US UP 32768\n" + + "TALK TO THE HAND var\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("-32768\n") + } + + it should "evaluate short overflow on declaration vol2" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE var\n" + + "YOU SET US UP 65537\n" + + "TALK TO THE HAND var\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("1\n") + } + + it should "evaluate short underflow on declaration" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE var\n" + + "YOU SET US UP -32769\n" + + "TALK TO THE HAND var\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("32767\n") + } + + it should "evaluate short underflow on declaration vol2" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE var\n" + + "YOU SET US UP -65538\n" + + "TALK TO THE HAND var\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("-2\n") + } + + it should "evaluate short overflow on addition" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE var\n" + + "YOU SET US UP 32767\n" + + "TALK TO THE HAND var\n" + + "GET TO THE CHOPPER var\n" + + "HERE IS MY INVITATION var\n" + + "GET UP 1\n" + + "ENOUGH TALK\n" + + "TALK TO THE HAND var\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("32767\n32768\n") + } + + it should "evaluate short overflow on addition vol2" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE var\n" + + "YOU SET US UP 32767\n" + + "TALK TO THE HAND var\n" + + "GET TO THE CHOPPER var\n" + + "HERE IS MY INVITATION var\n" + + "GET UP 32767\n" + + "GET UP 32767\n" + + "GET UP 32767\n" + + "GET UP 32767\n" + + "GET UP 32767\n" + + "GET UP 32767\n" + + "GET UP 32767\n" + + "GET UP 32767\n" + + "GET UP 32767\n" + + "ENOUGH TALK\n" + + "TALK TO THE HAND var\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("32767\n327670\n") + } + + it should "evaluate short overflow on multiplication" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE var\n" + + "YOU SET US UP 32767\n" + + "TALK TO THE HAND var\n" + + "GET TO THE CHOPPER var\n" + + "HERE IS MY INVITATION var\n" + + "YOU'RE FIRED 32767\n" + + "YOU'RE FIRED 32767\n" + + "YOU'RE FIRED 32767\n" + + "ENOUGH TALK\n" + + "TALK TO THE HAND var\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("32767\n2147352577\n") + } + it should "evaluate short overflow on multiplication vol2" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE var\n" + + "YOU SET US UP 32767\n" + + "TALK TO THE HAND var\n" + + "GET TO THE CHOPPER var\n" + + "HERE IS MY INVITATION var\n" + + "YOU'RE FIRED 32767\n" + + "YOU'RE FIRED 32767\n" + + "YOU'RE FIRED 32767\n" + + "YOU'RE FIRED 32767\n" + + "ENOUGH TALK\n" + + "TALK TO THE HAND var\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("32767\n-2147319809\n") + } + it should "evaluate short underflow on multiplication" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE var\n" + + "YOU SET US UP -32768\n" + + "TALK TO THE HAND var\n" + + "GET TO THE CHOPPER var\n" + + "HERE IS MY INVITATION var\n" + + "YOU'RE FIRED 32767\n" + + "YOU'RE FIRED 32767\n" + + "YOU'RE FIRED 32767\n" + + "ENOUGH TALK\n" + + "TALK TO THE HAND var\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("-32768\n1073774592\n") + } + it should "evaluate short underflow on multiplication vol2" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE var\n" + + "YOU SET US UP -32768\n" + + "TALK TO THE HAND var\n" + + "GET TO THE CHOPPER var\n" + + "HERE IS MY INVITATION var\n" + + "YOU'RE FIRED 32767\n" + + "YOU'RE FIRED 32767\n" + + "YOU'RE FIRED 32767\n" + + "YOU'RE FIRED 32767\n" + + "YOU'RE FIRED 32767\n" + + "ENOUGH TALK\n" + + "TALK TO THE HAND var\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("-32768\n-1073709056\n") + } + + it should "detect when a string with quote escaped quotes is printed" in { + val code: String = + "IT'S SHOWTIME\n" + + "TALK TO THE HAND \"he said \"\"Hello\"\"\"\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[ParsingException] { + getOutput(code) + } + } + + it should "detect when a string with backslash escaped quotes is printed" in { + val code: String = + "IT'S SHOWTIME\n" + + "TALK TO THE HAND \"he said \\\"Hello\\\"\"\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[ParsingException] { + getOutput(code) + } + } + + it should "detect when a string with backslashes is printed" in { + val code: String = + "IT'S SHOWTIME\n" + + "TALK TO THE HAND \"C:\\Temp\\readme.txt\"\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[ParsingException] { + getOutput(code) + } + } + + it should "detect when a string with backslash escaped backslashes is printed" in { + val code: String = + "IT'S SHOWTIME\n" + + "TALK TO THE HAND \"C:\\\\Temp\\\\readme.txt\"\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[ParsingException] { + getOutput(code) + } + } + it should "detect duplicate variable declarations" in { val code = "IT'S SHOWTIME\n" + @@ -397,4 +658,20 @@ class ArithmeticTest extends ArnoldGeneratorTest { getOutput(code) } } + + it should "detect divide by zero" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE var\n" + + "YOU SET US UP @I LIED\n" + + "GET TO THE CHOPPER var\n" + + "HERE IS MY INVITATION 1\n" + + "HE HAD TO SPLIT 0\n" + + "ENOUGH TALK\n" + + "TALK TO THE HAND var\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[java.lang.ArithmeticException] { + getOutput(code) + } + } } diff --git a/src/test/scala/org/arnoldc/BranchStatementTest.scala b/src/test/scala/org/arnoldc/BranchStatementTest.scala index d5ee202..6751503 100644 --- a/src/test/scala/org/arnoldc/BranchStatementTest.scala +++ b/src/test/scala/org/arnoldc/BranchStatementTest.scala @@ -1,5 +1,7 @@ package org.arnoldc +import org.parboiled.errors.ParsingException + class BranchStatementTest extends ArnoldGeneratorTest { it should "function using simple if statements" in { val code = @@ -130,4 +132,76 @@ class BranchStatementTest extends ArnoldGeneratorTest { "YOU HAVE BEEN TERMINATED\n" getOutput(code) should equal("1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n") } + + it should "function using variable from parent block scope" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE vartrue\n" + + "YOU SET US UP @NO PROBLEMO\n" + + "BECAUSE I'M GOING TO SAY PLEASE vartrue\n" + + "TALK TO THE HAND vartrue\n" + + "GET TO THE CHOPPER vartrue\n" + + "HERE IS MY INVITATION @I LIED\n" + + "ENOUGH TALK\n" + + "TALK TO THE HAND vartrue\n" + + "YOU HAVE NO RESPECT FOR LOGIC\n" + + "TALK TO THE HAND vartrue\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("1\n0\n0\n") + } + + it should "detect when variable redeclared in child block scope" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE vartrue\n" + + "YOU SET US UP @NO PROBLEMO\n" + + "TALK TO THE HAND vartrue\n" + + "BECAUSE I'M GOING TO SAY PLEASE vartrue\n" + + "HEY CHRISTMAS TREE vartrue\n" + + "YOU SET US UP @I LIED\n" + + "TALK TO THE HAND vartrue\n" + + "YOU HAVE NO RESPECT FOR LOGIC\n" + + "TALK TO THE HAND vartrue\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[ParsingException] { + getOutput(code) + } + } + + it should "detect when child block scope variable is redeclared" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE vartrue\n" + + "YOU SET US UP @NO PROBLEMO\n" + + "BECAUSE I'M GOING TO SAY PLEASE vartrue\n" + + "HEY CHRISTMAS TREE varfalse\n" + + "YOU SET US UP @I LIED\n" + + "TALK TO THE HAND varfalse\n" + + "YOU HAVE NO RESPECT FOR LOGIC\n" + + "HEY CHRISTMAS TREE varfalse\n" + + "YOU SET US UP @I LIED\n" + + "TALK TO THE HAND varfalse\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[ParsingException] { + getOutput(code) + } + } + + it should "detect when using variable declared in a child block scope" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE vartrue\n" + + "YOU SET US UP @NO PROBLEMO\n" + + "TALK TO THE HAND vartrue\n" + + "BECAUSE I'M GOING TO SAY PLEASE vartrue\n" + + "HEY CHRISTMAS TREE varfalse\n" + + "YOU SET US UP @I LIED\n" + + "TALK TO THE HAND varfalse\n" + + "YOU HAVE NO RESPECT FOR LOGIC\n" + + "TALK TO THE HAND varfalse\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[java.lang.VerifyError] { + getOutput(code) + } + } } diff --git a/src/test/scala/org/arnoldc/InputTest.scala b/src/test/scala/org/arnoldc/InputTest.scala index 95b1814..b796a0e 100644 --- a/src/test/scala/org/arnoldc/InputTest.scala +++ b/src/test/scala/org/arnoldc/InputTest.scala @@ -20,6 +20,275 @@ class InputTest extends ArnoldGeneratorTest{ getOutput(code) should equal("Input a number:\n123\nBye\n") } + it should "read max short from input" in { + writeToFile(path, "32767") + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE result\n" + + "YOU SET US UP 0\n" + + "GET YOUR ASS TO MARS result\n" + + "DO IT NOW\n" + + "I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY\n" + + "TALK TO THE HAND result\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("32767\n") + } + it should "read max int from input" in { + writeToFile(path, "2147483647") + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE result\n" + + "YOU SET US UP 0\n" + + "GET YOUR ASS TO MARS result\n" + + "DO IT NOW\n" + + "I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY\n" + + "TALK TO THE HAND result\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("2147483647\n") + } + it should "read min short from input" in { + writeToFile(path, "-32768") + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE result\n" + + "YOU SET US UP 0\n" + + "GET YOUR ASS TO MARS result\n" + + "DO IT NOW\n" + + "I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY\n" + + "TALK TO THE HAND result\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("-32768\n") + } + it should "read min int from input" in { + writeToFile(path, "-2147483648") + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE result\n" + + "YOU SET US UP 0\n" + + "GET YOUR ASS TO MARS result\n" + + "DO IT NOW\n" + + "I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY\n" + + "TALK TO THE HAND result\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("-2147483648\n") + } + it should "read leading zero from input" in { + writeToFile(path, "0200") + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE result\n" + + "YOU SET US UP 0\n" + + "GET YOUR ASS TO MARS result\n" + + "DO IT NOW\n" + + "I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY\n" + + "TALK TO THE HAND result\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("200\n") + } + it should "read leading plus sign and zero from input" in { + writeToFile(path, "+0200") + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE result\n" + + "YOU SET US UP 0\n" + + "GET YOUR ASS TO MARS result\n" + + "DO IT NOW\n" + + "I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY\n" + + "TALK TO THE HAND result\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("200\n") + } + it should "read number with leading and trailing whitespace from input" in { + writeToFile(path, " 2 ") + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE result\n" + + "YOU SET US UP 0\n" + + "GET YOUR ASS TO MARS result\n" + + "DO IT NOW\n" + + "I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY\n" + + "TALK TO THE HAND result\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("2\n") + } + it should "read number, space, number from input" in { + writeToFile(path, "2 2") + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE result\n" + + "YOU SET US UP 0\n" + + "GET YOUR ASS TO MARS result\n" + + "DO IT NOW\n" + + "I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY\n" + + "TALK TO THE HAND result\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("2\n") + } + it should "read number, space, alpha from input" in { + writeToFile(path, "2 a") + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE result\n" + + "YOU SET US UP 0\n" + + "GET YOUR ASS TO MARS result\n" + + "DO IT NOW\n" + + "I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY\n" + + "TALK TO THE HAND result\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("2\n") + } + + it should "detect alpha from input" in { + writeToFile(path, "a") + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE result\n" + + "YOU SET US UP 0\n" + + "GET YOUR ASS TO MARS result\n" + + "DO IT NOW\n" + + "I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY\n" + + "TALK TO THE HAND result\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[java.util.InputMismatchException] { + getOutput(code) + } + } + it should "detect alpha numeric from input" in { + writeToFile(path, "1a") + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE result\n" + + "YOU SET US UP 0\n" + + "GET YOUR ASS TO MARS result\n" + + "DO IT NOW\n" + + "I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY\n" + + "TALK TO THE HAND result\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[java.util.InputMismatchException] { + getOutput(code) + } + } + it should "detect float from input" in { + writeToFile(path, "1.0") + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE result\n" + + "YOU SET US UP 0\n" + + "GET YOUR ASS TO MARS result\n" + + "DO IT NOW\n" + + "I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY\n" + + "TALK TO THE HAND result\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[java.util.InputMismatchException] { + getOutput(code) + } + } + it should "detect hex from input" in { + writeToFile(path, "0x2") + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE result\n" + + "YOU SET US UP 0\n" + + "GET YOUR ASS TO MARS result\n" + + "DO IT NOW\n" + + "I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY\n" + + "TALK TO THE HAND result\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[java.util.InputMismatchException] { + getOutput(code) + } + } + it should "detect scientific notation from input" in { + writeToFile(path, "3.30e2") + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE result\n" + + "YOU SET US UP 0\n" + + "GET YOUR ASS TO MARS result\n" + + "DO IT NOW\n" + + "I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY\n" + + "TALK TO THE HAND result\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[java.util.InputMismatchException] { + getOutput(code) + } + } + it should "detect int overflow from input" in { + writeToFile(path, "2147483648") + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE result\n" + + "YOU SET US UP 0\n" + + "GET YOUR ASS TO MARS result\n" + + "DO IT NOW\n" + + "I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY\n" + + "TALK TO THE HAND result\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[java.util.InputMismatchException] { + getOutput(code) + } + } + it should "detect int underflow from input" in { + writeToFile(path, "-2147483649") + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE result\n" + + "YOU SET US UP 0\n" + + "GET YOUR ASS TO MARS result\n" + + "DO IT NOW\n" + + "I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY\n" + + "TALK TO THE HAND result\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[java.util.InputMismatchException] { + getOutput(code) + } + } + it should "detect only minus sign from input" in { + writeToFile(path, "-") + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE result\n" + + "YOU SET US UP 0\n" + + "GET YOUR ASS TO MARS result\n" + + "DO IT NOW\n" + + "I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY\n" + + "TALK TO THE HAND result\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[java.util.InputMismatchException] { + getOutput(code) + } + } + it should "detect minus sign, space, number from input" in { + writeToFile(path, "- 2") + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE result\n" + + "YOU SET US UP 0\n" + + "GET YOUR ASS TO MARS result\n" + + "DO IT NOW\n" + + "I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY\n" + + "TALK TO THE HAND result\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[java.util.InputMismatchException] { + getOutput(code) + } + } + it should "detect only plus sign from input" in { + writeToFile(path, "+") + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE result\n" + + "YOU SET US UP 0\n" + + "GET YOUR ASS TO MARS result\n" + + "DO IT NOW\n" + + "I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY\n" + + "TALK TO THE HAND result\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[java.util.InputMismatchException] { + getOutput(code) + } + } + val path = "test.in" override val byteCodeExecutor = new ByteCodeExecutor{ diff --git a/src/test/scala/org/arnoldc/LogicalTest.scala b/src/test/scala/org/arnoldc/LogicalTest.scala index e02abe3..f7cf501 100644 --- a/src/test/scala/org/arnoldc/LogicalTest.scala +++ b/src/test/scala/org/arnoldc/LogicalTest.scala @@ -110,7 +110,7 @@ class LogicalTest extends ArnoldGeneratorTest { getOutput(code) should equal("0\n") } - it should "True and True and True evaluates False" in { + it should "True and True and True evaluates True" in { val code = "IT'S SHOWTIME\n" + "HEY CHRISTMAS TREE var\n" + diff --git a/src/test/scala/org/arnoldc/MethodTest.scala b/src/test/scala/org/arnoldc/MethodTest.scala index 4b4a9fb..e5aaee3 100644 --- a/src/test/scala/org/arnoldc/MethodTest.scala +++ b/src/test/scala/org/arnoldc/MethodTest.scala @@ -53,6 +53,26 @@ class MethodTest extends ArnoldGeneratorTest { "HASTA LA VISTA, BABY" getOutput(code) should equal("Hello\n") } + it should "evalute a plain method call with keyword name" in { + val code = + "IT'S SHOWTIME\n" + + "DO IT NOW while\n" + + "YOU HAVE BEEN TERMINATED\n" + + "LISTEN TO ME VERY CAREFULLY while\n" + + "TALK TO THE HAND \"Hello\"\n" + + "HASTA LA VISTA, BABY" + getOutput(code) should equal("Hello\n") + } + it should "evalute a plain method call with keyword name vol2" in { + val code = + "IT'S SHOWTIME\n" + + "DO IT NOW CHILL\n" + + "YOU HAVE BEEN TERMINATED\n" + + "LISTEN TO ME VERY CAREFULLY CHILL\n" + + "TALK TO THE HAND \"Hello\"\n" + + "HASTA LA VISTA, BABY" + getOutput(code) should equal("Hello\n") + } it should "evalute a method call that takes an argument" in { val code = @@ -68,6 +88,34 @@ class MethodTest extends ArnoldGeneratorTest { getOutput(code) should equal("123\n") } + it should "evalute a method call that takes an argument with keyword as parameter name" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE throw\n" + + "YOU SET US UP 123\n" + + "DO IT NOW printInteger throw\n" + + "YOU HAVE BEEN TERMINATED\n" + + "LISTEN TO ME VERY CAREFULLY printInteger\n" + + "I NEED YOUR CLOTHES YOUR BOOTS AND YOUR MOTORCYCLE while\n" + + "TALK TO THE HAND while\n" + + "HASTA LA VISTA, BABY" + getOutput(code) should equal("123\n") + } + + it should "evalute a method call that takes an argument with keyword as parameter name vol2" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE BULLSHIT\n" + + "YOU SET US UP 123\n" + + "DO IT NOW printInteger BULLSHIT\n" + + "YOU HAVE BEEN TERMINATED\n" + + "LISTEN TO ME VERY CAREFULLY printInteger\n" + + "I NEED YOUR CLOTHES YOUR BOOTS AND YOUR MOTORCYCLE CHILL\n" + + "TALK TO THE HAND CHILL\n" + + "HASTA LA VISTA, BABY" + getOutput(code) should equal("123\n") + } + it should "evalute multiple method calls" in { val code = "IT'S SHOWTIME\n" + @@ -238,6 +286,101 @@ class MethodTest extends ArnoldGeneratorTest { getOutput(code) should equal("49\n") } + it should "evaluate same variable defined in main method and another method" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE vartrue\n" + + "YOU SET US UP @NO PROBLEMO\n" + + "TALK TO THE HAND vartrue\n" + + "DO IT NOW method\n" + + "TALK TO THE HAND vartrue\n" + + "YOU HAVE BEEN TERMINATED\n" + + "LISTEN TO ME VERY CAREFULLY method\n" + + "HEY CHRISTMAS TREE vartrue\n" + + "YOU SET US UP @I LIED\n" + + "TALK TO THE HAND vartrue\n" + + "HASTA LA VISTA, BABY\n" + getOutput(code) should equal("1\n0\n1\n") + } + + it should "evaluate same variable defined in two methods" in { + val code = + "IT'S SHOWTIME\n" + + "DO IT NOW method\n" + + "DO IT NOW method2\n" + + "YOU HAVE BEEN TERMINATED\n" + + "LISTEN TO ME VERY CAREFULLY method\n" + + "HEY CHRISTMAS TREE vartrue\n" + + "YOU SET US UP @NO PROBLEMO\n" + + "TALK TO THE HAND vartrue\n" + + "HASTA LA VISTA, BABY\n" + + "LISTEN TO ME VERY CAREFULLY method2\n" + + "HEY CHRISTMAS TREE vartrue\n" + + "YOU SET US UP @I LIED\n" + + "TALK TO THE HAND vartrue\n" + + "HASTA LA VISTA, BABY\n" + getOutput(code) should equal("1\n0\n") + } + + it should "evaluate variable name is the same as method name" in { + val code = + "LISTEN TO ME VERY CAREFULLY method\n" + + "HEY CHRISTMAS TREE method\n" + + "YOU SET US UP 2\n" + + "TALK TO THE HAND method\n" + + "HASTA LA VISTA, BABY\n" + + "IT'S SHOWTIME\n" + + "DO IT NOW method\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("2\n") + } + + it should "evaluate variable name is the same as method name vol2" in { + val code = + "LISTEN TO ME VERY CAREFULLY method\n" + + "HEY CHRISTMAS TREE method\n" + + "YOU SET US UP 2\n" + + "TALK TO THE HAND method\n" + + "HASTA LA VISTA, BABY\n" + + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE method\n" + + "YOU SET US UP 1\n" + + "TALK TO THE HAND method\n" + + "DO IT NOW method\n" + + "TALK TO THE HAND method\n" + + "YOU HAVE BEEN TERMINATED\n" + getOutput(code) should equal("1\n2\n1\n") + } + it should "evaluate variable name is the same as method name vol3" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE method\n" + + "YOU SET US UP 1\n" + + "TALK TO THE HAND method\n" + + "DO IT NOW method\n" + + "TALK TO THE HAND method\n" + + "YOU HAVE BEEN TERMINATED\n" + + "LISTEN TO ME VERY CAREFULLY method\n" + + "HEY CHRISTMAS TREE method\n" + + "YOU SET US UP 2\n" + + "TALK TO THE HAND method\n" + + "HASTA LA VISTA, BABY\n" + getOutput(code) should equal("1\n2\n1\n") + } + it should "evaluate when parameter name is the same as method name" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE method\n" + + "YOU SET US UP 123\n" + + "DO IT NOW method method\n" + + "YOU HAVE BEEN TERMINATED\n" + + "LISTEN TO ME VERY CAREFULLY method\n" + + "I NEED YOUR CLOTHES YOUR BOOTS AND YOUR MOTORCYCLE method\n" + + "TALK TO THE HAND method\n" + + "HASTA LA VISTA, BABY" + getOutput(code) should equal("123\n") + } + it should "detect unclosed main method" in { val code = "IT'S SHOWTIME\n" + @@ -258,6 +401,68 @@ class MethodTest extends ArnoldGeneratorTest { } } + it should "detect missing main method" in { + val code = + "LISTEN TO ME VERY CAREFULLY mymethod\n" + + "HASTA LA VISTA, BABY\n" + intercept[java.lang.NoSuchMethodException] { + getOutput(code) + } + } + + it should "detect duplicate main method" in { + val code = + "IT'S SHOWTIME\n" + + "YOU HAVE BEEN TERMINATED\n" + + "IT'S SHOWTIME\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[java.lang.ClassFormatError] { + getOutput(code) + } + } + + it should "detect duplicate method" in { + val code = + "IT'S SHOWTIME\n" + + "YOU HAVE BEEN TERMINATED\n" + + "LISTEN TO ME VERY CAREFULLY printHello\n" + + "TALK TO THE HAND \"Hello\"\n" + + "HASTA LA VISTA, BABY\n" + + "LISTEN TO ME VERY CAREFULLY printHello\n" + + "TALK TO THE HAND \"Hello\"\n" + + "HASTA LA VISTA, BABY\n" + intercept[java.lang.ClassFormatError] { + getOutput(code) + } + } + + it should "detect method nested in main method" in { + val code = + "IT'S SHOWTIME\n" + + "LISTEN TO ME VERY CAREFULLY printHello\n" + + "TALK TO THE HAND \"Hello\"\n" + + "HASTA LA VISTA, BABY\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[ParsingException] { + getOutput(code) + } + } + + it should "detect method nested in method" in { + val code = + "IT'S SHOWTIME\n" + + "YOU HAVE BEEN TERMINATED\n" + + "LISTEN TO ME VERY CAREFULLY printHello\n" + + "LISTEN TO ME VERY CAREFULLY printWorld\n" + + "TALK TO THE HAND \"World\"\n" + + "HASTA LA VISTA, BABY\n" + + "TALK TO THE HAND \"Hello\"\n" + + "HASTA LA VISTA, BABY\n" + intercept[ParsingException] { + getOutput(code) + } + } + it should "detect calls to methods that are not declared" in { val code = "IT'S SHOWTIME\n" + @@ -295,4 +500,77 @@ class MethodTest extends ArnoldGeneratorTest { getOutput(code) } } + + it should "detect a void return statement in main method" in { + val code = + "IT'S SHOWTIME\n" + + "I'LL BE BACK\n" + + "TALK TO THE HAND \"Hi\"\n" + + "YOU HAVE BEEN TERMINATED\n" + + intercept[ParsingException] { + getOutput(code) + } + } + + it should "detect a nonvoid return statement in main method" in { + val code = + "IT'S SHOWTIME\n" + + "I'LL BE BACK 0\n" + + "YOU HAVE BEEN TERMINATED\n" + + intercept[ParsingException] { + getOutput(code) + } + } + + it should "detect when using variable declared in a method from main" in { + val code = + "LISTEN TO ME VERY CAREFULLY method\n" + + "HEY CHRISTMAS TREE varfalse\n" + + "YOU SET US UP @I LIED\n" + + "TALK TO THE HAND varfalse\n" + + "HASTA LA VISTA, BABY\n" + + "IT'S SHOWTIME\n" + + "TALK TO THE HAND varfalse\n" + + "YOU HAVE BEEN TERMINATED\n" + intercept[ParsingException] { + getOutput(code) + } + } + + it should "detect when using variable declared in a method from another" in { + val code = + "IT'S SHOWTIME\n" + + "YOU HAVE BEEN TERMINATED\n" + + "LISTEN TO ME VERY CAREFULLY method\n" + + "HEY CHRISTMAS TREE varfalse\n" + + "YOU SET US UP @I LIED\n" + + "TALK TO THE HAND varfalse\n" + + "HASTA LA VISTA, BABY\n" + + "LISTEN TO ME VERY CAREFULLY method2\n" + + "TALK TO THE HAND varfalse\n" + + "HASTA LA VISTA, BABY\n" + intercept[ParsingException] { + getOutput(code) + } + } + + it should "detect when variable name is the same as a method parameter name" in { + val code = + "IT'S SHOWTIME\n" + + "HEY CHRISTMAS TREE argument\n" + + "YOU SET US UP 123\n" + + "DO IT NOW printInteger argument\n" + + "YOU HAVE BEEN TERMINATED\n" + + "LISTEN TO ME VERY CAREFULLY printInteger\n" + + "I NEED YOUR CLOTHES YOUR BOOTS AND YOUR MOTORCYCLE value\n" + + "HEY CHRISTMAS TREE value\n" + + "YOU SET US UP 321\n" + + "TALK TO THE HAND value\n" + + "HASTA LA VISTA, BABY" + intercept[ParsingException] { + getOutput(code) + } + } }