summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--Makefile14
-rw-r--r--README.md3
-rw-r--r--run_tests.c83
4 files changed, 100 insertions, 3 deletions
diff --git a/.gitignore b/.gitignore
index ffee194..106638d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
skullfuck
run_tests
-run_tests.c
+hello
+rot13
diff --git a/Makefile b/Makefile
index fdff5d7..c05ff2d 100644
--- a/Makefile
+++ b/Makefile
@@ -9,5 +9,19 @@ all: skullfuck
skullfuck: skullfuck.c
$(CC) $(CFLAGS) $(MACROS) -o skullfuck skullfuck.c
+run_tests: run_tests.c
+ $(CC) $(CFLAGS) -o run_tests run_tests.c
+
+hello: test/hello_world.bf
+ ./skullfuck -o hello test/hello_world.bf
+
+rot13: test/rot13.bf
+ ./skullfuck -o rot13 test/rot13.bf
+
+test: run_tests hello rot13
+ ./run_tests
+
clean: skullfuck
rm -f skullfuck
+
+.PHONY: clean test
diff --git a/README.md b/README.md
index e08c1d1..eccb13e 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
Skullfuck
=========
-Skullfuck is a dead simple optimizing compiler for Brainfuck, contained in a single C source file. It outputs binaries targeting x86_64 and i686 Linux.
+Skullfuck is an optimizing compiler for Brainfuck, contained in a single C source file. It outputs binaries targeting x86_64 and i686 Linux.
It depends on GNU Binutils, or any compatible implementation of `as` and `ld`.
@@ -12,6 +12,5 @@ Skullfuck is free software, licensed under the [GNU General Public License.](htt
TODO
----
* *BSD targeted binaries.
-* Automate test compilation/running.
* Static analysis to warn when undefined behavior has been detected.
* Optimize mutally cancelling operations.
diff --git a/run_tests.c b/run_tests.c
new file mode 100644
index 0000000..efabe25
--- /dev/null
+++ b/run_tests.c
@@ -0,0 +1,83 @@
+/* Copyright (C) 2017 Jakob Kreuze, All Rights Reserved.
+
+ Skullfuck is free software: you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by the
+ Free Software Foundation, either version 3 of the License, or (at
+ your option) any later version.
+
+ Skullfuck is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Skullfuck. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <sys/types.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define EXIT_SUCCESS 0
+#define EXIT_FAILURE 1
+
+
+/* Exits with a failing status code if a test binary does not exist. */
+static void assert_test_existence(void) {
+ if (access("hello", F_OK) == -1) {
+ fprintf(stderr, "Hello world binary does not exist.\n");
+ exit(EXIT_FAILURE);
+ } else if (access("rot13", F_OK) == -1) {
+ fprintf(stderr, "rot13 binary does not exist.\n");
+ exit(EXIT_FAILURE);
+ }
+}
+
+
+/* Prints `msg` to error output and quits with a failing status code. */
+static void panic(char *msg) {
+ fprintf(stderr, msg);
+ fprintf(stderr, "Tests failed! Terminating!");
+ exit(EXIT_FAILURE);
+}
+
+
+/* Tests the hello world binary. */
+static void test_hello_world(void) {
+ int pipefd[2];
+ char *buf;
+ pid_t pid;
+
+ if (pipe(pipefd) == -1) {
+ perror("pipe");
+ exit(EXIT_FAILURE);
+ }
+
+ if ((pid = fork()) == -1) {
+ perror("fork");
+ exit(EXIT_FAILURE);
+ }
+
+ if (pid == 0) {
+ dup2(pipefd[1], 1);
+ close(pipefd[0]);
+ execl("hello", "hello", NULL);
+ } else {
+ buf = malloc(0x100);
+ dup2(pipefd[0], 0);
+ close(pipefd[1]);
+ fgets(buf, 0x100, stdin);
+ if (!strcmp(buf, "Hello World!"))
+ panic("Hello World binary did not properly output text.\n");
+ free(buf);
+ }
+}
+
+
+int main(int argc, char **argv) {
+ assert_test_existence();
+ test_hello_world();
+ printf("All tests passed.\n");
+}