added boolean operators

This commit is contained in:
Marvin Kaiser
2020-01-20 16:01:25 +01:00
parent 1186bb4550
commit d27df13ec1
11 changed files with 126 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdbool.h>
#include "comparison.h"
#include "../print/print.h"
@@ -12,6 +13,16 @@ int comparisonTest(char* name, int x, int y, int expected, int result) {
}
}
int boolTest(char* name, bool a, bool b, bool expected, bool result) {
if (expected == result) {
bool_succInfixTwo(name, a, b, expected, result);
return 0;
} else {
bool_errInfixTwo(name, a, b, expected, result);
return 1;
}
}
void runComparisonTests() {
printf("\nComparison Tests \n");
comparisonTest("==", 1, 1, 1, eq(1, 1));
@@ -43,4 +54,9 @@ void runComparisonTests() {
comparisonTest(">=", 1, 0, 1, gte(1, 0));
comparisonTest(">=", 0, 1, 0, gte(0, 1));
comparisonTest(">=", 0, 0, 1, gte(0, 0));
boolTest("&&", true, true, true, and(true, true));
boolTest("&&", true, false, false, and(true, false));
boolTest("&&", false, true, false, and(false, true));
boolTest("&&", false, false, false, and(false, false));
}

View File

@@ -1,6 +1,12 @@
#include <stdbool.h>
int eq(int x, int y);
int neq(int x, int y);
int lt(int x, int y);
int lte(int x, int y);
int gt(int x, int y);
int gte(int x, int y);
bool and(bool a, bool b);
bool or(bool a, bool b);
bool not(bool a);

View File

@@ -1,3 +1,4 @@
#include <stdbool.h>
#include <stdio.h>
#include "print.h"
@@ -39,4 +40,22 @@ void succPrefixTwo(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);
}
void bool_succPrefixTwo(char* name, bool a, bool b, bool expected, bool result) {
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));
}
void bool_errPrefixTwo(char* name, bool a, bool b, bool expected, bool result) {
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));
}
char* printBool(bool a) {
if (a == true) {
return "true";
}
return "false";
}

View File

@@ -10,4 +10,7 @@ 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);
void errInfixTwo(char* name, int x, int y, int expected, int 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);

View File

@@ -162,4 +162,16 @@ function myFor(end) {
return x;
}
function and(a, b) {
return (a && b);
}
function or(a, b) {
return (a || b);
}
function not(a) {
return (!a);
}
add(1, 1);