/************************************************* * B_tree_tester * A class for testing B-trees. * * Author: Douglas Wilhelm Harder * Copyright (c) 2006-11 by Douglas Wilhelm Harder. All rights reserved. * * DO NOT EDIT THIS FILE *************************************************/ #ifndef B_TREE_TESTER_H #define B_TREE_TESTER_H #ifndef nullptr #define nullptr 0 #endif #include "Exception.h" #include "Tester.h" #include "B_tree.h" #include "B_tree_node.h" #include "B_tree_node_tester.h" #include template class B_tree_tester:public Tester< B_tree > { using Tester< B_tree >::object; using Tester< B_tree >::command; public: B_tree_tester( B_tree *obj = 0 ):Tester< B_tree >( obj ) { // empty } void process(); }; /**************************************************** * void process() * * Process the current command. For B-trees, these include: * * Accessors * * size n size the size equals n * empty b empty the result is the Boolean value b * root root the root is not 0 and sends the next N instructions to B_tree_node_tester * count n m count the count of n is m * draw draw draw the B tree * * Mutators * * insert n insert the element can be pushed onto the front (always succeeds) * * 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 B_tree_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 == "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 == "root" ) { // checks that the root is not null, and if it is not, // the next object gives the number of commands which should // be tested by the B_tree_node_tester B_tree_node *actual_root = object->root(); if ( actual_root == 0 ) { std::cout << "Failure in root(): expecting a non-null root pointer" << std::endl; } else { std::cout << "Okay" << std::endl; B_tree_node_tester tester( actual_root ); tester.run(); } } else if ( command == "count" ) { // check if the next object read in is in the B-tree 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 == "insert" ) { // insert the next object read into the B-tree Type n; std::cin >> n; object->insert( n ); std::cout << "Okay" << std::endl; } else if ( command == "draw" ) { std::cout << std::endl; object->draw(); } else { std::cout << command << ": Command not found." << std::endl; } } #endif