From aad2f1246bc675ca53839958533112c753f06762 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sun, 18 Nov 2007 20:00:46 +0100 Subject: [PATCH] Added unit tests for io and tokenizer modules. --- judge/Makefile | 8 ++++ judge/io.t | 7 ++++ judge/test-tok.c | 39 ++++++++++++++----- judge/token.t | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 9 deletions(-) create mode 100644 judge/io.t create mode 100644 judge/token.t diff --git a/judge/Makefile b/judge/Makefile index c9ddb07..ccb9838 100644 --- a/judge/Makefile +++ b/judge/Makefile @@ -13,8 +13,16 @@ judge-tok: LDLIBS+=-lm judge-shuff: judge-shuff.o $(JLIB) filter-cmt: filter-cmt.o $(JLIB) +tests: $(addsuffix .test,io token) +io.test: test-io +token.test: test-tok + +%.test: %.t tester + ./tester $< + clean:: rm -f `find . -name "*~" -or -name "*.[oa]" -or -name "\#*\#" -or -name TAGS -or -name core` rm -f test-io test-tok judge-tok filter-cmt + rm -rf tmp .PHONY: all clean distclean diff --git a/judge/io.t b/judge/io.t new file mode 100644 index 0000000..ed9f46a --- /dev/null +++ b/judge/io.t @@ -0,0 +1,7 @@ +# Test cases for io.c + +Run: ./test-io +In: abc + def +Out: abc + def diff --git a/judge/test-tok.c b/judge/test-tok.c index 92303bb..8927e05 100644 --- a/judge/test-tok.c +++ b/judge/test-tok.c @@ -1,26 +1,47 @@ #include +#include +#include #include "judge.h" -int main(void) +int main(int argc, char **argv) { struct stream *i = sopen_fd("stdin", 0); struct tokenizer t; tok_init(&t, i); - // t.maxtoken = 1000; - t.flags = TF_REPORT_LINES; + + int opt, verbose = 0; + while ((opt = getopt(argc, argv, "lsv")) >= 0) + switch (opt) + { + case 'l': + t.flags = TF_REPORT_LINES; + break; + case 's': + t.maxsize = 16; + break; + case 'v': + verbose++; + break; + default: + return 42; + } + char *tok; while (tok = get_token(&t)) { printf("<%s>", tok); #define T(f, type, fmt) { type x; if (to_##f(&t, &x)) printf(" = " #f " " fmt, x); } - T(int, int, "%d"); - T(uint, unsigned int, "%u"); - T(long, long int, "%ld"); - T(ulong, unsigned long int, "%lu"); - T(double, double, "%f"); - T(long_double, long double, "%Lf"); + if (verbose) + { + T(int, int, "%d"); + T(uint, unsigned int, "%u"); + T(long, long int, "%ld"); + T(ulong, unsigned long int, "%lu"); + T(double, double, "%f"); + T(long_double, long double, "%Lf"); + } #undef T putchar('\n'); } diff --git a/judge/token.t b/judge/token.t new file mode 100644 index 0000000..4f4aa2e --- /dev/null +++ b/judge/token.t @@ -0,0 +1,98 @@ +# Test cases for token.c +# BEWARE: This file contains strange spacing (trailing spaces etc.), which is vital. +# Please edit carefully. + +# A simple test case with several spaces +Name: std +Run: ./test-tok +In: abc + 10 20 30 +Out: + <10> + <20> + <30> + +# The same test case in line mode +Name: std-l +Run: ./test-tok -l +In: abc + 10 20 30 +Out: + <> + <10> + <20> + <30> + <> + +# An unterminated line +Name: unterm +Run: tr -d '\n' | ./test-tok +In: abc +Out: + +# An unterminated line in line mode +Name: unterm-l +Run: tr -d '\n' | ./test-tok -l +In: abc +Out: + +# Small token size limit, but fits +Name: big1 +Run: ./test-tok -s +In: abcdefghijklmnop +Out: + +# Small token size limit, does not fit +Name: big2 +Run: ./test-tok -s +In: abcdefghijklmnopq +Exit: 1 + +# Testing parsers +Name: parse1 +Run: ./test-tok -vl +In: abcdef + 0 5 -5 +Out: + <> + <0> = int 0 = uint 0 = long 0 = ulong 0 = double 0.000000 = long_double 0.000000 + <5> = int 5 = uint 5 = long 5 = ulong 5 = double 5.000000 = long_double 5.000000 + <-5> = int -5 = long -5 = double -5.000000 = long_double -5.000000 + <> + +# More parsing: integer extremes +Name: parse2 +Run: ./test-tok -v +In: -2147483647 2147483647 + -2147483648 2147483648 + -4294967295 4294967295 + -4294967296 4294967296 +Out: <-2147483647> = int -2147483647 = long -2147483647 = double -2147483647.000000 = long_double -2147483647.000000 + <2147483647> = int 2147483647 = uint 2147483647 = long 2147483647 = ulong 2147483647 = double 2147483647.000000 = long_double 2147483647.000000 + <-2147483648> = int -2147483648 = long -2147483648 = double -2147483648.000000 = long_double -2147483648.000000 + <2147483648> = uint 2147483648 = ulong 2147483648 = double 2147483648.000000 = long_double 2147483648.000000 + <-4294967295> = double -4294967295.000000 = long_double -4294967295.000000 + <4294967295> = uint 4294967295 = ulong 4294967295 = double 4294967295.000000 = long_double 4294967295.000000 + <-4294967296> = double -4294967296.000000 = long_double -4294967296.000000 + <4294967296> = double 4294967296.000000 = long_double 4294967296.000000 + +# More parsing: floating point numbers +Name: parse3 +In: 1000000000 + 0. .0 . + 0 +0 -0 + +5 -5 + +1e+5 -1e-5 + 1e+99999 +Out: <1000000000> = int 1000000000 = uint 1000000000 = long 1000000000 = ulong 1000000000 = double 1000000000.000000 = long_double 1000000000.000000 + <0.> = double 0.000000 = long_double 0.000000 + <.0> = double 0.000000 = long_double 0.000000 + <.> + <0> = int 0 = uint 0 = long 0 = ulong 0 = double 0.000000 = long_double 0.000000 + <+0> = int 0 = uint 0 = long 0 = ulong 0 = double 0.000000 = long_double 0.000000 + <-0> = int 0 = long 0 = double -0.000000 = long_double -0.000000 + <+5> = int 5 = uint 5 = long 5 = ulong 5 = double 5.000000 = long_double 5.000000 + <-5> = int -5 = long -5 = double -5.000000 = long_double -5.000000 + <+1e+5> = double 100000.000000 = long_double 100000.000000 + <-1e-5> = double -0.000010 = long_double -0.000010 + <1e+99999> -- 2.39.2