Added error counting to tests. Tests return ERROR_FAILURE when not all test were successful

This commit is contained in:
Marvin Kaiser
2019-12-17 16:13:04 +01:00
parent 3710c1f903
commit d4dfe27bc0
3 changed files with 58 additions and 44 deletions

View File

@@ -9,24 +9,28 @@ void printArgError(char* name, int expected, int result) {
printf("ERROR:\t\t%s(<argumentList>)\tGOT: %d\tExpected: %d\n", name, result, expected); printf("ERROR:\t\t%s(<argumentList>)\tGOT: %d\tExpected: %d\n", name, result, expected);
} }
void argumentTest(char* name, int expected, int result) { int argumentTest(char* name, int expected, int result) {
if (expected == result) { if (expected == result) {
printArgSuccess(name, expected, result); printArgSuccess(name, expected, result);
return 0;
} else { } else {
printArgError(name, expected, result); printArgError(name, expected, result);
return 1;
} }
} }
void runFunctionCallTests () { int runFunctionCallTests () {
int failed = 0;
printf("\nFunctionCallTests Tests \n"); printf("\nFunctionCallTests Tests \n");
argumentTest("arg1", 1, arg1(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); failed += argumentTest("arg1", 1, arg1(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
argumentTest("arg2", 2, arg2(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));
argumentTest("arg3", 3, arg3(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));
argumentTest("arg4", 4, arg4(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));
argumentTest("arg5", 5, arg5(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));
argumentTest("arg6", 6, arg6(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));
argumentTest("arg7", 7, arg7(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));
argumentTest("arg8", 8, arg8(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));
argumentTest("arg9", 9, arg9(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));
argumentTest("arg10", 10, arg10(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));
return failed;
} }

View File

@@ -1,4 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "tests.h" #include "tests.h"
#include "testCode.h" #include "testCode.h"
@@ -37,72 +38,81 @@ 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); printf("ERROR:\t\t%s(%d)\tGOT: %d\tExpected: %d\n", name, x, result, expected);
} }
void testExpected(char* name, int x, int y, int expected, int result) { int testExpected(char* name, int x, int y, int expected, int result) {
if (expected == result) { if (expected == result) {
printSuccess(name, x, y, expected, result); printSuccess(name, x, y, expected, result);
return 0;
} else { } else {
printError(name, x, y, expected, result); printError(name, x, y, expected, result);
return 1;
} }
} }
void test(char* name, int (*correctFunction)(int, int), int (*testFunction)(int, int), int x, int y) { int test(char* name, int (*correctFunction)(int, int), int (*testFunction)(int, int), int x, int y) {
int expected = correctFunction(x, y); int expected = correctFunction(x, y);
int result = testFunction(x, y); int result = testFunction(x, y);
testExpected(name, x, y, expected, result); return testExpected(name, x, y, expected, result);
} }
void testOneArg(char* name, int (*correctFunction)(int), int (*testFunction)(int), int x) { int testOneArg(char* name, int (*correctFunction)(int), int (*testFunction)(int), int x) {
int expected = correctFunction(x); int expected = correctFunction(x);
int result = testFunction(x); int result = testFunction(x);
if (expected == result) { if (expected == result) {
printSuccessOneArg(name, x, expected, result); printSuccessOneArg(name, x, expected, result);
return 0;
} else { } else {
printErrorOneArg(name, x, expected, result); printErrorOneArg(name, x, expected, result);
return 1;
} }
} }
int main(){ int main(){
int failed = 0;
printf("\nAddition Tests \n"); printf("\nAddition Tests \n");
test("add", cAdd, add, 0, 0); failed += test("add", cAdd, add, 0, 0);
test("add", cAdd, add, 1, 1); failed += test("add", cAdd, add, 1, 1);
test("add", cAdd, add, 2, 0); failed += test("add", cAdd, add, 2, 0);
test("add", cAdd, add, 1, 5); failed += test("add", cAdd, add, 1, 5);
test("add", cAdd, add, -1, -1); failed += test("add", cAdd, add, -1, -1);
printf("\nSubtraction Tests \n"); printf("\nSubtraction Tests \n");
test("sub", cSub, sub, 0, 0); failed += test("sub", cSub, sub, 0, 0);
test("sub", cSub, sub, 1, 1); failed += test("sub", cSub, sub, 1, 1);
test("sub", cSub, sub, 2, 0); failed += test("sub", cSub, sub, 2, 0);
test("sub", cSub, sub, 1, 5); failed += test("sub", cSub, sub, 1, 5);
test("sub", cSub, sub, -1, -1); failed += test("sub", cSub, sub, -1, -1);
printf("\nMultiplication Tests \n"); printf("\nMultiplication Tests \n");
test("mul", cMul, mul, 0, 0); failed += test("mul", cMul, mul, 0, 0);
test("mul", cMul, mul, 1, 1); failed += test("mul", cMul, mul, 1, 1);
test("mul", cMul, mul, 2, 0); failed += test("mul", cMul, mul, 2, 0);
test("mul", cMul, mul, 1, 5); failed += test("mul", cMul, mul, 1, 5);
test("mul", cMul, mul, -1, -1); failed += test("mul", cMul, mul, -1, -1);
printf("\nModulo Tests \n"); printf("\nModulo Tests \n");
test("modulo", cModulo, modulo, 1, 1); failed += test("modulo", cModulo, modulo, 1, 1);
test("modulo", cModulo, modulo, 1, 5); failed += test("modulo", cModulo, modulo, 1, 5);
test("modulo", cModulo, modulo, -1, -1); failed += test("modulo", cModulo, modulo, -1, -1);
test("modulo", cModulo, modulo, 1337, 42); failed += test("modulo", cModulo, modulo, 1337, 42);
printf("\nNegative Tests\n"); printf("\nNegative Tests\n");
testOneArg("neg", cNeg, neg, 0); failed += testOneArg("neg", cNeg, neg, 0);
testOneArg("neg", cNeg, neg, 1); failed += testOneArg("neg", cNeg, neg, 1);
testOneArg("neg", cNeg, neg, -1); failed += testOneArg("neg", cNeg, neg, -1);
printf("\nIdentity Tests\n"); printf("\nIdentity Tests\n");
testOneArg("id", cId, id, 0); failed += testOneArg("id", cId, id, 0);
testOneArg("id", cId, id, -1); failed += testOneArg("id", cId, id, -1);
testOneArg("id", cId, id, 15); failed += testOneArg("id", cId, id, 15);
printf("\nFunction Argument Tests\n"); printf("\nFunction Argument Tests\n");
runFunctionCallTests(); failed += runFunctionCallTests();
printf("\n=== Failed Tests: %d\n", failed);
return 0; if (failed > 0) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
} }

View File

@@ -2,4 +2,4 @@
Alle Test Module Alle Test Module
*/ */
void runFunctionCallTests (); int runFunctionCallTests ();