/************************************************* * Navigation_stack_tester * A class for testing undo-redo stacks * * Author: Douglas Wilhelm Harder * Copyright (c) 2006-8 by Douglas Wilhelm Harder. All rights reserved. * * DO NOT EDIT THIS FILE *************************************************/ #ifndef NAVIGATION_STACK_TESTER_H #define NAVIGATION_STACK_TESTER_H #ifndef nullptr #define nullptr 0 #endif #include "Exception.h" #include "Tester.h" #include "Navigation_stack.h" #include template class Navigation_stack_tester:public Tester< Navigation_stack > { using Tester< Navigation_stack >::object; using Tester< Navigation_stack >::command; public: Navigation_stack_tester( Navigation_stack *obj = 0 ):Tester< Navigation_stack >( obj ) { // empty } void process(); }; /**************************************************** * void process() * * Process the current command. For undo-redo stacks, these include: * * Accessors * * can_undo b can_undo checks that an undo can or cannot be performed * can_redo b can_undo checks that an undo can or cannot be performed * * Mutators * * event n event add an event to the undo stack * undo n undo undo to get the value n * undo! undo the underflow exception is thrown * redo n redo redo to get the value n * redo! redo the underflow exception is thrown * clear clear empty both stacks * * Others * * * 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 Navigation_stack_tester::process() { if ( command == "can_undo" ) { // check if the capacity equals the next integer read bool expected_undo; std::cin >> expected_undo; bool actual_undo = object->can_undo(); if ( actual_undo == expected_undo ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in can_undo(): expecting the value '" << expected_undo << "' but got '" << actual_undo << "'" << std::endl; } } else if ( command == "can_redo" ) { // check if the capacity equals the next integer read bool expected_redo; std::cin >> expected_redo; bool actual_redo = object->can_redo(); if ( actual_redo == expected_redo ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in can_redo(): expecting the value '" << expected_redo << "' but got '" << actual_redo << "'" << std::endl; } } else if ( command == "event" ) { // push the next integer read onto the undo stack Type n; std::cin >> n; object->event( n ); std::cout << "Okay" << std::endl; } else if ( command == "undo" ) { // perform an undo Type expected_undo_element; std::cin >> expected_undo_element; Type actual_undo_element = object->undo(); if ( actual_undo_element == expected_undo_element ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in undo(): expecting the value '" << expected_undo_element << "' but got '" << actual_undo_element << "'" << std::endl; } } else if ( command == "undo!" ) { // undo from an empty stack - catch an exception Type actual_undo_element; try { actual_undo_element = object->undo(); std::cout << "Failure in undo(): expecting to catch an exception but got '" << actual_undo_element << "'" << std::endl; } catch( underflow ) { std::cout << "Okay" << std::endl; } catch (...) { std::cout << "Failure in undo(): expecting an underflow exception but caught a different exception" << std::endl; } } else if ( command == "redo" ) { // perform an redo Type expected_redo_element; std::cin >> expected_redo_element; Type actual_redo_element = object->redo(); if ( actual_redo_element == expected_redo_element ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in redo(): expecting the value '" << expected_redo_element << "' but got '" << actual_redo_element << "'" << std::endl; } } else if ( command == "redo!" ) { // redo from an empty stack - catch an exception Type actual_redo_element; try { actual_redo_element = object->redo(); std::cout << "Failure in redo(): expecting to catch an exception but got '" << actual_redo_element << "'" << std::endl; } catch( underflow ) { std::cout << "Okay" << std::endl; } catch (...) { std::cout << "Failure in redo(): 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 == "cout" ) { //std::cout << *object << std::endl; } else { std::cout << command << ": Command not found." << std::endl; } } #endif