summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Van Doorn <vandoorn.nick@gmail.com>2019-04-08 14:43:03 -0700
committerNick Van Doorn <vandoorn.nick@gmail.com>2019-04-08 14:43:03 -0700
commit919fd254d87512e96ee15757dab645852c3ef166 (patch)
tree4e4e63283ae33b750562b8ef86b0b8442f4ed11f
Init commit
-rw-r--r--.gitignore2
-rw-r--r--Makefile18
-rw-r--r--example.c13
-rw-r--r--readme.md0
-rw-r--r--test-lib.c39
-rw-r--r--test-lib.h10
6 files changed, 82 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..25064ae
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+*.o
+example
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..b3dfb04
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,18 @@
+# lift life
+# http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/makefile.4
+CC=gcc
+CFLAGS=-I./src
+DEPS= test-lib.h
+OBJ = test-lib.o
+MAIN_OBJ = example.o
+
+%.o: %.c $(DEPS)
+ $(CC) -c -o $@ $< $(CFLAGS)
+
+example: $(OBJ) $(MAIN_OBJ)
+ $(CC) -o $@ $^ $(CFLAGS)
+
+.PHONY: clean
+clean:
+ rm -rf *.o
+
diff --git a/example.c b/example.c
new file mode 100644
index 0000000..07b9a3b
--- /dev/null
+++ b/example.c
@@ -0,0 +1,13 @@
+#include "test-lib.h"
+#include <stdio.h>
+
+int badTest() {
+ int* ptr = (int*)0;
+ *ptr = 5;
+ return 0;
+}
+
+int main() {
+ syncTest("badTest", "it segfaulted", badTest);
+ return 0;
+}
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/readme.md
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);
+}
diff --git a/test-lib.h b/test-lib.h
new file mode 100644
index 0000000..20f772b
--- /dev/null
+++ b/test-lib.h
@@ -0,0 +1,10 @@
+#ifndef TEST_LIB_H
+#define TEST_LIB_H
+
+typedef void (*asyncTestHandler_t)(void (*pass)(), void (*fail)(char* msg), char* msg);
+typedef int (*syncTestHandler_t)();
+
+int syncTest(char *testName, char *errMsg, int (*testCallback)());
+void asyncTest(char *testName, char *errMsg, asyncTestHandler_t callback);
+
+#endif