smarter and error test

This commit is contained in:
Yaossg 2024-12-24 11:31:50 +08:00
parent bf7f456967
commit 49ed7c5df5
37 changed files with 83 additions and 36 deletions

105
test/loop/loop.c Normal file
View file

@ -0,0 +1,105 @@
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");
}
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();
}

16
test/loop/loop.out Normal file
View file

@ -0,0 +1,16 @@
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
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

19
test/loop/parse.c Normal file
View file

@ -0,0 +1,19 @@
int getchar();
int is_digit(int ch) {
return '0' <= ch && ch <= '9';
}
int parse_int(int ch) {
int num = ch - '0';
while (is_digit(ch = getchar())) {
num = num * 10;
num = num + ch - '0';
}
return num;
}
int main() {
return parse_int(getchar());
}

1
test/loop/parse.in Normal file
View file

@ -0,0 +1 @@
42

1
test/loop/parse.out Normal file
View file

@ -0,0 +1 @@
42