Added automatic counting of successes/failures in tests

This commit is contained in:
Marvin Kaiser
2020-01-14 12:15:20 +01:00
parent 1ceba78059
commit 3ca9fe4de1
9 changed files with 119 additions and 108 deletions

View File

@@ -12,38 +12,35 @@ int comparisonTest(char* name, int x, int y, int expected, int result) {
} }
} }
int runComparisonTests() { void runComparisonTests() {
printf("\nComparison Tests \n"); printf("\nComparison Tests \n");
int failed = 0; comparisonTest("==", 1, 1, 1, eq(1, 1));
failed += comparisonTest("==", 1, 1, 1, eq(1, 1)); comparisonTest("==", 1, 0, 0, eq(1, 0));
failed += comparisonTest("==", 1, 0, 0, eq(1, 0)); comparisonTest("==", 0, 1, 0, eq(0, 1));
failed += comparisonTest("==", 0, 1, 0, eq(0, 1)); comparisonTest("==", 0, 0, 1, eq(0, 0));
failed += comparisonTest("==", 0, 0, 1, eq(0, 0));
failed += comparisonTest("!=", 1, 1, 0, neq(1, 1)); comparisonTest("!=", 1, 1, 0, neq(1, 1));
failed += comparisonTest("!=", 1, 0, 1, neq(1, 0)); comparisonTest("!=", 1, 0, 1, neq(1, 0));
failed += comparisonTest("!=", 0, 1, 1, neq(0, 1)); comparisonTest("!=", 0, 1, 1, neq(0, 1));
failed += comparisonTest("!=", 0, 0, 0, neq(0, 0)); comparisonTest("!=", 0, 0, 0, neq(0, 0));
failed += comparisonTest("<", 1, 1, 0, lt(1, 1)); comparisonTest("<", 1, 1, 0, lt(1, 1));
failed += comparisonTest("<", 1, 0, 0, lt(1, 0)); comparisonTest("<", 1, 0, 0, lt(1, 0));
failed += comparisonTest("<", 0, 1, 1, lt(0, 1)); comparisonTest("<", 0, 1, 1, lt(0, 1));
failed += comparisonTest("<", 0, 0, 0, lt(0, 0)); comparisonTest("<", 0, 0, 0, lt(0, 0));
failed += comparisonTest("<=", 1, 1, 1, lte(1, 1)); comparisonTest("<=", 1, 1, 1, lte(1, 1));
failed += comparisonTest("<=", 1, 0, 0, lte(1, 0)); comparisonTest("<=", 1, 0, 0, lte(1, 0));
failed += comparisonTest("<=", 0, 1, 1, lte(0, 1)); comparisonTest("<=", 0, 1, 1, lte(0, 1));
failed += comparisonTest("<=", 0, 0, 1, lte(0, 0)); comparisonTest("<=", 0, 0, 1, lte(0, 0));
failed += comparisonTest(">", 1, 1, 0, gt(1, 1)); comparisonTest(">", 1, 1, 0, gt(1, 1));
failed += comparisonTest(">", 1, 0, 1, gt(1, 0)); comparisonTest(">", 1, 0, 1, gt(1, 0));
failed += comparisonTest(">", 0, 1, 0, gt(0, 1)); comparisonTest(">", 0, 1, 0, gt(0, 1));
failed += comparisonTest(">", 0, 0, 0, gt(0, 0)); comparisonTest(">", 0, 0, 0, gt(0, 0));
failed += comparisonTest(">=", 1, 1, 1, gte(1, 1)); comparisonTest(">=", 1, 1, 1, gte(1, 1));
failed += comparisonTest(">=", 1, 0, 1, gte(1, 0)); comparisonTest(">=", 1, 0, 1, gte(1, 0));
failed += comparisonTest(">=", 0, 1, 0, gte(0, 1)); comparisonTest(">=", 0, 1, 0, gte(0, 1));
failed += comparisonTest(">=", 0, 0, 1, gte(0, 0)); comparisonTest(">=", 0, 0, 1, gte(0, 0));
return failed;
} }

