// Test program for forward linear search subroutines. #include #include typedef int32_t s32; // Can be compiled with various implementations of find(), including // with -Dfind=findnum, but they all need to fulfill the same // interface. size_t find(s32 *a, size_t n, s32 k); #define is_at(k, n, i) \ do { \ if (find(a, n, k) != i) { \ fprintf(stderr, "find(a, %d, %d) " \ "== %zd (not %d}\n", \ n, k, find(a, n, k), i); \ errs++; \ } \ } while(0) s32 a[] = {5, 3, 53, 37, 8, 2, 69, 37, 9, 4}; int main() { int errs = 0; printf("# a[] = {"); for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) { if (i) printf(", "); printf("%d", a[i]); } printf("}\n"); is_at(5, 10, 0); is_at(5, 0, 0); is_at(5, 4, 0); is_at(5, 3, 0); is_at(3, 10, 1); is_at(3, 0, 0); is_at(3, 1, 1); is_at(53, 10, 2); is_at(53, 4, 2); is_at(53, 3, 2); is_at(53, 1, 1); is_at(37, 10, 3); is_at(37, 4, 3); is_at(37, 2, 2); is_at(8, 10, 4); is_at(8, 5, 4); is_at(8, 8, 4); is_at(8, 3, 3); is_at(2, 10, 5); is_at(2, 6, 5); is_at(2, 8, 5); is_at(2, 4, 4); is_at(2, 5, 5); is_at(9, 10, 8); return errs; }