/* quicksort_unit_test.c - unit testing of a simple Quicksort. Samuel A. Rebelsky Version 1.1 of 11 February 2007 */ #include #include #include "quicksort.h" #define SIZE 64 /* Fill a with n random numbers, arranged in increasing order. */ void fill(int a[], int n) { int i; a[0] = random() % 100; for (i = 1; i < n; i++) { a[i] = a[i-1] + (abs(random() % 3)); } } /* fill(int[], int) */ void shuffle(int a[], int n) { int i; for (i = 0; i < n; i++) ia_swap(a, i, abs(random()) % n); } int main(int argc, char **argv) { /* Tests . */ int sorted[SIZE]; /* Original array, created in sorted order. */ int randomized[SIZE]; /* Copy of the array, randomized. */ int a[SIZE]; /* Copy that actually gets sorted. */ int n; /* Counter variable for sizes. */ int i; int mismatches = 0; int errors = 0; for (n = 0; n < SIZE; n++) { fill(sorted, n); ia_copy(sorted, randomized, n); shuffle(randomized, n); ia_copy(randomized, a, n); quicksort(a, n); mismatches = 0; for (i = 0; i < n; i++) if (a[i] != sorted[i]) ++mismatches; if (mismatches) { ++errors; printf("%d mismatches found in size %d.\n", mismatches, n); printf("Original: "); ia_print(stdout, randomized, n); printf("Result: "); ia_print(stdout, a, n); printf("Sorted: "); ia_print(stdout, sorted, n); } } /* for */ if (errors) { printf("%d errors encountered.\n", errors); exit(EXIT_FAILURE); } printf("No errors encountered.\n"); exit(EXIT_SUCCESS); } /* main(int, char**) */