/** * iio-utest: A simple set of unit tests for is_in_order. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include "is-in-order.h" #include #include // +---------+-------------------------------------------------------- // | Globals | // +---------+ int tests = 0; // How many tests we've conducted. int errors = 0; // How many errors wev've encountered // +---------+-------------------------------------------------------- // | Helpers | // +---------+ /** * Procedure: * print_array * Parameters: * a, an array of integers * size, an integer * Purpose: * Print out the first size elements of the array in human-readable * form (more or less). Does not print a final carriage return. * Produces: * [Nothing; Called for the side effect] * Preconditions: * a has at least size elements. * Postconditions: * a has not been modified. */ void print_array (int a[], int size) { int i; if (size == 0) { printf ("{}"); return; } printf ("{%d", a[0]); for (i = 1; i < size; i++) printf (",%d", a[i]); printf ("}"); } // print_array /** * Procedure: * test_iio * Parameters: * a, an array * size, an integer * expected, an integer * Purpose: * One test of the is_in_order procedure. Calls is_in_order on the * array and compares the result to expected, updating information for * testing. * Produces: * [Nothing; Called for the side effect] * Preconditions: * See preconditions for is_in_order. * Postconditions: * a is unmodified. * Global variables tests and errors may be updated. */ void test_iio (int a[], int size, int expected) { // Increment the number of tests ++tests; // Run the procedure int result = is_in_order (a, size); // If we get an unexpected result if (result != expected) { // Tell the user and update our count of errors ++errors; printf ("ERROR in is_in_order ("); print_array (a, size); printf (", %d) EXPECTED: %d, RECEIVED %d\n", size, expected, result); } // if (result != expected) } // test // +------+----------------------------------------------------------- // | Main | // +------+ int main () { // Size 0 arrays int a0a[0]; test_iio (a0a, 0, 1); // Size 1 arrays int a1a[1] = { 1 }; test_iio (a1a, 1, 1); int a1b[1] = { 2 }; test_iio (a1b, 1, 1); int a1c[1] = { -1 }; test_iio (a1c, 1, 1); // Size 2 arrays int a2a[2] = { 0, 1 }; test_iio (a2a, 2, 1); int a2b[2] = { 1, 0 }; test_iio (a2b, 2, 0); int a2c[2] = { 0, 0 }; test_iio (a2c, 2, 1); // Size 3 arrays int a3a[3] = { 1, 2, 3 }; test_iio (a3a, 3, 1); int a3b[3] = { 2, 1, 3 }; test_iio (a3b, 3, 0); int a3c[3] = { 1, 3, 2 }; test_iio (a3c, 3, 0); int a3d[3] = { 3, 1, 2 }; test_iio (a3d, 3, 0); int a3e[3] = { 2, 3, 1 }; test_iio (a3e, 3, 0); int a3f[3] = { 3, 2, 1 }; test_iio (a3f, 3, 0); // A few interesting bigger arrays int a4a[4] = { -3, -2, -1, 0 }; // Negative numbers test_iio (a4a, 4, 1); int a4b[4] = { 0, 0, 0, 0 }; // All duplicates test_iio (a4b, 4, 1); int a5a[5] = { INT_MIN, INT_MIN/2, 0, INT_MAX/2, INT_MAX }; test_iio (a5a, 5, 1); int a8a[8] = { 0, 0, 1, 1, 1, 2, 3, 3 }; // Some duplicates test_iio (a8a, 8, 1); int a8b[8] = { 0, 0, 1, 2, 1, 2, 3, 3 }; // Some duplicates, mid error test_iio (a8b, 8, 0); int a8c[8] = { 5, 0, 1, 1, 1, 2, 3, 3 }; // Some duplicates, left error test_iio (a8c, 8, 0); int a8d[8] = { 0, 0, 1, 1, 1, 2, 3, 0 }; // Some duplicates, rt error test_iio (a8d, 8, 0); // Print the results printf ("%d tests conducted.\n", tests); printf ("%d errors encountered.\n", errors); // And we're done return errors; } // main