implement tests

This commit is contained in:
2020-01-20 19:33:07 +01:00
parent 295bcaec8a
commit e5baad62e6
4 changed files with 49 additions and 18 deletions

View File

@@ -1,5 +1,4 @@
#include <stdio.h> #include <stdio.h>
#include <stdbool.h>
#include "comparison.h" #include "comparison.h"
#include "../print/print.h" #include "../print/print.h"
@@ -13,7 +12,17 @@ int comparisonTest(char* name, int x, int y, int expected, int result) {
} }
} }
int boolTest(char* name, bool a, bool b, bool expected, bool result) { int boolTestOne(char* name, bool x, bool expected, bool result) {
if (expected == result) {
bool_succPrefixOne(name, x, expected, result);
return 0;
} else {
bool_errPrefixOne(name, x, expected, result);
return 1;
}
}
int boolTestTwo(char* name, bool a, bool b, bool expected, bool result) {
if (expected == result) { if (expected == result) {
bool_succInfixTwo(name, a, b, expected, result); bool_succInfixTwo(name, a, b, expected, result);
return 0; return 0;
@@ -55,8 +64,16 @@ void runComparisonTests() {
comparisonTest(">=", 0, 1, 0, gte(0, 1)); comparisonTest(">=", 0, 1, 0, gte(0, 1));
comparisonTest(">=", 0, 0, 1, gte(0, 0)); comparisonTest(">=", 0, 0, 1, gte(0, 0));
boolTest("&&", true, true, true, and(true, true)); boolTestTwo("&&", true, true, true, and(true, true));
boolTest("&&", true, false, false, and(true, false)); boolTestTwo("&&", true, false, false, and(true, false));
boolTest("&&", false, true, false, and(false, true)); boolTestTwo("&&", false, true, false, and(false, true));
boolTest("&&", false, false, false, and(false, false)); boolTestTwo("&&", false, false, false, and(false, false));
boolTestTwo("||", true, true, true, or(true, true));
boolTestTwo("||", true, false, true, or(true, false));
boolTestTwo("||", false, true, true, or(false, true));
boolTestTwo("||", false, false, false, or(false, false));
boolTestOne("!", true, false, not(true));
boolTestOne("!", false, true, not(false));
} }

View File

@@ -1,7 +1,13 @@
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include "print.h" #include "print.h"
char* printBool(bool a) {
if (a == true) {
return "true";
}
return "false";
}
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(); 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);
@@ -42,20 +48,22 @@ 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); printf("\033[0;31mERROR:\t\t%s(%d, %d)\tGOT: %d\tExpected: %d\033[0;0m\n", name, x, y, result, expected);
} }
void bool_succPrefixTwo(char* name, bool a, bool b, bool expected, bool result) { void bool_succPrefixOne(char* name, bool x, bool expected, bool result) {
incSuccess(); incSuccess();
printf("\033[0;32mSUCCESS:\t%s(%s, %s)\tGOT: %s\tExpected: %s\033[0;0m\n", name, printBool(a), printBool(b), printBool(result), printBool(expected)); printf("\033[0;32mSUCCESS:\t%s%s\tGOT: %s\tExpected: %s\033[0;0m\n", name, printBool(x), printBool(result), printBool(expected));
} }
void bool_errPrefixTwo(char* name, bool a, bool b, bool expected, bool result) { void bool_errPrefixOne(char* name, bool x, bool expected, bool result) {
incFailure(); incFailure();
printf("\033[0;32mSUCCESS:\t%s(%s, %s)\tGOT: %s\tExpected: %s\033[0;0m\n", name, printBool(a), printBool(b), printBool(result), printBool(expected)); printf("\033[0;31mERROR:\t\t%s%s\tGOT: %s\tExpected: %s\033[0;0m\n", name, printBool(x), printBool(result), printBool(expected));
} }
char* printBool(bool a) { void bool_succInfixTwo(char* name, bool a, bool b, bool expected, bool result) {
if (a == true) { incSuccess();
return "true"; printf("\033[0;32mSUCCESS:\t%s %s %s\tGOT: %s\tExpected: %s\033[0;0m\n", printBool(a), name, printBool(b), printBool(result), printBool(expected));
} }
return "false";
void bool_errInfixTwo(char* name, bool a, bool b, bool expected, bool result) {
incFailure();
printf("\033[0;31mERROR:\t\t%s %s %s\tGOT: %s\tExpected: %s\033[0;0m\n", printBool(a), name, printBool(b), printBool(result), printBool(expected));
} }

View File

@@ -1,3 +1,5 @@
#include <stdbool.h>
void incSuccess(); void incSuccess();
void incFailure(); void incFailure();
@@ -6,11 +8,15 @@ void err(char* name, int expected, int result);
void succPrefixOne(char* name, int x, 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 errPrefixOne(char* name, int x, int expected, int result);
void succPrefixTwo(char* name, int x, int y, 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 errPrefixTwo(char* name, int x, int y, int expected, int result);
void succInfixTwo(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); void errInfixTwo(char* name, int x, int y, int expected, int result);
void bool_succPrefixOne(char* name, bool x, bool expected, bool result);
void bool_errPrefixOne(char* name, bool x, bool expected, bool result);
void bool_succInfixTwo(char* name, bool x, bool y, bool expected, bool result); void bool_succInfixTwo(char* name, bool x, bool y, bool expected, bool result);
void bool_errInfixTwo(char* name, bool x, bool y, bool expected, bool result); void bool_errInfixTwo(char* name, bool x, bool y, bool expected, bool result);

View File

@@ -171,7 +171,7 @@ function or(a, b) {
} }
function not(a) { function not(a) {
return (!a); return !a;
} }
add(1, 1); add(1, 1);