/** * ibs-utest.c - A more systematic test of the binary search. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include "ibs.h" #include #include // +-----------+------------------------------------------------------ // | Constants | // +-----------+ /** * The maximum size array we check. */ #define MAX_SIZE 33 // +---------+-------------------------------------------------------- // | Globals | // +---------+ /** * The number of tests that we've conducted. */ int tests = 0; /** * The number of errors that we have encountered. */ int errors = 0; // +---------+-------------------------------------------------------- // | Helpers | // +---------+ /** * Check the result of binary search in an array, printing an error * message and updating the state if the result is not what we expected. */ void check (int value, int values[], int size, int expected) { ++tests; int result = ibs (value, values, size); if (result != expected) { printf ("In searching for %d in the size %d array, " "expected %d, got %d.\n", value, size, expected, result); ++errors; } } // check // +------+----------------------------------------------------------- // | Main | // +------+ int main () { int i; int size; int values[MAX_SIZE]; // Fill values for (i = 0; i < MAX_SIZE; i++) values[i] = 2 * i; for (size = 0; size < MAX_SIZE; size++) { check (-1, values, size, -1); for (i = 0; i < size; i++) { check (2*i, values, size, i); check (2*i+1, values, size, -1); } // for each index } // for each size size printf ("%d tests conducted.\n", tests); printf ("%d errors encountered.\n", errors); return errors; } // main