/************************************************* * SingleListTester * A class for testing singly-linked lists. * * Author: Douglas Wilhelm Harder * Copyright (c) 2006-8 by Douglas Wilhelm Harder. All rights reserved. * * DO NOT EDIT THIS FILE *************************************************/ #ifndef SINGLE_LIST_TESTER_H #define SINGLE_LIST_TESTER_H #ifndef nullptr #define nullptr 0 #endif #include "Exception.h" #include "Tester.h" #include "SingleList.h" #include "SingleNode.h" #include "SingleNodeTester.h" #include template class SingleListTester:public Tester< SingleList > { using Tester< SingleList >::object; using Tester< SingleList >::command; public: SingleListTester( SingleList *obj = 0 ):Tester< SingleList >( obj ) { // empty } void process(); }; /**************************************************** * void process() * * Process the current command. For singly-linked lists, these include: * * Accessors * * size n size the size equals 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 * back n back n is the last element in the linked list * back! back the underflow exception is thrown * head head the head is not 0 and sends the next N instructions to SingleNodeTester * head0 head the head is null * tail tail the head is not 0 and sends the next N instructions to SingleNodeTester * tail0 tail the head is null * member n b member n is a member of the linked list * * Mutators * * push_front n push_front the element can be pushed onto the front (always succeeds) * push_back n push_back the element can be pushed onto the back (always succeeds) * pop_front n pop_front the front element can be popped and equals n * pop_front! pop_front the underflow exception is thrown * remove n b remove n can be removed from the linked list * * Others * 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 * ****************************************************/ template void SingleListTester::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 == "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 == "back" ) { // checks if the last object in the linked list equals the next object read Type expected_back; std::cin >> expected_back; Type actual_back = object->back(); if ( actual_back == expected_back ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in back(): expecting the value '" << expected_back << "' but got '" << actual_back << "'" << std::endl; } } else if ( command == "back!" ) { // back of an empty list - catch an exception Type actual_back; try { actual_back = object->back(); std::cout << "Failure in back(): expecting to catch an exception but got '" << actual_back << "'" << std::endl; } catch( underflow ) { std::cout << "Okay" << std::endl; } catch (...) { std::cout << "Failure in back(): expecting an underflow exception but caught a different exception" << std::endl; } } else if ( command == "head" ) { // checks that the head is not null, and if it is not, // the next object gives the number of commands which should // be tested by the SingleNodeTester SingleNode *actual_head = object->head(); if ( actual_head == 0 ) { std::cout << "Failure in head(): expecting a non-null head pointer" << std::endl; } else { std::cout << "Okay" << std::endl; SingleNodeTester tester( actual_head ); tester.run(); } } else if ( command == "head0" ) { // check that the head pointer is null if ( object->head() == 0 ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in head(): expecting a null head pointer" << std::endl; } } else if ( command == "tail" ) { // checks that the tail is not null, and if it is not, // the next object gives the number of commands which should // be tested by the SingleNodeTester SingleNode *actual_tail = object->tail(); if ( actual_tail == 0 ) { std::cout << "Failure in tail(): expecting a non-null tail pointer" << std::endl; } else { std::cout << "Okay" << std::endl; SingleNodeTester tester( actual_tail ); tester.run(); } } else if ( command == "tail0" ) { // check that the tail pointer is null if ( object->tail() == 0 ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in tail(): expecting a null tail pointer" << std::endl; } } else if ( command == "member" ) { // check if the next object read in is in the linked list Type element; bool membership; std::cin >> element; std::cin >> membership; if ( object->member( element ) == membership ) { std::cout << "Okay" << std::endl; } else { if ( membership ) { std::cout << "Failure in member(): expecting the value '" << element << "' to be in the linked list" << std::endl; } else { std::cout << "Failure in member(): not expecting the value '" << element << "' to be in the linked list" << std::endl; } } } else if ( command == "push_front" ) { // push the next object read to the front of the linked list Type n; std::cin >> n; object->push_front( n ); std::cout << "Okay" << std::endl; } else if ( command == "push_back" ) { // push the next object read to the back of the linked list Type n; std::cin >> n; object->push_back( n ); std::cout << "Okay" << std::endl; } else if ( command == "pop_front" ) { // pop the first object from the linked list Type expected_popped_element; std::cin >> expected_popped_element; Type actual_popped_element = object->pop_front(); if ( actual_popped_element == expected_popped_element ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in pop_front(): expecting the value '" << expected_popped_element << "' but got '" << actual_popped_element << "'" << std::endl; } } else if ( command == "pop_front!" ) { // pop from an empty list - catch an exception Type actual_popped_element; try { actual_popped_element = object->pop_front(); std::cout << "Failure in pop_front(): 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_front(): expecting an underflow exception but caught a different exception" << std::endl; } } else if ( command == "remove" ) { Type n; bool expected_Boolean; std::cin >> n; std::cin >> expected_Boolean; bool actual_Boolean = object->remove( n ); if ( actual_Boolean == expected_Boolean ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in remove( " << n << " ): expecting the value '" << expected_Boolean << "', but got " << actual_Boolean << std::endl; } } else if ( command == "assign" ) { SingleList *new_object = new SingleList(); *new_object = *(object); std::cout << "Okay" << std::endl; SingleListTester tester( new_object ); tester.run(); } else { std::cout << command << ": Command not found." << std::endl; } } #endif