View File

@@ -13,29 +13,27 @@ int argumentTest(char* name, int expected, int result) {
} }
int runFunctionCallTests () { int runFunctionCallTests () {
int failed = 0;
printf("\nFunction Call Tests \n"); printf("\nFunction Call Tests \n");
// Checks that parameters are correctly passed from gcc to functions // Checks that parameters are correctly passed from gcc to functions
failed += argumentTest("arg1(...args)", 1, arg1(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); 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)); 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)); 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)); 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)); 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)); 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)); 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)); 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)); 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)); 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 // Checks that parameters are correctly passed from klang to functions
failed += argumentTest("get1(...args)", 1, get1()); argumentTest("get1(...args)", 1, get1());
failed += argumentTest("get2(...args)", 2, get2()); argumentTest("get2(...args)", 2, get2());
failed += argumentTest("get3(...args)", 3, get3()); argumentTest("get3(...args)", 3, get3());
failed += argumentTest("get4(...args)", 4, get4()); argumentTest("get4(...args)", 4, get4());
failed += argumentTest("get5(...args)", 5, get5()); argumentTest("get5(...args)", 5, get5());
failed += argumentTest("get6(...args)", 6, get6()); argumentTest("get6(...args)", 6, get6());
failed += argumentTest("get7(...args)", 7, get7()); argumentTest("get7(...args)", 7, get7());
failed += argumentTest("get8(...args)", 8, get8()); argumentTest("get8(...args)", 8, get8());
failed += argumentTest("get9(...args)", 9, get9()); argumentTest("get9(...args)", 9, get9());
failed += argumentTest("get10(...args)", 10, get10()); argumentTest("get10(...args)", 10, get10());
return failed;
} }

View File

