// FILE: stringtest.cxx
// An interactive test program for the new string class
#include <cctype>       // Provides toupper
#include <iostream>     // Provides cout and cin
#include <cstdlib>      // Provides EXIT_SUCCESS
#include "mystring.h"   // Provides mystring class
using namespace std;
using namespace main_savitch_4;

// PROTOTYPES for functions used by this test program:
void print_menu( );
// Postcondition: A menu of choices for this program has been written to cout.

char get_user_command( );
// Postcondition: The user has been prompted to enter a one character command.
// The next character has been read (skipping blanks and newline characters), 
// and this character has been returned.

void skip_to_end( );
// Postcondition: Input has been read up to and including an end-of-line
// marker, and this input has been discarded.

void value_parameter_test(mystring s);
// Postcondition: The mystring s has been printed using the << operation.
// This tests the copy constructor since the copy constructor is used
// to copy a value parameter into a function.

int main( )
{
    mystring s1, s2, s3("Spider-Man"); // Strings that we'll test
    char choice;       // A command character entered by the user
    size_t i;          // An index into a mystring
    size_t n;          // An arguement for the reserve function
    
    cout << "I have initialized two empty mystrings s1 and s2," << endl;
    cout << "and a third mystring s3 set to \"Spider-Man\"." << endl;

    do
    {
        print_menu( );
        choice = toupper(get_user_command( ));
        switch (choice)
        {
	case '1':
	    cout << "Please type a mystring for s1: ";
	    getline(cin, s1);
	    break;
	case '2':
	    cout << "Please type a mystring for s2: ";
	    getline(cin, s2);
	    break;
	case '3':
	    cout << "Please type a mystring for s3: ";
	    getline(cin, s3);
	    break;
	case 'C': value_parameter_test(s1); break;
	case 'L':
	    cout << "Mystring lengths are: "
		 << s1.length( ) << ' ' << s2.length( ) << ' ' << s3.length( )
		 << endl;
	    break;
	case '!':
	    cout << "Characters of s1 are: ";
	    for (i = 0; i < s1.length( ); ++i)
		cout << s1[i] << ' ';
	    cout << endl;
	    break;
	case 'X': s1 += 'X'; break;
	case 'Y': s1 += "YYY"; break;
	case 'Z': s1 += s3; break;
	case '+': value_parameter_test(s2+s3);
	case 'R':
	    cout << "Please type a new reserve size for s1: ";
	    cin >> n;       // Read the new size
	    skip_to_end( );
	    s1.reserve(n);
	    break;
	case 'I':
	    cout << "Please type a mystring for s1: ";
            cin >> s1;
	    skip_to_end( );
	    break;
	case 'P':
	    cout << "s1: " << s1 << endl;
	    cout << "s2: " << s2 << endl;
	    cout << "s3: " << s3 << endl;
	    break;
	case '=':
	    cout << "s1 == s2 is " << ((s1 == s2) ? "true" : "false") << endl;
	    cout << "s1 != s2 is " << ((s1 != s2) ? "true" : "false") << endl;
	    cout << "s1 <= s2 is " << ((s1 <= s2) ? "true" : "false") << endl;
	    cout << "s1 >= s2 is " << ((s1 >= s2) ? "true" : "false") << endl;
	    cout << "s1 <  s2 is " << ((s1 < s2) ? "true" : "false") << endl;
	    cout << "s1 >  s2 is " << ((s1 > s2) ? "true" : "false") << endl;
	    break;
	case 'Q': cout << "Ridicule is the best test of truth." << endl;
	    break;
	default:  cout << choice << " is invalid." << endl;
        }
    }
    while ((choice != 'Q'));

    return EXIT_SUCCESS;
}

void print_menu( )
// Library facilities used: iostream.h
{
    cout << endl; // Print blank line before the menu
    cout << "The following choices are available: " << endl;
    cout << " 1   Run getline(cin, s1)" << endl;
    cout << " 2   Run getline(cin, s2)" << endl;
    cout << " 3   Run getline(cin, s3)" << endl;
    cout << " C   Use the copy constructor to copy s1, and then print the copy" << endl;
    cout << " L   Print the length of all three mystrings" << endl;
    cout << " !   Test the [ ] operator on all characters of s1" << endl;
    cout << " X   Run s1 += 'X'" << endl;
    cout << " Y   Run s1 += \"YYY\"" << endl;
    cout << " Z   Run s1 += s3" << endl;
    cout << " +   Use the copy constructor to copy s2+s3, and then print the copy" << endl;
    cout << " R   Test the reserve function" << endl;
    cout << " I   Run cin >> s1" << endl;
    cout << " P   Print all three mystrings with the operator <<" << endl;
    cout << " =   Print all comparison between s1 and s2" << endl;
    cout << " Q   Quit this test program" << endl;
}

char get_user_command( )
// Library facilities used: iostream
{
    char command;

    cout << "Enter choice: ";
    cin >> command; // Input of characters skips blanks and newline character
    skip_to_end( );
    
    return command;
}

void skip_to_end( )
{
    while (cin && cin.peek( ) != '\n')
	cin.ignore( );
    cin.ignore( );
}

void value_parameter_test(mystring s)
{
    cout << "The copy is: " << s << endl;
}

