more tests

This commit is contained in:
Yaossg 2025-03-09 22:28:36 +08:00
parent d0f8b3e0ad
commit 1bcff515b7
22 changed files with 191 additions and 58 deletions

20
test/function/hanoi.c Normal file
View file

@ -0,0 +1,20 @@
int printf(char* format, ...);
void move(char from_rod, char to_rod) {
printf("%c --> %c\n", from_rod, to_rod);
}
void hanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 1) {
move(from_rod, to_rod);
return;
}
hanoi(n - 1, from_rod, aux_rod, to_rod);
move(from_rod, to_rod);
hanoi(n - 1, aux_rod, to_rod, from_rod);
}
int main() {
int n = 3;
hanoi(n, 'A', 'C', 'B');
}

8
test/function/hanoi.out Normal file
View file

@ -0,0 +1,8 @@
A --> C
A --> B
C --> B
A --> C
B --> A
B --> C
A --> C
0

32
test/function/queen.c Normal file
View file

@ -0,0 +1,32 @@
int printf(char* format, ...);
int putchar(int ch);
int a[9];
int ok(int x, int y) {
for (int i = 1; i <= x - 1; ++i) {
if (a[i] == y || a[i] - i == y - x || a[i] + i == y + x) {
return 0;
}
}
return 1;
}
void queen(int x) {
if (x > 8) {
a[0]++;
return;
}
for (int y = 1; y <= 8; ++y) {
if (ok(x, y)) {
a[x] = y;
queen(x + 1);
a[x] = 0;
}
}
}
int main() {
queen(1);
printf("%d\n", a[0]);
}

2
test/function/queen.out Normal file
View file

@ -0,0 +1,2 @@
92
0

7
test/io/echo.c Normal file
View file

@ -0,0 +1,7 @@
int getchar();
int putchar(int ch);
int main() {
for (char ch; (ch = getchar()) != -1; putchar(ch));
putchar('\n');
}

1
test/io/echo.in Normal file
View file

@ -0,0 +1 @@
hello world

2
test/io/echo.out Normal file
View file

@ -0,0 +1,2 @@
hello world
0

View file

@ -1,14 +1,16 @@
int getchar();
int putchar(int ch);
int fprintf(void* file, char* format, ...);
extern void* stdout;
int offset = 0;
int get() {
return "hello\\n\\tworld\\\\\\'\\\"\\0\r\t\0"[offset++];
}
int putchar(int ch);
int main() {
char ch = 0["\t\r\""];;
while ((ch = getchar()) != -1) {
char ch;
while ((ch = get()) != 0) {
if (ch == '\\') {
ch = getchar();
ch = get();
if (ch == 'n') {
ch = '\n';
} else if (ch == 't') {
@ -16,7 +18,7 @@ int main() {
} else if (ch == 'r') {
ch = '\r';
} else if (ch == '0') {
ch = '\0';
break;
} else if (ch == '\\') {
ch = '\\';
} else if (ch == '\'') {
@ -24,8 +26,7 @@ int main() {
} else if (ch == '\"') {
ch = '\"';
} else {
fprintf(stdout, "unexpected escaped character: '\\%c'\n", ch);
return 1;
break;
}
}
putchar(ch);

2
test/io/escape.out Normal file
View file

@ -0,0 +1,2 @@
hello
world\'"0

16
test/io/extern.c Normal file
View file

@ -0,0 +1,16 @@
int fprintf(void* stream, char* format, ...);
int fscanf(void* stream, char* format, ...);
int sprintf(char* str, char* format, ...);
extern void* stdin;
extern void* stdout;
int main() {
char buffer[4096];
int a;
int b;
fscanf(stdin, "%d %d", &a, &b);
sprintf(buffer, "The sum of %d and %d is %d\n", a, b, a + b);
fprintf(stdout, buffer);
return 0;
}

1
test/io/extern.in Normal file
View file

@ -0,0 +1 @@
3 4

2
test/io/extern.out Normal file
View file

@ -0,0 +1,2 @@
The sum of 3 and 4 is 7
0

30
test/loop/diamond.c Normal file
View file

@ -0,0 +1,30 @@
int printf(char* format, ...);
int main() {
int n = 5; // height of the diamond
int i;
int j;
// upper half of the diamond
for (i = 1; i <= n; i++) {
for (j = i; j < n; j++) {
printf(" ");
}
for (j = 1; j <= (2 * i - 1); j++) {
printf("*");
}
printf("\n");
}
// lower half of the diamond
for (i = n - 1; i >= 1; i--) {
for (j = n; j > i; j--) {
printf(" ");
}
for (j = 1; j <= (2 * i - 1); j++) {
printf("*");
}
printf("\n");
}
}

10
test/loop/diamond.out Normal file
View file

@ -0,0 +1,10 @@
*
***
*****
*******
*********
*******
*****
***
*
0

View file

@ -1,20 +0,0 @@
int printf(char* format, ...);
void test() {
int i;
int j;
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)%c", i, j, " \n"[j == 4]);
}
}
}
int main() {
test();
printf("\n");
}

View file

@ -1,4 +0,0 @@
(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 +0,0 @@
hello\n\tworld\u

View file

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