void testPriorityQueue(){ PriorityQueue<int> mycontainer; cout << "\n\n Begin test function for the PriorityQueue<T> class\n"; // Testing the enqueue function cout << "Testing size of new empty container: " << mycontainer.length() << endl; cout << "Testing enqueue(1), length(), and isEmpty() functions. mycontainer is empty? " << (mycontainer.isEmpty() ? " true\n" : "false\n"); mycontainer.enqueue(1); cout << "Size is " << mycontainer.length() << endl; cout << "Testing enqueue(2), length(), and isEmpty() functions. mycontainer is empty? " << (mycontainer.isEmpty() ? " true\n" : "false\n"); mycontainer.enqueue(2); cout << "Size is " << mycontainer.length() << endl; cout << "Testing enqueue(2), length(), and isEmpty() functions. mycontainer is empty? " << (mycontainer.isEmpty() ? " true\n" : "false\n"); mycontainer.enqueue(2); cout << "Size is " << mycontainer.length() << endl; cout << "Testing enqueue(2), length(), and isEmpty() functions. mycontainer is empty? " << (mycontainer.isEmpty() ? " true\n" : "false\n"); mycontainer.enqueue(2); cout << "Size is " << mycontainer.length() << endl; cout << "Testing enqueue(2), length(), and isEmpty() functions. mycontainer is empty? " << (mycontainer.isEmpty() ? " true\n" : "false\n"); mycontainer.enqueue(2); cout << "Size is " << mycontainer.length() << endl; cout << "Testing enqueue(2), length(), and isEmpty() functions. mycontainer is empty? " << (mycontainer.isEmpty() ? " true\n" : "false\n"); cout << "Size is " << mycontainer.length() << endl << endl; int size = mycontainer.length(); cout << "Testing pop_back(), front(), back(), length() and isEmpty() functions \n" << "in a for loop with iterations greater than container size."; for (int i = 0; i < size + 1; i++) { cout << "\nIteration: " << i + 1 << "\n"; if (!mycontainer.isEmpty()) { cout << " mycontainer is empty? " << (mycontainer.isEmpty() ? " true\n" : "false\n"); cout << "PriorityQueue size before pop is " << mycontainer.length() << endl; //cout<<"Front of container is " << mycontainer.front()<<endl; //cout<<"Back of container is " << mycontainer.back()<<endl; //cout << "Popping: " << mycontainer.front() << endl << endl; mycontainer.pop_back(); } else { cout << "The PriorityQueue is empty.\n"; } cout << "PriorityQueue size is now: " << mycontainer.length() << endl; } cout << "\nFinished for loop\n"; cout << "\nTesting the reference for front() and back() functions.\n"; cout << "Start with int test = 7. mycontainer.enqueue(test)\n"; int test = 7; mycontainer.enqueue(test); cout << "Testing with test = 8. test=mycontainer.front(). mycontainer.front() = 13 \n"; test = 8; test = mycontainer.front(); mycontainer.front() = 13; cout << "Test is now " << test << " front of container is " << mycontainer.front() << " back of container is " << mycontainer.back() << endl; test = 11; mycontainer.enqueue(test); cout << "Back of container is: " << mycontainer.back() << endl; cout << "Test is now " << test << " front of container is " << mycontainer.front() << " back of container is " << mycontainer.back() << endl; mycontainer.back() = test; cout << "Test is now " << test << " front of container is " << mycontainer.front() << " back of container is " << mycontainer.back() << endl; cout << "mycontainer size is " << mycontainer.length() << endl; cout << "\nTesting the clear() function: \n"; mycontainer.clear(); cout << "mycontainer size now is " << mycontainer.length() << " mycontainer is empty: " << (mycontainer.isEmpty() ? " true\n" : "false\n"); cout << "\nTesting assignment operator: container2 = mycontainer\n"; cout << "Filling mycontainer with ints starting at 42\n"; size = 5; // Fill mycontainer with ints to test copy constructor for (int i = 0; i < size; i++) { mycontainer.enqueue(i + 41); } cout << "mycontainer size now is " << mycontainer.length() << " mycontainer is empty: " << (mycontainer.isEmpty() ? " true\n" : "false\n"); PriorityQueue<int> container2; container2 = mycontainer; cout << "mycontainer size is: " << mycontainer.length() << endl; cout << "container2 size is: " << container2.length() << endl; cout << "Testing the contents of container2 and mycontainer using back() and pop_back() functions:\n"; size = container2.length(); for (int i = 0; i < size; i++) { cout << "Attempting front and pop functions. Iteration: " << i + 1 << "\n"; // Don't perform the operation if either container is empty. // Output should be the same for both containers if (!container2.isEmpty() || !mycontainer.isEmpty()) { cout << "\tcontainer2 - front(): " << container2.front() << " back(): " << container2.back() << endl; container2.pop_back(); cout << "\tmycontainer - front(): " << mycontainer.front() << " back(): " << mycontainer.back() << endl; mycontainer.pop_back(); } else { cout << "Containers are empty.\n"; } } cout << "\nTesting the copy constructor. Filling mycontainer ints\n"; size = 5; // Fill mycontainer with ints to test copy constructor for (int i = 0; i < size; i++) { mycontainer.enqueue(i + 41); } cout << "mycontainer size now is " << mycontainer.length() << " mycontainer is empty: " << (mycontainer.isEmpty() ? " true\n" : "false\n"); PriorityQueue<int> container3(mycontainer); cout << "container3 size is: " << container3.length(); cout << "\nTesting the contents of container3 and mycontainer using back() and pop_back() functions:\n"; size = container3.length(); for (int i = 0; i < size; i++) { cout << "Attempting front and pop functions. Iteration: " << i + 1 << "\n"; // Don't perform the operation if either container is empty. // Output should be the same for both containers if (!container3.isEmpty() || !mycontainer.isEmpty()) { cout << "\tcontainer3 - front(): " << container3.front() << " back(): " << container3.back() << endl; container3.pop_back(); cout << "\tmycontainer - front(): " << mycontainer.front() << " back(): " << mycontainer.back() << endl; mycontainer.pop_back(); } else { cout << "Containers are empty.\n"; } } cout << "\nEnd of test function for the PriorityQueue<T> class\n\n"; }
int main() { // print my name and this assignment's title cout << "Lab 11a, The \"Write And Test A Priority Queue Template\" Program \n"; cout << "Programmer: Licong Wang\n"; cout << "Editor(s) used: Visual studio 2013\n"; cout << "Compiler(s) used: Microsoft c++ complier\n"; cout << "File: " << __FILE__ << endl; cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl; PriorityQueue<int> pq; cout << "\nTest size and empty" << endl; cout << "The expected size is 0, the acutal size is " << pq.size() << endl; assert(pq.size() == 0); cout << "Expect it to be empty: "; if (pq.empty()) cout << "Empty" << endl; else cout << "Not Empty" << endl; assert(pq.empty()); cout << "\nEnqueue values 1 to 5" << endl; for (int i = 1; i < 6; i++) { pq.enqueue(i); } cout << "\nTest size and empty again" << endl; cout << "The expected size is 5, the acutal size is " << pq.size() << endl; assert(pq.size() == 5); cout << "Expect it to be not empty: "; if (pq.empty()) cout << "Empty" << endl; else cout << "Not Empty" << endl; assert(!(pq.empty())); cout << "\nCheck its front and back, expecting 5 and 3, accroding to the rule of binary tree" << endl; cout << "Front: " << pq.front() << endl; cout << "back: " << pq.back() << endl; assert(pq.front() == 5); assert(pq.back() == 3); cout << "\nCheck the heap array using copy pop, expecting values from 5-1, " << "since the highest value is popped each time" << endl; for (PriorityQueue<int> copy = pq; copy.size(); ) cout << copy.dequeue() << " "; cout << "\n\nNow dequeue once" << endl; pq.dequeue(); cout << "\nTest size again" << endl; cout << "The expected size is 4, the acutal size is " << pq.size() << endl; assert(pq.size() == 4); cout << "\nCheck its front and back, expecting 4 and 1" << endl; cout << "Front: " << pq.front() << endl; cout << "back: " << pq.back() << endl; assert(pq.front() == 4); assert(pq.back() == 1); cout << "\ncheck the heap array using copy pop, expecting values from 4-1, " << "since 5 is already dequeued" << endl; for (PriorityQueue<int> copy = pq; copy.size(); ) cout << copy.dequeue() << " "; cout << endl; { cout << "\nObject Copy Testing" << endl; const PriorityQueue<int> copy = pq; cout << "\nTest size and empty" << endl; cout << "The expected size is 4, the acutal size is " << pq.size() << endl; assert(pq.size() == 4); cout << "Expect it to be not empty: "; if (pq.empty()) cout << "Empty" << endl; else cout << "Not Empty" << endl; assert(!(pq.empty())); cout << "\ncheck the heap array using copy pop, expecting values from 4-1" << endl; for (PriorityQueue<int> copy2 = copy; copy2.size(); ) cout << copy2.dequeue() << " "; cout << endl; } { cout << "\nObject Assignment Testing" << endl; PriorityQueue<int> copy; copy = pq; cout << "\nTest size and empty" << endl; cout << "The expected size is 4, the acutal size is " << pq.size() << endl; assert(pq.size() == 4); cout << "Expect it to be not empty: "; if (pq.empty()) cout << "Empty" << endl; else cout << "Not Empty" << endl; assert(!(pq.empty())); cout << "\ncheck the heap array using copy pop, expecting values from 4-1" << endl; for (PriorityQueue<int> copy2 = copy; copy2.size();) cout << copy2.dequeue() << " "; cout << endl; } cout << "\nBack to original object, test clear" << endl; pq.clear(); cout << "\nTest size and empty" << endl; cout << "The expected size is 0, the acutal size is " << pq.size() << endl; assert(pq.size() == 0); cout << "Expect it to be empty: "; if (pq.empty()) cout << "Empty" << endl; else cout << "Not Empty" << endl; assert(pq.empty()); cout << "\nCheck its front and back, expecting 0 and 0, dummy got returned" << endl; cout << "Front: " << pq.front() << endl; cout << "back: " << pq.back() << endl; assert(pq.front() == 0); assert(pq.back() == 0); cout << "\nPress ENTER to continue..." << endl; cin.get(); }
//this is the same as dijkstrasAlgorithm except for one thing which is commented vector<Node *> aStar(BasicGraph& graph, Vertex* start, Vertex* end) { graph.resetData(); Vertex* unDefined; Vertex* currentNode; start->cost=0.0; PriorityQueue<Vertex*> vertexPrioQueue; map<Vertex*,Vertex*> predecessor; for (Node *node :graph.getNodeSet()){ if (node!=start){ node->cost=INFINITY; predecessor[node]=unDefined; } vertexPrioQueue.enqueue(node,node->cost);; } double alt; while (!vertexPrioQueue.isEmpty()){ currentNode= vertexPrioQueue.front(); vertexPrioQueue.dequeue(); currentNode->setColor(YELLOW); currentNode->visited=true; if (currentNode==end){ break; } for(Node *node :graph.getNeighbors(currentNode)){ if (!node->visited){ alt=currentNode->cost+graph.getArc(currentNode,node)->cost; if (alt<node->cost){ node->cost=alt; predecessor[node]=currentNode; //this is the change, the queuepriority comes from the node cost + the heuristic vertexPrioQueue.changePriority(node,node->cost+node->heuristic((end))); } } } currentNode->setColor(GREEN); } if(predecessor[end]==unDefined){ vector<Vertex*> path; return path; } else{ stack<Vertex*> vertexStack; vector<Vertex*> path; currentNode=end; while (currentNode!=start){ vertexStack.push(currentNode); currentNode=predecessor[currentNode]; } vertexStack.push(start); while (!vertexStack.empty()){ path.push_back(vertexStack.top()); vertexStack.pop(); } return path; } }
vector<Node *> dijkstrasAlgorithm(BasicGraph& graph, Vertex* start, Vertex* end) { graph.resetData(); //set as predescessor if that is undefined Vertex* unDefined; //the current node we are checking Vertex* currentNode; //sets startnode cost to zero start->cost=0.0; //create prioqueue PriorityQueue<Vertex*> vertexPrioQueue; //used to keep track of all predeccesors map<Vertex*,Vertex*> predecessor; //set all costs, sets predecessor and adds the to the queue for (Node *node :graph.getNodeSet()){ //all nodes but start should have infinity cost if (node!=start){ node->cost=INFINITY; predecessor[node]=unDefined; } //add all nodes to queue vertexPrioQueue.enqueue(node,node->cost); } //keep track of the alternative cost double alt; //while the queue is not empty while (!vertexPrioQueue.isEmpty()){ //put current node to the one with highest priority currentNode= vertexPrioQueue.front(); vertexPrioQueue.dequeue(); currentNode->setColor(YELLOW); currentNode->visited=true; if (currentNode==end){ break; } //check all the node's neighbors for(Node *node :graph.getNeighbors(currentNode)){ //if we have not visited that node if (!node->visited){ //we check the alternative cost alt=currentNode->cost+graph.getArc(currentNode,node)->cost; //if the alternative cost is lower then we set that to our new cost if (alt<node->cost){ node->cost=alt; predecessor[node]=currentNode; vertexPrioQueue.changePriority(node,alt); } } } currentNode->setColor(GREEN); } //if we havent found end if(predecessor[end]==unDefined){ vector<Vertex*> path; return path; } else{ //if we have found end we trace through the predecessor map to find the path stack<Vertex*> vertexStack; vector<Vertex*> path; currentNode=end; while (currentNode!=start){ vertexStack.push(currentNode); currentNode=predecessor[currentNode]; } vertexStack.push(start); while (!vertexStack.empty()){ path.push_back(vertexStack.top()); vertexStack.pop(); } return path; } }
#include <vespa/vespalib/util/priority_queue.h> using namespace vespalib; TEST("require that default priority order works") { PriorityQueue<int> queue; EXPECT_EQUAL(true, queue.empty()); EXPECT_EQUAL(0u, queue.size()); queue.push(5); queue.push(3); queue.push(7); queue.push(10); queue.push(2); EXPECT_EQUAL(false, queue.empty()); EXPECT_EQUAL(5u, queue.size()); EXPECT_EQUAL(2, queue.front()); queue.front() = 6; queue.adjust(); EXPECT_EQUAL(3, queue.front()); queue.pop_front(); EXPECT_EQUAL(5, queue.front()); queue.pop_front(); EXPECT_EQUAL(6, queue.front()); queue.pop_front(); EXPECT_EQUAL(7, queue.front()); queue.pop_front(); EXPECT_EQUAL(10, queue.front()); queue.pop_front(); EXPECT_EQUAL(true, queue.empty()); EXPECT_EQUAL(0u, queue.size()); }
int main() { // print my name and this assignment's title cout << "LAB 11a: Write And Test A Priority Queue Template\n"; cout << "Programmer: Jacky Chow\n"; cout << "Editor(s) used: Notepad++\n"; cout << "Compiler(s) used: Visual C++\n"; cout << "File: " << __FILE__ << endl; cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl; PriorityQueue<int> pq; int temp; cout << "Created a PriorityQueue<int> named pq\n"; cout << "Its size should be 0. It is: " << pq.size() << endl; assert(0 == pq.size()); if(pq.empty()) cout << "pq.empty() returned true that it was empty\n"; else cout << "Error: pq.empty() returned false for empty PQ\n"; assert(pq.empty()); cout << "\nEnqueuing the ints 13, 8, 4, 20, 10 into pq\n"; pq.enqueue(13); pq.enqueue(8); pq.enqueue(4); pq.enqueue(20); pq.enqueue(10); if(!pq.empty()) cout << "pq.empty() returned false that it was empty\n"; else cout << "Error: pq.empty() returned true for a non-empty PQ\n"; assert(!pq.empty()); cout << "\nIts size should now be 5. It is: " << pq.size() << endl; assert(5 == pq.size()); cout << "The front should be 20. It is: " << pq.front() << endl; assert(20 == pq.front()); cout << "The back should be 10. It is: " << pq.back() << endl; assert(10 == pq.back()); //const copy constructor test { cout << "\nCreating const copy with copy constructor\n"; const PriorityQueue<int> copy = pq; if(!copy.empty()) cout << "copy.empty() returned false that it was empty\n"; else cout << "Error: copy.empty() returned true for a non-empty PQ\n"; assert(!copy.empty()); cout << "Copy size should now be 5. It is: " << copy.size() << endl; assert(5 == copy.size()); } //operator= copy test { cout << "\nCreating copy with with operator=\n"; PriorityQueue<int> copy; cout << "Should initially have size 0. It is: " << copy.size() << endl; assert(copy.empty()); cout << "Assigning copy to = pq\n"; copy = pq; if(!copy.empty()) cout << "copy.empty() returned false that it was empty\n"; else cout << "Error: copy.empty() returned true for a non-empty copy\n"; assert(!copy.empty()); cout << "Copy 2's size should now be 5. It is: " << copy.size() << endl; assert(5 == copy.size()); cout << "The front should be 20. It is: " << copy.front() << endl; assert(20 == copy.front()); cout << "The back should be 10. It is: " << copy.back() << endl; assert(10 == copy.back()); cout << "Dequeuing the entire copy of pq: \n"; for(; copy.size();) cout << copy.dequeue() << ' '; cout << "\nCopy should now be size 0. It is: " << copy.size() << endl; assert(copy.empty()); if(copy.empty()) cout << "copy.empty() returned true that it was empty\n"; else cout << "Error: copy.empty() returned false for an empty copy\n"; assert(copy.empty()); } temp = pq.dequeue(); cout << "\nDequeuing root of pq. It should return 20. It is: " << temp << endl; assert(20 == temp); cout << "\nIts size should now be 4. It is: " << pq.size() << endl; assert(4 == pq.size()); cout << "The front should be 13. It is: " << pq.front() << endl; assert(13 == pq.front()); cout << "The back should be 8. It is: " << pq.back() << endl; assert(8 == pq.back()); cout << "\nNow using clear to clear pq\n"; pq.clear(); cout << "Size should now be 0. It is: " << pq.size() << endl; assert(0 == pq.size()); if(pq.empty()) cout << "pq.empty() returned true that it was empty\n"; else cout << "Error: pq.empty() returned false for empty PQ\n"; assert(pq.empty()); }