/************************************************* * Dynamic_linear_hash_table * A class for testing dynamic hash table with linear probing * * Author: Douglas Wilhelm Harder *************************************************/ #ifndef DYNAMIC_LINEAR_HASH_TABLE_TESTER_H #define DYNAMIC_LINEAR_HASH_TABLE_TESTER_H #ifndef nullptr #define nullptr 0 #endif #include "Tester.h" #include "Dynamic_linear_hash_table.h" #include "Exception.h" #include class Dynamic_linear_hash_table_tester:public Tester< Dynamic_linear_hash_table > { using Tester< Dynamic_linear_hash_table >::object; using Tester< Dynamic_linear_hash_table >::command; public: Dynamic_linear_hash_table_tester( Dynamic_linear_hash_table *obj = 0 ):Tester< Dynamic_linear_hash_table >( obj ) { // empty } void process(); }; /**************************************************** * void process() * * Process the current commadn. For dynamic hash tables with linear probing, these include: * * Constructors * new constructor calls the constructor with the default value * new: n constructor calls the constructor with the argument n * * Accessors * * size n size the size equals n * capacity n capacity the capacity equals n * empty b empty the result is the Boolean value b (0/1) * bin i n bin check that m is in bin n * member n b member checks if the membership of n is b * load_factor d load_factor checks if the load factor is d * print bin & capacity not used for testing * * Mutators * * insert n insert successfully inserts the number n * remove n b remove removes n if possible (returns success) * clear clear empties the deque ****************************************************/ void Dynamic_linear_hash_table_tester::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 == "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 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 == "bin" ) { // checks if the contents of bin n is the expected value int n; std::cin >> n; int expected_bin; std::cin >> expected_bin; int actual_bin = object->bin( n ); if ( actual_bin == expected_bin ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in bin(" << n << "): expecting the value '" << expected_bin << "' but got '" << actual_bin << "'" << std::endl; } } else if ( command == "load_factor" ) { // checks if the load factor is the next read double double expected_load_factor; std::cin >> expected_load_factor; double actual_load_factor = object->load_factor(); if ( actual_load_factor == expected_load_factor ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in load_factor(): expecting the value '" << expected_load_factor << "' but got '" << actual_load_factor << "'" << std::endl; } } else if ( command == "member" ) { // checks for membership of the next integer read checking the return value against the next Boolean value read int n; std::cin >> n; bool expected_member; std::cin >> expected_member; bool actual_member = object->member( n ); if ( actual_member == expected_member ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in member(" << n << "): expecting the value '" << expected_member << "' but got '" << actual_member << "'" << std::endl; } } else if ( command == "insert" ) { // insert the next integer read into the hash table int n; std::cin >> n; object->insert( n ); std::cout << "Okay" << std::endl; } else if ( command == "remove" ) { // attempt to remove the next integer read into the hash table int n; std::cin >> n; bool expected_remove; std::cin >> expected_remove; bool actual_remove = object->remove( n ); if ( actual_remove == expected_remove ) { std::cout << "Okay" << std::endl; } else { std::cout << "Failure in remove(" << n << "): expecting the value '" << expected_remove << "' but got '" << actual_remove << "'" << std::endl; } } else if ( command == "clear" ) { object->clear(); std::cout << "Okay" << std::endl; } else if ( command == "print" ) { if ( object->bin( 0 ) >= 0 ) { std::cout << object->bin( 0 ); } else { std::cout << "x"; } for ( int i = 1; i < object->capacity(); ++i ) { if ( object->bin( i ) >= 0 ) { std::cout << "-" << object->bin( i ); } else { std::cout << "-x"; } } std::cout << std::endl; } else if ( command == "cout" ) { std::cout << *object << std::endl; } else { std::cout << command << ": Command not found." << std::endl; } } #endif