smarter and error test
This commit is contained in:
parent
bf7f456967
commit
49ed7c5df5
37 changed files with 83 additions and 36 deletions
39
test/array/arith.c
Normal file
39
test/array/arith.c
Normal 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
1
test/array/arith.out
Normal file
|
@ -0,0 +1 @@
|
|||
0
|
|
@ -2,13 +2,6 @@ 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 swap(int* a, int* b) {
|
||||
int t = *a;
|
||||
*a = *b;
|
||||
|
@ -20,12 +13,6 @@ void sort(int a[], int n) {
|
|||
for (int j = i + 1; j < n; j++) {
|
||||
if (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
3
test/basic/main.c
Normal file
|
@ -0,0 +1,3 @@
|
|||
int main() {
|
||||
return 42;
|
||||
}
|
3
test/error/array_pointer.c
Normal file
3
test/error/array_pointer.c
Normal file
|
@ -0,0 +1,3 @@
|
|||
int main() {
|
||||
int* a[10];
|
||||
}
|
1
test/loop/parse.out
Normal file
1
test/loop/parse.out
Normal file
|
@ -0,0 +1 @@
|
|||
42
|
|
@ -1 +0,0 @@
|
|||
5 4
|
|
@ -1,11 +1,8 @@
|
|||
int printf(char* format, ...);
|
||||
int scanf(char* format, ...);
|
||||
|
||||
int main() {
|
||||
int a;
|
||||
int b;
|
||||
scanf("%d", &a);
|
||||
scanf("%d", &b);
|
||||
int a = 5;
|
||||
int b = 4;
|
||||
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);
|
Loading…
Add table
Add a link
Reference in a new issue