RVBTCC/test/array/arith.c

40 lines
819 B
C
Raw Normal View History

2024-12-24 03:31:50 +00:00
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);
}