#include "zlist.h" #include #define NUMTEST 10000000 //#define DEBUG #define TEST(X,Y) printf("Running test [%s]: ",(X)); \ x = (Y)(); \ printf("%s!\n", result[x]); \ if (!x) return 1; int test_size(void) { zlist *list = NULL; int *num, tests = NUMTEST, x; for (x=0; x < tests; x++) { if (!(num = malloc(sizeof(int)))) return 0; *num++ = x; if (!(list = zlist_append(list, num))) return 0; } if (zlist_count(list) != NUMTEST) return 0; zlist_free(list); return 1; } int test_insert(void) { zlist *l, *m, *list = NULL; char *data; char *tests[] = { "sup", "one in the list!", "dawg", "what", "we", "have", "here", "is", "a", "test", "for", "zlist", "list", "insert", "!", "and", "this", "is", "the", "last", NULL }; int x = 0, y, ret; for (y = 0; tests[y]; y++) continue; if (!(list = zlist_append(list, tests[x++]))) return 0; if (!(list = zlist_append(list, tests[x++]))) return 0; l = list->prev; for (; tests[x]; x++) if (!(l = zlist_insert(l, zlist_tail(l), tests[x]))) return 0; if (y != zlist_count(list)) return 0; m = zlist_head(list); ZLIST_FOR(m, l, data) { #ifdef DEBUG printf("%s ", data); #else if (data) continue; else return 0; #endif } for (y=0; y < x; y++) if (!zlist_list_find(list, zlist_Nth(list, y+1))) ret = 0; else ret = 1; zlist_free(list); return ret; } int test_list_find(void) { zlist *l, *list = NULL; char *tests[] = { "sup", "dawg", "what", "we", "have", "here", "is", "a", "test", "for", "zlist", "list", "finds", "!", "and", "this", "is", "the", "last", "one in the list!", NULL }; int x, y, ret; for (y = 0; tests[y]; y++) continue; for (x = 0; tests[x]; x++) if (!(list = zlist_append(list, tests[x]))) return 0; if (y != zlist_count(list)) return 0; for (y=0; y < x; y++) { l = zlist_Nth(list, y+1); #ifdef DEBUG printf("%s ", (char*)l->data); #endif if (!zlist_list_find(list, l)) ret = 0; else ret = 1; } zlist_free(list); return ret; } int test_append(void) { zlist *l, *m, *list = NULL; char *data; char *tests[] = { "sup", "dawg", "what", "we", "have", "here", "is", "a", "test", "for", "zlist", "appends", "!", "and", "this", "is", "the", "last", "one in the list!", NULL }; int x, y; for (y = 0; tests[y]; y++) continue; for (x = 0; tests[x]; x++) if (!(list = zlist_append(list, strdup(tests[x])))) return 0; if (y != zlist_count(list)) return 0; m = zlist_head(list); ZLIST_FOR(m, l, data) { #ifdef DEBUG printf("%s ", data); #else if (data) continue; #endif } x = 1; ZLIST_FREE(m, data) free(data); if (m) return 0; return 1; } int test_prepend(void) { zlist *l, *m, *list = NULL; char *data; char *tests[] = { "sup", "dawg", "what", "we", "have", "here", "is", "a", "test", "for", "zlist", "prepends", "!", "and", "this", "is", "the", "last", "one in the list!", NULL }; int x, y; for (y = 0; tests[y]; y++) continue; for (x = 0; tests[x]; x++) if (!(list = zlist_prepend(list, strdup(tests[x])))) return 0; if (y != zlist_count(list)) return 0; m = zlist_tail(list); ZLIST_FOR_REVERSE(m, l, data) { #ifdef DEBUG printf("%s ", data); #else if (data) continue; #endif } m = zlist_head(list); x = 1; ZLIST_FREE(m, data) free(data); if (m) return 0; return 1; } int main() { int x; char *result[] = { "FAILED", "PASSED!", NULL }; TEST("APPENDS",test_append) TEST("PREPENDS",test_prepend) TEST("INSERT",test_insert) TEST("LIST_FIND",test_list_find) TEST("LIST_SIZE",test_size) return 0; }