cleaned up test suit

This commit is contained in:
Marvin Kaiser
2020-01-14 11:47:04 +01:00
parent be43cc9a77
commit d2d50f6d4f
13 changed files with 218 additions and 205 deletions

View File

@@ -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

View File

@@ -1,20 +1,13 @@
#include <stdio.h>
#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;
}
}

View File

@@ -1,20 +1,13 @@
#include <stdio.h>
#include "functionCall.h"
void printArgSuccess(char* name, int expected, int result) {
printf("SUCCESS:\t%s(<argumentList>)\tGOT: %d\tExpected: %d\n", name, result, expected);
}
void printArgError(char* name, int expected, int result) {
printf("ERROR:\t\t%s(<argumentList>)\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;
}

View File

@@ -1,20 +1,13 @@
#include <stdio.h>
#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;
}
}

103
src/test/math/math.c Normal file
View File

@@ -0,0 +1,103 @@
#include <stdio.h>
#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;
}

34
src/test/print/print.c Normal file
View File

@@ -0,0 +1,34 @@
#include <stdio.h>
#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);
}

10
src/test/print/print.h Normal file
View File

@@ -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);

View File

@@ -1,20 +1,13 @@
#include <stdio.h>
#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;
}
}

31
src/test/test.c Normal file
View File

@@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdlib.h>
#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;
}
}

View File

@@ -5,4 +5,5 @@
int runFunctionCallTests();
int runRecursiveTests();
int runComparisonTests();
int runLoopTests();
int runLoopTests();
int runMathTests();

View File

@@ -1,138 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#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;
}
}