/** * ith-utest.c * Unit tests for the ith_smallest procedure. */ // +--------+--------------------------------------------------------- // | Design | // +--------+ /** * We start with a known sorted array, build all permutations, * and check that the ith value in the permuation is an expected * value. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include #include #include #include #include "ith-smallest.h" #include "swap.h" // +-----------+------------------------------------------------------ // | Variables | // +-----------+ static int errors = 0; // A count of errors static int tests = 0; // A count of tests conducted // +---------+-------------------------------------------------------- // | Helpers | // +---------+ /** * Copy an array */ static void copy_array (char *source[], char *target[], int len) { int i; for (i = 0; i < len; i++) target[i] = source[i]; } // copy_array /** * Print an array */ static void print_array (char *a[], int len) { int i; if (len == 0) printf ("{ }"); else { printf ("{ %s", a[0]); for (i = 1; i < len; i++) printf (", %s", a[i]); printf (" }"); } } // print_array /** * Report an error */ void report_error (char *original[], int len, int i, char *result, char *expected) { } // report_error /** * Procedure: * test_permutations * Parameters: * perm, an array of strings * len, an integer * i, an integer * k, in integer * expected, a string * Purpose: * Make a bunch of permutations of perm (by permuting the first k * elements), sort each permutation, and then check to see whether * the ith element is expected. */ void test_permutations (char *perm[], int len, int i, int k, char *expected) { char *copy[len]; char *result; if (k < 0) return; else if (k == 0) { ++tests; copy_array (perm, copy, len); #ifdef PRINT_TESTS printf ("Checking element %d of: ", i); print_array (perm, len); printf ("\n"); #endif result = ith_smallest (perm, len, i); if (strcmp (result, expected) != 0) { ++errors; /* // Skipping result printing because there are too many errors // in the stub version printf ("Encountered error.\n"); printf (" Array: "); print_array (perm, len); printf ("\n"); printf (" i: %d\n", i); printf (" result: %s\n", result); printf (" expected: %s\n", expected); */ } } // if (k == 0) else // if (k > 0) { int j; for (j = 0; j < k; j++) { copy_array (perm, copy, len); swap (copy, j, k-1); test_permutations (copy, len, i, k-1, expected); } } // if (k > 0) } // test_permutations // +------+----------------------------------------------------------- // | Main | // +------+ int main () { char *values[] = { "anna", "bill", "cali", "dave", "edna", "fred", "gail", "hank" }; int size = sizeof(values) / sizeof(char *); int s; int i; for (s = 1; s <= size; s++) for (i = 0; i < s; i++) test_permutations (values, s, i, s, values[i]); printf ("%d tests conducted.\n", tests); printf ("%d errors encountered.\n", errors); if (errors) printf ("FAILED!\n"); else printf ("Passed.\n"); return errors; } // main