/** * is-permutation.utest.c * Some unit tests for the is_in_order procedure. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include #include #include #include "is-permutation.h" // +-----------+------------------------------------------------------ // | Variables | // +-----------+ static int errors = 0; // A count of errors static int tests = 0; // A count of tests conducted // +---------+-------------------------------------------------------- // | Helpers | // +---------+ /** * Print an array of ints. */ void print_int_array (int values[], int size) { int i; if (size == 0) { printf ("{}"); return; } printf ("{%3d", values[0]); for (i = 1; i < size; i++) printf(",%3d", values[i]); printf ("}"); } // print_int_array /** * Swap two elements in an array of ints. */ void swap (int a[], int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } // swap /** * Copy an array */ void copy_array (int source[], int target[], int len) { int i; for (i = 0; i < len; i++) target[i] = source[i]; } // copy_array /** * Procedure: * test_permutations * Parameters: * arr, an array of integers * len, an integer * k, in integer * Purpose: * Make a bunch of permutations of arr (by permuting the first k * elements), and see if each is considered a permutation of original. */ void test_permutations (int arr[], int len, int k, int original[]) { int i; int copy[len]; if (k == 0) { // printf ("Current permutation: "); // print_int_array (arr, len); // printf ("\n"); ++tests; if (! is_permutation (original, arr, len)) { ++errors; printf ("is_permutation() incorrectly returned FALSE for\n"); printf (" A1: "); print_int_array (original, len); printf ("\n"); printf (" A2: "); print_int_array (arr, len); printf ("\n"); } } // if (k == 0) else // if (k > 0) { for (i = 0; i < k; i++) { copy_array (arr, copy, len); swap (copy, i, k-1); test_permutations (copy, len, k-1, original); } } // if (k > 0) } // test_permutations void test_all_permutations (int arr[], int len) { test_permutations (arr, len, len, arr); } // test_all_permutations void test_non_permutation (int a1[], int a2[], int len) { int result = is_permutation (a1, a2, len); ++tests; if (result != 0) { ++errors; printf ("is_permutation incorrectly returned returned TRUE for\n"); printf (" A1: "); print_int_array (a1, len); printf ("\n"); printf (" A2: "); print_int_array (a2, len); printf ("\n"); } } // test_non_permutation void test_permutation (int a1[], int a2[], int len) { int result = is_permutation (a1, a2, len); ++tests; if (!result) { ++errors; printf ("is_permutation incorrectly returned returned FALSE for\n"); printf (" A1: "); print_int_array (a1, len); printf ("\n"); printf (" A2: "); print_int_array (a2, len); printf ("\n"); } } // test_permutation // +------+----------------------------------------------------------- // | Main | // +------+ int main () { int a0a[] = { }; int a1a[] = { 1 }; int a1b[] = { 2 }; int a2a[] = { 1, 1 }; int a2b[] = { 1, 2 }; int a2c[] = { 2, 1 }; int a3a[] = { 1, 1, 1 }; int a3b[] = { 1, 2, 3 }; int a3c[] = { 1, 3, 2 }; int a3d[] = { 2, 1, 3 }; int a3e[] = { 2, 3, 1 }; int a3f[] = { 3, 1, 2 }; int a3g[] = { 3, 2, 1 }; int a5b[] = { 1, 2, 3, 4, 5 }; test_permutation (a0a, a0a, 0); test_permutation (a1a, a1a, 1); test_permutation (a2a, a2a, 2); test_permutation (a2b, a2b, 2); test_permutation (a2b, a2c, 2); test_non_permutation (a2b, a2a, 2); test_permutation (a3a, a3a, 3); test_permutation (a3b, a3b, 3); test_permutation (a3b, a3c, 3); test_permutation (a3b, a3d, 3); test_permutation (a3b, a3e, 3); test_permutation (a3b, a3f, 3); test_permutation (a3b, a3g, 3); test_permutations (a5b, 5, 5, a5b); test_non_permutation ((int[]) {1,2,3}, (int []) {1,3,3}, 3); test_non_permutation ((int[]) {1,1,3}, (int []) {1,3,3}, 3); printf ("%d tests conducted.\n", tests); if (errors) { printf ("%d errors encountered.\n", errors); return EXIT_FAILURE; } else { printf ("All tests succeeded!\n"); return EXIT_SUCCESS; } } // main