/** * utest_index_of_largest.c - unit tests for the largest 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 | // +---------+ /** * A quick test of largest. */ static void test_index_of_largest_helper (int a[], int limit, int expected) { int result = index_of_largest (a, limit); if (result != expected) { printf ("Expected %d as index of largest, but found %d.\n", expected, result); ++errors; } } // test_index_of_largest_helper /** * Lots of tests of largest. Uses def as the value everywhere in the * array except for where it puts large. */ static void test_index_of_largest (int a[], int limit, int def, int large) { int i; // Set up the array for (i = 0; i < limit; i++) a[i] = def; // Try each of the position in the array for (i = 0; i < limit; i++) { a[i] = large; test_index_of_largest_helper (a, limit, i); a[i] = def; } } // test_index_of_largest // +------+----------------------------------------------------------- // | Main | // +------+ int main () { int values[SIZE]; int i; test_index_of_largest (values, SIZE, 0, 1); test_index_of_largest (values, SIZE, INT_MIN, INT_MIN+1); test_index_of_largest (values, SIZE, INT_MAX-1, INT_MAX); if (errors) { printf ("%d errors encountered.\n", errors); return EXIT_FAILURE; } else { return EXIT_SUCCESS; } } // main