#include "strlist.h"
#include <iostream.h>
#include <stdio.h>
#include <time.h>
#include <sys/timeb.h>

/*******************************************************************\
 This driver outputs the following:

MAX_LENGTH is set to 15

1 ********************************************************
getCount() returned  0
isEmpty() returned 1
isPresent("Ten") returned 0
Displaying:

2 ********************************************************
getCount() returned  3
isEmpty() returned 0
One 
Two 
Three 

3 ********************************************************
One Two Three 

4 ********************************************************
Five Four One Two Three 

5 ********************************************************
isPresent("Four") returned 1
isPresent("Seven") returned 0
isPresent("Three") returned 1
isPresent("Five") returned 1

6 ********************************************************
getItem(-1) returned NULL
getItem(1) returned Five
getItem(3) returned One
getItem(5) returned Three
getItem(10) returned NULL

7 ********************************************************
Removing 'One'
Five Four Two Three 

8 ********************************************************
Removing 'Nine'
Five Four Two Three 

9 ********************************************************
Prepended 50 character word
123456789012345 
Five 
Four 
Two 
Three 

10 *******************************************************
Removed 5 items from the list. Displaying the list:

11 *******************************************************
Adding 2000 items to the list.
getItem(1900) returned  1900

12 *******************************************************
Removing 2000 items from the list
Removed 2000 items from the list. Displaying the list:
Done.
Total running time: 440 ms.

\*****************************************************************/

