/* * Function: MoreQueueTest * Usage: MoreQueueTest(); * ----------------------- * Tests a few more enqueue, dequeueMax, some boundary cases explored. * Reports results of test to cout. */ void MorePQueueTest() { PQueue pq; cout << boolalpha; cout << endl << "----------- More pqueue testing functions -----------" << endl; cout << endl << "Enqueuing integers from 15 downto 1 (decreasing order)" << endl; for (int i = 15; i > 0; i--) pq.enqueue(i); cout << "Enqueuing duplicates for even numbers 2 to 14" << endl; for (int j = 2; j <= 14; j += 2) pq.enqueue(j); cout << "Dequeuing the top 10 elements: "; for (int k = 0; k < 10; k++) cout << pq.dequeueMax() << " "; cout << endl << "Dequeuing all the rest: "; while (!pq.isEmpty()) cout << pq.dequeueMax() << " "; cout << endl << "Pqueue should be empty. Is it empty? " << pq.isEmpty() << endl; // The line below calling DequeueMax should cause your program to halt with an error. // Once you've verified that your PQueue correctly raises this error // you should comment out the call, so that you can continue on with the other tests. cout << endl << "This next test raises an error if your pqueue is working correctly." << endl; cout << "Once you verify the test, comment it out to move on to the other tests." << endl; cout << "(Comment out line " << __LINE__ + 1 << " in the file " << __FILE__ << ")." << endl; cout << "Dequeue from empty pqueue returns " << pq.dequeueMax() << endl; cout << endl << "Hit return to continue: "; GetLine(); }
/* * Function: BasicPQueueTest * Usage: BasicQueueTest(); * ------------------------ * Runs a test of the PQueue focusing on simple enqueue, dequeueMax. * Reports results of test to cout. */ void BasicPQueueTest() { PQueue pq; cout << boolalpha; // configure stream to print booleans as true/false instead of 1/0 cout << endl << "----------- Testing Basic PQueue functions -----------" << endl; cout << "The pqueue was just created. Is it empty? " << pq.isEmpty() << endl; cout << endl << "Now enqueuing integers from 1 to 10 (increasing order)" << endl; for (int i = 1; i <= 10; i++) pq.enqueue(i); cout << "Pqueue should not be empty. Is it empty? " << pq.isEmpty() << endl; cout << "Pqueue should have size = 10. What is size? " << pq.size() << endl; pq.printDebuggingInfo(); cout << "Dequeuing the top 5 elements: "; for (int j = 0; j < 5; j++) cout << pq.dequeueMax() << " "; cout << endl << "Pqueue should have size = 5. What is size? " << pq.size() << endl; pq.printDebuggingInfo(); cout << endl << "Dequeuing all the rest: "; while (!pq.isEmpty()) cout << pq.dequeueMax() << " "; cout << endl << "Pqueue should be empty. Is it empty? " << pq.isEmpty() << endl; pq.printDebuggingInfo(); cout << endl << "Hit return to continue: "; GetLine(); }
/* * Function: MoreQueueTest * Usage: MoreQueueTest(); * ----------------------- * Tests a few more enqueue, dequeueMax, some boundary cases explored. * Reports results of test to cout. */ void MorePQueueTest() { PQueue<int, int> pq; cout << boolalpha; cout << endl << "----------- More pqueue testing functions -----------" << endl; cout << endl << "Enqueuing integers from 15 downto 1 (decreasing order)" << endl; for (int i = 15; i > 0; i--) pq.enqueue(i, i); cout << "Enqueuing duplicates for even numbers 2 to 14" << endl; for (int j = 2; j <= 14; j += 2) pq.enqueue(j, j); cout << "Dequeuing the top 10 elements: "; for (int k = 0; k < 10; k++) cout << pq.dequeueMax() << " "; cout << endl << "Dequeuing all the rest: "; while (!pq.isEmpty()) cout << pq.dequeueMax() << " "; cout << endl << "Pqueue should be empty. Is it empty? " << pq.isEmpty() << endl; }
void PQSort(int array[], int nElems) { PQueue pq; for (int i = 0; i < nElems; i++) pq.enqueue(array[i]); for (int i = nElems-1; i >= 0; i--) array[i] = pq.dequeueMax(); }
/* * Function: RunEnqueueDequeueTrial * -------------------------------- * Runs the enqueue & dequeue time trials for the specified * pqueue size. Reports results to cout. * Note that we just randomly choose numbers to insert. * The amount of time it takes to do one enqueue/dequeue is * too small to be accurately measured, so we do many iterations in * a loop and time that. */ void RunEnqueueDequeueTrial(int size) { PQueue pq; for (int i = 0; i < size; i++) pq.enqueue(RandomInteger(1,size)); cout << "Time to enqueue into " << size << "-element pqueue: " << flush; double start = GetCurrentTime(); for (int j = 0; j < NumRepetitions; j++) pq.enqueue(RandomInteger(1, 2*size)); cout << 1000*(GetCurrentTime() - start)/NumRepetitions << " usecs" << endl; cout << "Time to dequeue from " << size << "-element pqueue: " << flush; start = GetCurrentTime(); for (int k = 0; k < NumRepetitions; k++) pq.dequeueMax(); cout << 1000*(GetCurrentTime() - start)/NumRepetitions << " usecs" << endl; }
/* * Function: RunMemoryTrial * ------------------------ * Fills a pqueue to specified size and reports memory usage. Then does * a bunch of enqueue-dequeue operations to jumble things up and reports * on the memory usage again. Reports results to cout. */ void RunMemoryTrial(int size) { PQueue pq; cout << endl << "Running memory trial on " << size << "-element pqueue" << endl; for (int i = 0; i < size; i++) pq.enqueue(RandomInteger(1, size)); cout << "After consecutive enqueues, " << size << "-element pqueue is using " << pq.bytesUsed()/1000 << " KB of memory" << endl; int num = size; for (int j = 0; j < NumRepetitions; j++) { /* do a bunch of enqueue/dequeue ops */ if (RandomChance(.5)) { pq.enqueue(RandomInteger(0, size)); num++; } else { pq.dequeueMax(); num--; } } cout << "After more enqueue/dequeue, " << num << "-element pqueue is using " << pq.bytesUsed()/1000 << " KB of memory" << endl; }