diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2020-03-03 17:05:28 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2020-03-03 17:05:28 -0500 |
commit | 2bf840925ba7781d16406ac28438f8bdc5f7d08c (patch) | |
tree | e70ca149eafd8eb4366a6ecf1edb1d4bed9e5aee /tests | |
parent | 0a3bb0c9ce4a2e96c7be3d0519aa0f5fa458c1af (diff) |
new, better test system
Diffstat (limited to 'tests')
-rw-r--r-- | tests/.gitignore | 2 | ||||
-rw-r--r-- | tests/arr.toc (renamed from tests/arr/arr.toc) | 0 | ||||
-rwxr-xr-x | tests/arr/test.sh | 3 | ||||
-rw-r--r-- | tests/arr2.toc (renamed from tests/arr2/arr2.toc) | 0 | ||||
-rwxr-xr-x | tests/arr2/test.sh | 3 | ||||
-rw-r--r-- | tests/arr2_expected (renamed from tests/arr/expected) | 0 | ||||
-rw-r--r-- | tests/arr3.toc (renamed from tests/arr3/arr3.toc) | 0 | ||||
-rw-r--r-- | tests/arr3/io.toc | 57 | ||||
-rwxr-xr-x | tests/arr3/test.sh | 3 | ||||
-rw-r--r-- | tests/arr3_expected (renamed from tests/arr3/expected) | 0 | ||||
-rw-r--r-- | tests/arr_expected (renamed from tests/arr2/expected) | 0 | ||||
-rw-r--r-- | tests/bf.toc (renamed from tests/bf/bf.toc) | 16 | ||||
-rwxr-xr-x | tests/bf/test.sh | 9 | ||||
-rw-r--r-- | tests/bf_expected | 2 | ||||
-rw-r--r-- | tests/bf_hw0 (renamed from tests/bf/hw0.bf) | 0 | ||||
-rw-r--r-- | tests/bf_hw1 (renamed from tests/bf/hw1.bf) | 0 | ||||
-rw-r--r-- | tests/foreign.toc (renamed from tests/foreign/foreign.toc) | 0 | ||||
-rwxr-xr-x | tests/foreign/test.sh | 3 | ||||
-rw-r--r-- | tests/foreign_expected (renamed from tests/foreign/expected) | 0 | ||||
-rw-r--r-- | tests/io.toc (renamed from tests/nms/io.toc) | 26 | ||||
-rw-r--r-- | tests/nms.toc (renamed from tests/nms/nms.toc) | 0 | ||||
-rwxr-xr-x | tests/nms/test.sh | 3 | ||||
-rw-r--r-- | tests/nms_expected (renamed from tests/nms/expected) | 0 | ||||
-rw-r--r-- | tests/params.toc (renamed from tests/params/params.toc) | 0 | ||||
-rwxr-xr-x | tests/params/test.sh | 3 | ||||
-rw-r--r-- | tests/params_expected (renamed from tests/params/expected) | 0 | ||||
-rwxr-xr-x | tests/test.sh | 28 |
27 files changed, 34 insertions, 124 deletions
diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..d0c3d10 --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1,2 @@ +got +out.c diff --git a/tests/arr/arr.toc b/tests/arr.toc index eee514a..eee514a 100644 --- a/tests/arr/arr.toc +++ b/tests/arr.toc diff --git a/tests/arr/test.sh b/tests/arr/test.sh deleted file mode 100755 index d50fb1a..0000000 --- a/tests/arr/test.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -./arr.bin > got || exit 1 -diff got expected || exit 1 diff --git a/tests/arr2/arr2.toc b/tests/arr2.toc index e016cf2..e016cf2 100644 --- a/tests/arr2/arr2.toc +++ b/tests/arr2.toc diff --git a/tests/arr2/test.sh b/tests/arr2/test.sh deleted file mode 100755 index 0c1a5b7..0000000 --- a/tests/arr2/test.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -./arr2.bin > got || exit 1 -diff got expected || exit 1 diff --git a/tests/arr/expected b/tests/arr2_expected index c0a3e45..c0a3e45 100644 --- a/tests/arr/expected +++ b/tests/arr2_expected diff --git a/tests/arr3/arr3.toc b/tests/arr3.toc index a6d1b14..a6d1b14 100644 --- a/tests/arr3/arr3.toc +++ b/tests/arr3.toc diff --git a/tests/arr3/io.toc b/tests/arr3/io.toc deleted file mode 100644 index 5a69269..0000000 --- a/tests/arr3/io.toc +++ /dev/null @@ -1,57 +0,0 @@ -get_type_with_size ::= fn(size :: i64) Type { - if size == 1 { i8 } - elif size == 2 { i16 } - elif size == 4 { i32 } - elif size == 8 { i64 } - else { f32 } -}; - -get_utype_with_size ::= fn(size :: i64) Type { - if size == 1 { u8 } - elif size == 2 { u16 } - elif size == 4 { u32 } - elif size == 8 { u64 } - else { f32 } -}; - -c_int ::= get_type_with_size(#builtin("sizeof int")); -c_size_t ::= get_utype_with_size(#builtin("sizeof size_t")); - -c_putchar ::= #foreign("putchar", "libc.so.6") fn(#C int) #C int; -toc_putchar ::= fn(x: char) { - c_putchar(x as c_int); -}; - -c_fwrite ::= #foreign("fwrite", "libc.so.6") fn(&u8, #C size_t, #C size_t, &u8) #C size_t; - -stdout_fwrite ::= fn(data: &u8, size: u64, nmemb: u64) { - c_fwrite(data, size as c_size_t, nmemb as c_size_t, #builtin("stdout")); -}; - -puts ::= fn(x: []char) { - stdout_fwrite(&x[0] as &u8, 1, x.len as u64); - toc_putchar('\n'); -}; - -puti ::= fn(x: int) { - if x < 0 { - toc_putchar('-'); - // NOTE: don't do x = -x; here to make sure I64_MIN works - } - if x == 0 { - toc_putchar('0'); - } else { - abs ::= fn(x: int) int { if x < 0 { -x } else { x } }; - scan_digit := 1000000000000000000; - started := false; - while scan_digit > 0 { - digit := abs((x / scan_digit) % 10); - if digit > 0 { started = true; } - if started { - toc_putchar((('0' as int) + digit) as char); - } - scan_digit /= 10; - } - } - toc_putchar('\n'); -}; diff --git a/tests/arr3/test.sh b/tests/arr3/test.sh deleted file mode 100755 index 135a6e0..0000000 --- a/tests/arr3/test.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -./arr3.bin > got || exit 1 -diff got expected || exit 1 diff --git a/tests/arr3/expected b/tests/arr3_expected index 101ae98..101ae98 100644 --- a/tests/arr3/expected +++ b/tests/arr3_expected diff --git a/tests/arr2/expected b/tests/arr_expected index c0a3e45..c0a3e45 100644 --- a/tests/arr2/expected +++ b/tests/arr_expected diff --git a/tests/bf/bf.toc b/tests/bf.toc index 2331467..e1f8781 100644 --- a/tests/bf/bf.toc +++ b/tests/bf.toc @@ -1,10 +1,11 @@ -getstdin ::= fn() []char { -#C("extern void *stdin; extern char *fgets(char *buf, size_t sz, void *f);"); +readfile ::= fn(filename: []char) []char { +#C("extern void *fopen(char *name, char const *mode); extern char *fgets(char *buf, size_t sz, void *f);"); + fp : &u8 = #C("fopen(&(((char *)filename.data)[0]), \"r\")"); contents : []char; contents_sz : int; contents_len : int; buffer : [1024]char; - while #C("fgets(buffer, 1024, stdin)") { + while #C("fgets(buffer, 1024, fp)") { buffer_len : int; while buffer[buffer_len] { buffer_len = buffer_len + 1; @@ -39,9 +40,9 @@ extern int printf(const char *fmt, ...); #C("printf(\"%ld\\n\", x);"); }; -main ::= fn() { +runfile ::= fn(filename: []char) { #C("extern int putchar(int c);"); - code := getstdin(); + code := readfile(filename); tape_sz := 3; tape := new(int, tape_sz); ptr := tape_sz / 2; @@ -112,3 +113,8 @@ main ::= fn() { del(tape); del(code); }; + +main ::= fn() { + runfile("bf_hw0"); + runfile("bf_hw1"); +}; diff --git a/tests/bf/test.sh b/tests/bf/test.sh deleted file mode 100755 index 8b76794..0000000 --- a/tests/bf/test.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/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/bf_expected b/tests/bf_expected new file mode 100644 index 0000000..f28b391 --- /dev/null +++ b/tests/bf_expected @@ -0,0 +1,2 @@ +Hello World! +Hello World! diff --git a/tests/bf/hw0.bf b/tests/bf_hw0 index ea2b641..ea2b641 100644 --- a/tests/bf/hw0.bf +++ b/tests/bf_hw0 diff --git a/tests/bf/hw1.bf b/tests/bf_hw1 index db5525f..db5525f 100644 --- a/tests/bf/hw1.bf +++ b/tests/bf_hw1 diff --git a/tests/foreign/foreign.toc b/tests/foreign.toc index d88a223..d88a223 100644 --- a/tests/foreign/foreign.toc +++ b/tests/foreign.toc diff --git a/tests/foreign/test.sh b/tests/foreign/test.sh deleted file mode 100755 index 1d6eb8f..0000000 --- a/tests/foreign/test.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -./foreign.bin > got || exit 1 -diff got expected || exit 1 diff --git a/tests/foreign/expected b/tests/foreign_expected index af5626b..af5626b 100644 --- a/tests/foreign/expected +++ b/tests/foreign_expected diff --git a/tests/nms/io.toc b/tests/io.toc index 5a69269..9e94147 100644 --- a/tests/nms/io.toc +++ b/tests/io.toc @@ -1,31 +1,13 @@ -get_type_with_size ::= fn(size :: i64) Type { - if size == 1 { i8 } - elif size == 2 { i16 } - elif size == 4 { i32 } - elif size == 8 { i64 } - else { f32 } -}; - -get_utype_with_size ::= fn(size :: i64) Type { - if size == 1 { u8 } - elif size == 2 { u16 } - elif size == 4 { u32 } - elif size == 8 { u64 } - else { f32 } -}; - -c_int ::= get_type_with_size(#builtin("sizeof int")); -c_size_t ::= get_utype_with_size(#builtin("sizeof size_t")); -c_putchar ::= #foreign("putchar", "libc.so.6") fn(#C int) #C int; +putchar ::= #foreign("putchar", "libc.so.6") fn(#C int) #C int; toc_putchar ::= fn(x: char) { - c_putchar(x as c_int); + putchar(x as #C int); }; -c_fwrite ::= #foreign("fwrite", "libc.so.6") fn(&u8, #C size_t, #C size_t, &u8) #C size_t; +fwrite ::= #foreign("fwrite", "libc.so.6") fn(#C &"void", #C size_t, #C size_t, #C &"void") #C size_t; stdout_fwrite ::= fn(data: &u8, size: u64, nmemb: u64) { - c_fwrite(data, size as c_size_t, nmemb as c_size_t, #builtin("stdout")); + fwrite(data, size as #C size_t, nmemb as #C size_t, #builtin("stdout")); }; puts ::= fn(x: []char) { diff --git a/tests/nms/nms.toc b/tests/nms.toc index 413da8d..413da8d 100644 --- a/tests/nms/nms.toc +++ b/tests/nms.toc diff --git a/tests/nms/test.sh b/tests/nms/test.sh deleted file mode 100755 index 8f1a481..0000000 --- a/tests/nms/test.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -./nms.bin > got || exit 1 -diff got expected || exit 1 diff --git a/tests/nms/expected b/tests/nms_expected index d26c123..d26c123 100644 --- a/tests/nms/expected +++ b/tests/nms_expected diff --git a/tests/params/params.toc b/tests/params.toc index 4d37cf1..4d37cf1 100644 --- a/tests/params/params.toc +++ b/tests/params.toc diff --git a/tests/params/test.sh b/tests/params/test.sh deleted file mode 100755 index 582bdec..0000000 --- a/tests/params/test.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -./params.bin > got || exit 1 -diff got expected || exit 1 diff --git a/tests/params/expected b/tests/params_expected index 082b847..082b847 100644 --- a/tests/params/expected +++ b/tests/params_expected diff --git a/tests/test.sh b/tests/test.sh index 6d534d3..1d47dff 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -1,10 +1,11 @@ #!/bin/bash -DIR=$(pwd)/$(dirname $0) -TOC=$DIR/../toc +STARTPWD=$(pwd) +cd $(pwd)/$(dirname $0) +TOC=../toc CFLAGS="-g -Wno-parentheses-equality" echo $$ -compile() { +compile_c() { EXTRA_FLAGS="" if [ "$CC" = "gcc -O0 -g" ]; then EXTRA_FLAGS="-Wno-builtin-declaration-mismatch" @@ -13,24 +14,24 @@ compile() { elif [ "$CC" = "tcc" ]; then EXTRA_FLAGS="-w" fi - $CC $CFLAGS $EXTRA_FLAGS -o $DIR/$1/$1.bin $DIR/$1/$1.c || exit 1 + $CC $CFLAGS $EXTRA_FLAGS -o a.out out.c || exit 1 } do_tests() { - cd "$DIR/$1" - valgrind -q --exit-on-first-error=yes --error-exitcode=1 $TOC "$DIR/$1/$1.toc" -o "$DIR/$1/$1.c" >/dev/null || exit 1 + valgrind -q --exit-on-first-error=yes --error-exitcode=1 $TOC "$1.toc" -o out.c || exit 1 for CC in "gcc -O0 -g" "tcc" "clang -O3 -s"; do printf "Running test $1 with C compiler $CC... " - compile "$1" - ./test.sh || { printf "\x1b[91mfailed!\x1b[0m\n"; exit 1; } - printf '\x1b[92mpassed!\x1b[0m\n' + compile_c "$1" + ./a.out > got + if diff "$1"_expected got; then + printf '\x1b[92mpassed!\x1b[0m\n' + else + printf '\x1b[91mfailed!\x1b[0m\n' + exit 1 + fi done - cd $STARTPWD - } -STARTPWD="$(pwd)" - do_tests bf do_tests arr do_tests arr2 @@ -38,3 +39,4 @@ do_tests arr3 do_tests foreign do_tests params do_tests nms +rm got a.out out.c |