/************************************************* * Queue_tester * A class for testing queues-as-arrays. * * Author: Douglas Wilhelm Harder * Copyright (c) 2009 by Douglas Wilhelm Harder. All rights reserved. * * DO NOT EDIT THIS FILE *************************************************/ #ifndef QUEUE_TESTER_H #define QUEUE_TESTER_H #ifndef nullptr #define nullptr 0 #endif #include "Exception.h" #include "Tester.h" #include "Queue.h" #include template class Queue_tester:public Tester< Queue > { using Tester< Queue >::object; using Tester< Queue >::command; public: Queue_tester( Queue *obj = 0 ):Tester< Queue >( obj ) { // empty } void process(); }; /**************************************************** * void process() * * Process the current command. For dynamic queues-as-arrays, these include: * * Constructors * * new constructor create a queue with the default array size * new: n constructor create a queue with an array size of n * * Accessors * * capacity n capacity the capcity equals n * empty b empty the queue is empty (0 or 1) * Boolean value b * size n size the number of objects in the queue * front n front n is the first element on the queue * front! front the underflow exception is thrown * * Mutators * * push n push n is pushed onto the queue * push! n pop the overflow exception is thrown * pop n pop n is popped off the queue * pop! pop the underflow exception is thrown * * Others * * assign operator = assign this queue to a new queue * 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_front 3 * 6 !! 7 // same as push_front 7 * !n use the command used in line n 7 front 7 * 8 !7 9 // same as push_front 9 * ****************************************************/ template void Queue_tester::process() { 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 == "capacity" ) { // check if the capacity equals the next integer read int expected_capacity; std::cin >> expected_capacity; int actual_capacity = object->capacity(); if ( actual_capacity == expected_capacity ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in capacity(): expecting the value '" << expected_capacity << "' but got '" << actual_capacity << "'" << 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 integer in the queue equals the next integer 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 queue - 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 integer read to the front of the queue Type obj; std::cin >> obj; object->push( obj ); std::cout << "Okay" << std::endl; } else if ( command == "push!" ) { // push from an empty queue - catch an exception Type obj; std::cin >> obj; try { object->push( obj ); std::cout << "Failure in push( " << obj << " ): expecting to catch an exception but did not" << std::endl; } catch( overflow ) { std::cout << "Okay" << std::endl; } catch (...) { std::cout << "Failure in push( " << obj << " ): expecting an overflow exception but caught a different exception" << std::endl; } } else if ( command == "pop" ) { // pop the first integer from the queue 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 queue - catch an exception try { Type 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 == "clear" ) { object->clear(); std::cout << "Okay" << std::endl; } else if ( command == "assign" ) { Queue *new_object = new Queue(); *new_object = *object; std::cout << "Okay" << std::endl; 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