From 6ed3d421006f7d56c5d96fbaf1b971bb3561d1ed Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Wed, 23 Oct 2019 21:44:03 -0400 Subject: added a test --- tests/bf.toc | 86 -------------------------------------------- tests/bf/bf.toc | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/bf/hw0.bf | 1 + tests/bf/hw1.bf | 2 ++ tests/bf/test.sh | 9 +++++ tests/test.sh | 29 +++++++++++++++ 6 files changed, 149 insertions(+), 86 deletions(-) delete mode 100644 tests/bf.toc create mode 100644 tests/bf/bf.toc create mode 100644 tests/bf/hw0.bf create mode 100644 tests/bf/hw1.bf create mode 100755 tests/bf/test.sh create mode 100755 tests/test.sh (limited to 'tests') diff --git a/tests/bf.toc b/tests/bf.toc deleted file mode 100644 index d0ca413..0000000 --- a/tests/bf.toc +++ /dev/null @@ -1,86 +0,0 @@ -getstdin @= fn() []char { - contents : []char; - contents_sz : int; - contents_len : int; - buffer : [1024]char; - while #C("fgets(buffer, 1024, stdin)") { - buffer_len : int; - while buffer[buffer_len] { - buffer_len = buffer_len + 1; - } - if contents_sz < buffer_len { - old_contents := contents; - contents_sz = 2*contents_sz + 1024; - contents = new(char, contents_sz); - i := 0; - while i < contents_len { - contents[i] = old_contents[i]; - i = i + 1; - } - del(old_contents); - } - i := 0; - while i < buffer_len { - contents[contents_len] = buffer[i]; - contents_len = contents_len + 1; - i = i + 1; - } - } - contents[contents_len] = 0 as char; - contents -}; - -main @= fn() { - code := getstdin(); - tape_sz := 100; - tape := new(int, tape_sz); - ptr := tape_sz / 2; - i := 0; - while code[i] { - if code[i] == '+' { - tape[ptr] = tape[ptr]+1; - } elif code[i] == '-' { - tape[ptr] = tape[ptr]-1; - } elif code[i] == '>' { - ptr = ptr + 1; - if ptr >= tape_sz { - // TODO - } - } elif code[i] == '<' { - ptr = ptr - 1; - if ptr < 0 { - // TODO - } - } elif code[i] == '[' { - if !tape[ptr] { - // jump past matching ] - level := 0; - i = i + 1; - while level >= 0 { - level = level + (if code[i] == '[' { 1 } elif code[i] == ']' { -1 } else {0}); - i = i + 1; - } - i = i + 1; - } - } elif code[i] == ']' { - if tape[ptr] { - // jump to right after matching [ - level := 0; - i = i - 1; - while level <= 0 { - level = level + (if code[i] == '[' { 1 } elif code[i] == ']' { -1 } else {0}); - i = i - 1; - } - i = i + 1; - } - } elif code[i] == '.' { - c := tape[ptr] as char; - #C("putc(c, stdout)"); - } elif code[i] == ',' { - c : char = #C("getc(stdin)"); - tape[ptr] = c as int; - } - i = i + 1; - } - -}; \ No newline at end of file diff --git a/tests/bf/bf.toc b/tests/bf/bf.toc new file mode 100644 index 0000000..ef0c086 --- /dev/null +++ b/tests/bf/bf.toc @@ -0,0 +1,108 @@ +getstdin @= fn() []char { + contents : []char; + contents_sz : int; + contents_len : int; + buffer : [1024]char; + while #C("fgets(buffer, 1024, stdin)") { + buffer_len : int; + while buffer[buffer_len] { + buffer_len = buffer_len + 1; + } + if contents_sz < contents_len + buffer_len { + old_contents := contents; + contents_sz = 2*contents_sz + 1024; + contents = new(char, contents_sz); + i := 0; + while i < contents_len { + contents[i] = old_contents[i]; + i = i + 1; + } + del(old_contents); + } + i := 0; + while i < buffer_len { + contents[contents_len] = buffer[i]; + contents_len = contents_len + 1; + i = i + 1; + } + } + contents[contents_len] = 0 as char; + contents +}; + +puti @= fn(x: int) { + #C("printf(\"%ld\\n\", x)"); +}; + +main @= fn() { + code := getstdin(); + tape_sz := 3; + tape := new(int, tape_sz); + ptr := tape_sz / 2; + i := 0; + while code[i] { + // puti(ptr); + // puti(tape_sz); + if code[i] == '+' { + tape[ptr] = tape[ptr]+1; + } elif code[i] == '-' { + tape[ptr] = tape[ptr]-1; + } elif code[i] == '>' { + ptr = ptr + 1; + if ptr >= tape_sz { + // extend to the right + newtape := new(int, 2*tape_sz); + j := 0; + while j < tape_sz { + newtape[j] = tape[j]; + j = j + 1; + } + tape_sz = tape_sz * 2; + del(tape); + tape = newtape; + } + } elif code[i] == '<' { + ptr = ptr - 1; + if ptr < 0 { + // extend to the left + newtape := new(int, 2*tape_sz); + j := 0; + while j < tape_sz { + newtape[j+tape_sz] = tape[j]; + j = j + 1; + } + tape_sz = tape_sz * 2; + del(tape); + tape = newtape; + ptr = ptr + tape_sz; + } + } elif code[i] == '[' { + if !tape[ptr] { + // jump to matching ] + level := 0; + while level >= 0 { + i = i + 1; + level = level + (if code[i] == '[' { 1 } elif code[i] == ']' { -1 } else {0}); + } + } + } elif code[i] == ']' { + if tape[ptr] { + // jump to matching [ + level := 0; + while level <= 0 { + i = i - 1; + level = level + (if code[i] == '[' { 1 } elif code[i] == ']' { -1 } else {0}); + } + } + } elif code[i] == '.' { + c := tape[ptr] as char; + #C("putc(c, stdout)"); + } elif code[i] == ',' { + // Input doesn't really work, because you + // need to send an EOF to end the program. + } + i = i + 1; + } + del(tape); + del(code); +}; \ No newline at end of file diff --git a/tests/bf/hw0.bf b/tests/bf/hw0.bf new file mode 100644 index 0000000..ea2b641 --- /dev/null +++ b/tests/bf/hw0.bf @@ -0,0 +1 @@ +++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++. \ No newline at end of file diff --git a/tests/bf/hw1.bf b/tests/bf/hw1.bf new file mode 100644 index 0000000..db5525f --- /dev/null +++ b/tests/bf/hw1.bf @@ -0,0 +1,2 @@ +>++++++++[-<+++++++++>]<.>>+>-[+]++>++>+++[>[->+++<<+++>]<<]>-----.>-> ++++..+++.>-.<<+[>[+>+]>>]<--------------.>>.+++.------.--------.>+.>+. \ No newline at end of file diff --git a/tests/bf/test.sh b/tests/bf/test.sh new file mode 100755 index 0000000..8b76794 --- /dev/null +++ b/tests/bf/test.sh @@ -0,0 +1,9 @@ +#!/bin/sh +if [ "$(cat hw0.bf | ./bf.bin)" != "Hello World!" ]; then + echo "hello world 0 failed." + exit 1 +fi +if [ "$(cat hw1.bf | ./bf.bin)" != "Hello World!" ]; then + echo "hello world 1 failed." + exit 1 +fi diff --git a/tests/test.sh b/tests/test.sh new file mode 100755 index 0000000..470251e --- /dev/null +++ b/tests/test.sh @@ -0,0 +1,29 @@ +#!/bin/sh +DIR=$(dirname $0) +TOC=$DIR/../toc +CFLAGS="-g -Wno-parentheses-equality" +if [ "$COMPILERS" = "" ]; then + COMPILERS="gcc tcc clang" +fi + +echo $$ + +compile() { + $TOC $DIR/$1/$1.toc -o $DIR/$1/$1.c > /dev/null || exit 1 + $CC $EXTRA_CFLAGS $CFLAGS -o $DIR/$1/$1.bin $DIR/$1/$1.c || exit 1 +} + +STARTPWD="$(pwd)" + +for CC in $COMPILERS; do + + for EXTRA_CFLAGS in "-O0 -g" "-O3 -s"; do + echo "Running tests with C compiler $CC and flags $EXTRA_CFLAGS." + printf "bf... " + compile bf + cd $DIR/bf + ./test.sh || exit 1 + echo "passed!" + cd $STARTPWD + done +done -- cgit v1.2.3