From 2d0d7691d70b8a8c8296fbff7d7e9a7edc9858ff Mon Sep 17 00:00:00 2001 From: overflowerror Date: Sat, 4 May 2024 19:16:35 +0200 Subject: [PATCH] feat: Add build infra + tests for brainfuck interpreter --- brainfuck/.gitignore | 1 + brainfuck/Makefile | 34 +++++++++++++ brainfuck/obj/.gitignore | 2 + brainfuck/{ => src}/main.c | 0 .../test/cases/001-simple-hello-world.in | 1 + .../test/cases/001-simple-hello-world.stderr | 0 .../test/cases/001-simple-hello-world.stdin | 0 .../test/cases/001-simple-hello-world.stdout | 1 + .../test/cases/002-minimal-hello-world.in | 1 + .../test/cases/002-minimal-hello-world.stderr | 0 .../test/cases/002-minimal-hello-world.stdin | 0 .../test/cases/002-minimal-hello-world.stdout | 1 + brainfuck/test/generate-results.sh | 18 +++++++ brainfuck/test/tests.sh | 48 +++++++++++++++++++ 14 files changed, 107 insertions(+) create mode 100644 brainfuck/.gitignore create mode 100644 brainfuck/Makefile create mode 100644 brainfuck/obj/.gitignore rename brainfuck/{ => src}/main.c (100%) create mode 100644 brainfuck/test/cases/001-simple-hello-world.in create mode 100644 brainfuck/test/cases/001-simple-hello-world.stderr create mode 100644 brainfuck/test/cases/001-simple-hello-world.stdin create mode 100644 brainfuck/test/cases/001-simple-hello-world.stdout create mode 100644 brainfuck/test/cases/002-minimal-hello-world.in create mode 100644 brainfuck/test/cases/002-minimal-hello-world.stderr create mode 100644 brainfuck/test/cases/002-minimal-hello-world.stdin create mode 100644 brainfuck/test/cases/002-minimal-hello-world.stdout create mode 100755 brainfuck/test/generate-results.sh create mode 100755 brainfuck/test/tests.sh diff --git a/brainfuck/.gitignore b/brainfuck/.gitignore new file mode 100644 index 0000000..af393a2 --- /dev/null +++ b/brainfuck/.gitignore @@ -0,0 +1 @@ +bf \ No newline at end of file diff --git a/brainfuck/Makefile b/brainfuck/Makefile new file mode 100644 index 0000000..648ad53 --- /dev/null +++ b/brainfuck/Makefile @@ -0,0 +1,34 @@ +CC = gcc +CFLAGS = -std=c11 -D_POSIX_C_SOURCE=200809L -Wall -Wpedantic -g +LD = gcc +LDFLAGS = +AR = ar +ARFLAGS = rcs + + +OBJS = obj/main.o +DEPS = $(OBJS:%.o=%.d) + +-include $(DEPS) + +all: bf test + +obj/%.o: src/%.c obj + $(CC) $(CFLAGS) -c -o $@ $< + + +bf: $(OBJS) + $(LD) $(LDFLAGS) -o $@ $^ + + +test: bf + ./test/tests.sh "./bf" + + +FORCE: ; + +clean: + @echo "Cleaning up" + @rm -f obj/*.o + @rm -f obj/*.d + @rm bf diff --git a/brainfuck/obj/.gitignore b/brainfuck/obj/.gitignore new file mode 100644 index 0000000..6142305 --- /dev/null +++ b/brainfuck/obj/.gitignore @@ -0,0 +1,2 @@ +*.o +*.d diff --git a/brainfuck/main.c b/brainfuck/src/main.c similarity index 100% rename from brainfuck/main.c rename to brainfuck/src/main.c diff --git a/brainfuck/test/cases/001-simple-hello-world.in b/brainfuck/test/cases/001-simple-hello-world.in new file mode 100644 index 0000000..e0a8381 --- /dev/null +++ b/brainfuck/test/cases/001-simple-hello-world.in @@ -0,0 +1 @@ +++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>. \ No newline at end of file diff --git a/brainfuck/test/cases/001-simple-hello-world.stderr b/brainfuck/test/cases/001-simple-hello-world.stderr new file mode 100644 index 0000000..e69de29 diff --git a/brainfuck/test/cases/001-simple-hello-world.stdin b/brainfuck/test/cases/001-simple-hello-world.stdin new file mode 100644 index 0000000..e69de29 diff --git a/brainfuck/test/cases/001-simple-hello-world.stdout b/brainfuck/test/cases/001-simple-hello-world.stdout new file mode 100644 index 0000000..980a0d5 --- /dev/null +++ b/brainfuck/test/cases/001-simple-hello-world.stdout @@ -0,0 +1 @@ +Hello World! diff --git a/brainfuck/test/cases/002-minimal-hello-world.in b/brainfuck/test/cases/002-minimal-hello-world.in new file mode 100644 index 0000000..971ae91 --- /dev/null +++ b/brainfuck/test/cases/002-minimal-hello-world.in @@ -0,0 +1 @@ ++[>>>->-[>->----<<<]>>]>.---.>+..+++.>>.<.>>---.<<<.+++.------.<-.>>+. \ No newline at end of file diff --git a/brainfuck/test/cases/002-minimal-hello-world.stderr b/brainfuck/test/cases/002-minimal-hello-world.stderr new file mode 100644 index 0000000..e69de29 diff --git a/brainfuck/test/cases/002-minimal-hello-world.stdin b/brainfuck/test/cases/002-minimal-hello-world.stdin new file mode 100644 index 0000000..e69de29 diff --git a/brainfuck/test/cases/002-minimal-hello-world.stdout b/brainfuck/test/cases/002-minimal-hello-world.stdout new file mode 100644 index 0000000..30f51a3 --- /dev/null +++ b/brainfuck/test/cases/002-minimal-hello-world.stdout @@ -0,0 +1 @@ +hello, world! \ No newline at end of file diff --git a/brainfuck/test/generate-results.sh b/brainfuck/test/generate-results.sh new file mode 100755 index 0000000..55f6f51 --- /dev/null +++ b/brainfuck/test/generate-results.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +tmpfile1="/tmp/$$-1.tmp" +tmpfile2="/tmp/$$-2.tmp" + +ls cases/*.in test/cases/*.in | sed -E 's/.in$//g' | while read test; do + brainfuck < "$test.in" > $tmpfile1 2> $tmpfile2 + if test ! -e "$test.stdout"; then + cp "$tmpfile1" "$test.stdout" + fi + + if test ! -e "$test.stderr"; then + cp "$tmpfile2" "$test.stderr" + fi +done + +rm "$tmpfile1" +rm "$tmpfile2" \ No newline at end of file diff --git a/brainfuck/test/tests.sh b/brainfuck/test/tests.sh new file mode 100755 index 0000000..c21100f --- /dev/null +++ b/brainfuck/test/tests.sh @@ -0,0 +1,48 @@ +#/bin/sh + +executable="$1" + +tmpfile1="/tmp/$$-1.tmp" +tmpfile2="/tmp/$$-2.tmp" +resultsfile="/tmp/$$.results" + +touch "$resultsfile" + +run_testcase() { + test="$1" + echo "Test: $test" + + if "$executable" "$test.in" < "$test.stdin" > "$tmpfile1" 2> "$tmpfile2" ; then + if diff -q "$test.stderr" "$tmpfile2" > /dev/null; then + if diff -q "$test.stdout" "$tmpfile1" > /dev/null; then + printf " \033[32msuccess\033[0m\n" + echo "$test: success" >> "$resultsfile" + else + printf " \033[31mfailed with stdout diff\033[0m\n" + echo "$test: fail" >> "$resultsfile" + fi + else + printf " \033[31mfailed with stderr diff\033[0m\n" + echo "$test: fail" >> "$resultsfile" + fi + else + printf " \033[31mfailed with error\033[0m\n" + echo "$test: fail" >> "$resultsfile" + fi + + rm "$tmpfile1"; + rm "$tmpfile2"; +} + +ls cases/*.in test/cases/*.in | sed -E 's/.in$//g' | while read test; do + run_testcase "$test" +done + +echo +echo +echo "$(cat "$resultsfile" | wc -l) tests in total" +echo "$(grep ": fail" "$resultsfile" | wc -l) tests failed" +echo "$(grep ": success" "$resultsfile" | wc -l) tests succeeded" +echo + +rm "$resultsfile"