/* Test the Dictionary */ #include #include #include #include "Boolean.h" #include "Dictionary.h" #define BUF_LEN 100 #define MAX_WORDS 1000 int ParseLine( char *buf, char **w1, char **w2 ) { char *cp; assert( buf != NULL ); assert( w1 != NULL ); assert( w2 != NULL ); /* Parse the line and add the word to the dictionary */ cp = buf; /* Skip any leading spaces */ while( (*cp == ' ') ) cp++; /* Save the pointer to the beginning of the English word */ *w1 = cp; /* Skip until we find a TAB */ while( (*cp != '\t') ) cp++; /* Overwrite the tab with a null to terminate the string */ *cp = 0; /* Move the pointer on 1 character */ cp++; /* Skip any more tabs */ while( (*cp == '\t') ) cp++; /* Save the pointer to the beginning of the Foreign word */ *w2 = cp; /* Skip until the end of line or a space */ while( (*cp != ' ') && (*cp != '\n') && (*cp != '\t') ) cp++; /* Overwrite the terminating character */ *cp = 0; return TRUE; } int LoadDictionary( Dictionary d, char *file_name ) { FILE *f; int cnt; char buf[BUF_LEN], *cp, *w1, *w2; assert( d != NULL ); assert( file_name != NULL ); assert( strlen(file_name) > 0 ); cnt = 0; f = fopen( file_name, "r" ); if ( f != NULL ) { while( fgets( buf, BUF_LEN, f ) != NULL ) { /* Parse the line for the two words */ if ( ParseLine( buf, &w1, &w2 ) ) { /* Now add to the dictionary */ /* Ensure pre-condition is met .. OUR responsibility! */ if ( (FindEnglish( d, w1 ) != NULL) || (FindForeign( d, w2 ) != NULL) ) { printf("Word already in dictionary: [%s] or [%s]\n", w1, w2 ); } if ( AddTranslation( d, w1, w2 ) ) { cnt++; assert( cnt == WordCount(d) ); } else printf("Error adding (%s,%s) to dictionary\n", w1, w2 ); } } } else printf("File [%s] not found\n", file_name ); printf("%d words loaded into dictionary\n", cnt ); return cnt; } struct t_test { char *word; int in; } tests[] = { {"you", 1}, {"we", 0}, {"greed",1}, {"socialism",1} }; #define N_TESTS sizeof(tests)/sizeof(struct t_test) void main( int argc, char *argv[] ) { int n, i; char *fn, *cp; Dictionary d; fn = "eng_us.dat"; d = ConsDictionary( MAX_WORDS ); if ( LoadDictionary( d, fn ) > 0 ) { printf("Word count: %d\n", WordCount( d ) ); } /* Run the tests */ for(i=0;i %s\n", tests[i].word, (cp==NULL)?"NO ENTRY":cp ); if( (cp == NULL) == (tests[i].in == 0) ) { } else printf("ERROR at %d\n", i ); } }