//-------------------------------------------------------------------- // // Laboratory 3 test3.cpp // // Test program for the operations in the String ADT // //-------------------------------------------------------------------- #include #include "stradt.h" //-------------------------------------------------------------------- // // Function prototype void dummy ( stradt copyString ); // copyString is passed by value //-------------------------------------------------------------------- void main() { stradt a("a"), // Predefined test strings alp("alp"), alpha("alpha"), epsilon("epsilon"), empty, assignStr(5), // Destination for assignment inputStr(5); // Input string int n; // Input subscript char ch, // Character specified by subscript selection; // Input test selection // Get user test selection. cout << endl << "Tests:" << endl; cout << " 1 Tests the constructors" << endl; cout << " 2 Tests the length operation" << endl; cout << " 3 Tests the subscript operation" << endl; cout << " 4 Tests the assignment and clear operations" << endl; cout << " 5 Tests the copy constructor (Inactive : " << "In-lab Exercise 2)" << endl; cout << " 6 Tests the relational operations (Inactive : " << "In-lab Exercise 3)" << endl; cout << "Select the test to run : "; cin >> selection; // Execute the selected test. cout << endl; switch ( selection ) { case '1' : // Test 1 : Tests the constructors. cout << "Structure of various strings: " << endl; cout << "string: alpha" << endl; alpha.showStructure(); cout << "string: epsilon" << endl; epsilon.showStructure(); cout << "string: a" << endl; a.showStructure(); cout << "empty string" << endl; empty.showStructure(); break; case '2' : // Test 2 : Tests the length operation. cout << "Lengths of various strings:" << endl; cout << " alpha : " << alpha.length() << endl; cout << " epsilon : " << epsilon.length() << endl; cout << " a : " << a.length() << endl; cout << " empty : " << empty.length() << endl; break; case '3' : // Test 3 : Tests the subscript operation. cout << "Enter a subscript : "; cin >> n; ch = alpha[n]; cout << " alpha[" << n << "] : "; if ( ch == '\0' ) cout << "\\0" << endl; else cout << ch << endl; break; case '4' : // Test 4 : Tests the assignment and clear operations. cout << "Assignments:" << endl; cout << "assignStr = alpha" << endl; assignStr = alpha; assignStr.showStructure(); cout << "assignStr = a" << endl; assignStr = a; assignStr.showStructure(); cout << "assignStr = empty" << endl; assignStr = empty; assignStr.showStructure(); cout << "assignStr = epsilon" << endl; assignStr = epsilon; assignStr.showStructure(); cout << "assignStr = assignStr" << endl; assignStr = assignStr; assignStr.showStructure(); cout << "assignStr = alpha" << endl; assignStr = alpha; assignStr.showStructure(); cout << "Clear assignStr" << endl; assignStr.clear(); assignStr.showStructure(); cout << "Confirm that alpha has not been cleared" << endl; alpha.showStructure(); break; //5 case '5' : // In-lab Exercise 2 //5 // Test 5 : Tests the copy constructor. //5 cout << "Calls by value:" << endl; //5 cout << "alpha before call" << endl; //5 alpha.showStructure(); //5 dummy(alpha); //5 cout << "alpha after call" << endl; //5 alpha.showStructure(); //5 cout << "a before call" << endl; //5 a.showStructure(); //5 dummy(a); //5 cout << "a after call" << endl; //5 a.showStructure(); //5 break; //6 case '6' : // In-lab Exercise 3 //6 // Test 6 : Tests the relational operations. //6 cout << " left right < == > " << endl; //6 cout << "--------------------------------" << endl; //6 cout << " alpha epsilon " << (alphaepsilon) << endl; //6 cout << " epsilon alpha " << (epsilonalpha) << endl; //6 cout << " alpha alpha " << (alphaalpha) << endl; //6 cout << " alp alpha " << (alpalpha) << endl; //6 cout << " alpha alp " << (alphaalp) << endl; //6 cout << " a alpha " << (aalpha) << endl; //6 cout << " alpha a " << (alphaa) << endl; //6 cout << " empty alpha " << (emptyalpha) << endl; //6 cout << " alpha empty " << (alphaempty) << endl; //6 cout << " empty empty " << (emptyempty) << endl; //6 break; default : cout << "Inactive or invalid test" << endl; } } //-------------------------------------------------------------------- void dummy ( stradt copyString ) // Dummy routine that is passed a string using call by value. Outputs // copyString and clears it. { cout << "Copy of string" << endl; copyString.showStructure(); cout << "Clear copy" << endl; copyString.clear(); copyString.showStructure(); }