From 54db58d362dc3037b8d0d35c846dd95ca26838de Mon Sep 17 00:00:00 2001 From: Yaossg Date: Sat, 7 Dec 2024 14:13:34 +0800 Subject: [PATCH] unit test --- .gitignore | 6 ++++-- README.md | 10 +++++++--- cov.sh | 2 +- demo/{add.c => misc.c} | 0 run.sh | 3 ++- test.sh | 20 ++++++++++++++++++++ test/add.c | 10 ++++++++++ test/add.in | 1 + test/add.out | 2 ++ {demo => test}/hello.c | 0 test/hello.in | 0 test/hello.out | 2 ++ {demo => test}/parse.c | 0 test/parse.in | 1 + test/parse.out | 1 + {demo => test}/sort.c | 2 -- test/sort.in | 11 +++++++++++ test/sort.out | 2 ++ 18 files changed, 64 insertions(+), 9 deletions(-) rename demo/{add.c => misc.c} (100%) create mode 100644 test.sh create mode 100644 test/add.c create mode 100644 test/add.in create mode 100644 test/add.out rename {demo => test}/hello.c (100%) create mode 100644 test/hello.in create mode 100644 test/hello.out rename {demo => test}/parse.c (100%) create mode 100644 test/parse.in create mode 100644 test/parse.out rename {demo => test}/sort.c (83%) create mode 100644 test/sort.in create mode 100644 test/sort.out diff --git a/.gitignore b/.gitignore index e76bce3..39f9aff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ build cov -*.out +a.out +*.c.out *.o -*.s \ No newline at end of file +*.s +*.ans \ No newline at end of file diff --git a/README.md b/README.md index e124a13..61e6617 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ ### 真机运行 -编译运行程序,src 为本语言源代码。可以编译 demo 文件夹下的实例。 +编译运行程序,src 为本语言源代码。可以编译 demo 或 test 文件夹下的实例。 ```sh $ sh run-native.sh @@ -33,7 +33,7 @@ $ sh boot-native.sh sudo apt install gcc-12-riscv64-linux-gnu qemu-user qemu-system-misc ``` -编译运行程序,src 为本语言源代码。可以编译 demo 文件夹下的实例。 +编译运行程序,src 为本语言源代码。可以编译 demo 或 test 文件夹下的实例。 ```sh $ sh run.sh @@ -180,7 +180,11 @@ $ sh boot.sh ### 单元测试 -TODO +直接运行 + +```sh +$ sh test.sh +``` ### 覆盖率 diff --git a/cov.sh b/cov.sh index e0d0dec..3adf988 100644 --- a/cov.sh +++ b/cov.sh @@ -3,4 +3,4 @@ rm * gcc --coverage -g -O0 ../boot.c ./a.out < ../boot.c > /dev/null gcov a-boot.c -python3 -m gcovr --html-details --html-theme github.green -o report.html -r .. \ No newline at end of file +python3 -m gcovr --html-details --html-theme github.green -o report.html -r .. diff --git a/demo/add.c b/demo/misc.c similarity index 100% rename from demo/add.c rename to demo/misc.c diff --git a/run.sh b/run.sh index eadcfb4..c2d5f84 100644 --- a/run.sh +++ b/run.sh @@ -2,4 +2,5 @@ gcc boot.c && ./a.out < $1 > $1.s && riscv64-linux-gnu-gcc-12 -static $1.s -o $1.out && qemu-riscv64 $1.out -echo $? \ No newline at end of file +echo $? +rm $1.s $1.out diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..36969af --- /dev/null +++ b/test.sh @@ -0,0 +1,20 @@ +cd test +gcc ../boot.c +all=0 +succ=0 +for i in *.c; do + all=$((all+1)) + i=$(basename $i .c) + ./a.out < $i.c > $i.s && + riscv64-linux-gnu-gcc-12 -static $i.s -o $i.c.out && + qemu-riscv64 $i.c.out < $i.in > $i.ans + echo $? >> $i.ans + if cmp $i.out $i.ans; then + succ=$((succ+1)) + echo "Test '$i' passed" + else + echo "Test '$i' failed" + fi +done +echo "Passed $succ/$all tests" +rm *.c.out *.s *.ans a.out diff --git a/test/add.c b/test/add.c new file mode 100644 index 0000000..08875dc --- /dev/null +++ b/test/add.c @@ -0,0 +1,10 @@ +int printf(char* format, ...); +int scanf(char* format, ...); + +int main() { + int a; + int b; + scanf("%d", &a); + scanf("%d", &b); + printf("%d\n", a + b); +} \ No newline at end of file diff --git a/test/add.in b/test/add.in new file mode 100644 index 0000000..e7e7d8e --- /dev/null +++ b/test/add.in @@ -0,0 +1 @@ +3 4 \ No newline at end of file diff --git a/test/add.out b/test/add.out new file mode 100644 index 0000000..a8148db --- /dev/null +++ b/test/add.out @@ -0,0 +1,2 @@ +7 +0 diff --git a/demo/hello.c b/test/hello.c similarity index 100% rename from demo/hello.c rename to test/hello.c diff --git a/test/hello.in b/test/hello.in new file mode 100644 index 0000000..e69de29 diff --git a/test/hello.out b/test/hello.out new file mode 100644 index 0000000..ff980b8 --- /dev/null +++ b/test/hello.out @@ -0,0 +1,2 @@ +hello world 42 +0 diff --git a/demo/parse.c b/test/parse.c similarity index 100% rename from demo/parse.c rename to test/parse.c diff --git a/test/parse.in b/test/parse.in new file mode 100644 index 0000000..f70d7bb --- /dev/null +++ b/test/parse.in @@ -0,0 +1 @@ +42 \ No newline at end of file diff --git a/test/parse.out b/test/parse.out new file mode 100644 index 0000000..d81cc07 --- /dev/null +++ b/test/parse.out @@ -0,0 +1 @@ +42 diff --git a/demo/sort.c b/test/sort.c similarity index 83% rename from demo/sort.c rename to test/sort.c index c8e5c0f..b0793f0 100644 --- a/demo/sort.c +++ b/test/sort.c @@ -16,9 +16,7 @@ void sort(int a[], int n) { int main() { int n; int a[100]; - printf("Enter the number of elements in the array: "); scanf("%d", &n); - printf("Enter the elements of the array: "); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } diff --git a/test/sort.in b/test/sort.in new file mode 100644 index 0000000..2532cac --- /dev/null +++ b/test/sort.in @@ -0,0 +1,11 @@ +100 + 13 49 15 58 24 74 80 81 69 23 + 67 88 59 39 1 12 73 50 55 53 + 71 63 9 90 87 89 51 75 40 84 + 25 94 68 47 48 14 99 33 62 79 + 66 85 56 31 38 29 86 46 70 6 + 10 19 64 72 45 4 11 42 78 7 + 95 27 93 57 21 35 5 22 76 54 + 44 98 61 32 17 92 65 36 20 28 + 83 2 18 60 16 41 30 37 100 97 + 77 3 82 8 26 34 91 43 96 52 \ No newline at end of file diff --git a/test/sort.out b/test/sort.out new file mode 100644 index 0000000..7c8f65f --- /dev/null +++ b/test/sort.out @@ -0,0 +1,2 @@ +1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 +0