Exemplo n.º 1
0
int main()
{

    srand(time(0));

    cout<<"Creating new doubly linked list\n";
    D_LinkedList<int> dll(10);
    cout<<"Outputting list\n";
    dll.printForward();
    cout<<"\n\n";

    cout<<"Appending Values\n";
    for (int var = 10; var < 20; ++var) {
        dll.append(var+1);
    }

    cout<<"Outputting list with list Size: "<<dll.size()<<endl;
    dll.printForward();
    cout<<"\n\n";

    cout<<"Prepending Values\n";
    for (int var = 10; var > 0; --var) {
        dll.prepend(var-1);
    }

    cout<<"Outputting list with list Size: "<<dll.size()<<endl;
    dll.printForward();
    cout<<"\n\n";
    cout<<"Outputting list in reverse order with list Size: "<<dll.size()<<endl;
    dll.printBackward();
    cout<<"\n\n";

    cout<<"First: "<<dll.first()<<endl;
    cout<<"Last: "<<dll.last()<<endl;
    cout<<"\n\n";

    cout<<"Pulling First: "<<dll.pullFront()<<endl;
    cout<<"Pulling Last: "<<dll.pullBack()<<endl;
    cout<<"Outputting list with list Size: "<<dll.size()<<endl;
    dll.printForward();
    cout<<"\n\n";

    cout<<"Inserting value 100 after\n";
    dll.insertAfter(9,100);
    cout<<"Outputting list with list Size: "<<dll.size()<<endl;
    dll.printForward();
    cout<<"\n\n";

    cout<<"Inserting value 50 before\n";
    dll.insertBefore(9,50);
    cout<<"Outputting list with list Size: "<<dll.size()<<endl;
    dll.printForward();
    cout<<"\n\n";

    cout<<"Testing operator[] \n";
    dll[9]=75;
    cout<<dll.get(9)<<endl;
    cout<<"\n\n";

    cout<<"Testing Extract\n";
    cout<<dll.extract(9)<<endl;
    cout<<"Outputting list with list Size: "<<dll.size()<<endl;
    dll.printForward();
    cout<<"\n\n";




    cout<<"\n****************************************************************\n";
    cout<<"Creating new circular linked list\n";
    C_LinkedList<int> cll(10);
    cout<<"Outputting list\n";
    cll.printList();
    cout<<"\n\n";

    cout<<"Appending Values\n";
    for (int var = 10; var < 20; ++var) {
        cll.append(var+1);
    }

    cout<<"Outputting list with list Size: "<<cll.size()<<endl;
    cll.printList();
    cout<<"\n\n";

    cout<<"Prepending Values\n";
    for (int var = 10; var > 0; --var) {
        cll.prepend(var-1);
    }
    cout<<"Outputting list with list Size: "<<cll.size()<<endl;
    cll.printList();
    cout<<"\n\n";

    cout<<"First: "<<cll.first()<<endl;
    cout<<"Last: "<<cll.last()<<endl;
    cout<<"\n\n";

    cout<<"Pulling First: "<<cll.pullFront()<<endl;
    cout<<"Pulling Last: "<<cll.pullBack()<<endl;
    cout<<"Outputting list with list Size: "<<cll.size()<<endl;
    cll.printList();
    cout<<"\n\n";

    cout<<"Inserting value 100 after\n";
    cll.insertAfter(9,100);
    cout<<"Outputting list with list Size: "<<cll.size()<<endl;
    cll.printList();
    cout<<"\n\n";

    cout<<"Inserting value 50 before\n";
    cll.insertBefore(9,50);
    cout<<"Outputting list with list Size: "<<cll.size()<<endl;
    cll.printList();
    cout<<"\n\n";

    cout<<"Testing operator[] \n";
    cll[9]=75;
    cout<<cll.get(9)<<endl;
    cout<<"\n\n";

    cout<<"Testing Extract\n";
    cout<<cll.extract(9)<<endl;
    cout<<"Outputting list with list Size: "<<cll.size()<<endl;
    cll.printList();
    cout<<"\n\n";





    Stack<int> stack(0);

    cout << "Populating Stack"<<endl;

    for (int var = 0; var < 10; ++var) {
        stack.push(var+1);
    }

    cout<<"Stack Size: "<<stack.size()<<endl;

    cout<<"\n\n";
    cout<<"Testing top() and pop() functions\n";
    cout<<stack.top()<<endl;
    stack.pop();
    cout<<stack.pop()<<endl;
    cout<<"\n\n";

    cout<<"testing copy construcor\n";
    Stack<int> newStack(stack);
    cout<<"stack top(): "<<stack.top()<<endl;
    cout<<"stack size(): "<<stack.size()<<endl;
    cout<<"newStack top(): "<<newStack.top()<<endl;
    cout<<"newStack size(): "<<newStack.size()<<endl;
    cout<<"\n\n";


//    cout<<"Clearing newStack\n";
//    newStack.clear();
//    cout<<"stack size(): "<<stack.size()<<endl;
//    cout<<"stack top(): "<<stack.top()<<endl;
//    cout<<"newStack size(): "<<newStack.size()<<endl;//outputs 0
//    cout<<"newStack top(): "<<newStack.top()<<endl;//causes subscript error
//    cout<<"\n\n";


    Queue<int> queue(0);

    cout << "Populating queue"<<endl;

    for (int var = 0; var < 10; ++var) {
        queue.pushBack(var+1);
    }

    cout<<"Queue Size: "<<queue.size()<<endl;

    cout<<"\n\n";
    cout<<"Testing front() and back() functions\n";
    cout<<queue.front()<<endl;
    cout<<queue.back()<<endl;
    cout<<"\n\n";

    cout<<"Testing PopFront() function\n";
    cout<<queue.popFront()<<endl;
    cout<<"Queue Size: "<<queue.size()<<endl;
    cout<<"\n\n";

    cout<<"testing copy construcor\n";
    Queue<int> newQueue(queue);
    cout<<"queue front(): "<<queue.front()<<endl;
    cout<<"queue size(): "<<queue.size()<<endl;
    cout<<"newQueue front(): "<<newQueue.front()<<endl;
    cout<<"newQueue size(): "<<newQueue.size()<<endl;
    cout<<"\n\n";

    cout<<"Popping Valued from newQueue\n";
    for (int i = 0; i < 5; ++i) {
        cout<<newQueue.popFront()<<endl;
    }
    cout<<"queue front(): "<<queue.front()<<endl;
    cout<<"queue size(): "<<queue.size()<<endl;
    cout<<"newQueue front(): "<<newQueue.front()<<endl;
    cout<<"newQueue size(): "<<newQueue.size()<<endl;
    cout<<"\n\n";



    cout<<"***********************************\nTesting Priority Linked List\n";

    P_LinkedList<int> pll;

    for (int i = 0; i < 20; ++i) {
        pll.append(i+1);
    }

    cout<<"Outputting list of size: "<<pll.size()<<endl;
    pll.printList();
    cout<<"\n\n";

    cout<<"Outputting peekMax()\n";
    cout<<pll.peekMax()<<endl;
    cout<<"Outputting popMax()\n";
    cout<<pll.popMax()<<endl;
     cout<<"Outputting list of size: "<<pll.size()<<endl;
     pll.printList();
     cout<<"\n\n";

     cout<<"Outputting peekMin()\n";
     cout<<pll.peekMin()<<endl;
     cout<<"Outputting popMin()\n";
     cout<<pll.popMin()<<endl;
     cout<<"Outputting list of size: "<<pll.size()<<endl;
     pll.printList();
     cout<<"\n\n";



    cout<<"*****************************************\nTesting Priority Queue\n";

    PriorityQueue<int> pq;

    for (int i = 0; i < 20; ++i) {
        pq.push((rand()%30)+1);
    }

    cout<<"Outputting list of size: "<<pq.size()<<endl;
    pq.printQueue();
    cout<<"\n\n";

    cout<<"OutPutting front()\n";
    cout<< pq.front()   <<endl;
    cout<<"OutPutting back()\n";
    cout<< pq.back()   <<endl;
    cout<<"\n\n";


    cout<<"pop()ing 10 elements\n";
    for (int i = 0; i < 10; ++i) {
        cout<<pq.pop()<<endl;
    }
    cout<<"\n\n";

    cout<<"Outputting list of size: "<<pq.size()<<endl;
    pq.printQueue();
    cout<<"\n\n";

    cout<<"Instanciating descending object\n";
    PriorityQueue<int> pqd(SortOrder::DEC);

    for (int i = 0; i < 10; ++i) {
        pqd.push((rand()%15)+1);
    }
    cout<<"Outputting list of size: "<<pqd.size()<<endl;
    pqd.printQueue();
    cout<<"\n\n";

    cout<<"OutPutting front()\n";
    cout<< pqd.front()   <<endl;
    cout<<"OutPutting back()\n";
    cout<< pqd.back()   <<endl;
    cout<<"\n\n";


    cout<<"pop()ing 5 elements\n";
    for (int i = 0; i < 5; ++i) {
        cout<<pqd.pop()<<endl;
    }
    cout<<"\n\n";

    cout<<"Outputting list of size: "<<pqd.size()<<endl;
    pqd.printQueue();
    cout<<"\n\n";


    cout<<"*************************************\nTesting Sorted Linked List\n";
    S_LinkedList<int> sll(SortOrder::ASC);

    for (int i = 0; i < 100; ++i) {
        sll.push((rand()%50)+1);
    }

    cout<<"Outputting lis of size: "<<sll.size()<<endl;
    sll.printList();
    cout<<"\n\n";

    return 0;
}
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();
}
Exemplo n.º 3
0
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";
}
Exemplo n.º 4
0
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());  
}