/************************************************* * BinaryMinHeapTester * A class for testing binary min heaps. * * Author: Douglas Wilhelm Harder * Copyright (c) 2006-8 by Douglas Wilhelm Harder. All rights reserved. * * DO NOT EDIT THIS FILE *************************************************/ #ifndef BINARY_MIN_HEAP_TESTER_H #define BINARY_MIN_HEAP_TESTER_H #ifndef nullptr #define nullptr 0 #endif #include "Exception.h" #include "Tester.h" #include "BinaryMinHeap.h" #include template class BinaryMinHeapTester:public Tester< BinaryMinHeap > { using Tester< BinaryMinHeap >::object; using Tester< BinaryMinHeap >::command; public: BinaryMinHeapTester( BinaryMinHeap *obj = 0 ):Tester< BinaryMinHeap >( obj ) { // empty } void process(); }; /**************************************************** * void process() * * Process the current command. For binary min heaps, these include: * * new new BinaryMinHeap() * new: n new BinaryMinHeap( n ) * size n size() == n * capacity n capacity() == n * empty b empty() == b * full b full() == b * head obj head() == obj * head! head->underflow() * retrieve n obj retrieve( n ) == obj * member n b member( n ) == b * push_heap obj push_heap( obj ) * push_heap! push_heap( Type() )->overflow() * pop_heap obj pop_heap() == obj * pop_heap! pop_heap( Type() )->underflow() * remove obj b remove( obj ) == b * clear clear() * * assign operator = * * Others * 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 BinaryMinHeapTester::process() { if ( command == "size" ) { 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" ) { 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" ) { 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 == "full" ) { bool expected_full; std::cin >> expected_full; bool actual_full = object->full(); if ( actual_full == expected_full ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in full(): expecting the value '" << expected_full << "' but got '" << actual_full << "'" << std::endl; } } else if ( command == "head" ) { Type expected_head; std::cin >> expected_head; Type actual_head = object->head(); if ( actual_head == expected_head ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in head(): expecting the value '" << expected_head << "' but got '" << actual_head << "'" << std::endl; } } else if ( command == "head!" ) { Type actual_head; try { actual_head = object->head(); std::cout << "Failure in head(): expecting to catch an exception but got '" << actual_head << "'" << std::endl; } catch( underflow ) { std::cout << "Okay" << std::endl; } catch (...) { std::cout << "Failure in head(): expecting an underflow exception but caught a different exception" << std::endl; } } else if ( command == "retrieve" ) { int n; Type expected_retrieve; std::cin >> n; std::cin >> expected_retrieve; Type actual_retrieve = object->retrieve( n ); if ( actual_retrieve == expected_retrieve ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in retrieve( " << n << " ): expecting the value '" << expected_retrieve << "' but got '" << actual_retrieve << "'" << std::endl; } } else if ( command == "member" ) { 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 binary min heap" << std::endl; } else { std::cout << "Failure in member(): not expecting the value '" << element << "' to be in the binary min heap" << std::endl; } } } else if ( command == "push_heap" ) { Type n; std::cin >> n; object->push_heap( n ); std::cout << "Okay" << std::endl; } else if ( command == "push_heap!" ) { try { object->push_heap( Type() ); std::cout << "Failure in push_heap( obj ): expecting to catch an exception" << std::endl; } catch( overflow ) { std::cout << "Okay" << std::endl; } catch (...) { std::cout << "Failure in push_heap( obj ): expecting an overflow exception but caught a different exception" << std::endl; } } else if ( command == "pop_heap" ) { Type expected_popped_element; std::cin >> expected_popped_element; Type actual_popped_element = object->pop_heap(); if ( actual_popped_element == expected_popped_element ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in pop_heap(): expecting the value '" << expected_popped_element << "' but got '" << actual_popped_element << "'" << std::endl; } } else if ( command == "pop_heap!" ) { Type actual_popped_element; try { actual_popped_element = object->pop_heap(); std::cout << "Failure in pop_heap(): 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_heap(): 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 == "clear" ) { object->clear(); std::cout << "Okay" << std::endl; } else if ( command == "assign" ) { BinaryMinHeap *new_object = new BinaryMinHeap(); *new_object = *(object); std::cout << "Okay" << std::endl; BinaryMinHeapTester tester( new_object ); tester.run(); } else { std::cout << command << ": Command not found." << std::endl; } } #endif