Example #1
0
/*
 * 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;
    
    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;
    
    cout << endl << "Dequeuing all the rest: ";
    while (!pq.isEmpty())
        cout << pq.dequeueMax() << " ";
    cout << endl << "Pqueue should be empty.  Is it empty? " << pq.isEmpty() << endl;
    
}
Example #2
0
int main(){
    int numItems = 50;
    PQueue<int, 100> q;  // use PQ tempalate
    int i, x;
    srand(time(0));
    cout << "Enqueuing..." << endl;
    for( i = 1; i < numItems; i++) {
        x = rand() % 100;
        cout << x << " ";
        q.insert( x );
    }
    
    cout << "\n\nDequeuing..." << endl;
    for( i = 1; i < numItems; i++ ) {
        x = q.findMin();
        q.deleteMin();
        cout << x << " ";
    }
    cout << endl;
    
    assert(q.size() == 0);
    q.insert(3);
    q.insert(10);
    q.insert(9);
    
    x = q.findMin();
    q.deleteMin();
    cout << x << " ";

    x = q.findMin();
    q.deleteMin();
    cout << x << " ";


    q.insert(8);
    q.insert(2);
    
    x = q.findMin();
    q.deleteMin();
    cout << x << " ";
    
    x = q.findMin();
    q.deleteMin();
    cout << x << " ";
    
    x = q.findMin();
    q.deleteMin();
    cout << x << " ";
    
}
Example #3
0
void Encoding::buildEncodingForInput(ibstream& infile){


	map<int, int> charMap;
	getCharCount(infile, charMap);
	PQueue<node*> pq;
	map<int, int>::iterator iter;
	for (iter = charMap.begin(); iter != charMap.end(); iter++){
		node* tempNode = new node;
		tempNode->letter = iter->first;
		tempNode->weight = iter->second;
		tempNode->leftChild = tempNode->rightChild = NULL;
		//cout << tempNode->letter << endl;
		pq.add(tempNode, iter->second);
	}
	map <int, string> encodingMap;
	while (pq.size() != 1){
		node *parent = new node;
		parent->letter = 26;

		parent->weight = 0; //probably don't need this
		parent->leftChild = pq.extractMin();
		parent->leftChild->path +='0';

		parent->rightChild = pq.extractMin();

		parent->rightChild->path += '1';


		parent->weight = parent->leftChild->weight+parent->rightChild->weight;
		pq.add(parent, parent->weight);
	}

	//for testing

	map<int, int>::iterator it;
	node* tree = pq.extractMin();
	for (it = charMap.begin(); it != charMap.end(); it++){
		BuildEncodingMap(tree, mp[it->first], it->first);
		string temps = mp[it->first];
		char tempc = it->first;
		//cout << tempc << " has the encoding " << temps << endl;
	}

	//PrintTree(tree);
}
Example #4
0
void basicStructuralTests() {
    beginTest("Basic Structural Tests");

    /* This try ... catch system is designed to catch any errors that the
     * program explicitly generates.  It does not guard against runtime
     * crashes from errors like following a bad pointer, so if your
     * program crashes and pulls up the debugger, you should be looking
     * for memory errors.
     */
    try {

        /* The use of curly braces here introduces a new block scope.  We
         * use this so that we can construct multiple different priority
         * queues.
         */

        /* Basic test: Add 5 elements to the queue and ensure that
         * the size is correct at each step.
         */
        {
            logInfo("These tests will check size() isEmpty() without calling dequeueMin().");

            PQueue queue;
            checkCondition(queue.isEmpty(), "New priority queue should be empty.");
            checkCondition(queue.size() == 0, "New priority queue should have size 0.");

            for (int i = 0; i < 5; i++) {
                queue.enqueue("Test String");

                checkCondition(queue.size() == i + 1, "Queue should have proper size after inserting a value.");
                checkCondition(!queue.isEmpty(), "Queue containing elements should not be empty.");
                cout<<i<<endl;
            }
        }

        /* Slightly more complex test: Enqueue and dequeue elements, ensuring the
         * size matches at each step.
         */
        {
            logInfo("We're about to start calling dequeueMin().");
            PQueue queue;

            for (int i = 0; i < 5; i++) {
                queue.enqueue("Test String");
            }

            for (int i = 5; i > 0; i--) {
                checkCondition(queue.size() == i, "Queue should have proper size after dequeues.");
                checkCondition(!queue.isEmpty(), "Queue should not be empty before all elements are removed.");
                queue.dequeueMin();
            }

            checkCondition(queue.size() == 0, "After removing all elements, the queue should have size 0.");
            checkCondition(queue.isEmpty(), "After removing all elements, the queue should be empty.");
        }

        /* Getting harder: The value dequeued should always match the value of peek(). */
        {
            logInfo("This next test will check whether peek() matches dequeueMin().");
            PQueue queue;

            for (int i = 0; i < 5; i++) {
                queue.enqueue(randomString());
            }

            while (!queue.isEmpty()) {
                string expected = queue.peek();
                checkCondition(queue.dequeueMin() == expected, "Value returned by peek() matches value returned by dequeueMin()");
            }
        }

        /* A different one - let's make sure that peeking at an empty queue causes an
         * error.
         */
        {
            PQueue queue;
            bool didThrow = false;
            try {
                logInfo("About to peek into an empty queue.  This may cause a crash");
                logInfo("if your implementation is incorrect.");
                queue.peek();
            } catch (ErrorException&) {
                didThrow = true;
            }

            checkCondition(didThrow, "Priority queue uses 'error' when peek() called on empty queue.");
        }

        /* In the same vein - what happens if we dequeue from an empty queue? */
        {
            PQueue queue;
            bool didThrow = false;
            try {
                logInfo("About to dequeue from an empty queue.  This may cause a crash");
                logInfo("if your implementation is incorrect.");
                queue.dequeueMin();
            } catch (ErrorException&) {
                didThrow = true;
            }

            checkCondition(didThrow, "Priority queue uses 'error' when dequeueMin() called on empty queue.");
        }

    } catch (ErrorException& e) {
        cout << "TEST FAILURE: Unexpected exception: " << e.getMessage() << endl;
    } catch (exception& e) {
        cout << "TEST FAILURE: Unexpected exception: " << e.what() << endl;
    } catch (...) {
        cout << "TEST FAILURE: Unknown exception." << endl;
    }

    endTest("Basic Structural Tests");
}