/** * perm-utest.c * Unit tests for our permutation library. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include // For output #include // For INT_MAX #include "perm.h" // Information on the functions we're testing. #include "bool.h" // I like TRUE and FALSE // +---------+-------------------------------------------------------- // | Globals | // +---------+ /** * A count of all the errors we've encountered. */ static int errors = 0; /** * A count of all the tests we've conducted. */ static int tests = 0; // +---------+-------------------------------------------------------- // | Helpers | // +---------+ /** * Print out an array. Does not print a terminal newline. */ static void ia_print (int a[], int size, FILE *fp) { if (size == 0) { fprintf (fp, "{}"); return; } int i; fprintf (fp, "{ %d", a[0]); for (i = 1; i < size; i++) fprintf (fp, ", %d", a[i]); fprintf (fp, " }"); } // ia_print /** * Determine whether ia_is_permutation returns the value we expect. */ static void test_ia_is_permutation (int a0[], int a1[], int size, int expected) { ++tests; int result = ia_is_permutation (a0, a1, size); if (result != expected) { ++errors; fprintf (stderr, "FAILED EXPERIMENT!\n"); fprintf (stderr, " a0: "); ia_print (a0, size, stderr); fprintf (stderr, "\n"); fprintf (stderr, " a1: "); ia_print (a1, size, stderr); fprintf (stderr, "\n"); if (expected) fprintf (stderr, " Expected true, got false\n"); else fprintf (stderr, " Expected false, got true\n"); } } // test_ia_is_permutation // +------+----------------------------------------------------------- // | Main | // +------+ int main () { // Test the empty array int empty0[] = { }; int empty1[] = { }; test_ia_is_permutation (empty0, empty1, 0, TRUE); // Test some singleton arrays test_ia_is_permutation ((int []) { 0 }, (int []) { 0 }, 1, TRUE); test_ia_is_permutation ((int []) { INT_MAX }, (int []) { INT_MAX }, 1, TRUE); test_ia_is_permutation ((int []) { INT_MAX }, (int []) { INT_MIN }, 1, FALSE); test_ia_is_permutation ((int []) { 0 }, (int []) { 1 }, 1, FALSE); // Test some two-element arrays test_ia_is_permutation ((int []) { 0, 0 }, (int []) { 0, 0 }, 2, TRUE); test_ia_is_permutation ((int []) { 0, 1 }, (int []) { 1, 0 }, 2, TRUE); test_ia_is_permutation ((int []) { 0, 1 }, (int []) { 1, 1 }, 2, FALSE); test_ia_is_permutation ((int []) { 1, 1 }, (int []) { 1, 0 }, 2, FALSE); // Test some three-element arrays test_ia_is_permutation ((int []) { 1, 1, 1 }, (int []) { 1, 1, 1 }, 3, TRUE); test_ia_is_permutation ((int []) { 1, 1, 0 }, (int []) { 1, 1, 0 }, 3, TRUE); test_ia_is_permutation ((int []) { 1, 1, 0 }, (int []) { 1, 0, 1 }, 3, TRUE); test_ia_is_permutation ((int []) { 1, 1, 0 }, (int []) { 0, 1, 1 }, 3, TRUE); test_ia_is_permutation ((int []) { 0, 1, 0 }, (int []) { 0, 1, 1 }, 3, FALSE); test_ia_is_permutation ((int []) { 1, 2, 3 }, (int []) { 1, 2, 3 }, 3, TRUE); test_ia_is_permutation ((int []) { 1, 2, 3 }, (int []) { 1, 3, 2 }, 3, TRUE); test_ia_is_permutation ((int []) { 1, 2, 3 }, (int []) { 2, 1, 3 }, 3, TRUE); test_ia_is_permutation ((int []) { 1, 2, 3 }, (int []) { 2, 3, 1 }, 3, TRUE); test_ia_is_permutation ((int []) { 1, 2, 3 }, (int []) { 3, 1, 2 }, 3, TRUE); test_ia_is_permutation ((int []) { 1, 2, 3 }, (int []) { 3, 2, 1 }, 3, TRUE); test_ia_is_permutation ((int []) { 1, 1, 2 }, (int []) { 1, 2, 3 }, 3, FALSE); test_ia_is_permutation ((int []) { 1, 2, 3 }, (int []) { 1, 1, 3 }, 3, FALSE); // A few longer interesting examples. test_ia_is_permutation ((int []) { 5, 7, 7, 3, 3, 3, 1, 1, 1, 1 }, (int []) { 1, 3, 7, 5, 1, 3, 7, 1, 3, 1 }, 10, TRUE); test_ia_is_permutation ((int []) { 5, 7, 7, 3, 3, 3, 1, 1, 1, 1 }, (int []) { 1, 3, 7, 5, 3, 3, 7, 1, 3, 1 }, 10, FALSE); // Report fprintf (stderr, "%d tests conducted.\n", tests); fprintf (stderr, "%d errors encountered.\n", errors); // And we're done return errors; } // main