@@ -14,12 +14,10 @@ int loopTest(char* name, int x, int expected, int result) {
int runLoopTests() { int runLoopTests() {
printf("\nLoop Tests \n"); printf("\nLoop Tests \n");
int failed = 0;
failed += loopTest("while", 5, 5, myWhile(5)); loopTest("while", 5, 5, myWhile(5));
failed += loopTest("doWhile", 0, 1, myDoWhile(0)); loopTest("doWhile", 0, 1, myDoWhile(0));
failed += loopTest("doWhile", 1, 1, myDoWhile(1)); loopTest("doWhile", 1, 1, myDoWhile(1));
failed += loopTest("for", 5, 5, myFor(5)); loopTest("for", 5, 5, myFor(5));
failed += loopTest("for", 0, 0, myFor(0)); loopTest("for", 0, 0, myFor(0));
return failed;
} }

View File

@@ -55,49 +55,46 @@ int math_testOneArg(char* name, int (*correctFunction)(int), int (*testFunction)
} }
int runMathTests() { int runMathTests() {
int failed = 0; printf("\nAddition Tests \n");
printf("\nAddition Tests \n"); math_test("add", cAdd, add, 0, 0);
failed += math_test("add", cAdd, add, 0, 0); math_test("add", cAdd, add, 1, 1);
failed += math_test("add", cAdd, add, 1, 1); math_test("add", cAdd, add, 2, 0);
failed += math_test("add", cAdd, add, 2, 0); math_test("add", cAdd, add, 1, 5);
failed += math_test("add", cAdd, add, 1, 5); math_test("add", cAdd, add, -1, -1);
failed += math_test("add", cAdd, add, -1, -1);
printf("\nSubtraction Tests \n"); printf("\nSubtraction Tests \n");
failed += math_test("sub", cSub, sub, 0, 0); math_test("sub", cSub, sub, 0, 0);
failed += math_test("sub", cSub, sub, 1, 1); math_test("sub", cSub, sub, 1, 1);
failed += math_test("sub", cSub, sub, 2, 0); math_test("sub", cSub, sub, 2, 0);
failed += math_test("sub", cSub, sub, 1, 5); math_test("sub", cSub, sub, 1, 5);
failed += math_test("sub", cSub, sub, -1, -1); math_test("sub", cSub, sub, -1, -1);
printf("\nMultiplication Tests \n"); printf("\nMultiplication Tests \n");
failed += math_test("mul", cMul, mul, 0, 0); math_test("mul", cMul, mul, 0, 0);
failed += math_test("mul", cMul, mul, 1, 1); math_test("mul", cMul, mul, 1, 1);
failed += math_test("mul", cMul, mul, 2, 0); math_test("mul", cMul, mul, 2, 0);
failed += math_test("mul", cMul, mul, 1, 5); math_test("mul", cMul, mul, 1, 5);
failed += math_test("mul", cMul, mul, -1, -1); math_test("mul", cMul, mul, -1, -1);
printf("\nModulo Tests \n"); printf("\nModulo Tests \n");
failed += math_test("modulo", cModulo, modulo, 1, 1); math_test("modulo", cModulo, modulo, 1, 1);
failed += math_test("modulo", cModulo, modulo, 1, 5); math_test("modulo", cModulo, modulo, 1, 5);
failed += math_test("modulo", cModulo, modulo, -1, -1); math_test("modulo", cModulo, modulo, -1, -1);
failed += math_test("modulo", cModulo, modulo, 1337, 42); math_test("modulo", cModulo, modulo, 1337, 42);
printf("\nNegative Tests\n"); printf("\nNegative Tests\n");
failed += math_testOneArg("neg", cNeg, neg, 0); math_testOneArg("neg", cNeg, neg, 0);
failed += math_testOneArg("neg", cNeg, neg, 1); math_testOneArg("neg", cNeg, neg, 1);
failed += math_testOneArg("neg", cNeg, neg, -1); math_testOneArg("neg", cNeg, neg, -1);
printf("\nIdentity Tests\n"); printf("\nIdentity Tests\n");
failed += math_testOneArg("id", cId, id, 0); math_testOneArg("id", cId, id, 0);
failed += math_testOneArg("id", cId, id, -1); math_testOneArg("id", cId, id, -1);
failed += math_testOneArg("id", cId, id, 15); math_testOneArg("id", cId, id, 15);
printf("\nMisc Tests\n"); printf("\nMisc Tests\n");
failed += math_testOneArg("selfMinus", cSelfMinus, selfMinus, 5); math_testOneArg("selfMinus", cSelfMinus, selfMinus, 5);
failed += math_testOneArg("selfMinus", cSelfMinus, selfMinus, 0); math_testOneArg("selfMinus", cSelfMinus, selfMinus, 0);
failed += math_testOneArg("selfMinus", cSelfMinus, selfMinus, 100); math_testOneArg("selfMinus", cSelfMinus, selfMinus, 100);
failed += math_testOneArg("selfMinus", cSelfMinus, selfMinus, -50); math_testOneArg("selfMinus", cSelfMinus, selfMinus, -50);
return failed;
} }

View File

