#include "general-list.h"
#include "smartall.h"

/****
 *
 * Test driver for exercising the functions of GeneralList.
 *
 */
int main() {

    char* intArray[] = {"1","2","3"};	// Intializing array
    GeneralList* listOfStrings; 	// Test list holding strings
    GeneralList* listOfIntegers;	// Test list holding Integers

    /*
     * Allocate.
     */
    listOfStrings = newGeneralList();
    listOfIntegers = newGeneralListArray((char**) intArray,
	sizeof(intArray) / sizeof(char*));

    /*
     * Call the constructive methods.
     */
    putLast(putLast(putLast(listOfStrings, "x"), "y"), "z");
    putFirst(putFirst(putFirst(listOfStrings, "c"), "b"), "a");
    put(put(listOfStrings, "m", 3), "n", 4);
    set(listOfStrings, "N", 4);
    put(listOfIntegers, "4", 3);

    /*
     * Print out the results of the constructive methods.
     */
    printf("\nConstruction results:\n");
    printf("%s\n", toString(listOfStrings));
    printf("%s\n", toString(listOfIntegers));


    /*
     * Call the non-destructive access methods, printing out results
     * incrementally.
     */
    printf(
      "\nFirst, last, and middle elements of string list are: %s, %s, %s\n",
	getFirst(listOfStrings),
	getLast(listOfStrings),
	get(listOfStrings, 3)
    );


    /*
     * Call the destructive access methods, printing out results incrementally.
     */
    printf(
      "\nRemoved first, last, and middle elements of string list are: ");
    printf("%s, ", removeFirst(listOfStrings));
    printf("%s, ", removeLast(listOfStrings));
    printf("%s\n", removeIth(listOfStrings, 2));
    printf("String list after removals is: %s\n",
	toString(listOfStrings));
    printf(
      "\nMore removed -- first, last, and middle elements of string list are: ");
    printf("%s, ", removeIth(listOfStrings, 0));
    printf("%s, ", removeIth(listOfStrings, 3));
    printf("%s\n", removeIth(listOfStrings, 1));
    printf("String list after more removals is: %s\n",
	toString(listOfStrings));


    /*
     * Put back some elements for utility method testing.
     */
    put(put(put(listOfStrings, "b", 0), "N", 2), "y", 4);
    printf("String list after put backs: %s\n",
	toString(listOfStrings));

    /*
     * Call the utility methods.
     */
    printf(
	"\nResults of elementOf(\"b\"), elementOf(\"X\"), findIndex(\"N\"), findIndex(\"X\"):\n"
    );
	printf("%s, %s, %d, %d\n",
	    elementOf(listOfStrings, "b") ? "true" : "false",
	    elementOf(listOfStrings, "X") ? "true" : "false",
	    findIndex(listOfStrings, "N"),
	    findIndex(listOfStrings, "X")
	);
    printf("\nResults of sorting: %s\n", toString(sort(listOfStrings)));


    /*
     * Call the utility methods.
     */
    printf(
	"\nResults of subList(2,3), length, isEmpty, equals, and toString:\n"
    );
	printf("%s, %d, %s, %s, %s\n",
	    toString(subList(listOfStrings, 2, 3)),
	    length(listOfStrings),
	    isEmpty(listOfStrings) ? "true" : "false",
	    equals(listOfStrings, listOfIntegers) ? "true" : "false",
	    toString(listOfStrings)
	);

    /*
     * Do another sort.
     */
    putLast(putLast(putLast(putLast(listOfStrings, "4"), "3"), "2"), "1");
    printf("\nBefore 2nd sort: %s\n", toString(listOfStrings));
    printf("Results of 2nd sort: %s\n", toString(sort(listOfStrings)));
    printf("\n");
    
    /* 
     * Have smartalloc do its thing.
     */
    sm_dump(0);

}