#include #include #include "queue.h" #define MAXNODES 4 #define BASE 9 void message(char *text); void error(char *function, char *message); int errors = 0; /* total number of error */ /****************************************************************************** message sends a string to stdout. ******************************************************************************/ void message(char *text){ printf("%s\n", text); } /****************************************************************************** "error" is used for reporting the failed function and a message indicating a possible reason for the failure. The error counter is incremented. ******************************************************************************/ void error(char *function, char *message){ errors++; printf("Error with %s:\n%s\n\n", function, message); } /****************************************************************************** main ******************************************************************************/ int main(){ qnode nodes[4]; /* pool of queue nodes */ qnode q; /* head of the queue */ qnode *qp; /* pointer to the head of the queue */ qnode *element; /* pointer to elements returned by remqu */ int i; /* index variable */ message("Initializing."); for ( i = 0; i < MAXNODES; i++ ) /* initialize the nodes and qp */ nodes[i].key = BASE + i; qp = &q; initqu(qp); /* the following tests are pretty self-explanatory */ message("Testing an empty queue."); if ( !emptyqu(qp) ) error("emptyqu", "Just-Initialized queue not reported as empty."); if ( remqu(qp) != NULL ) error("remqu", "Not returning NULL from an empty queue."); message("Inserting an element into the queue."); insqu(qp, &nodes[0]); message("Testing a queue with one element."); if ( emptyqu(qp) ) error("emptyqu", "Queue with one element reported as empty."); message("Removing queue element."); if ( (element = remqu(qp->next)) == NULL ) error("remqu", "Not returning queue element."); message("Checking key value of returned element."); if ( element->key != BASE + 0 ) error("remqu", "Returned the wrong element."); message("Checking empty queue again."); if ( !emptyqu(qp) ) error("emptyqu", "Removed last element; queue should be empty."); if ( remqu(qp->next) != NULL ) error("remqu", "Does not recognize an empty queue."); else if ( remqu(qp->prev) != NULL ) error("remqu", "Does not recognize an empty queue."); message("Inserting 4 elements into the queue."); insqu(qp, &nodes[0]); insqu(qp, &nodes[1]); insqu(qp->prev, &nodes[2]); insqu(qp->prev, &nodes[3]); message("Removing elements and checking keys."); if ( (remqu(qp->next))->key != BASE + 1) error("remqu", "Element removed was not the expected element."); if ( (remqu(qp->next))->key != BASE + 0) error("remqu", "Element removed was not the expected element."); if ( (remqu(qp->next))->key != BASE + 2) error("remqu", "Element removed was not the expected element."); if ( (remqu(qp->prev))->key != BASE + 3) error("remqu", "Element removed was not the expected element."); message("Checking empty queue."); if ( !emptyqu(qp) ) error("emptyqu", "Removed last element; queue should be empty."); message("Beginning testing of \"ord\" functions."); nodes[0].key = nodes[1].key = BASE; message("Inserting two elements with the same key."); ordinsqu(qp, &nodes[0]); ordinsqu(qp, &nodes[1]); message("Checking order of insertion."); if (remqu(qp->next) != &nodes[0]) error("ordinsqu", "Elements not inserted in order."); message("Checking that \"search\" for non-existent element returns NULL."); if (ordremqu(qp, &nodes[2]) != NULL) error("ordremqu", "Should have returned NULL."); remqu(qp->next); if (!emptyqu(qp)) error("emptyqu", "Queue not empty."); message("Inserting two elements with the same key."); ordinsqu(qp, &nodes[0]); ordinsqu(qp, &nodes[1]); message("Checking that first element is removed."); if (ordremqu(qp, &nodes[0]) != &nodes[0]) error("ordremqu", "Removed wrong element."); remqu(qp->next); if (!emptyqu(qp)) error("emptyqu", "Queue not empty."); for ( i = 0; i < MAXNODES; i++ ) /* re-initialize the nodes */ nodes[i].key = BASE + i; message("Inserting 4 unique elements."); ordinsqu(qp, &nodes[1]); ordinsqu(qp, &nodes[3]); ordinsqu(qp, &nodes[0]); ordinsqu(qp, &nodes[2]); message("Checking order of insertion."); for (i = 0; i < MAXNODES; i++) if ((element = remqu(qp->next)) != &nodes[i]) error("ordinsqu", "Element not inserted in correct order."); message("Inserting 4 unique elements again."); ordinsqu(qp, &nodes[1]); ordinsqu(qp, &nodes[3]); ordinsqu(qp, &nodes[0]); ordinsqu(qp, &nodes[2]); message("Removing one element."); if (ordremqu(qp, &nodes[1]) == NULL) error("ordremqu", "Element not removed."); message("Attempting to remove non-existent element."); nodes[1].key = MAXNODES + 1; if (ordremqu(qp, &nodes[1]) != NULL) error("ordremqu", "Erroneously removed element."); message("Checking deletion order."); if (remqu(qp->next) != &nodes[0]) error("ordinsqu, ordremqu", "Insertion/Deletion error."); if (remqu(qp->next) != &nodes[2]) error("ordinsqu, ordremqu", "Insertion/Deletion error."); if (remqu(qp->next) != &nodes[3]) error("ordinsqu, ordremqu", "Insertion/Deletion error."); /* report findings */ message("\nTesting finished."); if ( errors > 0 ) printf("%d error%s reported.\n", errors, (errors == 1) ? "" : "s"); else message("No errors reported."); return 0; }