@@ -2,33 +2,41 @@
#include "print.h" #include "print.h"
void succInfixTwo(char* name, int x, int y, int expected, int result) { void succInfixTwo(char* name, int x, int y, int expected, int result) {
incSuccess();
printf("\033[0;32mSUCCESS:\t%d %s %d\tGOT: %d\tExpected: %d\033[0;0m\n", x, name, y, result, expected); 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) { void errInfixTwo(char* name, int x, int y, int expected, int result) {
incFailure();
printf("\033[0;31mERROR:\t\t%d %s %d\tGOT: %d\tExpected: %d\033[0;0m\n", x, name, y, result, expected); 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) { void succ(char* name, int expected, int result) {
incSuccess();
printf("\033[0;32mSUCCESS:\t%s:\tGOT: %d\tExpected: %d\033[0;0m\n", name, result, expected); 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) { void err(char* name, int expected, int result) {
incFailure();
printf("\033[0;31mERROR:\t\t%s:\tGOT: %d\tExpected: %d\033[0;0m\n", name, result, expected); 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) { void succPrefixOne(char* name, int x, int expected, int result) {
incSuccess();
printf("\033[0;32mSUCCESS:\t%s(%d)\tGOT: %d\tExpected: %d\033[0;0m\n", name, x, result, expected); 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) { void errPrefixOne(char* name, int x, int expected, int result) {
incFailure();
printf("\033[0;31mERROR:\t\t%s(%d)\tGOT: %d\tExpected: %d\033[0;0m\n", name, x, result, expected); 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) { void succPrefixTwo(char* name, int x, int y, int expected, int result) {
incSuccess();
printf("\033[0;32mSUCCESS:\t%s(%d, %d)\tGOT: %d\tExpected: %d\033[0;0m\n", name, x, y, result, expected); 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) { void errPrefixTwo(char* name, int x, int y, int expected, int result) {
incFailure();
printf("\033[0;31mERROR:\t\t%s(%d, %d)\tGOT: %d\tExpected: %d\033[0;0m\n", name, x, y, result, expected); printf("\033[0;31mERROR:\t\t%s(%d, %d)\tGOT: %d\tExpected: %d\033[0;0m\n", name, x, y, result, expected);
} }

View File

@@ -1,3 +1,6 @@
void incSuccess();
void incFailure();
void succ(char* name, int expected, int result); void succ(char* name, int expected, int result);
void err(char* name, int expected, int result); void err(char* name, int expected, int result);

View File

@@ -14,8 +14,6 @@ int recursiveTest(char* name, int x, int expected, int result) {
int runRecursiveTests() { int runRecursiveTests() {
printf("\nRecursive Tests \n"); printf("\nRecursive Tests \n");
int failed = 0;
failed += recursiveTest("fac", 5, 120, fac(5));
return failed; recursiveTest("fac", 5, 120, fac(5));
} }

View File

@@ -2,29 +2,41 @@
#include <stdlib.h> #include <stdlib.h>
#include "test.h" #include "test.h"
int main(){ int successes, failures = 0;
int failed = 0;
void incSuccess() {
successes += 1;
}
void incFailure() {
failures += 1;
}
int main(){
// Tests for various math related functions // Tests for various math related functions
failed += runMathTests(); runMathTests();
// Tests for passing arguments to functions // Tests for passing arguments to functions
failed += runFunctionCallTests(); runFunctionCallTests();
// Tests for recursive funtions // Tests for recursive funtions
failed += runRecursiveTests(); runRecursiveTests();
// Tests for comparison expressions // Tests for comparison expressions
failed += runComparisonTests(); runComparisonTests();
// Tests for while loop // Tests for while loop
failed += runLoopTests(); runLoopTests();
printf("\n=== Failed Tests: %d\n", failed); printf("\n%d tests in total\n", successes + failures);
if (failed > 0) { if (failures > 0) {
printf("\033[0;31m%d tests were successful\033[0;0m\n", successes);
printf("\033[0;31m%d tests failed\033[0;0m\n", failures);
return EXIT_FAILURE; return EXIT_FAILURE;
} else { } else {
printf("\033[0;32m%d tests were successful\033[0;0m\n", successes);
printf("\033[0;32m%d tests failed\033[0;0m\n", failures);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
} }

View File

@@ -2,8 +2,8 @@
Alle Test Module Alle Test Module
*/ */
int runFunctionCallTests(); void runFunctionCallTests();
int runRecursiveTests(); void runRecursiveTests();
int runComparisonTests(); void runComparisonTests();
int runLoopTests(); void runLoopTests();
int runMathTests(); void runMathTests();