// /*--------------------------------------------------------------------*\ // | Main Program: Simple test driver for Sorting_Machine_Kernel // |*--------------------------------------------------------------------*| // | Date: 22 September 1997 // | Author: Scott Pike // | // | Brief User's Manual: // | Allows user to test an implementation of Sorting_Machine_Kernel, // | interactively or in batch mode. // | // \*--------------------------------------------------------------------*/ ///--------------------------------------------------------------------- /// Global Context ----------------------------------------------------- ///--------------------------------------------------------------------- #include "RESOLVE_Foundation.h" #include "CT/Sorting_Machine/Put_To_1.h" #include "CT/Sorting_Machine/Kernel_2_C.h" #include "CT/Queue/Kernel_1a_C.h" #include "CT/Array/Kernel_1_C.h" #include "CT/Array/Are_In_Order_At_1_C.h" #include "CT/Array/Exchange_At_1_C.h" #include "CI/Text/Are_In_Order_1.h" ///--------------------------------------------------------------------- /// Interface ---------------------------------------------------------- ///--------------------------------------------------------------------- concrete_instance class Queue_Of_Text : instantiates Queue_Kernel_1a_C {}; //---------------------------------------------------------------------- concrete_instance class Array_Of_Text_Base : instantiates Array_Kernel_1_C {}; //---------------------------------------------------------------------- concrete_instance class Array_Of_Text_AIO : instantiates Array_Are_In_Order_At_1_C < Text, Text_Are_In_Order_1, Array_Of_Text_Base > {}; //---------------------------------------------------------------------- concrete_instance class Array_Of_Text : instantiates Array_Exchange_At_1_C < Text, Array_Of_Text_AIO > {}; //---------------------------------------------------------------------- concrete_instance class Sorting_Machine_Of_Text_Base : instantiates Sorting_Machine_Kernel_2_C < Text, Text_Are_In_Order_1, Queue_Of_Text, Array_Of_Text > {}; //---------------------------------------------------------------------- concrete_instance class Sorting_Machine : instantiates Sorting_Machine_Put_To_1 < Text, Sorting_Machine_Of_Text_Base > {}; //---------------------------------------------------------------------- concrete_instance class Array_Of_Sorting_Machine : instantiates Array_Kernel_1_C {}; //---------------------------------------------------------------------- //---------------------------------------------------------------------- // Program control objects object Boolean interactive; object Text command_line; object Character command_code; object Character_IStream ins; object Character_OStream outs; // Test objects object Array_Of_Sorting_Machine m; //---------------------------------------------------------------------- //---------------------------------------------------------------------- // Global operations (omitted for brevity) //---------------------------------------------------------------------- //---------------------------------------------------------------------- // Global operation bodies procedure_body Determine_Interactive_Mode () { outs << "Run in interactive mode (y/n)? "; ins >> command_line; command_code = command_line[0]; interactive = (command_code == 'y'); } //------------------------------------------------------------------------ procedure_body Get_Command () { if (interactive) { // Show menu of commands: // Concept-specific operations outs << "\n\nCommand: s [Swap]\n" << " p [Put_To]\n" << " i [Insert]\n" << " f [Remove_First]\n" << " a [Remove_Any]\n" << " c [Change_To_Extraction_Phase]\n" << " x [Is_In_Extraction_Phase]\n" << " z [Size]\n" << " C [Clear]\n" // Tester control operations << " q [Quit]: "; } // Get next command line and code ins >> command_line; command_code = command_line[0]; } //------------------------------------------------------------------------ procedure_body Do_Swap () { object Integer id1, id2; object Sorting_Machine temp; if (interactive) { outs << "Swap which Sorting Machine? "; } ins >> id1; if (interactive) { outs << " ... with which Machine? "; } ins >> id2; outs << "\n--------------------------------------------" << "\n |"; outs << "\n--------------------------------------------" << "\nm" << id1 << " &= m" << id2 << "; |"; temp &= m[id1]; temp &= m[id2]; temp &= m[id1]; outs << "\n--------------------------------------------" << "\n |"; outs << "\n--------------------------------------------"; } //------------------------------------------------------------------------ procedure_body Do_Put_To () { object Integer id; if (interactive) { outs << "Put which Sorting Machine? "; } ins >> id; outs << "\n--------------------------------------------" << "\nm" << id << " = "; outs << m[id]; outs << "\n--------------------------------------------"; } //------------------------------------------------------------------------ procedure_body Do_Insert () { object Integer id; object Text x; if (interactive) { outs << "Insert into which Sorting Machine? "; } ins >> id; if (interactive) { outs << " ... x value? "; } ins >> x; outs << "\n--------------------------------------------" << "\n | x = " << x; outs << "\n--------------------------------------------" << "\nm" << id << ".Insert (x); |"; m[id].Insert (x); outs << "\n--------------------------------------------" << "\n | x = " << x; outs << "\n--------------------------------------------"; } //------------------------------------------------------------------------ procedure_body Do_Remove_First () { object Integer id; object Text x; if (interactive) { outs << "Remove_First from which Sorting Machine? "; } ins >> id; if (interactive) { outs << " ... x value? "; } ins >> x; outs << "\n--------------------------------------------" << "\n | x = " << x; outs << "\n--------------------------------------------" << "\nm" << id << ".Remove_First (x); |"; m[id].Remove_First (x); outs << "\n--------------------------------------------" << "\n | x = " << x; outs << "\n--------------------------------------------"; } //------------------------------------------------------------------------ procedure_body Do_Remove_Any () { object Integer id; object Text x; if (interactive) { outs << "Remove_Any from which Sorting Machine? "; } ins >> id; if (interactive) { outs << " ... x value? "; } ins >> x; outs << "\n--------------------------------------------" << "\n | x = " << x; outs << "\n--------------------------------------------" << "\nm" << id << ".Remove_Any (x); |"; m[id].Remove_Any (x); outs << "\n--------------------------------------------" << "\n | x = " << x; outs << "\n--------------------------------------------"; } //------------------------------------------------------------------------ procedure_body Do_Change_To_Extraction_Phase () { object Integer id; object Text phase; if (interactive) { outs << "Change_To_Extraction_Phase for which Sorting Machine? "; } ins >> id; if (m[id].Is_In_Extraction_Phase () == true) { phase = "extracting"; } else { phase = "inserting"; } outs << "\n--------------------------------------------" << "\n | phase = " << phase; outs << "\n--------------------------------------------" << "\nm" << id << ".Change_To_Extraction_Phase (); |"; m[id].Change_To_Extraction_Phase (); if (m[id].Is_In_Extraction_Phase () == true) { phase = "extracting"; } else { phase = "inserting"; } outs << "\n--------------------------------------------" << "\n | phase = " << phase; outs << "\n--------------------------------------------"; } //------------------------------------------------------------------------ procedure_body Do_Is_In_Extraction_Phase () { object Integer id; object Boolean answer; if (interactive) { outs << "Is_In_Extraction_Phase for which Sorting Machine? "; } ins >> id; outs << "\n--------------------------------------------" << "\n | extracting = ?"; outs << "\n--------------------------------------------" << "\nm" << id << ".Is_In_Extraction_Phase (); |"; answer = m[id].Is_In_Extraction_Phase (); outs << "\n--------------------------------------------" << "\n | extracting = " << answer; outs << "\n--------------------------------------------"; } //------------------------------------------------------------------------ procedure_body Do_Size () { object Integer id; object Integer size; if (interactive) { outs << "Get size of which Sorting Machine? "; } ins >> id; outs << "\n--------------------------------------------" << "\n | size = ?"; outs << "\n--------------------------------------------" << "\ni = m" << id << ".Size (); |"; size = m[id].Size (); outs << "\n--------------------------------------------" << "\n | size = " << size; outs << "\n--------------------------------------------"; } //------------------------------------------------------------------------ procedure_body Do_Clear () { object Integer id; if (interactive) { outs << "Clear which Sorting Machine? "; } ins >> id; outs << "\n--------------------------------------------" << "\nm" << id << " = " << m[id]; outs << "\n--------------------------------------------" << "\ni = m" << id << ".Clear (); |"; m[id].Clear (); outs << "\n--------------------------------------------" << "\nm" << id << " = " << m[id]; outs << "\n--------------------------------------------"; } //------------------------------------------------------------------------ //------------------------------------------------------------------------ program_body main () { object Boolean finished; // Open input and output streams ins.Open_External (""); outs.Open_External (""); // Set number of test objects (= 10) m.Set_Bounds (0, 9); // Ask user about interactive mode Determine_Interactive_Mode (); // Execute interactive testing loop until finished while (not finished) { // Ask user for next command Get_Command (); // Perform next command case_select (command_code) { // Concept-specific operations case 's': { Do_Swap (); } break; case 'p': { Do_Put_To (); } break; case 'i': { Do_Insert (); } break; case 'f': { Do_Remove_First (); } break; case 'a': { Do_Remove_Any (); } break; case 'c': { Do_Change_To_Extraction_Phase (); } break; case 'x': { Do_Is_In_Extraction_Phase (); } break; case 'z': { Do_Size (); } break; case 'C': { Do_Clear (); } break; // Tester control operations case 'q': { finished = true; } break; default: { outs << "\n" << command_line; } break; } } // Close input and output streams ins.Close_External (); outs.Close_External (); }