more tests
This commit is contained in:
parent
d0f8b3e0ad
commit
1bcff515b7
22 changed files with 191 additions and 58 deletions
24
boot.c
24
boot.c
|
@ -230,25 +230,15 @@ int parse_int(int ch) {
|
|||
|
||||
int get_escaped_char() {
|
||||
int 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 {
|
||||
if (ch == 'n') return '\n';
|
||||
if (ch == 't') return '\t';
|
||||
if (ch == 'r') return '\r';
|
||||
if (ch == '0') return '\0';
|
||||
if (ch == '\\') return '\\';
|
||||
if (ch == '\'') return '\'';
|
||||
if (ch == '\"') return '\"';
|
||||
fprintf(stderr, "unexpected escaped character: '\\%c'\n", ch);
|
||||
exit(1);
|
||||
}
|
||||
return ch;
|
||||
}
|
||||
|
||||
int token_state;
|
||||
|
|
|
@ -30,6 +30,7 @@ void queen(int x) {
|
|||
if (x > 8) {
|
||||
output();
|
||||
a[0]++;
|
||||
return;
|
||||
}
|
||||
for (int y = 1; y <= 8; ++y) {
|
||||
if (ok(x, y)) {
|
||||
|
|
36
demo/sort.c
Normal file
36
demo/sort.c
Normal file
|
@ -0,0 +1,36 @@
|
|||
int printf(char* format, ...);
|
||||
int scanf(char* format, ...);
|
||||
int exit(int status);
|
||||
|
||||
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]) {
|
||||
swap(&a[i], &a[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int a[100];
|
||||
int n;
|
||||
|
||||
int main() {
|
||||
printf("Enter the number of elements: ");
|
||||
scanf("%d", &n);
|
||||
printf("Enter %d integers: ", n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
scanf("%d", &a[i]);
|
||||
}
|
||||
sort(a, n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
printf("%d ", a[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
2
run.sh
2
run.sh
|
@ -14,4 +14,4 @@ gcc boot.c -o boot.elf &&
|
|||
./boot.elf < $1 > $1.s &&
|
||||
compile_and_run $1
|
||||
echo $?
|
||||
rm $1.s $1.s.elf boot.elf 2> /dev/null
|
||||
rm $1.s $1.elf boot.elf 2> /dev/null
|
20
test/function/hanoi.c
Normal file
20
test/function/hanoi.c
Normal 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
8
test/function/hanoi.out
Normal 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
32
test/function/queen.c
Normal 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
2
test/function/queen.out
Normal file
|
@ -0,0 +1,2 @@
|
|||
92
|
||||
0
|
7
test/io/echo.c
Normal file
7
test/io/echo.c
Normal 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
1
test/io/echo.in
Normal file
|
@ -0,0 +1 @@
|
|||
hello world
|
2
test/io/echo.out
Normal file
2
test/io/echo.out
Normal file
|
@ -0,0 +1,2 @@
|
|||
hello world
|
||||
0
|
|
@ -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
2
test/io/escape.out
Normal file
|
@ -0,0 +1,2 @@
|
|||
hello
|
||||
world\'"0
|
16
test/io/extern.c
Normal file
16
test/io/extern.c
Normal 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
1
test/io/extern.in
Normal file
|
@ -0,0 +1 @@
|
|||
3 4
|
2
test/io/extern.out
Normal file
2
test/io/extern.out
Normal file
|
@ -0,0 +1,2 @@
|
|||
The sum of 3 and 4 is 7
|
||||
0
|
30
test/loop/diamond.c
Normal file
30
test/loop/diamond.c
Normal 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
10
test/loop/diamond.out
Normal file
|
@ -0,0 +1,10 @@
|
|||
*
|
||||
***
|
||||
*****
|
||||
*******
|
||||
*********
|
||||
*******
|
||||
*****
|
||||
***
|
||||
*
|
||||
0
|
|
@ -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");
|
||||
}
|
|
@ -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
|
|
@ -1 +0,0 @@
|
|||
hello\n\tworld\u
|
|
@ -1,3 +0,0 @@
|
|||
hello
|
||||
worldunexpected escaped character: '\u'
|
||||
1
|
Loading…
Add table
Add a link
Reference in a new issue