void main(void)
{
  try
  {
    struct timeb start, end;
    ftime(&start);

    StrList list;

    cout << "MAX_LENGTH is set to " << MAX_LENGTH << endl;

    cout << "\n1 ********************************************************" << endl;
    
    try 
    {
      cout << "getCount() returned  " << list.getCount() << endl;
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION getCount()" << endl;
    }
    
    try
    {
      cout << "isEmpty() returned " << list.isEmpty() << endl;
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION isEmpty() on empty list" << endl;
    }


    try
    {
      cout << "isPresent(\"Ten\") returned " << list.isPresent("Ten") << endl;  
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION isPresent() on empty list" << endl;
    }
    cout << "Displaying:" << endl;


    try
    {
      list.Display();
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION to display empty list" << endl;
    }


    try
    {
      list.Append("One");
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION to Append to empty list" << endl;
    }


    try
    {
      list.Append("Two");
      list.Append("Three");
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION to Append 2 items to list" << endl;
    }


    cout << "\n2 ********************************************************" << endl;
    try
    {
      cout << "getCount() returned  " << list.getCount() << endl;
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION to getCount() on 3 item list" << endl;
    }


    try
    {
      cout << "isEmpty() returned " << list.isEmpty() << endl;
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION isEmpty() on 3 item list" << endl;
    }


    try
    {
      list.Display();
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION Display() on 3 item list" << endl;
    }


    cout << "\n3 ********************************************************" << endl;
    try
    {
      list.Display(FALSE);
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION Display(FALSE) on 3 item list" << endl;
    }
    

    try
    {
      list.Prepend("Four");
      list.Prepend("Five");
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION to Prepend 2 items to list" << endl;
    }


    cout << "\n4 ********************************************************" << endl;
    try
    {
      list.Display(FALSE);
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION Display(FALSE) on 5 item list" << endl;
    }


    cout << "\n5 ********************************************************" << endl;
    try
    {
      cout << "isPresent(\"Four\") returned " << list.isPresent("Four") << endl;  
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION isPresent(Four)" << endl;
    }


    try
    {
      cout << "isPresent(\"Seven\") returned " << list.isPresent("Seven") << endl;  
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION isPresent(Seven)" << endl;
    }


    try
    {
      cout << "isPresent(\"Three\") returned " << list.isPresent("Three") << endl;  
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION isPresent(Three)" << endl;
    }


    try
    {
      cout << "isPresent(\"Five\") returned " << list.isPresent("Five") << endl;  
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION isPresent(Five)" << endl;
    }


    cout << "\n6 ********************************************************" << endl;
    try
    {
      const char *s0 = list.getItem(-1);
      if (s0)
        cout << "getItem(-1) returned " << s0 << endl;
      else
        cout << "getItem(-1) returned NULL" << endl;
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION getItem(-1)" << endl;
    }


    try
    {
      const char *s1 = list.getItem(1);
      if (s1)
        cout << "getItem(1) returned " << s1 << endl;
      else
        cout << "getItem(1) returned NULL" << endl;
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION getItem(1)" << endl;
    }


    try
    {
    const char *s2 = list.getItem(3);
    if (s2)
      cout << "getItem(3) returned " << s2 << endl;
    else
      cout << "getItem(3) returned NULL" << endl;
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION getItem(3)" << endl;
    }


    try
    {
      const char *s3 = list.getItem(5);
      if (s3)
        cout << "getItem(5) returned " << s3 << endl;
      else
        cout << "getItem(5) returned NULL" << endl;
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION getItem(5)" << endl;
    }


    try
    {
      const char *s4 = list.getItem(10);
      if (s4)
        cout << "getItem(10) returned " << s4 << endl;
      else
        cout << "getItem(10) returned NULL" << endl;
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION getItem(10)" << endl;
    }


    cout << "\n7 ********************************************************" << endl;
    cout << "Removing 'One'" << endl;
    try
    {
      list.Remove("One");
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION Remove(One)" << endl;
    }


    try
    {
      list.Display(FALSE);
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION Display(FALSE)" << endl;
    }


    cout << "\n8 ********************************************************" << endl;
    cout << "Removing 'Nine'" << endl;
    try
    {
      list.Remove("Nine");
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION Remove(Nine)" << endl;
    }

    try
    {
      list.Display(FALSE);
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION Display(FALSE)" << endl;
    }

 
    cout << "\n9 ********************************************************" << endl;
    cout << "Prepended 50 character word" << endl;
    try
    {
      list.Prepend("12345678901234567890123456789012345678901234567890");
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION Prepend long word" << endl;
    }

    try
    {
      list.Display();
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION Display()" << endl;
    }


    cout << "\n10 *******************************************************" << endl;
    try
    {
      int count = 0;
      while (!list.isEmpty())
      {
        const char *s3 = list.getItem(1);
        list.Remove(s3);
        count++;
      }
      cout << "Removed " << count << " items from the list. Displaying the list:" << endl;
      list.Display();
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION removing all items" << endl;
    }


    cout << "\n11 *******************************************************" << endl;
    cout << "Adding 2000 items to the list." << endl;
    try
    {
      for (int i = 1; i <= 2000; i++)
      {
        char temp[10];
        sprintf(temp, "%5i", i);
        list.Append(temp);
      }
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION adding 2000 items to the list" << endl;
    }


    try
    {
      const char *s5 = list.getItem(1900);
      if (s5)
        cout << "getItem(1900) returned " << s5 << endl;
      else
        cout << "getItem(1900) returned NULL" << endl;
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION getItem(1900)" << endl;
    }


    cout << "\n12 *******************************************************" << endl;
    cout << "Removing 2000 items from the list" << endl;
    try
    {
      int count = 0;
      while (!list.isEmpty())
      {
        const char *s3 = list.getItem(1);
        list.Remove(s3);
        count++;
      }
      cout << "Removed " << count << " items from the list. Displaying the list:" << endl;
      cout << "Done." << endl;
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION removing 2000 items" << endl;
    }


    try
    {
      list.Display();
    }
    catch(...)
    {
      cout << "ABNORMAL TERMINATION Display()" << endl;
    }

    ftime(&end);
    cout << "Total running time: " << (end.time * 1000 + end.millitm) - (start.time * 1000 + start.millitm) << " ms." << endl;

  }
  catch(...)
  {
    cout << "Unhandled exception, probably a bad constructor" << endl;
  }
}  
