//
// testcs.cpp
//
// Additional testing for the cstring class
//
// Computer Science S-111, Harvard University
//

#include <iostream.h>
#include <fstream.h>
#include "cstring.hpp"


void
get_choice(int &choice)
{
    cout << "Make your choice of test file to run." << endl;
    cout << "\t1 for csinput1.ts" << endl;
    cout << "\t2 for csinput2.ts" << endl;
    cout << "\t3 for csinput3.ts" << endl;
    cout << "\t4 for csinput4.ts" << endl;

    cin >> choice;
    while(cin.fail() || choice < 1 || choice > 4) {
        cout << "Invalid input.  Re-type your choice." << endl;
        cin.clear();
        cin.ignore(80, '\n');
        cin >> choice;
    } 
}


int
main ()
{
    cstring *str1, *str3;
    ifstream in;
    int choice, n;
    const char f1[] = "csinput1.ts";
    const char f2[] = "csinput2.ts";
    const char f3[] = "csinput3.ts";
    const char f4[] = "csinput4.ts";
    char ch, line[100], name[40];

    // Get the user's choice of test file and open it.
    do {
	get_choice(choice);
    
	switch (choice) {
        case 1:
	    strcpy(name, f1);
	    break;
        case 2:
	    strcpy(name, f2);
	    break;
        case 3:
	    strcpy(name, f3);
	    break;
        case 4:
	    strcpy(name, f4);
	    break;
	}

	in.open(name);

	if (in == NULL) {
	    cout << "Invalid file. Make sure you copy all the test files ";
	    cout << "into your current directory." << endl;
	    cout << "Please re-enter the choice of the test file." << endl;
	}
    } while (in == NULL);

    // operator >>() and length()
    cout << endl;
    in >> str1;
    in.getline(line, 80);
    cout << line << endl;
    in.getline(line, 80);
    cout << line << endl;
    cout << "\tYour test string is: " << str1 << endl;
    cout << "\tIts length is " << length(str1) << " characters.\n\n";

    // copystr()
    str3 = copystr(str1);
    cout << "Note: a copy of the original string has been made.\n";
    cout << "The value of the copy should not change below.\n"; 
    cout << "If it does, your copystr() is not working.\n";

    // extract()
    cout << endl;
    in >> n;
    in.ignore(80, '\n');
    in.getline(line, 80);
    cout << line << endl;
    in.getline(line, 80);
    cout << line << endl;
    cout << "\t";
    ch = extract(str1, n);
    if (ch > 0) 
	cout << "Character #" << n << " is " << ch << ".";
    else if (ch == 0)
        cout << "The string is too short.";
    cout << endl;
    
    // index()
    cout << endl;
    in >> ch;
    in.ignore(80, '\n');
    in.getline(line, 80);
    cout << line << endl;
    in.getline(line, 80);
    cout << line << endl;
    n = index(str1, ch);
    if (n == 0) 
	cout << "\tThe character " << ch << " is not found in the string.\n";
    else 
	cout << "\t" << ch << " is character #" << n << " in the string.\n";

    // split()
    cout << endl;
    in >> n;
    in.ignore(80, '\n');
    in.getline(line, 80);
    cout << line << endl;
    in.getline(line, 80);
    cout << line << endl;
    cstring *head, *tail;
    split(str1, n, head, tail);
    cout << "\tHead: ";
    cout << head << endl;
    cout << "\tTail: ";
    cout << tail << endl;

    // cdelete()
    cout << endl;
    in >> n;
    in.ignore(80, '\n');
    in.getline(line, 80);
    cout << line << endl;
    in.getline(line, 80);
    cout << line << endl;
    cout << "\t";
    cdelete(str1, n);
    if (n > 0)
	cout << str1;
    cout << endl;
    cout << "\nTesting that the copy of the original string is unchanged:\n";
    cout << "Here is your copy:\n\t" << str3 << endl;

    // insert
    cout << endl;
    in >> n;
    in.ignore(80, '\n');
    in >> ch;
    in.ignore(80, '\n');
    in.getline(line, 80);
    cout << line << endl;
    in.getline(line, 80);
    cout << line << endl;
    cout << "\t";
    insert(str1, n, ch);
    if (n > 0)
	cout << str1;
    cout << endl;
    cout << "\nTesting that the copy is still unchanged:\n";
    cout << "Here is your copy:\n\t" << str3 << endl;

    // concat()
    cstring *str2;
    cout << endl;
    in >> str2;
    in.getline(line, 80);
    cout << line << endl;
    in.getline(line, 80);
    cout << line << endl;
    cout << "\t" << concat(str1, str2) << endl;
    cout << "\nTesting that the copy is still unchanged:\n";
    cout << "Here is your copy:\n\t" << str3 << endl;

    in.close();
}
