diff --git a/README.md b/README.md index 61e6617..56f9a02 100644 --- a/README.md +++ b/README.md @@ -197,7 +197,15 @@ $ pip install gcovr 然后 ```sh -$ sh cov.sh +$ sh cov-boot.sh ``` -就会在 cov 文件夹下生成自举的 coverage 数据 +或 + +```sh +$ sh cov-test.sh +``` + + +就会在 cov 文件夹下生成自举或测试的 coverage 数据 + diff --git a/boot.c b/boot.c index 0b88404..742cc52 100644 --- a/boot.c +++ b/boot.c @@ -246,7 +246,7 @@ int get_escaped_char() { } else if (ch == '\"') { ch = '\"'; } else { - fprintf(stderr, "unexpected escaped character: %c\n", ch); + fprintf(stderr, "unexpected escaped character: '\\%c'\n", ch); exit(1); } return ch; diff --git a/cov.sh b/cov-boot.sh similarity index 100% rename from cov.sh rename to cov-boot.sh diff --git a/cov-test.sh b/cov-test.sh new file mode 100644 index 0000000..1a3e9f3 --- /dev/null +++ b/cov-test.sh @@ -0,0 +1,11 @@ +mkdir -p cov && cd cov && +rm * +gcc --coverage -g -O0 ../boot.c +for i in ../test/*.c; do + i=$(basename $i .c) + echo "Running coverage for test '$i'" + ./a.out < ../test/$i.c > /dev/null + gcov a-boot.c +done + +python3 -m gcovr --html-details --html-theme github.green -o report.html -r .. \ No newline at end of file diff --git a/demo/misc.c b/demo/misc.c deleted file mode 100644 index 45876ad..0000000 --- a/demo/misc.c +++ /dev/null @@ -1,26 +0,0 @@ -int printf(char* format, ...); - -void should_be(int expected, int actual) { - if (expected != actual) { - printf("Expected %d, but got %d\n", expected, actual); - } else { - printf("Passed\n"); - } -} - -int* p = 0; -int f1() { - int a = 1; - return *(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(p))))))))))); // p[10] -} - - -int main() { - int a[15]; - p = a; - for (int i = 0; i < 15; a[i] = i, ++i); - p -= 5; - should_be(5, f1()); - should_be(5, a - p); - should_be(10, 5); -} \ No newline at end of file diff --git a/demo/strcmp.c b/demo/strcmp.c deleted file mode 100644 index c880e7d..0000000 --- a/demo/strcmp.c +++ /dev/null @@ -1,16 +0,0 @@ -int printf(char* format, ...); - -int strcmp(char* s1, char* s2) { - while (*s1 && *s2 && *s1 == *s2) { - s1++; - s2++; - } - return *s1 - *s2; -} - -int main() { - char* s1 = "helloworld"; - char* s2 = "world"; - printf("%d\n", strcmp(s1, s2)); - printf("%d\n", strcmp(s1 + 5, s2)); -} \ No newline at end of file diff --git a/test.sh b/test.sh index 36969af..7025834 100644 --- a/test.sh +++ b/test.sh @@ -12,9 +12,10 @@ for i in *.c; do if cmp $i.out $i.ans; then succ=$((succ+1)) echo "Test '$i' passed" + rm $i.ans else echo "Test '$i' failed" fi done echo "Passed $succ/$all tests" -rm *.c.out *.s *.ans a.out +rm *.c.out a.out diff --git a/test/add.c b/test/add.c index 08875dc..b8dcd22 100644 --- a/test/add.c +++ b/test/add.c @@ -6,5 +6,14 @@ int main() { int b; scanf("%d", &a); scanf("%d", &b); - printf("%d\n", a + b); + 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); + 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); + 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); + printf("%d>>%d=%d\n", a, b, a >> b); } \ No newline at end of file diff --git a/test/add.in b/test/add.in index e7e7d8e..82070f9 100644 --- a/test/add.in +++ b/test/add.in @@ -1 +1 @@ -3 4 \ No newline at end of file +5 4 \ No newline at end of file diff --git a/test/add.out b/test/add.out index a8148db..b013c92 100644 --- a/test/add.out +++ b/test/add.out @@ -1,2 +1,11 @@ -7 +5+4=9 +5-4=1 +5*4=20 +5/4=1 +5%4=1 +5&4=4 +5|4=5 +5^4=1 +5<<4=80 +5>>4=0 0 diff --git a/test/enum.c b/test/enum.c new file mode 100644 index 0000000..145b98b --- /dev/null +++ b/test/enum.c @@ -0,0 +1,8 @@ +enum { + A, B, C = 1, D, E = A, F +}; + +int main() { + return A + B + C + D + E + F; + // 0 + 1 + 1 + 2 + 0 + 1 = 5 +} \ No newline at end of file diff --git a/test/enum.in b/test/enum.in new file mode 100644 index 0000000..e69de29 diff --git a/test/enum.out b/test/enum.out new file mode 100644 index 0000000..7ed6ff8 --- /dev/null +++ b/test/enum.out @@ -0,0 +1 @@ +5 diff --git a/test/escape.c b/test/escape.c new file mode 100644 index 0000000..8a0d33e --- /dev/null +++ b/test/escape.c @@ -0,0 +1,32 @@ +int getchar(); +int putchar(int ch); +int printf(char* format, ...); + +int main() { + int ch; + while ((ch = getchar()) != -1) { + if (ch == '\\') { + ch = getchar(); + if (ch == 'n') { + ch = '\n'; + } else if (ch == 't') { + ch = '\t'; + } else if (ch == 'r') { + ch = '\r'; + } else if (ch == '0') { + ch = '\0'; + } else if (ch == '\\') { + ch = '\\'; + } else if (ch == '\'') { + ch = '\''; + } else if (ch == '\"') { + ch = '\"'; + } else { + printf("unexpected escaped character: '\\%c'\n", ch); + return 1; + } + } + putchar(ch); + } + return 0; +} \ No newline at end of file diff --git a/test/escape.in b/test/escape.in new file mode 100644 index 0000000..0ee2bcc --- /dev/null +++ b/test/escape.in @@ -0,0 +1 @@ +hello\n\tworld\u \ No newline at end of file diff --git a/test/escape.out b/test/escape.out new file mode 100644 index 0000000..3230064 --- /dev/null +++ b/test/escape.out @@ -0,0 +1,3 @@ +hello + worldunexpected escaped character: '\u' +1 diff --git a/test/hello.c b/test/hello.c index 65311ff..27cbf15 100644 --- a/test/hello.c +++ b/test/hello.c @@ -1,5 +1,7 @@ int printf(char* format, ...); +int gi = 42; + int main() { - printf("hello world %d\n", 42); + printf("hello world %d\n", gi); } \ No newline at end of file diff --git a/test/loop.c b/test/loop.c new file mode 100644 index 0000000..98c3e21 --- /dev/null +++ b/test/loop.c @@ -0,0 +1,87 @@ +int printf(char* format, ...); +int scanf(char* format, ...); + +/* **dummy** loop test generated by copilot */ + +void test_continue() { + int i; + + // For loop + printf("For loop:\n"); + for (i = 0; i < 5; i++) { + if (i == 3) { + continue; // Skip the rest of the loop when i is 3 + } + printf("%d ", i); + } + printf("\n"); + + // While loop + printf("While loop:\n"); + i = 0; + while (i < 5) { + if (i == 3) { + i++; + continue; // Skip the rest of the loop when i is 3 + } + printf("%d ", i); + i++; + } + printf("\n"); + + // Do-while loop + printf("Do-while loop:\n"); + i = 0; + do { + if (i == 3) { + i++; + continue; // Skip the rest of the loop when i is 3 + } + printf("%d ", i); + i++; + } while (i < 5); + printf("\n"); +} + +void test_break() { + int i; + + // For loop + printf("For loop:\n"); + for (i = 0; i < 5; i++) { + if (i == 3) { + break; // Exit the loop when i is 3 + } + printf("%d ", i); + } + printf("\n"); + + // While loop + printf("While loop:\n"); + i = 0; + while (i < 5) { + if (i == 3) { + break; // Exit the loop when i is 3 + } + printf("%d ", i); + i++; + } + printf("\n"); + + // Do-while loop + printf("Do-while loop:\n"); + i = 0; + do { + if (i == 3) { + break; // Exit the loop when i is 3 + } + printf("%d ", i); + i++; + } while (i < 5); + printf("\n"); +} + +int main() { + test_continue(); + test_break(); +} \ No newline at end of file diff --git a/test/loop.in b/test/loop.in new file mode 100644 index 0000000..e69de29 diff --git a/test/loop.out b/test/loop.out new file mode 100644 index 0000000..460695d --- /dev/null +++ b/test/loop.out @@ -0,0 +1,13 @@ +For loop: +0 1 2 4 +While loop: +0 1 2 4 +Do-while loop: +0 1 2 4 +For loop: +0 1 2 +While loop: +0 1 2 +Do-while loop: +0 1 2 +0 diff --git a/test/overflow.c b/test/overflow.c new file mode 100644 index 0000000..8bb07f7 --- /dev/null +++ b/test/overflow.c @@ -0,0 +1,5 @@ +int main() { + // int placeholder[1024]; + int a = 1; + return (a=(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(0)))))))))))))))))))))), (a = a); +} diff --git a/test/overflow.in b/test/overflow.in new file mode 100644 index 0000000..e69de29 diff --git a/test/overflow.out b/test/overflow.out new file mode 100644 index 0000000..209e3ef --- /dev/null +++ b/test/overflow.out @@ -0,0 +1 @@ +20