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);
}
void argumentTest(char* name, int expected, int result) {
int argumentTest(char* name, int expected, int result) {
if (expected == result) {
printArgSuccess(name, expected, result);
return 0;
} else {
printArgError(name, expected, result);
return 1;
}
}
void runFunctionCallTests () {
int runFunctionCallTests () {
int failed = 0;
printf("\nFunctionCallTests Tests \n");
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));
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));
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));
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));
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("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));
return failed;
}

View File

@@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include "tests.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);
}
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) {
printSuccess(name, x, y, expected, result);
return 0;
} else {
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 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 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");
test("add", cAdd, add, 0, 0);
test("add", cAdd, add, 1, 1);
test("add", cAdd, add, 2, 0);
test("add", cAdd, add, 1, 5);
test("add", cAdd, add, -1, -1);
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");
test("sub", cSub, sub, 0, 0);
test("sub", cSub, sub, 1, 1);
test("sub", cSub, sub, 2, 0);
test("sub", cSub, sub, 1, 5);
test("sub", cSub, sub, -1, -1);
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");
test("mul", cMul, mul, 0, 0);
test("mul", cMul, mul, 1, 1);
test("mul", cMul, mul, 2, 0);
test("mul", cMul, mul, 1, 5);
test("mul", cMul, mul, -1, -1);
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");
test("modulo", cModulo, modulo, 1, 1);
test("modulo", cModulo, modulo, 1, 5);
test("modulo", cModulo, modulo, -1, -1);
test("modulo", cModulo, modulo, 1337, 42);
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");
testOneArg("neg", cNeg, neg, 0);
testOneArg("neg", cNeg, neg, 1);
testOneArg("neg", cNeg, neg, -1);
failed += testOneArg("neg", cNeg, neg, 0);
failed += testOneArg("neg", cNeg, neg, 1);
failed += testOneArg("neg", cNeg, neg, -1);
printf("\nIdentity Tests\n");
testOneArg("id", cId, id, 0);
testOneArg("id", cId, id, -1);
testOneArg("id", cId, id, 15);
failed += testOneArg("id", cId, id, 0);
failed += testOneArg("id", cId, id, -1);
failed += testOneArg("id", cId, id, 15);
printf("\nFunction Argument Tests\n");
runFunctionCallTests();
failed += runFunctionCallTests();
printf("\n=== Failed Tests: %d\n", failed);
if (failed > 0) {
return EXIT_FAILURE;
}
return 0;
return EXIT_SUCCESS;
}

View File

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