/** * Alex Cohn and Jordan Shkolnick * 3/9/10 * CSC323: make * qtest.c: Tests the quicksort method **/ #include "quicksort.h" #include #include #include #include int main (int argc, char** argv) { //Seed the random number generator srand(time(NULL)); int i; //Array of 10 strings in sorted order, to test char* correct[10] = {"alpha","bravo","charlie","delta", "echo","foxtrot","golf","hotel","india","juliet"}; //Smoke test: If it can't sort the first ten words of // the phonetic alphabet correctly, something's wrong. char* alpha[10] = {"hotel","india","foxtrot","bravo", "juliet","charlie","alpha","delta","echo","golf"}; quicksort(alpha,0,9); for (i=0;i<10;i++) assert(strcmp(alpha[i],correct[i])==0); //Test an array that is already sorted correctly char* sorted[10] = {"alpha","bravo","charlie","delta", "echo","foxtrot","golf","hotel","india","juliet"}; quicksort(sorted,0,9); for (i=0;i<10;i++) { assert(strcmp(correct[i],sorted[i])==0); } //Test an array with a swap in the middle, but the ends correct char* middle[10] = {"alpha","bravo","charlie","delta", "foxtrot","echo","golf","hotel","india","juliet"}; quicksort(middle,0,9); for (i=0;i<10;i++) assert(strcmp(correct[i],middle[i])==0); //Test arrays of various lengths char* length[10] = {"hotel","india","foxtrot","bravo", "juliet","charlie","alpha","delta","echo","golf"}; int j; for (j=0;j<10;j++) { quicksort(length,0,j); } for (i = 0; i < 10; i++) assert(strcmp(correct[i],length[i])==0); //Test an empty array. This shouldn't segfault. char* empty[0] = {}; quicksort(empty,0,0); //Test an array with one element in it. char* one[1] = {"one"}; quicksort(one,0,1); assert(strcmp(one[0],"one")); //Test an array with two equal elements in it char* two[2] = {"one","one"}; quicksort(two,0,1); assert(strcmp(two[0],"one")==0); assert(strcmp(two[1],"one")==0); //If we make it here, no assertions have failed. Report that fact. fprintf(stderr, "All tests succeeded.\n"); return 0; }