/* tester.c * * An interactive test program for linkedlist.c * * AUTHORS: * * Rackham Hoke and Ashfaqur Rahman * * REVISION HISTORY: * * 2/17/00 by Rackham Hoke * - Added test cases for new library functions (see * revision history in stringlist.c) * - NOTE: a description of all functions available in * the tester is included in the help function. * * 2/10/00 by Ashfaqur Rahman and Rackham Hoke * - Added test cases for: * help() * isEmpty() * listFromFile() * listToFile() * * 2/10/00 by Rackham Hoke * - Created this file and basic program structure. * - Implemented readLine(). * - Added tester.c to Makefile. */ #include #include "stringlist.h" #define MAX_LINE MAX_VALUE_LENGTH /* Maximum length of input. */ /*************************/ /* FUNCTION DECLARATIONS */ /*************************/ /* Reads a line from standard input. This function was written to * avoid the use of gets() while losing the newline character that * fgets() includes. * Preconditions: buf can hold up to max characters. * Postconditions: buf holds a line of input. * Problems: none. * Parameters: * buf - the character array to read into. * max - the maximum length of input. * Return: a pointer to buf. */ char* readLine(char* buf,int max); /* Displays information on using the tester. * Preconditions: none. * Postconditions: information output to screen. * Problems: none. * Parameters: none. * Return: nothing. */ void printHelp(void); /* The body of the program. */ int main(void) { stringList* list = newList(); stringList* tmpList; /* Used to check value returned by * listFromFile(). */ int writeSuccess; /* Used to check value returned by * listToFile(). */ char command[MAX_LINE]; /* The command read from the prompt. */ char value[MAX_LINE]; /* Additional values read from the prompt. */ /* Present a friendly greeting. */ printf("\nWelcome to the interactive linked list tester!\n"); printf("An empty list has been created for you.\n\n"); printHelp(); /* Loop until the user quits explicitly. */ while(1) { /* Display standard debugging information -- the current list, * current list reversed (verifies list integrity), and the current * node value. */ printf("\nYour current list: "); listToScreen(list); printf("The list reversed: "); listToScreenReversed(list); printf("Current node: %s\n", currentValue(list)); /* Prompt the user for input. */ printf("\n>"); readLine(command,MAX_LINE); /* Handle the various possible commands. */ if (strcmp(command,"addToFront") == 0) { printf("What value? "); readLine(value,MAX_LINE); addToFront(list,value); } else if (strcmp(command,"addToEnd") == 0) { printf("What value? "); readLine(value,MAX_LINE); addToEnd(list,value); } else if (strcmp(command,"deleteCurrent") == 0) { deleteCurrent(list); } else if (strcmp(command,"quicksortList") == 0) { quicksortList(list); } else if (strcmp(command,"isEmpty") == 0) { if (isEmpty(list)) printf("The list is empty.\n"); else printf("The list is not empty.\n"); } else if (strcmp(command,"moveNext") == 0) { if (!moveNext(list)) printf("Already at the end of the list.\n"); } else if (strcmp(command,"moveBack") == 0) { if (!moveBack(list)) printf("Already at the front of the list.\n"); } else if (strcmp(command,"atEnd") == 0) { if (atEnd(list)) printf("You are at the end of the list.\n"); else printf("You are not at the end of the list.\n"); } else if (strcmp(command,"atFront") == 0) { if (atFront(list)) printf("You are at the front of the list.\n"); else printf("You are not at the front of the list.\n"); } else if (strcmp(command,"listFromFile") == 0) { printf("Enter file name: "); readLine(value,MAX_LINE); tmpList = listFromFile(value); if (tmpList) list = tmpList; else printf("\nThere was trouble reading from the file.\n"); } else if (strcmp(command,"listToFile") == 0) { printf("WARNING: previous data will be lost!\n"); printf("Enter ! to cancel, or enter file name: "); /* Only continue if the user gives the go-ahead. */ if (strcmp(readLine(value,MAX_LINE),"!") != 0) { writeSuccess = listToFile(list,value); if (!writeSuccess) printf("There was an error writing the list to the file.\n"); else printf("File written.\n"); } else printf("File operation canceled.\n"); } else if (strcmp(command,"reverseList") == 0) { reverseList(list); } else if (strcmp(command,"resetCurrent") == 0) { resetCurrent(list); } else if (strcmp(command,"help") == 0) { printHelp(); } else if (strcmp(command,"quit") == 0) { return(0); } else printf("Unknown command.\n"); } } /* BEGIN readLine() */ char* readLine(char* buf,int max) { char* nlPos; /* The position of the newline */ /* Get the input, including newline */ fgets(buf,max,stdin); /* Replace the newline with a string terminator */ nlPos = (char*) strchr(buf,'\n'); if (nlPos) *nlPos = '\0'; return buf; } /* END readLine() */ /* BEGIN printHelp() */ void printHelp(void) { /* Let the user know the options */ printf("You have your choice of the following operations:\n\n"); printf("addToFront -Adds a value to the front of the list.\n"); printf("addToEnd -Adds a value to the end of the list.\n"); printf("deleteCurrent -Deletes the current node in the list.\n"); printf("quicksortList -Sorts the list by value.\n"); printf("isEmpty -Tells you if the list is empty.\n"); printf("moveNext -Advances the current node.\n"); printf("moveBack -Moves back the current node.\n"); printf("atEnd -Tells if you've reached the end of the list.\n"); printf("atFront -Tells if you've reached the front of the list.\n"); printf("listFromFile -Reads a list from a file.\n"); printf("listToFile -Writes the current list to a file.\n"); printf("reverseList -Reverses the list.\n"); printf("resetCurrent -Sets current node to front of the list.\n"); printf("help -Display this help message.\n"); printf("quit -Quit the tester.\n\n"); } /* END printHelp() */