/************************************************* * Dynamic_dual_stack_tester * A class for testing dynamic dual stacks. * * Author: Douglas Wilhelm Harder * Copyright (c) 2009 by Douglas Wilhelm Harder. All rights reserved. * * DO NOT EDIT THIS FILE *************************************************/ #ifndef DYNAMIC_DUAL_STACK_TESTER_H #define DYNAMIC_DUAL_STACK_TESTER_H #ifndef nullptr #define nullptr 0 #endif #include "Exception.h" #include "Tester.h" #include "Dynamic_dual_stack.h" #include template class Dynamic_dual_stack_tester:public Tester< Dynamic_dual_stack > { using Tester< Dynamic_dual_stack >::object; using Tester< Dynamic_dual_stack >::command; public: Dynamic_dual_stack_tester( Dynamic_dual_stack *obj = 0 ):Tester< Dynamic_dual_stack >( obj ) { // empty } void process(); }; /**************************************************** * void process() * * Process the current command. For dynamic dual stacks, these include: * * Constructors * * new constructor create a stack with the default array size * new: n constructor create a stack with an array size of n * * Accessors * * capacity n capacity the capcity equals n * empty m b empty the result for the mth stack is the * Boolean value b * size m n size the size of the mth stack is n * top m n top n is the first element in the mth stack * top! m top the underflow exception is thrown * * Mutators * * push m n push n is pushed onto the mth stack * pop m n pop n is popped off the mth stack * pop! m pop the underflow exception is thrown * * Others * * assign operator = assign this stack to a new stack * 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 Dynamic_dual_stack_tester::process() { if ( command == "new" ) { object = new Dynamic_dual_stack(); std::cout << "Okay" << std::endl; } else if ( command == "new:" ) { int n; std::cin >> n; object = new Dynamic_dual_stack( n ); std::cout << "Okay" << std::endl; } else if ( command == "size" ) { // check if the size equals the next integer read int m, expected_size; std::cin >> m; std::cin >> expected_size; int actual_size = object->size( m ); if ( actual_size == expected_size ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in size( " << m << " ): 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 int m; bool expected_empty; std::cin >> m; std::cin >> expected_empty; bool actual_empty = object->empty( m ); if ( actual_empty == expected_empty ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in empty( " << m << " ): expecting the value '" << expected_empty << "' but got '" << actual_empty << "'" << std::endl; } } else if ( command == "top" ) { // checks if the first integer in the stack equals the next integer read int m; Type expected_top; std::cin >> m; std::cin >> expected_top; Type actual_top = object->top( m ); if ( actual_top == expected_top ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in top( " << m << " ): expecting the value '" << expected_top << "' but got '" << actual_top << "'" << std::endl; } } else if ( command == "top!" ) { // top of an empty stack - catch an exception int m; Type actual_top; std::cin >> m; try { actual_top = object->top( m ); std::cout << "Failure in top( " << m << " ): expecting to catch an exception but got '" << actual_top << "'" << std::endl; } catch( underflow ) { std::cout << "Okay" << std::endl; } catch (...) { std::cout << "Failure in top(): 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 stack int m; Type obj; std::cin >> m; std::cin >> obj; object->push( m, obj ); std::cout << "Okay" << std::endl; } else if ( command == "pop" ) { // pop the first integer from the stack int m; Type expected_popped_element; std::cin >> m; std::cin >> expected_popped_element; Type actual_popped_element = object->pop( m ); if ( actual_popped_element == expected_popped_element ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in pop( " << m << " ): expecting the value '" << expected_popped_element << "' but got '" << actual_popped_element << "'" << std::endl; } } else if ( command == "pop!" ) { // pop from an empty stack - catch an exception int m; Type actual_popped_element; std::cin >> m; try { actual_popped_element = object->pop( m ); std::cout << "Failure in pop( " << m << " ): 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( " << m << " ): 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" ) { Dynamic_dual_stack *new_object = new Dynamic_dual_stack(); *new_object = *object; std::cout << "Okay" << std::endl; Dynamic_dual_stack_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