Skip to content
Snippets Groups Projects
testPQ.cpp 1.97 KiB
Newer Older
Marcus M. Darden's avatar
Marcus M. Darden committed
/*
 * Compile this test against your .h files to make sure they compile. Note how
 * the eecs281 priority queues can be constructed with the different types. We
 * suggest adding to this file or creating your own test cases to test your
 * priority queue implementations. 
 *
 * These tests are /not/ a complete test of your priority queues!
 */
#include "BinaryPQ.h"
#include "Eecs281PQ.h"
#include "JarJarPQ.h"
#include "PairingPQ.h"
#include "SortedPQ.h"

#include <cassert>

#include <iostream>
using std::cout;
using std::endl;

#include <string>
using std::string;

#include <vector>
using std::vector;

void testPriorityQueue(Eecs281PQ<int>& pq, const string& pqType);
void testPairing(vector<int>&);

int main()
{
    JarJarPQ<int> jarJarPq;
    SortedPQ<int> sortedPq;
    BinaryPQ<int> binaryPq;

    // Jar-Jar should pass already.
    testPriorityQueue(jarJarPq, "Jar-Jar");
    testPriorityQueue(sortedPq, "Sorted");
    testPriorityQueue(binaryPq, "Binary");

    vector<int> vec;
    vec.push_back(0);
    vec.push_back(1);
    testPairing(vec);
}

void testPriorityQueue(Eecs281PQ<int>& pq, const string& pqType)
{
    cout << "Testing priority queue: " << pqType << endl;

    pq.push(3);
    pq.push(4);
    assert(pq.size() == 2);
    assert(pq.top() == 4);

    pq.pop();
    assert(pq.size() == 1);
    assert(pq.top() == 3);
    assert(!pq.empty());

    pq.pop();
    assert(pq.size() == 0);
    assert(pq.empty());
}

void testPairing(vector<int> & vec)
{
    Eecs281PQ<int> * pq1 = new PairingPQ<int>(vec.begin(), vec.end());
    Eecs281PQ<int> * pq2 = new PairingPQ<int>(*((PairingPQ<int> *)pq1));
    // This line is different just to show two different ways to declare a
    // pairing heap: as an Eecs281PQ and as a PairingPQ. Yay for inheritance!
    PairingPQ<int> * pq3 = new PairingPQ<int>();
    *pq3 = *((PairingPQ<int> *)pq2);

    pq1->push(3);
    pq2->pop();
    pq1->size();
    pq1->empty();
    assert(pq1->top() == 3);

    delete pq1;
    delete pq2;
    delete pq3;
}