/************************************************* * Linked_queue_tester * A class for testing linked queues. * * Author: Douglas Wilhelm Harder * Copyright (c) 2014 by Douglas Wilhelm Harder. All rights reserved. * * DO NOT EDIT THIS FILE *************************************************/ #ifndef LINKD_QUEUE_TESTER_H #define LINKD_QUEUE_TESTER_H #ifndef nullptr #define nullptr 0 #endif #include "Exception.h" #include "Tester.h" #include "Linked_queue.h" #include template class Linked_queue_tester:public Tester< Linked_queue > { using Tester< Linked_queue >::object; using Tester< Linked_queue >::command; public: Linked_queue_tester( Linked_queue *obj = nullptr ):Tester< Linked_queue >( obj ) { // empty } void process(); }; /**************************************************** * void process() * * Process the current command. For linked queues, these include: * * Accessors * * size n size the size equals n * list_size n list_size the size of the linked list is n * empty b empty the result is the Boolean value b * front n front n is the first element in the linked list * front! front the underflow exception is thrown * * Mutators * * push n push the element can be pushed onto the back (always succeeds) * pop n pop the front element can be popped and equals n * pop! pop the underflow exception is thrown * * Others * cout cout << list print the list (for testing only) * assign operator = assign this list to a new list * summary prints the amount of memory allocated * minus the memory deallocated * details prints a detailed description of which * memory was allocated with details * !! use the previous command, e.g. 5 push 3 * 6 !! 7 // same as push 7 * !n use the command used in line n 7 front 7 * 8 !7 9 // same as push 9 * ****************************************************/ template void Linked_queue_tester::process() { if ( command == "new" ) { object = new Linked_queue(); std::cout << "Okay" << std::endl; } else if ( command == "size" ) { // check if the size equals the next integer read int expected_size; std::cin >> expected_size; int actual_size = object->size(); if ( actual_size == expected_size ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in size(): expecting the value '" << expected_size << "' but got '" << actual_size << "'" << std::endl; } } else if ( command == "list_size" ) { // check if the list size equals the next integer read int expected_list_size; std::cin >> expected_list_size; int actual_list_size = object->list_size(); if ( actual_list_size == expected_list_size ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in list_size(): expecting the value '" << expected_list_size << "' but got '" << actual_list_size << "'" << std::endl; } } else if ( command == "empty" ) { // check if the empty status equals the next Boolean read bool expected_empty; std::cin >> expected_empty; bool actual_empty = object->empty(); if ( actual_empty == expected_empty ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in empty(): expecting the value '" << expected_empty << "' but got '" << actual_empty << "'" << std::endl; } } else if ( command == "front" ) { // checks if the first object in the linked list equals the next object read Type expected_front; std::cin >> expected_front; Type actual_front = object->front(); if ( actual_front == expected_front ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in front(): expecting the value '" << expected_front << "' but got '" << actual_front << "'" << std::endl; } } else if ( command == "front!" ) { // front of an empty list - catch an exception Type actual_front; try { actual_front = object->front(); std::cout << "Failure in front(): expecting to catch an exception but got '" << actual_front << "'" << std::endl; } catch( underflow ) { std::cout << "Okay" << std::endl; } catch (...) { std::cout << "Failure in front(): expecting an underflow exception but caught a different exception" << std::endl; } } else if ( command == "push" ) { // push the next object read to the front of the linked list Type n; std::cin >> n; object->push( n ); std::cout << "Okay" << std::endl; } else if ( command == "pop" ) { // pop the first object from the linked list Type expected_popped_element; std::cin >> expected_popped_element; Type actual_popped_element = object->pop(); if ( actual_popped_element == expected_popped_element ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in pop(): expecting the value '" << expected_popped_element << "' but got '" << actual_popped_element << "'" << std::endl; } } else if ( command == "pop!" ) { // pop from an empty list - catch an exception Type actual_popped_element; try { actual_popped_element = object->pop(); std::cout << "Failure in pop(): expecting to catch an exception but got '" << actual_popped_element << "'" << std::endl; } catch( underflow ) { std::cout << "Okay" << std::endl; } catch (...) { std::cout << "Failure in pop(): expecting an underflow exception but caught a different exception" << std::endl; } } else if ( command == "assign" ) { Linked_queue *new_object = new Linked_queue(); *new_object = *object; std::cout << "Okay" << std::endl; Linked_queue_tester tester( new_object ); tester.run(); } else if ( command == "cout" ) { std::cout << *object << std::endl; } else { std::cout << command << "Command not found." << std::endl; } } #endif