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

View File

@ -1,10 +1,9 @@
mkdir -p cov && cd cov && mkdir -p cov && cd cov &&
rm * rm *
gcc --coverage -g -O0 ../boot.c -o boot.elf gcc --coverage -g -O0 ../boot.c -o boot.elf
for i in ../test/*.c; do for i in ../test/**/*.c; do
i=$(basename $i .c) echo "Running coverage for test '$(basename $i .c)'"
echo "Running coverage for test '$i'" ./boot.elf < $i > /dev/null
./boot.elf < ../test/$i.c > /dev/null
gcov boot.elf-boot.c gcov boot.elf-boot.c
done done

44
test.sh
View File

@ -1,28 +1,46 @@
cd test cd test
gcc ../boot.c -o boot.elf gcc ../boot.c -o boot.elf
all=0 all_cnt=0
succ=0 succ_cnt=0
for D in *; do for D in *; do
if [ -d "${D}" ]; then if [ -d "${D}" ]; then
echo "Testing subdirectory '$D'" echo "Testing subdirectory '$D'"
cd $D cd $D
for i in *.c; do for i in *.c; do
all=$((all+1)) all_cnt=$((all_cnt+1))
failed=1
i=$(basename $i .c) i=$(basename $i .c)
../boot.elf < $i.c > $i.s && if [ -f $i.out ]; then
riscv64-linux-gnu-gcc-12 -static $i.s -o $i.elf && ../boot.elf < $i.c > $i.s &&
qemu-riscv64 $i.elf < $i.in > $i.ans riscv64-linux-gnu-gcc-12 -static $i.s -o $i.elf
echo $? >> $i.ans if [[ $? == 0 ]]; then
if cmp $i.out $i.ans; then if [ -f $i.in ]; then
succ=$((succ+1)) qemu-riscv64 $i.elf < $i.in > $i.ans
echo "Test '$i' passed" else
rm $i.ans $i.s $i.elf qemu-riscv64 $i.elf > $i.ans
fi
echo $? >> $i.ans
cmp $i.out $i.ans
failed=$?
if [[ $failed == 0 ]]; then
rm $i.ans $i.elf $i.s
fi
else
failed=1
fi
else else
echo "Test '$i' failed" ../boot.elf < $i.c > /dev/null 2>/dev/null
failed=$((!$?))
fi
if [[ $failed == 0 ]]; then
echo "Test '$D/$i' passed"
succ_cnt=$((succ_cnt+1))
else
echo "Test '$D/$i' failed"
fi fi
done done
cd .. cd ..
fi fi
done done
echo "Passed $succ/$all tests" echo "Passed $succ_cnt/$all_cnt tests"
rm boot.elf rm boot.elf

39
test/array/arith.c Normal file
View File

@ -0,0 +1,39 @@
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 check(int a[], int i, int 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)));
}
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);
}
}
}
}
int global[100];
int main() {
int local[100];
check_all(global, 100);
check_all(local, 100);
}

1
test/array/arith.out Normal file
View File

@ -0,0 +1 @@
0

View File

@ -2,13 +2,6 @@ int printf(char* format, ...);
int scanf(char* format, ...); int scanf(char* format, ...);
int exit(int status); 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) { void swap(int* a, int* b) {
int t = *a; int t = *a;
*a = *b; *a = *b;
@ -20,12 +13,6 @@ void sort(int a[], int n) {
for (int j = i + 1; j < n; j++) { for (int j = i + 1; j < n; j++) {
if (a[i] > a[j]) { if (a[i] > a[j]) {
swap(&a[i], &a[j]); 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)));
} }
} }
} }

3
test/basic/main.c Normal file
View File

@ -0,0 +1,3 @@
int main() {
return 42;
}

View File

@ -0,0 +1,3 @@
int main() {
int* a[10];
}

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

@ -0,0 +1 @@
42

View File

@ -1 +0,0 @@
5 4

View File

View File

View File

View File

View File

@ -1,11 +1,8 @@
int printf(char* format, ...); int printf(char* format, ...);
int scanf(char* format, ...);
int main() { int main() {
int a; int a = 5;
int b; int b = 4;
scanf("%d", &a);
scanf("%d", &b);
printf("%d+%d=%d\n", a, b, a + b); printf("%d+%d=%d\n", a, b, a + b);
printf("%d-%d=%d\n", a, b, a - b); printf("%d-%d=%d\n", a, b, a - b);
printf("%d*%d=%d\n", a, b, a * b); printf("%d*%d=%d\n", a, b, a * b);