//--------------------------------------------------------------------
//
//  Copy constructor lab                                   test8.cpp
//
//  Test program for the copy constructor and assignment operations 
//  in the List ADT
//
//--------------------------------------------------------------------

#include <iostream>
#include "listlnk2.cpp"

//--------------------------------------------------------------------

template < class DT >
void dummy ( List<DT> copyList )

// Dummy routine that is passed a list using call by value. Outputs
// copyList and clears it.

{
    cout << "Copy of list:  " << endl;
    copyList.showStructure();
    copyList.clear();
    cout << "Copy cleared:   " << endl;
    copyList.showStructure();
    cout << endl;
}

//--------------------------------------------------------------------

void main()
{
    List<char> testList(8),     // Test list
               assignList(4),   // List assigned to
               *lPtr;           // Pointer to list
    char cmd,                   // Input command
         testElement;           // Test element

    cout << endl << "Commands:" << endl;
    cout << "  + x  : Insert element" << endl;
    cout << "  N    : Go to next" << endl;
    cout << "  P    : Go to prior" << endl;
    cout << "  C    : Copy constructor" << endl;
    cout << "  =    : Assignment operator" << endl;
    cout << "  !    : Double check assignment operator" << endl;
    cout << "  ?    : Check operator== (in-lab 2)" << endl;
    cout << "  S    : Check assignment to self (in-lab 3)" << endl;
    cout << "  Q    : Quit the test program" << endl;
    cout << endl;

    do
    {
        cout << "testList: " << endl;
        testList.showStructure();                     // Output list

        cout << endl << "Command: ";                  // Read command
        cin >> cmd;
        if ( cmd == '+' )
           cin >> testElement;

        switch ( cmd )
        {
          case '+' :                              // Insert
               cout << "Insert " << testElement << endl;
               testList.insert(testElement);
               break;

          case 'N' : case 'n' :                   // Go to next
               cout << "Goto next" << endl;
               testList.gotoNext();
               break;

          case 'P' : case 'p' :                   // Go to prior
               cout << "Goto prior" << endl;
               testList.gotoPrior();
               break;

          case 'C' : case 'c' :                   // Copy constructor
               cout << "Call by value" << endl;
               dummy(testList);
               break;

          case '=' :                              // Assignment operator
               assignList = testList;
               cout << "assignList:" << endl;
               assignList.showStructure();
               cout << endl;
               break;

//?       case '?' :                              // In-lab 2. Operator ==
//?            if ( assignList == testList )
//?                cout << "the lists are equal" << endl;
//?            else
//?                cout << "the lists are NOT equal" << endl;
//?            cout << "clearing assignList" << endl;
//?            assignList.clear();
//?            break;

//S       case 'S' :                              // In-lab 3. Test for 
//S            lPtr = &testList;                  // assignment side effects. 
//S            cout << "testList:" << endl;
//S            testList.showStructure();
//S            cout << endl;
//S            testList = *lPtr;                  // Try fooling compiler
//S            // Should show empty list if not handled correctly
//S            testList.showStructure();
//S            cout << endl;
//S            break;

          case '!' :                              // Assignment operator
               assignList.gotoEnd();              // double check
               assignList.insert('!');
               cout << "! appended to assignList" << endl;
               testList.gotoEnd();
               testList.insert('?');
               cout << "? appended to testList" << endl;   
               cout << "List assigned to:" << endl;
               assignList.showStructure();
               cout << endl;
               break;

          case 'Q' : case 'q' :                   // Quit test program
               break;

          default :                               // Invalid command
               cout << "Inactive or invalid command" << endl;
        }
    }
    while ( cmd != 'Q'  &&  cmd != 'q' );
}


