/** * utest_index_of.c - unit tests for the index_of procedure. */ #include #include #include #include "array_utils.h" // +-----------+------------------------------------------------------ // | Constants | // +-----------+ /** The size of the array we use for testing. */ #define SIZE 16 // +-----------+------------------------------------------------------ // | Variables | // +-----------+ /** * A count of errors. */ static int errors = 0; // +---------+-------------------------------------------------------- // | Helpers | // +---------+ void check (int values[], int size, int value, int expected) { int result = index_of (values, size, value); if (result != expected) { printf ("In searching for %d in the size %d array, " "expected %d, got %d.\n", value, size, expected, result); ++errors; } } // +------+----------------------------------------------------------- // | Main | // +------+ int main () { int i; int values[SIZE]; // Fill values for (i = 0; i < SIZE; i++) values[i] = 2 * i; check (values, SIZE, -1, -1); for (i = 0; i < SIZE; i++) { check (values, SIZE, 2*i, i); check (values, SIZE, 2*i+1, -1); } if (errors) { printf ("%d errors encountered.\n", errors); return EXIT_FAILURE; } else { return EXIT_SUCCESS; } } // main