test coverage
This commit is contained in:
parent
54db58d362
commit
3b95608233
12
README.md
12
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 数据
|
||||
|
||||
|
2
boot.c
2
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;
|
||||
|
11
cov-test.sh
Normal file
11
cov-test.sh
Normal 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 ..
|
26
demo/misc.c
26
demo/misc.c
@ -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);
|
||||
}
|
@ -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));
|
||||
}
|
3
test.sh
3
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
|
||||
|
11
test/add.c
11
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);
|
||||
}
|
@ -1 +1 @@
|
||||
3 4
|
||||
5 4
|
11
test/add.out
11
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
|
||||
|
8
test/enum.c
Normal file
8
test/enum.c
Normal 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
0
test/enum.in
Normal file
1
test/enum.out
Normal file
1
test/enum.out
Normal file
@ -0,0 +1 @@
|
||||
5
|
32
test/escape.c
Normal file
32
test/escape.c
Normal 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
1
test/escape.in
Normal file
@ -0,0 +1 @@
|
||||
hello\n\tworld\u
|
3
test/escape.out
Normal file
3
test/escape.out
Normal file
@ -0,0 +1,3 @@
|
||||
hello
|
||||
worldunexpected escaped character: '\u'
|
||||
1
|
@ -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);
|
||||
}
|
87
test/loop.c
Normal file
87
test/loop.c
Normal 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
0
test/loop.in
Normal file
13
test/loop.out
Normal file
13
test/loop.out
Normal 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
5
test/overflow.c
Normal 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
0
test/overflow.in
Normal file
1
test/overflow.out
Normal file
1
test/overflow.out
Normal file
@ -0,0 +1 @@
|
||||
20
|
Loading…
Reference in New Issue
Block a user