feat: Add build infra + tests for brainfuck interpreter

This commit is contained in:
overflowerror 2024-05-04 19:16:35 +02:00
parent 9b1d59808e
commit 2d0d7691d7
14 changed files with 107 additions and 0 deletions

1
brainfuck/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
bf

34
brainfuck/Makefile Normal file
View file

@ -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

2
brainfuck/obj/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
*.o
*.d

View file

@ -0,0 +1 @@
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

View file

@ -0,0 +1 @@
Hello World!

View file

@ -0,0 +1 @@
+[>>>->-[>->----<<<]>>]>.---.>+..+++.>>.<.>>---.<<<.+++.------.<-.>>+.

View file

@ -0,0 +1 @@
hello, world!

View file

@ -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"

48
brainfuck/test/tests.sh Executable file
View file

@ -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"