From d2d50f6d4f5475fac959dc394bb646904ee858ff Mon Sep 17 00:00:00 2001 From: Marvin Kaiser Date: Tue, 14 Jan 2020 11:47:04 +0100 Subject: [PATCH] cleaned up test suit --- makefile | 12 +-- src/test/comparison/comparison.c | 13 +-- src/test/functionCall/functionCall.c | 53 +++++----- src/test/loop/loop.c | 13 +-- src/test/math/math.c | 103 ++++++++++++++++++++ src/test/{testCode.h => math/math.h} | 0 src/test/print/print.c | 34 +++++++ src/test/print/print.h | 10 ++ src/test/recursive/recursive.c | 13 +-- src/test/test.c | 31 ++++++ src/test/{tests.h => test.h} | 3 +- src/test/{tests.k => test.k} | 0 src/test/testCode.c | 138 --------------------------- 13 files changed, 218 insertions(+), 205 deletions(-) create mode 100644 src/test/math/math.c rename src/test/{testCode.h => math/math.h} (100%) create mode 100644 src/test/print/print.c create mode 100644 src/test/print/print.h create mode 100644 src/test/test.c rename src/test/{tests.h => test.h} (72%) rename src/test/{tests.k => test.k} (100%) delete mode 100644 src/test/testCode.c diff --git a/makefile b/makefile index 0411731..1c8c9c2 100644 --- a/makefile +++ b/makefile @@ -14,17 +14,17 @@ build: clean target/klang-1.0-jar-with-dependencies.jar target/klang-1.0-jar-with-dependencies.jar: mvn package -runTest: ./src/test/test +test: ./src/test/test ./src/test/test -./src/test/test: ./src/test/tests.s - gcc -o ./src/test/test ./src/test/tests.s ./src/test/functionCall/functionCall.c ./src/test/loop/loop.c ./src/test/recursive/recursive.c ./src/test/comparison/comparison.c ./src/test/testCode.c +./src/test/test: ./src/test/test.s + gcc -o ./src/test/test ./src/test/test.s ./src/test/**/*.c ./src/test/test.c -./src/test/tests.s: target/klang-1.0-jar-with-dependencies.jar - java -cp target/klang-1.0-jar-with-dependencies.jar de.hsrm.compiler.Klang.Klang < ./src/test/tests.k > ./src/test/tests.s +./src/test/test.s: target/klang-1.0-jar-with-dependencies.jar + java -cp target/klang-1.0-jar-with-dependencies.jar de.hsrm.compiler.Klang.Klang < ./src/test/test.k > ./src/test/test.s clean: - rm -f ./src/test/tests.s + rm -f ./src/test/test.s rm -f ./src/test/test rm -f code.s rm -f target/klang-1.0-jar-with-dependencies.jar diff --git a/src/test/comparison/comparison.c b/src/test/comparison/comparison.c index b450b72..57c03ec 100644 --- a/src/test/comparison/comparison.c +++ b/src/test/comparison/comparison.c @@ -1,20 +1,13 @@ #include #include "comparison.h" - -void printSuccessComp(char* name, int x, int y, int expected, int result) { - printf("SUCCESS:\t%d %s %d\tGOT: %d\tExpected: %d\n", x, name, y, result, expected); -} - -void printErrorComp(char* name, int x, int y, int expected, int result) { - printf("ERROR:\t\t%d %s %d\tGOT: %d\tExpected: %d\n", x, name, y, result, expected); -} +#include "../print/print.h" int comparisonTest(char* name, int x, int y, int expected, int result) { if (expected == result) { - printSuccessComp(name, x, y, expected, result); + succInfixTwo(name, x, y, expected, result); return 0; } else { - printErrorComp(name, x, y, expected, result); + errInfixTwo(name, x, y, expected, result); return 1; } } diff --git a/src/test/functionCall/functionCall.c b/src/test/functionCall/functionCall.c index b097e95..decf48c 100644 --- a/src/test/functionCall/functionCall.c +++ b/src/test/functionCall/functionCall.c @@ -1,20 +1,13 @@ #include #include "functionCall.h" - -void printArgSuccess(char* name, int expected, int result) { - printf("SUCCESS:\t%s()\tGOT: %d\tExpected: %d\n", name, result, expected); -} - -void printArgError(char* name, int expected, int result) { - printf("ERROR:\t\t%s()\tGOT: %d\tExpected: %d\n", name, result, expected); -} +#include "../print/print.h" int argumentTest(char* name, int expected, int result) { if (expected == result) { - printArgSuccess(name, expected, result); + succ(name, expected, result); return 0; } else { - printArgError(name, expected, result); + err(name, expected, result); return 1; } } @@ -23,26 +16,26 @@ int runFunctionCallTests () { int failed = 0; printf("\nFunction Call Tests \n"); // Checks that parameters are correctly passed from gcc to functions - failed += argumentTest("arg1", 1, arg1(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); - failed += argumentTest("arg2", 2, arg2(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); - failed += argumentTest("arg3", 3, arg3(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); - failed += argumentTest("arg4", 4, arg4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); - failed += argumentTest("arg5", 5, arg5(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); - failed += argumentTest("arg6", 6, arg6(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); - failed += argumentTest("arg7", 7, arg7(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); - failed += argumentTest("arg8", 8, arg8(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); - failed += argumentTest("arg9", 9, arg9(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); - failed += argumentTest("arg10", 10, arg10(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); + failed += argumentTest("arg1(...args)", 1, arg1(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); + failed += argumentTest("arg2(...args)", 2, arg2(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); + failed += argumentTest("arg3(...args)", 3, arg3(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); + failed += argumentTest("arg4(...args)", 4, arg4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); + failed += argumentTest("arg5(...args)", 5, arg5(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); + failed += argumentTest("arg6(...args)", 6, arg6(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); + failed += argumentTest("arg7(...args)", 7, arg7(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); + failed += argumentTest("arg8(...args)", 8, arg8(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); + failed += argumentTest("arg9(...args)", 9, arg9(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); + failed += argumentTest("arg10(...args)", 10, arg10(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); // Checks that parameters are correctly passed from klang to functions - failed += argumentTest("get1()", 1, get1()); - failed += argumentTest("get2()", 2, get2()); - failed += argumentTest("get3()", 3, get3()); - failed += argumentTest("get4()", 4, get4()); - failed += argumentTest("get5()", 5, get5()); - failed += argumentTest("get6()", 6, get6()); - failed += argumentTest("get7()", 7, get7()); - failed += argumentTest("get8()", 8, get8()); - failed += argumentTest("get9()", 9, get9()); - failed += argumentTest("get10()", 10, get10()); + failed += argumentTest("get1(...args)", 1, get1()); + failed += argumentTest("get2(...args)", 2, get2()); + failed += argumentTest("get3(...args)", 3, get3()); + failed += argumentTest("get4(...args)", 4, get4()); + failed += argumentTest("get5(...args)", 5, get5()); + failed += argumentTest("get6(...args)", 6, get6()); + failed += argumentTest("get7(...args)", 7, get7()); + failed += argumentTest("get8(...args)", 8, get8()); + failed += argumentTest("get9(...args)", 9, get9()); + failed += argumentTest("get10(...args)", 10, get10()); return failed; } \ No newline at end of file diff --git a/src/test/loop/loop.c b/src/test/loop/loop.c index b647e51..8de8840 100644 --- a/src/test/loop/loop.c +++ b/src/test/loop/loop.c @@ -1,20 +1,13 @@ #include #include "loop.h" - -void printLoopSuccess(char* name, int x, int expected, int result) { - printf("\033[0;32mSUCCESS:\t%s(%d)\tGOT: %d\tExpected: %d\033[0;0m\n", name, x, result, expected); -} - -void printLoopError(char* name, int x, int expected, int result) { - printf("\033[0;31mERROR:\t\t%s(%d)\tGOT: %d\tExpected: %d\033[0;0m\n", name, x, result, expected); -} +#include "../print/print.h" int loopTest(char* name, int x, int expected, int result) { if (expected == result) { - printLoopSuccess(name, x, expected, result); + succPrefixOne(name, x, expected, result); return 0; } else { - printLoopError(name, x, expected, result); + errPrefixOne(name, x, expected, result); return 1; } } diff --git a/src/test/math/math.c b/src/test/math/math.c new file mode 100644 index 0000000..174bcbd --- /dev/null +++ b/src/test/math/math.c @@ -0,0 +1,103 @@ +#include +#include "math.h" +#include "../print/print.h" + +int cAdd(int x, int y) { + return x + y; +} +int cSub(int x, int y) { + return x - y; +} +int cMul(int x, int y) { + return x * y; +} +int cModulo(int x, int y) { + return x % y; +} +int cNeg(int x) { + return -x; +} +int cId(int x) { + return x; +} +int cSelfMinus(int x) { + x = x - 1; + return x; +} + + +int math_testExpected(char* name, int x, int y, int expected, int result) { + if (expected == result) { + succPrefixTwo(name, x, y, expected, result); + return 0; + } else { + errPrefixTwo(name, x, y, expected, result); + return 1; + } +} + +int math_test(char* name, int (*correctFunction)(int, int), int (*testFunction)(int, int), int x, int y) { + int expected = correctFunction(x, y); + int result = testFunction(x, y); + return math_testExpected(name, x, y, expected, result); +} + +int math_testOneArg(char* name, int (*correctFunction)(int), int (*testFunction)(int), int x) { + int expected = correctFunction(x); + int result = testFunction(x); + if (expected == result) { + succPrefixOne(name, x, expected, result); + return 0; + } else { + errPrefixOne(name, x, expected, result); + return 1; + } +} + +int runMathTests() { + int failed = 0; + printf("\nAddition Tests \n"); + failed += math_test("add", cAdd, add, 0, 0); + failed += math_test("add", cAdd, add, 1, 1); + failed += math_test("add", cAdd, add, 2, 0); + failed += math_test("add", cAdd, add, 1, 5); + failed += math_test("add", cAdd, add, -1, -1); + + printf("\nSubtraction Tests \n"); + failed += math_test("sub", cSub, sub, 0, 0); + failed += math_test("sub", cSub, sub, 1, 1); + failed += math_test("sub", cSub, sub, 2, 0); + failed += math_test("sub", cSub, sub, 1, 5); + failed += math_test("sub", cSub, sub, -1, -1); + + printf("\nMultiplication Tests \n"); + failed += math_test("mul", cMul, mul, 0, 0); + failed += math_test("mul", cMul, mul, 1, 1); + failed += math_test("mul", cMul, mul, 2, 0); + failed += math_test("mul", cMul, mul, 1, 5); + failed += math_test("mul", cMul, mul, -1, -1); + + printf("\nModulo Tests \n"); + failed += math_test("modulo", cModulo, modulo, 1, 1); + failed += math_test("modulo", cModulo, modulo, 1, 5); + failed += math_test("modulo", cModulo, modulo, -1, -1); + failed += math_test("modulo", cModulo, modulo, 1337, 42); + + printf("\nNegative Tests\n"); + failed += math_testOneArg("neg", cNeg, neg, 0); + failed += math_testOneArg("neg", cNeg, neg, 1); + failed += math_testOneArg("neg", cNeg, neg, -1); + + printf("\nIdentity Tests\n"); + failed += math_testOneArg("id", cId, id, 0); + failed += math_testOneArg("id", cId, id, -1); + failed += math_testOneArg("id", cId, id, 15); + + printf("\nMisc Tests\n"); + failed += math_testOneArg("selfMinus", cSelfMinus, selfMinus, 5); + failed += math_testOneArg("selfMinus", cSelfMinus, selfMinus, 0); + failed += math_testOneArg("selfMinus", cSelfMinus, selfMinus, 100); + failed += math_testOneArg("selfMinus", cSelfMinus, selfMinus, -50); + + return failed; +} \ No newline at end of file diff --git a/src/test/testCode.h b/src/test/math/math.h similarity index 100% rename from src/test/testCode.h rename to src/test/math/math.h diff --git a/src/test/print/print.c b/src/test/print/print.c new file mode 100644 index 0000000..a8e43bb --- /dev/null +++ b/src/test/print/print.c @@ -0,0 +1,34 @@ +#include +#include "print.h" + +void succInfixTwo(char* name, int x, int y, int expected, int result) { + printf("\033[0;32mSUCCESS:\t%d %s %d\tGOT: %d\tExpected: %d\033[0;0m\n", x, name, y, result, expected); +} + +void errInfixTwo(char* name, int x, int y, int expected, int result) { + printf("\033[0;31mERROR:\t\t%d %s %d\tGOT: %d\tExpected: %d\033[0;0m\n", x, name, y, result, expected); +} + +void succ(char* name, int expected, int result) { + printf("\033[0;32mSUCCESS:\t%s:\tGOT: %d\tExpected: %d\033[0;0m\n", name, result, expected); +} + +void err(char* name, int expected, int result) { + printf("\033[0;31mERROR:\t\t%s:\tGOT: %d\tExpected: %d\033[0;0m\n", name, result, expected); +} + +void succPrefixOne(char* name, int x, int expected, int result) { + printf("\033[0;32mSUCCESS:\t%s(%d)\tGOT: %d\tExpected: %d\033[0;0m\n", name, x, result, expected); +} + +void errPrefixOne(char* name, int x, int expected, int result) { + printf("\033[0;31mERROR:\t\t%s(%d)\tGOT: %d\tExpected: %d\033[0;0m\n", name, x, result, expected); +} + +void succPrefixTwo(char* name, int x, int y, int expected, int result) { + printf("\033[0;32mSUCCESS:\t%s(%d, %d)\tGOT: %d\tExpected: %d\033[0;0m\n", name, x, y, result, expected); +} + +void errPrefixTwo(char* name, int x, int y, int expected, int result) { + printf("\033[0;31mERROR:\t\t%s(%d, %d)\tGOT: %d\tExpected: %d\033[0;0m\n", name, x, y, result, expected); +} \ No newline at end of file diff --git a/src/test/print/print.h b/src/test/print/print.h new file mode 100644 index 0000000..1d874cb --- /dev/null +++ b/src/test/print/print.h @@ -0,0 +1,10 @@ +void succ(char* name, int expected, int result); +void err(char* name, int expected, int result); + +void succPrefixOne(char* name, int x, int expected, int result); +void errPrefixOne(char* name, int x, int expected, int result); +void succPrefixTwo(char* name, int x, int y, int expected, int result); +void errPrefixTwo(char* name, int x, int y, int expected, int result); + +void succInfixTwo(char* name, int x, int y, int expected, int result); +void errInfixTwo(char* name, int x, int y, int expected, int result); \ No newline at end of file diff --git a/src/test/recursive/recursive.c b/src/test/recursive/recursive.c index db9febc..045a800 100644 --- a/src/test/recursive/recursive.c +++ b/src/test/recursive/recursive.c @@ -1,20 +1,13 @@ #include #include "recursive.h" - -void printRecSuccess(char* name, int x, int expected, int result) { - printf("SUCCESS:\t%s(%d)\tGOT: %d\tExpected: %d\n", name, x, result, expected); -} - -void printRecError(char* name, int x, int expected, int result) { - printf("ERROR:\t\t%s(%d)\tGOT: %d\tExpected: %d\n", name, x, result, expected); -} +#include "../print/print.h" int recursiveTest(char* name, int x, int expected, int result) { if (expected == result) { - printRecSuccess(name, x, expected, result); + succPrefixOne(name, x, expected, result); return 0; } else { - printRecError(name, x, expected, result); + errPrefixOne(name, x, expected, result); return 1; } } diff --git a/src/test/test.c b/src/test/test.c new file mode 100644 index 0000000..9b8797a --- /dev/null +++ b/src/test/test.c @@ -0,0 +1,31 @@ +#include +#include +#include "test.h" + +int main(){ + int failed = 0; + + // Tests for various math related functions + failed += runMathTests(); + + // Tests for passing arguments to functions + failed += runFunctionCallTests(); + + // Tests for recursive funtions + failed += runRecursiveTests(); + + // Tests for comparison expressions + failed += runComparisonTests(); + + // Tests for while loop + failed += runLoopTests(); + + printf("\n=== Failed Tests: %d\n", failed); + + if (failed > 0) { + return EXIT_FAILURE; + } else { + return EXIT_SUCCESS; + } +} + \ No newline at end of file diff --git a/src/test/tests.h b/src/test/test.h similarity index 72% rename from src/test/tests.h rename to src/test/test.h index 735466d..bfccf4b 100644 --- a/src/test/tests.h +++ b/src/test/test.h @@ -5,4 +5,5 @@ int runFunctionCallTests(); int runRecursiveTests(); int runComparisonTests(); -int runLoopTests(); \ No newline at end of file +int runLoopTests(); +int runMathTests(); \ No newline at end of file diff --git a/src/test/tests.k b/src/test/test.k similarity index 100% rename from src/test/tests.k rename to src/test/test.k diff --git a/src/test/testCode.c b/src/test/testCode.c deleted file mode 100644 index ee19542..0000000 --- a/src/test/testCode.c +++ /dev/null @@ -1,138 +0,0 @@ -#include -#include -#include "tests.h" -#include "testCode.h" - -int cAdd(int x, int y) { - return x + y; -} -int cSub(int x, int y) { - return x - y; -} -int cMul(int x, int y) { - return x * y; -} -int cModulo(int x, int y) { - return x % y; -} -int cNeg(int x) { - return -x; -} -int cId(int x) { - return x; -} -int cSelfMinus(int x) { - x = x - 1; - return x; -} - -void printSuccess(char* name, int x, int y, int expected, int result) { - printf("SUCCESS:\t%s(%d, %d)\tGOT: %d\tExpected: %d\n", name, x, y, result, expected); -} - -void printSuccessOneArg(char* name, int x, int expected, int result) { - printf("SUCCESS:\t%s(%d)\tGOT: %d\tExpected: %d\n", name, x, result, expected); -} - -void printError(char* name, int x, int y, int expected, int result) { - printf("ERROR:\t\t%s(%d, %d)\tGOT: %d\tExpected: %d\n", name, x, y, result, expected); -} - -void printErrorOneArg(char* name, int x, int expected, int result) { - printf("ERROR:\t\t%s(%d)\tGOT: %d\tExpected: %d\n", name, x, result, expected); -} - -int testExpected(char* name, int x, int y, int expected, int result) { - if (expected == result) { - printSuccess(name, x, y, expected, result); - return 0; - } else { - printError(name, x, y, expected, result); - return 1; - } -} - -int test(char* name, int (*correctFunction)(int, int), int (*testFunction)(int, int), int x, int y) { - int expected = correctFunction(x, y); - int result = testFunction(x, y); - return testExpected(name, x, y, expected, result); -} - -int testOneArg(char* name, int (*correctFunction)(int), int (*testFunction)(int), int x) { - int expected = correctFunction(x); - int result = testFunction(x); - if (expected == result) { - printSuccessOneArg(name, x, expected, result); - return 0; - } else { - printErrorOneArg(name, x, expected, result); - return 1; - } -} - -int main(){ - int failed = 0; - printf("\nAddition Tests \n"); - failed += test("add", cAdd, add, 0, 0); - failed += test("add", cAdd, add, 1, 1); - failed += test("add", cAdd, add, 2, 0); - failed += test("add", cAdd, add, 1, 5); - failed += test("add", cAdd, add, -1, -1); - - printf("\nSubtraction Tests \n"); - failed += test("sub", cSub, sub, 0, 0); - failed += test("sub", cSub, sub, 1, 1); - failed += test("sub", cSub, sub, 2, 0); - failed += test("sub", cSub, sub, 1, 5); - failed += test("sub", cSub, sub, -1, -1); - - printf("\nMultiplication Tests \n"); - failed += test("mul", cMul, mul, 0, 0); - failed += test("mul", cMul, mul, 1, 1); - failed += test("mul", cMul, mul, 2, 0); - failed += test("mul", cMul, mul, 1, 5); - failed += test("mul", cMul, mul, -1, -1); - - printf("\nModulo Tests \n"); - failed += test("modulo", cModulo, modulo, 1, 1); - failed += test("modulo", cModulo, modulo, 1, 5); - failed += test("modulo", cModulo, modulo, -1, -1); - failed += test("modulo", cModulo, modulo, 1337, 42); - - printf("\nNegative Tests\n"); - failed += testOneArg("neg", cNeg, neg, 0); - failed += testOneArg("neg", cNeg, neg, 1); - failed += testOneArg("neg", cNeg, neg, -1); - - printf("\nIdentity Tests\n"); - failed += testOneArg("id", cId, id, 0); - failed += testOneArg("id", cId, id, -1); - failed += testOneArg("id", cId, id, 15); - - printf("\nMisc Tests\n"); - failed += testOneArg("selfMinus", cSelfMinus, selfMinus, 5); - failed += testOneArg("selfMinus", cSelfMinus, selfMinus, 0); - failed += testOneArg("selfMinus", cSelfMinus, selfMinus, 100); - failed += testOneArg("selfMinus", cSelfMinus, selfMinus, -50); - - // Tests for passing arguments to functions - failed += runFunctionCallTests(); - - // Tests for recursive funtions - failed += runRecursiveTests(); - - // Tests for comparison expressions - failed += runComparisonTests(); - - // Tests for while loop - failed += runLoopTests(); - - printf("\n=== Failed Tests: %d\n", failed); - - if (failed > 0) { - return EXIT_FAILURE; - } else { - return EXIT_SUCCESS; - } -} - \ No newline at end of file