summaryrefslogtreecommitdiff
path: root/test-lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'test-lib.c')
-rw-r--r--test-lib.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/test-lib.c b/test-lib.c
new file mode 100644
index 0000000..ca3c433
--- /dev/null
+++ b/test-lib.c
@@ -0,0 +1,39 @@
+#include "test-lib.h"
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+static const char *GREEN = "\033[0;32m";
+static const char *RED = "\033[0;31m";
+static const char *ESCAPE = "\e[0m";
+
+static void printPass() { printf("%s\tPassed\t%s\n", GREEN, ESCAPE); }
+
+static void printFail(char *msg) {
+ printf("%s\tFailed\n\t%s%s\n", RED, msg, ESCAPE);
+}
+
+int syncTest(char *testName, char *errMsg, syncTestHandler_t callback) {
+ int status;
+ pid_t pid;
+ printf("Running %s...\n", testName);
+ pid = fork();
+ if(pid == 0) {
+ return callback();
+ }
+ else {
+ waitpid(pid, &status, 0);
+ if(status == 0) {
+ printPass();
+ }
+ else {
+ printFail(errMsg);
+ }
+ return status;
+ }
+}
+
+void asyncTest(char *testName, char *errMsg, asyncTestHandler_t callback) {
+ printf("Running %s\n", testName);
+ callback(printPass, printFail, errMsg);
+}