test coverage

This commit is contained in:
Yaossg 2024-12-08 11:40:47 +08:00
parent 54db58d362
commit 3b95608233
23 changed files with 199 additions and 50 deletions

View File

@ -197,7 +197,15 @@ $ pip install gcovr
然后 然后
```sh ```sh
$ sh cov.sh $ sh cov-boot.sh
``` ```
就会在 cov 文件夹下生成自举的 coverage 数据
```sh
$ sh cov-test.sh
```
就会在 cov 文件夹下生成自举或测试的 coverage 数据

2
boot.c
View File

@ -246,7 +246,7 @@ int get_escaped_char() {
} else if (ch == '\"') { } else if (ch == '\"') {
ch = '\"'; ch = '\"';
} else { } else {
fprintf(stderr, "unexpected escaped character: %c\n", ch); fprintf(stderr, "unexpected escaped character: '\\%c'\n", ch);
exit(1); exit(1);
} }
return ch; return ch;

View File

11
cov-test.sh Normal file
View File

@ -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 ..

View File

@ -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);
}

View File

@ -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));
}

View File

@ -12,9 +12,10 @@ for i in *.c; do
if cmp $i.out $i.ans; then if cmp $i.out $i.ans; then
succ=$((succ+1)) succ=$((succ+1))
echo "Test '$i' passed" echo "Test '$i' passed"
rm $i.ans
else else
echo "Test '$i' failed" echo "Test '$i' failed"
fi fi
done done
echo "Passed $succ/$all tests" echo "Passed $succ/$all tests"
rm *.c.out *.s *.ans a.out rm *.c.out a.out

View File

@ -6,5 +6,14 @@ int main() {
int b; int b;
scanf("%d", &a); scanf("%d", &a);
scanf("%d", &b); 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);
} }

View File

@ -1 +1 @@
3 4 5 4

View File

@ -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 0

8
test/enum.c Normal file
View File

@ -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
}

0
test/enum.in Normal file
View File

1
test/enum.out Normal file
View File

@ -0,0 +1 @@
5

32
test/escape.c Normal file
View File

@ -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;
}

1
test/escape.in Normal file
View File

@ -0,0 +1 @@
hello\n\tworld\u

3
test/escape.out Normal file
View File

@ -0,0 +1,3 @@
hello
worldunexpected escaped character: '\u'
1

View File

@ -1,5 +1,7 @@
int printf(char* format, ...); int printf(char* format, ...);
int gi = 42;
int main() { int main() {
printf("hello world %d\n", 42); printf("hello world %d\n", gi);
} }

87
test/loop.c Normal file
View File

@ -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();
}

0
test/loop.in Normal file
View File

13
test/loop.out Normal file
View File

@ -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

5
test/overflow.c Normal file
View File

@ -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);
}

0
test/overflow.in Normal file
View File

1
test/overflow.out Normal file
View File

@ -0,0 +1 @@
20