From a84655600d17d3be690c5633d6a12e55999ab611 Mon Sep 17 00:00:00 2001 From: Yaossg Date: Mon, 5 May 2025 16:20:27 +0800 Subject: [PATCH] final tests --- boot.c | 2 +- test.sh | 10 ++++- test/array/arith.c | 5 +-- test/array/bigint.c | 54 ++++++++++++++++++++++++ test/array/bigint.in | 2 + test/array/bigint.out | 2 + test/array/{sort.c => bubble.c} | 1 - test/array/{sort.in => bubble.in} | 0 test/array/{sort.out => bubble.out} | 0 test/array/fib.c | 18 ++++++++ test/array/fib.in | 1 + test/array/fib.out | 2 + test/array/heap.c | 65 +++++++++++++++++++++++++++++ test/array/heap.in | 11 +++++ test/array/heap.out | 2 + test/array/select.c | 41 ++++++++++++++++++ test/array/select.in | 11 +++++ test/array/select.out | 2 + test/branch/if-else-if.c | 13 ++++++ test/branch/if-else-if.out | 2 + test/branch/if-else.c | 10 +++++ test/branch/if-else.out | 2 + test/branch/if.c | 9 ++++ test/branch/if.out | 2 + test/loop/break.c | 12 ++---- test/loop/break.out | 3 -- test/loop/continue.c | 15 +++---- test/loop/continue.out | 6 +-- test/loop/do-while.c | 11 +++++ test/loop/do-while.out | 2 + test/loop/for.c | 10 +++++ test/loop/for.out | 2 + test/loop/while.c | 11 +++++ test/loop/while.out | 2 + 34 files changed, 309 insertions(+), 32 deletions(-) create mode 100644 test/array/bigint.c create mode 100644 test/array/bigint.in create mode 100644 test/array/bigint.out rename test/array/{sort.c => bubble.c} (96%) rename test/array/{sort.in => bubble.in} (100%) rename test/array/{sort.out => bubble.out} (100%) create mode 100644 test/array/fib.c create mode 100644 test/array/fib.in create mode 100644 test/array/fib.out create mode 100644 test/array/heap.c create mode 100644 test/array/heap.in create mode 100644 test/array/heap.out create mode 100644 test/array/select.c create mode 100644 test/array/select.in create mode 100644 test/array/select.out create mode 100644 test/branch/if-else-if.c create mode 100644 test/branch/if-else-if.out create mode 100644 test/branch/if-else.c create mode 100644 test/branch/if-else.out create mode 100644 test/branch/if.c create mode 100644 test/branch/if.out create mode 100644 test/loop/do-while.c create mode 100644 test/loop/do-while.out create mode 100644 test/loop/for.c create mode 100644 test/loop/for.out create mode 100644 test/loop/while.c create mode 100644 test/loop/while.out diff --git a/boot.c b/boot.c index ff6b4ee..d6749d8 100644 --- a/boot.c +++ b/boot.c @@ -1870,7 +1870,7 @@ void parse_function(char* name) { void parse_global_variable(int id, char* name, int type) { printf(".data\n"); - printf(".globl %s\n", name); + printf(".global %s\n", name); printf(".align 5\n"); printf("%s:\n", name); if (token_type == TOKEN_ASSIGN) { diff --git a/test.sh b/test.sh index 85ce94e..28c9b28 100644 --- a/test.sh +++ b/test.sh @@ -35,17 +35,23 @@ for D in *; do if [ -f $i.out ]; then ../boot.elf < $i.c > $i.s && compile $i + gcc $i.c -o $i.ref.elf 2>/dev/null if [[ $? == 0 ]]; then if [ -f $i.in ]; then run_with_input $i + echo $? >> $i.ans + ./$i.ref.elf < $i.in > $i.out + echo $? >> $i.out else run_without_input $i + echo $? >> $i.ans + ./$i.ref.elf > $i.out + echo $? >> $i.out fi - echo $? >> $i.ans cmp $i.out $i.ans failed=$? if [[ $failed == 0 ]]; then - rm $i.ans $i.elf $i.s + rm $i.ans $i.elf $i.s $i.ref.elf fi else failed=1 diff --git a/test/array/arith.c b/test/array/arith.c index 6046875..63c253f 100644 --- a/test/array/arith.c +++ b/test/array/arith.c @@ -1,5 +1,4 @@ int printf(char* format, ...); -int scanf(char* format, ...); int exit(int status); void assert_eq(int expected, int actual) { @@ -23,9 +22,7 @@ void check(int a[], int i, int j) { void check_all(int a[], int n) { for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { - if (a[i] > a[j]) { - check(a, i, j); - } + check(a, i, j); } } } diff --git a/test/array/bigint.c b/test/array/bigint.c new file mode 100644 index 0000000..4e05b82 --- /dev/null +++ b/test/array/bigint.c @@ -0,0 +1,54 @@ +int printf(char* format, ...); +int getchar(); + + +void read(int* a) { + a[0] = 0; + while (1) { + int c = getchar(); + if (c == '\n' || c == -1) { + break; + } + if (c >= '0' && c <= '9') { + a[++a[0]] = c - '0'; + } + } + for (int j = 1; j <= a[0] / 2; j++) { + int temp = a[j]; + a[j] = a[a[0] - j + 1]; + a[a[0] - j + 1] = temp; + } +} + +void write(int* a) { + int n = a[0]; + for (int i = n; i > 0; --i) { + printf("%d", a[i]); + } + printf("\n"); +} + +void add(int* a, int* b) { + int n = a[0] > b[0] ? a[0] : b[0]; + int carry = 0; + for (int i = 1; i <= n; i++) { + int sum = a[i] + b[i] + carry; + a[i] = sum % 10; + carry = sum / 10; + } + if (carry) { + a[n + 1] = carry; + a[0] = n + 1; + } else { + a[0] = n; + } +} + +int main() { + int a[100]; + int b[100]; + read(a); + read(b); + add(a, b); + write(a); +} \ No newline at end of file diff --git a/test/array/bigint.in b/test/array/bigint.in new file mode 100644 index 0000000..c19b030 --- /dev/null +++ b/test/array/bigint.in @@ -0,0 +1,2 @@ +98111111111111111111111111111111107777777777777777777777777777 +89222222222222222222222222222222208888888888888888888888888888 diff --git a/test/array/bigint.out b/test/array/bigint.out new file mode 100644 index 0000000..11b00ff --- /dev/null +++ b/test/array/bigint.out @@ -0,0 +1,2 @@ +187333333333333333333333333333333316666666666666666666666666665 +0 diff --git a/test/array/sort.c b/test/array/bubble.c similarity index 96% rename from test/array/sort.c rename to test/array/bubble.c index d38e16e..fac27bd 100644 --- a/test/array/sort.c +++ b/test/array/bubble.c @@ -1,6 +1,5 @@ int printf(char* format, ...); int scanf(char* format, ...); -int exit(int status); void swap(int* a, int* b) { int t = *a; diff --git a/test/array/sort.in b/test/array/bubble.in similarity index 100% rename from test/array/sort.in rename to test/array/bubble.in diff --git a/test/array/sort.out b/test/array/bubble.out similarity index 100% rename from test/array/sort.out rename to test/array/bubble.out diff --git a/test/array/fib.c b/test/array/fib.c new file mode 100644 index 0000000..808f521 --- /dev/null +++ b/test/array/fib.c @@ -0,0 +1,18 @@ +int printf(char* format, ...); +int scanf(char* format, ...); + +int fib[100]; + +int main() { + int n; + scanf("%d", &n); + fib[0] = 0; + fib[1] = 1; + for (int i = 2; i < n; i++) { + fib[i] = fib[i - 1] + fib[i - 2]; + } + for (int i = 0; i < n; i++) { + printf("%d ", fib[i]); + } + printf("\n"); +} \ No newline at end of file diff --git a/test/array/fib.in b/test/array/fib.in new file mode 100644 index 0000000..2edeafb --- /dev/null +++ b/test/array/fib.in @@ -0,0 +1 @@ +20 \ No newline at end of file diff --git a/test/array/fib.out b/test/array/fib.out new file mode 100644 index 0000000..3cfb40f --- /dev/null +++ b/test/array/fib.out @@ -0,0 +1,2 @@ +0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 +0 diff --git a/test/array/heap.c b/test/array/heap.c new file mode 100644 index 0000000..f6e1e4c --- /dev/null +++ b/test/array/heap.c @@ -0,0 +1,65 @@ +int printf(char* format, ...); +int scanf(char* format, ...); + +// sort by a max heap + +int heap[100]; +int heap_size = 0; + +void push_heap(int value) { + heap[heap_size] = value; + int i = heap_size; + heap_size++; + while (i > 0) { + int parent = (i - 1) / 2; + if (heap[i] > heap[parent]) { + int temp = heap[i]; + heap[i] = heap[parent]; + heap[parent] = temp; + i = parent; + } else { + break; + } + } +} + +int pop_heap() { + int value = heap[0]; + heap_size--; + heap[0] = heap[heap_size]; + int i = 0; + while (i < heap_size) { + int left = 2 * i + 1; + int right = 2 * i + 2; + if (left >= heap_size) { + break; + } + int max_child = left; + if (right < heap_size && heap[right] > heap[left]) { + max_child = right; + } + if (heap[i] < heap[max_child]) { + int temp = heap[i]; + heap[i] = heap[max_child]; + heap[max_child] = temp; + i = max_child; + } else { + break; + } + } + return value; +} + +int main() { + int n; + scanf("%d", &n); + for (int i = 0; i < n; i++) { + int value; + scanf("%d", &value); + push_heap(value); + } + for (int i = 0; i < n; i++) { + printf("%d ", pop_heap()); + } + printf("\n"); +} \ No newline at end of file diff --git a/test/array/heap.in b/test/array/heap.in new file mode 100644 index 0000000..2532cac --- /dev/null +++ b/test/array/heap.in @@ -0,0 +1,11 @@ +100 + 13 49 15 58 24 74 80 81 69 23 + 67 88 59 39 1 12 73 50 55 53 + 71 63 9 90 87 89 51 75 40 84 + 25 94 68 47 48 14 99 33 62 79 + 66 85 56 31 38 29 86 46 70 6 + 10 19 64 72 45 4 11 42 78 7 + 95 27 93 57 21 35 5 22 76 54 + 44 98 61 32 17 92 65 36 20 28 + 83 2 18 60 16 41 30 37 100 97 + 77 3 82 8 26 34 91 43 96 52 \ No newline at end of file diff --git a/test/array/heap.out b/test/array/heap.out new file mode 100644 index 0000000..7e998ff --- /dev/null +++ b/test/array/heap.out @@ -0,0 +1,2 @@ +100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 +0 diff --git a/test/array/select.c b/test/array/select.c new file mode 100644 index 0000000..ccbadd0 --- /dev/null +++ b/test/array/select.c @@ -0,0 +1,41 @@ +int printf(char* format, ...); +int scanf(char* format, ...); + +int a[100]; +int n; + +void swap(int* a, int* b) { + int t = *a; + *a = *b; + *b = t; +} + +int* max_element(int a[], int n) { + int* max = a; + for (int i = 1; i < n; i++) { + if (a[i] > *max) { + max = &a[i]; + } + } + return max; +} + +void sort(int a[], int n) { + for (int i = 0; i < n; i++) { + int* max = max_element(a, n - i); + swap(max, &a[n - i - 1]); + } +} + + +int main() { + scanf("%d", &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"); +} \ No newline at end of file diff --git a/test/array/select.in b/test/array/select.in new file mode 100644 index 0000000..2532cac --- /dev/null +++ b/test/array/select.in @@ -0,0 +1,11 @@ +100 + 13 49 15 58 24 74 80 81 69 23 + 67 88 59 39 1 12 73 50 55 53 + 71 63 9 90 87 89 51 75 40 84 + 25 94 68 47 48 14 99 33 62 79 + 66 85 56 31 38 29 86 46 70 6 + 10 19 64 72 45 4 11 42 78 7 + 95 27 93 57 21 35 5 22 76 54 + 44 98 61 32 17 92 65 36 20 28 + 83 2 18 60 16 41 30 37 100 97 + 77 3 82 8 26 34 91 43 96 52 \ No newline at end of file diff --git a/test/array/select.out b/test/array/select.out new file mode 100644 index 0000000..7c8f65f --- /dev/null +++ b/test/array/select.out @@ -0,0 +1,2 @@ +1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 +0 diff --git a/test/branch/if-else-if.c b/test/branch/if-else-if.c new file mode 100644 index 0000000..9e4ee4f --- /dev/null +++ b/test/branch/if-else-if.c @@ -0,0 +1,13 @@ +int printf(char* format, ...); + +int main() { + int a = 5; + if (a == 3) { + printf("a is 3\n"); + } else if (a == 4) { + printf("a is 4\n"); + } else { + printf("a is not 3 or 4\n"); + } + return 0; +} \ No newline at end of file diff --git a/test/branch/if-else-if.out b/test/branch/if-else-if.out new file mode 100644 index 0000000..bd022c2 --- /dev/null +++ b/test/branch/if-else-if.out @@ -0,0 +1,2 @@ +a is not 3 or 4 +0 diff --git a/test/branch/if-else.c b/test/branch/if-else.c new file mode 100644 index 0000000..9f36db8 --- /dev/null +++ b/test/branch/if-else.c @@ -0,0 +1,10 @@ +int printf(char* format, ...); + +int main() { + int a = 3; + if (a == 3) { + printf("a is 3\n"); + } else { + printf("a is not 3\n"); + } +} \ No newline at end of file diff --git a/test/branch/if-else.out b/test/branch/if-else.out new file mode 100644 index 0000000..9001f38 --- /dev/null +++ b/test/branch/if-else.out @@ -0,0 +1,2 @@ +a is 3 +0 diff --git a/test/branch/if.c b/test/branch/if.c new file mode 100644 index 0000000..19facd3 --- /dev/null +++ b/test/branch/if.c @@ -0,0 +1,9 @@ +int printf(char* format, ...); + +int main() { + int a = 3; + if (a != 3) { + printf("a is not 3\n"); + } + printf("a = %d\n", a); +} \ No newline at end of file diff --git a/test/branch/if.out b/test/branch/if.out new file mode 100644 index 0000000..6783c06 --- /dev/null +++ b/test/branch/if.out @@ -0,0 +1,2 @@ +a = 3 +0 diff --git a/test/loop/break.c b/test/loop/break.c index bfa4699..940e632 100644 --- a/test/loop/break.c +++ b/test/loop/break.c @@ -3,34 +3,28 @@ int printf(char* format, ...); int main() { 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 + break; } 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 + break; } 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 + break; } printf("%d ", i); i++; diff --git a/test/loop/break.out b/test/loop/break.out index e8ad5e7..25adb52 100644 --- a/test/loop/break.out +++ b/test/loop/break.out @@ -1,7 +1,4 @@ -For loop: 0 1 2 -While loop: 0 1 2 -Do-while loop: 0 1 2 0 diff --git a/test/loop/continue.c b/test/loop/continue.c index 214a62d..bb4cd5b 100644 --- a/test/loop/continue.c +++ b/test/loop/continue.c @@ -3,36 +3,33 @@ int printf(char* format, ...); int main() { 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 + if (i > 3) { + continue; } printf("%d ", i); } printf("\n"); - // While loop printf("While loop:\n"); i = 0; while (i < 5) { - if (i == 3) { + if (i > 3) { i++; - continue; // Skip the rest of the loop when i is 3 + continue; } printf("%d ", i); i++; } printf("\n"); - // Do-while loop printf("Do-while loop:\n"); i = 0; do { - if (i == 3) { + if (i > 3) { i++; - continue; // Skip the rest of the loop when i is 3 + continue; } printf("%d ", i); i++; diff --git a/test/loop/continue.out b/test/loop/continue.out index a59c441..16f0ca2 100644 --- a/test/loop/continue.out +++ b/test/loop/continue.out @@ -1,7 +1,7 @@ For loop: -0 1 2 4 +0 1 2 3 While loop: -0 1 2 4 +0 1 2 3 Do-while loop: -0 1 2 4 +0 1 2 3 0 diff --git a/test/loop/do-while.c b/test/loop/do-while.c new file mode 100644 index 0000000..a1eab0c --- /dev/null +++ b/test/loop/do-while.c @@ -0,0 +1,11 @@ +int printf(char* format, ...); + +int main() { + int i = 0; + do { + printf("%d ", i); + i++; + } while (i < 5); + printf("\n"); + return 0; +} \ No newline at end of file diff --git a/test/loop/do-while.out b/test/loop/do-while.out new file mode 100644 index 0000000..a6b5528 --- /dev/null +++ b/test/loop/do-while.out @@ -0,0 +1,2 @@ +0 1 2 3 4 +0 diff --git a/test/loop/for.c b/test/loop/for.c new file mode 100644 index 0000000..b1ef9c9 --- /dev/null +++ b/test/loop/for.c @@ -0,0 +1,10 @@ +int printf(char* format, ...); + +int main() { + int i = 0; + for (i = 0; i < 5; i++) { + printf("%d ", i); + } + printf("\n"); + return 0; +} \ No newline at end of file diff --git a/test/loop/for.out b/test/loop/for.out new file mode 100644 index 0000000..a6b5528 --- /dev/null +++ b/test/loop/for.out @@ -0,0 +1,2 @@ +0 1 2 3 4 +0 diff --git a/test/loop/while.c b/test/loop/while.c new file mode 100644 index 0000000..563cf1c --- /dev/null +++ b/test/loop/while.c @@ -0,0 +1,11 @@ +int printf(char* format, ...); + +int main() { + int i = 0; + while (i < 5) { + printf("%d ", i); + i++; + } + printf("\n"); + return 0; +} \ No newline at end of file diff --git a/test/loop/while.out b/test/loop/while.out new file mode 100644 index 0000000..a6b5528 --- /dev/null +++ b/test/loop/while.out @@ -0,0 +1,2 @@ +0 1 2 3 4 +0