From c24d807e24b4ea7dafe5872db618163fc683cdca Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Sun, 15 Mar 2020 15:05:39 -0400 Subject: removed where --- tests/printf.toc | 37 +++++++++++++++++++++++++++++++++++++ tests/printf_expected | 3 +++ tests/test.sh | 4 ++-- tests/where.toc | 30 ------------------------------ tests/where_expected | 1 - 5 files changed, 42 insertions(+), 33 deletions(-) create mode 100644 tests/printf.toc create mode 100644 tests/printf_expected delete mode 100644 tests/where.toc delete mode 100644 tests/where_expected (limited to 'tests') diff --git a/tests/printf.toc b/tests/printf.toc new file mode 100644 index 0000000..e030af4 --- /dev/null +++ b/tests/printf.toc @@ -0,0 +1,37 @@ +printf ::= #foreign("printf","libc.so.6") fn(#C &"const char", #C ..) #C int; + + +// NOTE: this doesn't work (e.g. "%%%") +tprintf_valid ::= fn(fmt :: []char, nargs: int) bool { + if fmt[fmt.len-1] != '\0' { + return false; + } + count := 0; + for x, i := fmt { + if x == '%' { + if i == fmt.len-1 { + count += 1; + } elif fmt[i+1] != '%' { + count += 1; + } else { + count -= 1; + } + } + } + count == nargs +}; + + +tprintf ::= fn(fmt :: []char, args: ..) { + #if !tprintf_valid(fmt, args.len) { + #error "Invalid printf format"; + } + f := fmt; + printf(&f[0], args); +}; + +main ::= fn() { + tprintf("%d %d%%\n\0", 3, 4); + tprintf("%d %d %d%%\n\0", 3, 4, 5); + tprintf("Hello!\n\0"); +}; diff --git a/tests/printf_expected b/tests/printf_expected new file mode 100644 index 0000000..a5e51ec --- /dev/null +++ b/tests/printf_expected @@ -0,0 +1,3 @@ +3 4% +3 4 5% +Hello! diff --git a/tests/test.sh b/tests/test.sh index 31ccb6b..d30de7c 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -8,7 +8,7 @@ foreign params nms varargs -where +printf misc' STARTPWD=$(pwd) @@ -22,7 +22,7 @@ compile_c() { if [ "$CC" = "gcc -O0 -g" ]; then EXTRA_FLAGS="-Wno-builtin-declaration-mismatch" elif [ "$CC" = "clang -O3 -s" ]; then - EXTRA_FLAGS="-Wno-builtin-requires-header" + EXTRA_FLAGS="-Wno-builtin-requires-header -Wno-format-security" elif [ "$CC" = "tcc" ]; then EXTRA_FLAGS="-w" fi diff --git a/tests/where.toc b/tests/where.toc deleted file mode 100644 index 4223656..0000000 --- a/tests/where.toc +++ /dev/null @@ -1,30 +0,0 @@ -printf ::= #foreign("printf","libc.so.6") fn(#C &"const char", #C ..) #C int; - -tprintf_valid ::= fn(fmt :: []char, nargs: int) bool { - if fmt[fmt.len-1] != '\0' { - return false; - } - count := 0; - for x, i := fmt { - if x == '%' { - if i == fmt.len-1 { - count += 1; - } elif fmt[i+1] != '%' { - count += 1; - } else { - count -= 1; - } - } - } - count == nargs -}; - - -tprintf ::= fn(fmt :: []char, args: ..) where tprintf_valid(fmt, args.len) { - f := fmt; - printf(&f[0], args); -}; - -main ::= fn() { - tprintf("%d %d%%\n\0", 3, 4); -}; diff --git a/tests/where_expected b/tests/where_expected deleted file mode 100644 index 622c000..0000000 --- a/tests/where_expected +++ /dev/null @@ -1 +0,0 @@ -3 4% -- cgit v1.2.3