CC=gcc
LD=gcc
CFLAGS =-g -Wall `sdl-config --cflags`
-#CFLAGS +=-O9 -funroll-loops -fomit-frame-pointer
+CFLAGS +=-O9 -funroll-loops -fomit-frame-pointer
LDFLAGS=`sdl-config --libs` -lSDL_mixer
C_FILES=src/dlb.c src/linked.c src/sprite.c src/ag.c
OBJ_FILES=src/dlb.o src/linked.o src/sprite.o src/ag.o
TEST_OBJS=$(OBJ_FILES:src/ag.o=src/ag_test.o)
-OUT_FILE=ag
all:ag
src/ag_test.o: src/ag_test.c
$(CC) $(CFLAGS) -DUNIT_TEST -c -o $@ $^
-tests: test_ag test_linked
+tests: test_ag test_linked test_dlb
test_ag: ag $(TEST_OBJS) src/unittest.h
$(LD) $(LDFLAGS) -o $@ $(TEST_OBJS)
test_linked: src/linked.c src/unittest.h
$(CC) $(CFLAGS) -Isrc -DUNIT_TEST $< -o $@
+test_dlb: src/dlb.c src/unittest.h
+ $(CC) $(CFLAGS) -Isrc -DUNIT_TEST $< -o $@
+
clean:
rm -f src/*.o
rm -f test_linked test_ag
* -------------------------------------------------------------------
*/
+#include <sys/types.h>
+#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
outputs: a random word
***********************************************************/
-static char *
-getRandomWord()
+static void
+getRandomWord(char *output, size_t length)
{
- FILE* wordlist;
- int filelocation;
- int i;
- char* wordFromList = malloc(sizeof(char) * 50);
- int len=0;
- int done = 0;
+ FILE *fp;
+ struct stat statbuf;
+ char buffer[64];
- filelocation = rand()%10000;
+ assert(length == 9);
strcpy(txt,language);
- wordlist = fopen(strcat(txt,"wordlist.txt"),"r");
-
- for (i = 0; i <= filelocation; i++) {
+ strcat(txt,"wordlist.txt");
+ fp = fopen(txt,"r");
- if (fscanf(wordlist, "%s", wordFromList) != EOF) {
- /* spin on */
- } else {
- /* go back to the start of the file */
- fseek(wordlist, 0L, SEEK_SET);
- }
- }
+ stat(txt, &statbuf);
+ fseek(fp, (rand() % statbuf.st_size), SEEK_SET);
+ if (fscanf(fp, "%63s", buffer) == EOF)
+ fseek(fp, 0, SEEK_SET);
/* ok random location reached */
- while (!done) {
- len = strlen(wordFromList);
- if ((len==7)) {/* ||(len==6)||(len==5)){ */
- done = 1;
- } else {
- if (fscanf(wordlist, "%s", wordFromList) != EOF) {
- /* spin on */
- } else {
- /* go back to the start of the file */
- fseek(wordlist, 0L, SEEK_SET);
- i = fscanf(wordlist, "%s", wordFromList);
- assert(i != EOF);
- }
+ while (strlen(buffer) != 7) {
+ if (fscanf(fp, "%63s", buffer) == EOF) {
+ /* go back to the start of the file */
+ int i;
+ fseek(fp, 0L, SEEK_SET);
+ i = fscanf(fp, "%63s", buffer);
+ assert(i != EOF);
}
}
- fclose(wordlist);
+ fclose(fp);
/* add in our space character */
- wordFromList[len] = ' ';
- wordFromList[len+1] = '\0';
-
- return wordFromList;
+ strcpy(output, buffer);
+ strcat(output, " ");
+ return;
}
/***********************************************************
remain = malloc(sizeof(char)*50);
while (!happy) {
+ char buffer[9];
+ getRandomWord(buffer, sizeof(buffer));
strcpy(guess,"");
- strcpy(rootWord, getRandomWord());
+ strcpy(rootWord, buffer);
bigWordLen = strlen(rootWord)-1;
strcpy(remain,rootWord);
/* identify the resource locale */
init_locale(argc, argv);
+ /* create dictionary */
+ strcpy(txt, language);
+ if (!dlb_create(&dlbHead, strcat(txt, "wordlist.txt"))) {
+ printf("failed to open word list file\n");
+ exit(1);
+ }
+
if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0){
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
bufferSounds(&soundCache);
- /* create dictionary */
- createDLBTree(&dlbHead, language);
-
/* cache in-game graphics */
strcpy(txt, language);
letterBank = SDL_LoadBMP(strcat(txt,"images/letterBank.bmp"));
free(rootWord);
Mix_CloseAudio();
clearSoundBuffer(&soundCache);
- /* trashDLBTree(); */
+ dlb_free(dlbHead);
destroyLetters(&letters);
+ destroyAnswers(&head);
SDL_FreeSurface(screen);
SDL_FreeSurface(letterBank);
SDL_FreeSurface(smallLetterBank);
return 0;
}
+static int test_getRandomWord()
+{
+ char buffer[9];
+ strcpy(language, "i18n/en_GB/");
+ getRandomWord(buffer, sizeof(buffer));
+ test_equals_int("randword last char is space", ' ', buffer[7]);
+ return 0;
+}
+
struct unit_test_t unit_tests[] = {
{NULL, test_shiftLeftKill, NULL},
{NULL, test_shiftLeft, NULL},
{NULL, test_nextBlank, NULL},
{NULL, test_whereinstr, NULL},
+ {NULL, test_getRandomWord, NULL},
};
int