/* 
 * Test program for WordLoop (Homework 1X, part 1)
 *
 * You may want add more test cases!
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "WordLoop.h"

/*
 * Creates a gazillion lists with a bazillion nodes.
 * If destroy is 1, it will delete each list after it has been "used".
 */
#define TOTALRUNS 300000
#define PRINTINFO  30000

void testDestroy(int destroy) {
    int i, j ;
    char nodeNum[] = "Node xxxxxx" ;
    char *node0 = "Node 000000" ;
    for (i=0; i<TOTALRUNS; ++i) {
        if (i%PRINTINFO==0) {
            printf("%6d\n", i) ;
        }
        WordLoop w = CreateWL(node0) ;
	if (w == NULL) {
            printf("Failure on   0 at %d'th run\n", i) ;
            return ;
        }
        for (j=1; j<1000; ++j) {
            sprintf(&nodeNum[5], "%6d", j) ;
	    if (AddWordWL(w, nodeNum)) {
                printf("Failure on %3d at %6d'th run\n", j, i) ;
                return ;
            }
        }
        if (destroy) DestroyWL(w) ;
    }
}


int main(int argc, char** argv) {

    char cBuff[10] ;
    WordLoop test1, test2 ;
    puts("Performing the university test") ;
    test1 = CreateWL("NCSU") ;
    if (test1 == NULL) {
        puts("Failure @") ;
        return(EXIT_FAILURE) ;
    }
    AddWordWL(test1, "UNCA") ;
    AddWordWL(test1, "UNC-W") ;
    
    if (strcmp(NextWordWL(test1), "NCSU")) {
        puts("Failure A") ;
    }
    
    AdvanceWL(test1) ;
    if (strcmp(NextWordWL(test1), "UNCA")) {
        puts("Failure B") ;
    }

    AdvanceWL(test1) ;
    if (strcmp(NextWordWL(test1), "UNC-W")) {
        puts("Failure C") ;
    }

    AdvanceWL(test1) ;
    if (strcmp(NextWordWL(test1), "NCSU")) {
        puts("Failure D") ;
    }

    puts("Performing the shared buffer test") ;
    strcpy(cBuff, "First") ;
    test2 = CreateWL(cBuff) ;
    strcpy(cBuff, "Second") ;
    AddWordWL(test2, cBuff) ;
    if (strcmp(NextWordWL(test2), "First")) {
        puts("Failure First") ;
    }
    AdvanceWL(test2) ;
    if (strcmp(NextWordWL(test2), "Second")) {
        puts("Failure Second") ;
    }

    puts("Testing with destroy on") ;
    testDestroy(1) ;
    puts("Testing with destroy off") ;
    puts("Eventually this should use up all memory"); 
    testDestroy(0) ;
    return (EXIT_SUCCESS);
}

