/************************************************* * Stable Binary Heap * 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 STABLE_BINARY_HEAP_TESTER_H #define STABLE_BINARY_HEAP_TESTER_H #ifndef nullptr #define nullptr 0 #endif #include "Exception.h" #include "Tester.h" #include "Stable_binary_heap.h" #include template class Stable_binary_heap_tester:public Tester< Stable_binary_heap > { using Tester< Stable_binary_heap >::object; using Tester< Stable_binary_heap >::command; public: Stable_binary_heap_tester( Stable_binary_heap *obj = 0 ):Tester< Stable_binary_heap >( obj ) { // empty } void process(); }; /**************************************************** * void process() * * Process the current command. For binary min heaps, these include: * * new new Stable_binary_heap() * new: n new Stable_binary_heap( n ) * size n size() == n * capacity n capacity() == n * empty b empty() == b * full b full() == b * top obj top() == obj * top! top->underflow() * retrieve n obj retrieve( n ) == obj * key n m key( n ) == m * count obj m count( obj ) == m * push obj push( obj ) * push! push( Type() )->overflow() * pop obj pop() == obj * pop! pop( Type() )->underflow() * clear clear() * * assign operator = * * Others * cout cout << heap print the heap (for testing only) * 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 Stable_binary_heap_tester::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 == "top" ) { Type expected_top; std::cin >> expected_top; Type actual_top = object->top(); if ( actual_top == expected_top ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in top(): expecting the value '" << expected_top << "' but got '" << actual_top << "'" << std::endl; } } else if ( command == "top!" ) { Type actual_top; try { actual_top = object->top(); std::cout << "Failure in top(): 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 == "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 == "key" ) { int n; Type expected_key; std::cin >> n; std::cin >> expected_key; Type actual_key = object->key( n ); if ( actual_key == expected_key ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in key( " << n << " ): expecting the value '" << expected_key << "' but got '" << actual_key << "'" << std::endl; } } else if ( command == "count" ) { Type element; int expected_count; std::cin >> element; std::cin >> expected_count; int actual_count = object->count( element ); if ( actual_count == expected_count ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in count(" << element << "): expecting the value " << expected_count << " but got " << actual_count << std::endl; } } else if ( command == "push" ) { Type n; std::cin >> n; object->push( n ); std::cout << "Okay" << std::endl; } else if ( command == "push!" ) { try { object->push( Type() ); std::cout << "Failure in push( obj ): expecting to catch an exception" << 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" ) { 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!" ) { Type actual_popped_element; try { 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" ) { Stable_binary_heap *new_object = new Stable_binary_heap(); *new_object = *(object); std::cout << "Okay" << std::endl; Stable_binary_heap_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