From 49ed7c5df581f2d1d7e84eab6fd5434e28f33969 Mon Sep 17 00:00:00 2001 From: Yaossg Date: Tue, 24 Dec 2024 11:31:50 +0800 Subject: [PATCH] smarter and error test --- cov-test.sh | 7 ++-- test.sh | 44 ++++++++++++++++++-------- test/array/arith.c | 39 +++++++++++++++++++++++ test/array/arith.out | 1 + test/{old => array}/sort.c | 13 -------- test/{old => array}/sort.in | 0 test/{old => array}/sort.out | 0 test/{old => basic}/enum.c | 0 test/{old => basic}/enum.out | 0 test/{old => basic}/hello.c | 0 test/{old => basic}/hello.out | 0 test/basic/main.c | 3 ++ test/{old/parse.out => basic/main.out} | 0 test/error/array_pointer.c | 3 ++ test/{old => loop}/loop.c | 0 test/{old => loop}/loop.out | 0 test/{old => loop}/parse.c | 0 test/{old => loop}/parse.in | 0 test/loop/parse.out | 1 + test/{old => misc}/escape.c | 0 test/{old => misc}/escape.in | 0 test/{old => misc}/escape.out | 0 test/{old => misc}/overflow.c | 0 test/{old => misc}/overflow.in | 0 test/{old => misc}/overflow.out | 0 test/old/add.in | 1 - test/old/enum.in | 0 test/old/hello.in | 0 test/old/inc.in | 0 test/old/loop.in | 0 test/{old/add.c => op/binary.c} | 7 ++-- test/{old/add.out => op/binary.out} | 0 test/{old => op}/inc.c | 0 test/{old => op}/inc.out | 0 test/{old => op}/short.c | 0 test/{old => op}/short.in | 0 test/{old => op}/short.out | 0 37 files changed, 83 insertions(+), 36 deletions(-) create mode 100644 test/array/arith.c create mode 100644 test/array/arith.out rename test/{old => array}/sort.c (56%) rename test/{old => array}/sort.in (100%) rename test/{old => array}/sort.out (100%) rename test/{old => basic}/enum.c (100%) rename test/{old => basic}/enum.out (100%) rename test/{old => basic}/hello.c (100%) rename test/{old => basic}/hello.out (100%) create mode 100644 test/basic/main.c rename test/{old/parse.out => basic/main.out} (100%) create mode 100644 test/error/array_pointer.c rename test/{old => loop}/loop.c (100%) rename test/{old => loop}/loop.out (100%) rename test/{old => loop}/parse.c (100%) rename test/{old => loop}/parse.in (100%) create mode 100644 test/loop/parse.out rename test/{old => misc}/escape.c (100%) rename test/{old => misc}/escape.in (100%) rename test/{old => misc}/escape.out (100%) rename test/{old => misc}/overflow.c (100%) rename test/{old => misc}/overflow.in (100%) rename test/{old => misc}/overflow.out (100%) delete mode 100644 test/old/add.in delete mode 100644 test/old/enum.in delete mode 100644 test/old/hello.in delete mode 100644 test/old/inc.in delete mode 100644 test/old/loop.in rename test/{old/add.c => op/binary.c} (82%) rename test/{old/add.out => op/binary.out} (100%) rename test/{old => op}/inc.c (100%) rename test/{old => op}/inc.out (100%) rename test/{old => op}/short.c (100%) rename test/{old => op}/short.in (100%) rename test/{old => op}/short.out (100%) diff --git a/cov-test.sh b/cov-test.sh index 115419a..a155324 100644 --- a/cov-test.sh +++ b/cov-test.sh @@ -1,10 +1,9 @@ mkdir -p cov && cd cov && rm * gcc --coverage -g -O0 ../boot.c -o boot.elf -for i in ../test/*.c; do - i=$(basename $i .c) - echo "Running coverage for test '$i'" - ./boot.elf < ../test/$i.c > /dev/null +for i in ../test/**/*.c; do + echo "Running coverage for test '$(basename $i .c)'" + ./boot.elf < $i > /dev/null gcov boot.elf-boot.c done diff --git a/test.sh b/test.sh index 41bf9cb..3b257b5 100644 --- a/test.sh +++ b/test.sh @@ -1,28 +1,46 @@ cd test gcc ../boot.c -o boot.elf -all=0 -succ=0 +all_cnt=0 +succ_cnt=0 for D in *; do if [ -d "${D}" ]; then echo "Testing subdirectory '$D'" cd $D for i in *.c; do - all=$((all+1)) + all_cnt=$((all_cnt+1)) + failed=1 i=$(basename $i .c) - ../boot.elf < $i.c > $i.s && - riscv64-linux-gnu-gcc-12 -static $i.s -o $i.elf && - qemu-riscv64 $i.elf < $i.in > $i.ans - echo $? >> $i.ans - if cmp $i.out $i.ans; then - succ=$((succ+1)) - echo "Test '$i' passed" - rm $i.ans $i.s $i.elf + if [ -f $i.out ]; then + ../boot.elf < $i.c > $i.s && + riscv64-linux-gnu-gcc-12 -static $i.s -o $i.elf + if [[ $? == 0 ]]; then + if [ -f $i.in ]; then + qemu-riscv64 $i.elf < $i.in > $i.ans + else + qemu-riscv64 $i.elf > $i.ans + fi + echo $? >> $i.ans + cmp $i.out $i.ans + failed=$? + if [[ $failed == 0 ]]; then + rm $i.ans $i.elf $i.s + fi + else + failed=1 + fi else - echo "Test '$i' failed" + ../boot.elf < $i.c > /dev/null 2>/dev/null + failed=$((!$?)) + fi + if [[ $failed == 0 ]]; then + echo "Test '$D/$i' passed" + succ_cnt=$((succ_cnt+1)) + else + echo "Test '$D/$i' failed" fi done cd .. fi done -echo "Passed $succ/$all tests" +echo "Passed $succ_cnt/$all_cnt tests" rm boot.elf diff --git a/test/array/arith.c b/test/array/arith.c new file mode 100644 index 0000000..6046875 --- /dev/null +++ b/test/array/arith.c @@ -0,0 +1,39 @@ +int printf(char* format, ...); +int scanf(char* format, ...); +int exit(int status); + +void assert_eq(int expected, int actual) { + if (expected != actual) { + printf("expected: %d, actual: %d\n", expected, actual); + exit(1); + } +} + + +void check(int a[], int i, int j) { + assert_eq(i - j, &a[i] - &a[j]); + assert_eq(j - i, &a[j] - &a[i]); + assert_eq(a[i], *(a + i)); + assert_eq(i[a], *(i + a)); + assert_eq(a[j - i], *(a + (j - i))); + assert_eq(j[a - i], *(j + (a - i))); +} + + +void check_all(int a[], int n) { + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + if (a[i] > a[j]) { + check(a, i, j); + } + } + } +} + +int global[100]; + +int main() { + int local[100]; + check_all(global, 100); + check_all(local, 100); +} diff --git a/test/array/arith.out b/test/array/arith.out new file mode 100644 index 0000000..573541a --- /dev/null +++ b/test/array/arith.out @@ -0,0 +1 @@ +0 diff --git a/test/old/sort.c b/test/array/sort.c similarity index 56% rename from test/old/sort.c rename to test/array/sort.c index 970571f..d38e16e 100644 --- a/test/old/sort.c +++ b/test/array/sort.c @@ -2,13 +2,6 @@ int printf(char* format, ...); int scanf(char* format, ...); int exit(int status); -void assert_eq(int expected, int actual) { - if (expected != actual) { - printf("expected: %d, actual: %d\n", expected, actual); - exit(1); - } -} - void swap(int* a, int* b) { int t = *a; *a = *b; @@ -20,12 +13,6 @@ void sort(int a[], int n) { for (int j = i + 1; j < n; j++) { if (a[i] > a[j]) { swap(&a[i], &a[j]); - assert_eq(i - j, &a[i] - &a[j]); - assert_eq(j - i, &a[j] - &a[i]); - assert_eq(a[i], *(a + i)); - assert_eq(i[a], *(i + a)); - assert_eq(a[j - i], *(a + (j - i))); - assert_eq(j[a - i], *(j + (a - i))); } } } diff --git a/test/old/sort.in b/test/array/sort.in similarity index 100% rename from test/old/sort.in rename to test/array/sort.in diff --git a/test/old/sort.out b/test/array/sort.out similarity index 100% rename from test/old/sort.out rename to test/array/sort.out diff --git a/test/old/enum.c b/test/basic/enum.c similarity index 100% rename from test/old/enum.c rename to test/basic/enum.c diff --git a/test/old/enum.out b/test/basic/enum.out similarity index 100% rename from test/old/enum.out rename to test/basic/enum.out diff --git a/test/old/hello.c b/test/basic/hello.c similarity index 100% rename from test/old/hello.c rename to test/basic/hello.c diff --git a/test/old/hello.out b/test/basic/hello.out similarity index 100% rename from test/old/hello.out rename to test/basic/hello.out diff --git a/test/basic/main.c b/test/basic/main.c new file mode 100644 index 0000000..c31f2d2 --- /dev/null +++ b/test/basic/main.c @@ -0,0 +1,3 @@ +int main() { + return 42; +} \ No newline at end of file diff --git a/test/old/parse.out b/test/basic/main.out similarity index 100% rename from test/old/parse.out rename to test/basic/main.out diff --git a/test/error/array_pointer.c b/test/error/array_pointer.c new file mode 100644 index 0000000..1e83dbd --- /dev/null +++ b/test/error/array_pointer.c @@ -0,0 +1,3 @@ +int main() { + int* a[10]; +} \ No newline at end of file diff --git a/test/old/loop.c b/test/loop/loop.c similarity index 100% rename from test/old/loop.c rename to test/loop/loop.c diff --git a/test/old/loop.out b/test/loop/loop.out similarity index 100% rename from test/old/loop.out rename to test/loop/loop.out diff --git a/test/old/parse.c b/test/loop/parse.c similarity index 100% rename from test/old/parse.c rename to test/loop/parse.c diff --git a/test/old/parse.in b/test/loop/parse.in similarity index 100% rename from test/old/parse.in rename to test/loop/parse.in diff --git a/test/loop/parse.out b/test/loop/parse.out new file mode 100644 index 0000000..d81cc07 --- /dev/null +++ b/test/loop/parse.out @@ -0,0 +1 @@ +42 diff --git a/test/old/escape.c b/test/misc/escape.c similarity index 100% rename from test/old/escape.c rename to test/misc/escape.c diff --git a/test/old/escape.in b/test/misc/escape.in similarity index 100% rename from test/old/escape.in rename to test/misc/escape.in diff --git a/test/old/escape.out b/test/misc/escape.out similarity index 100% rename from test/old/escape.out rename to test/misc/escape.out diff --git a/test/old/overflow.c b/test/misc/overflow.c similarity index 100% rename from test/old/overflow.c rename to test/misc/overflow.c diff --git a/test/old/overflow.in b/test/misc/overflow.in similarity index 100% rename from test/old/overflow.in rename to test/misc/overflow.in diff --git a/test/old/overflow.out b/test/misc/overflow.out similarity index 100% rename from test/old/overflow.out rename to test/misc/overflow.out diff --git a/test/old/add.in b/test/old/add.in deleted file mode 100644 index 82070f9..0000000 --- a/test/old/add.in +++ /dev/null @@ -1 +0,0 @@ -5 4 \ No newline at end of file diff --git a/test/old/enum.in b/test/old/enum.in deleted file mode 100644 index e69de29..0000000 diff --git a/test/old/hello.in b/test/old/hello.in deleted file mode 100644 index e69de29..0000000 diff --git a/test/old/inc.in b/test/old/inc.in deleted file mode 100644 index e69de29..0000000 diff --git a/test/old/loop.in b/test/old/loop.in deleted file mode 100644 index e69de29..0000000 diff --git a/test/old/add.c b/test/op/binary.c similarity index 82% rename from test/old/add.c rename to test/op/binary.c index b8dcd22..870897a 100644 --- a/test/old/add.c +++ b/test/op/binary.c @@ -1,11 +1,8 @@ int printf(char* format, ...); -int scanf(char* format, ...); int main() { - int a; - int b; - scanf("%d", &a); - scanf("%d", &b); + int a = 5; + int b = 4; printf("%d+%d=%d\n", a, b, a + b); printf("%d-%d=%d\n", a, b, a - b); printf("%d*%d=%d\n", a, b, a * b); diff --git a/test/old/add.out b/test/op/binary.out similarity index 100% rename from test/old/add.out rename to test/op/binary.out diff --git a/test/old/inc.c b/test/op/inc.c similarity index 100% rename from test/old/inc.c rename to test/op/inc.c diff --git a/test/old/inc.out b/test/op/inc.out similarity index 100% rename from test/old/inc.out rename to test/op/inc.out diff --git a/test/old/short.c b/test/op/short.c similarity index 100% rename from test/old/short.c rename to test/op/short.c diff --git a/test/old/short.in b/test/op/short.in similarity index 100% rename from test/old/short.in rename to test/op/short.in diff --git a/test/old/short.out b/test/op/short.out similarity index 100% rename from test/old/short.out rename to test/op/short.out