coverage all

This commit is contained in:
Yaossg 2024-12-08 15:41:38 +08:00
parent a9054f0a64
commit 5784a90d7e
15 changed files with 152 additions and 14 deletions

View File

@ -146,6 +146,7 @@ $ sh boot.sh
### 其它支持与不支持
- 不允许在一个声明中声明多个变量或函数(如 `int a, b;`)请写成多个声明。
- 支持全局变量和局部变量,局部变量遮挡全局变量。
- 不支持局部变量之间的遮挡,重名的局部变量为同一变量。
- 函数只支持最多八个参数。函数声明中支持可变参数,仅用于兼容 C 语言库。

View File

@ -15,6 +15,7 @@ for i in *.c; do
rm $i.ans
else
echo "Test '$i' failed"
exit 1
fi
done
echo "Passed $succ/$all tests"

View File

@ -1,5 +1,5 @@
enum {
A, B, C = 1, D, E = A, F
A, B, C = 1, D, E = A, F,
};
int main() {

View File

@ -1,9 +1,11 @@
int getchar();
int putchar(int ch);
int printf(char* format, ...);
int fprintf(void* file, char* format, ...);
extern void* stdout;
int main() {
int ch;
char ch = 0["\t\r\""];;
while ((ch = getchar()) != -1) {
if (ch == '\\') {
ch = getchar();
@ -22,7 +24,7 @@ int main() {
} else if (ch == '\"') {
ch = '\"';
} else {
printf("unexpected escaped character: '\\%c'\n", ch);
fprintf(stdout, "unexpected escaped character: '\\%c'\n", ch);
return 1;
}
}

14
test/inc.c Normal file
View File

@ -0,0 +1,14 @@
int printf(char* format, ...);
int main() {
int a = 4;
printf("a = %d\n", a);
printf("a++ = %d\n", a++);
printf("a = %d\n", a);
printf("++a = %d\n", ++a);
printf("a = %d\n", a);
printf("a-- = %d\n", a--);
printf("a = %d\n", a);
printf("--a = %d\n", --a);
printf("a = %d\n", a);
}

0
test/inc.in Normal file
View File

10
test/inc.out Normal file
View File

@ -0,0 +1,10 @@
a = 4
a++ = 4
a = 5
++a = 6
a = 6
a-- = 6
a = 5
--a = 4
a = 4
0

View File

@ -81,7 +81,25 @@ void test_break() {
printf("\n");
}
void test_nested() {
int i;
int j;
// For loop
printf("For loop:\n");
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
if (i >= 2 && j >= 2 && i + j >= 5) {
return; // Exit nested loop via return
}
printf("(%d, %d) ", i, j);
}
printf("\n");
}
}
int main() {
test_continue();
test_break();
test_nested();
}

View File

@ -10,4 +10,7 @@ While loop:
0 1 2
Do-while loop:
0 1 2
0
For loop:
(0, 0) (0, 1) (0, 2) (0, 3) (0, 4)
(1, 0) (1, 1) (1, 2) (1, 3) (1, 4)
(2, 0) (2, 1) (2, 2) 0

View File

@ -1,5 +1,41 @@
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);
int printf(char* format, ...);
enum {
a = 1
};
int get_20() {
return (a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(- -0)))))))))))))))))))));
}
void dummy(int a0, int a1, int a2, int a3, int a4, int a5, int a6, int a7) {
a0 += a1;
printf("%d\n", a0);
a0 -= a1;
printf("%d\n", a0);
a0 *= a1;
printf("%d\n", a0);
a0 /= a1;
printf("%d\n", a0);
a0 %= a1;
printf("%d\n", a0);
a0 &= a1;
printf("%d\n", a0);
a0 |= a1;
printf("%d\n", a0);
a0 ^= a2;
printf("%d\n", a0);
a0 <<= a1;
printf("%d\n", a0);
a0 >>= a1;
printf("%d\n", a0);
}
int main() {
char placeholder[4096];
int a = 1;
dummy((a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(! !0))))))))))))))))))))), 3, a, a, a, a, a, a);
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);
}

View File

@ -1 +1,11 @@
23
20
60
20
2
2
3
2
16
2
20

17
test/short.c Normal file
View File

@ -0,0 +1,17 @@
int printf(char* format, ...);
int f(int i) {
printf("f(%d)\n", i);
return i % 2;
}
int main() {
1 && f(1);
0 && f(2);
1 || f(3);
0 || f(4);
1 ? f(5) : f(6);
0 ? f(7) : f(8);
1 && f(9) || f(10);
0 && f(11) || f(12);
}

0
test/short.in Normal file
View File

7
test/short.out Normal file
View File

@ -0,0 +1,7 @@
f(1)
f(4)
f(5)
f(8)
f(9)
f(12)
0

View File

@ -1,21 +1,40 @@
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;
*b = t;
}
void sort(int a[], int n) {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] > a[j]) {
int t = a[i];
a[i] = a[j];
a[j] = t;
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)));
}
}
}
}
int a[100];
int n;
int main() {
int n;
int a[100];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);