/** * is-permutation.utest.c * Some unit tests for the is_in_order procedure. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include #include #include #include "int-testing-tools.h" #include "string-testing-tools.h" // +-----------+------------------------------------------------------ // | Variables | // +-----------+ static int errors = 0; // A count of errors static int tests = 0; // A count of tests conducted // +---------+-------------------------------------------------------- // | Helpers | // +---------+ /** * 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) { ++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 // +------+----------------------------------------------------------- // | Main | // +------+ int main () { test_all_permutations ((int[]) { }, 0); test_all_permutations ((int[]) { 1 }, 1); test_all_permutations ((int[]) { 1, 1 }, 2); test_all_permutations ((int[]) { 1, 2 }, 2); test_non_permutation ((int []) { 1 }, (int []) { 2 }, 1); test_non_permutation ((int []) { 2 }, (int []) { 1 }, 1); test_non_permutation ((int []) { 1,1 }, (int []) { 2,2 }, 2); test_non_permutation ((int []) { 1,1 }, (int []) { 1,2 }, 2); test_non_permutation ((int []) { 1,1 }, (int []) { 2,1 }, 2); test_non_permutation ((int []) { 2,1 }, (int []) { 1,1 }, 2); test_non_permutation ((int []) { 1,1,2 }, (int []) { 1,2,2 }, 3); if (errors) { printf ("%d tests conducted.\n", tests); printf ("%d errors encountered.\n", errors); return EXIT_FAILURE; } else { return EXIT_SUCCESS; } } // main