/* * test for queue.h macros */ /******************************************************************************/ #include #include #include "queue.h" #define FAIL(x) printf("%s:%u:FAILED %s\n", __func__, __LINE__, (x) ) /******************************************************************************/ struct test_slist { SLIST_ENTRY(test_slist) entries; int value; }; static struct test_slist *new_slist(void) { struct test_slist *ret; static int value=1; ret=malloc(sizeof *ret); ret->value=value++; return ret; } static void test_slist(void) { SLIST_HEAD(, test_slist) head = SLIST_HEAD_INITIALIZER(head); struct test_slist *curr, *newent; SLIST_INIT(&head); newent=new_slist(); SLIST_INSERT_HEAD(&head, newent, entries); if(SLIST_EMPTY(&head)) { FAIL("SLIST_EMPTY"); return; } curr=SLIST_FIRST(&head); if(curr!=newent) { FAIL("SLIST_FIRST or SLIST_INSERT_HEAD"); return; } newent=new_slist(); SLIST_INSERT_AFTER(curr, newent, entries); curr=SLIST_NEXT(curr, entries); if(curr!=newent) { FAIL("SLIST_NEXT or SLIST_INSERT_AFTER"); return; } SLIST_FOREACH(curr, &head, entries) { if(!curr) { FAIL("SLIST_FOREACH"); return; } printf("%s:%u:entry=%d (%p)\n", __func__, __LINE__, curr->value, (void*)curr); } curr=SLIST_FIRST(&head); newent=new_slist(); SLIST_INSERT_HEAD(&head, newent, entries); SLIST_REMOVE(&head, curr, test_slist, entries); free(curr); while((curr=SLIST_FIRST(&head))) { SLIST_REMOVE_HEAD(&head, entries); free(curr); } } /******************************************************************************/ struct test_simpleq { SIMPLEQ_ENTRY(test_simpleq) entries; int value; }; /* we're just testing the initializer, it can only be used on globals/statics */ SIMPLEQ_HEAD(, test_simpleq) sq_head = SIMPLEQ_HEAD_INITIALIZER(sq_head); static struct test_simpleq *new_simpleq(void) { struct test_simpleq *ret; static int value=1; ret=malloc(sizeof *ret); ret->value=value++; return ret; } static void test_simpleq(void) { SIMPLEQ_HEAD(, test_simpleq) head; struct test_simpleq *curr, *newent; SIMPLEQ_INIT(&head); newent=new_simpleq(); SIMPLEQ_INSERT_HEAD(&head, newent, entries); if(SIMPLEQ_EMPTY(&head)) { FAIL("SIMPLEQ_EMPTY"); return; } curr=SIMPLEQ_FIRST(&head); if(curr!=newent) { FAIL("SIMPLEQ_FIRST or SIMPLEQ_INSERT_HEAD"); return; } newent=new_simpleq(); SIMPLEQ_INSERT_AFTER(&head, curr, newent, entries); curr=SIMPLEQ_NEXT(curr, entries); if(curr!=newent) { FAIL("SIMPLEQ_NEXT or SIMPLEQ_INSERT_AFTER"); return; } SIMPLEQ_FOREACH(curr, &head, entries) { if(!curr) { FAIL("SIMPLEQ_FOREACH"); return; } printf("%s:%u:entry=%d (%p)\n", __func__, __LINE__, curr->value, (void*)curr); } curr=SIMPLEQ_FIRST(&head); newent=new_simpleq(); SIMPLEQ_INSERT_TAIL(&head, newent, entries); SIMPLEQ_REMOVE(&head, curr, test_simpleq, entries); free(curr); while((curr=SIMPLEQ_FIRST(&head))) { SIMPLEQ_REMOVE_HEAD(&head, entries); free(curr); } } /******************************************************************************/ struct test_stailq { STAILQ_ENTRY(test_stailq) entries; }; /* we're just testing the initializer, it can only be used on globals/statics */ STAILQ_HEAD(, test_stailq) stq_head = STAILQ_HEAD_INITIALIZER(stq_head); static void test_stailq(void) { STAILQ_HEAD(, test_stailq) head; STAILQ_INIT(&head); printf("%s() incomplete!\n", __func__); } /******************************************************************************/ struct test_list { LIST_ENTRY(test_list) entries; }; /* we're just testing the initializer, it can only be used on globals/statics */ LIST_HEAD(, test_list) l_head = LIST_HEAD_INITIALIZER(l_head); static void test_list(void) { LIST_HEAD(, test_list) head; LIST_INIT(&head); printf("%s() incomplete!\n", __func__); } /******************************************************************************/ struct test_tailq { TAILQ_ENTRY(test_tailq) entries; }; /* we're just testing the initializer, it can only be used on globals/statics */ TAILQ_HEAD(, test_tailq) tq_head = TAILQ_HEAD_INITIALIZER(tq_head); static void test_tailq(void) { TAILQ_HEAD(, test_tailq) head; TAILQ_INIT(&head); printf("%s() incomplete!\n", __func__); } /******************************************************************************/ struct test_circleq { CIRCLEQ_ENTRY(test_circleq) entries; }; /* we're just testing the initializer, it can only be used on globals/statics */ CIRCLEQ_HEAD(, test_circleq) cq_head = CIRCLEQ_HEAD_INITIALIZER(cq_head); static void test_circleq(void) { CIRCLEQ_HEAD(, test_circleq) head; CIRCLEQ_INIT(&head); printf("%s() incomplete!\n", __func__); } /******************************************************************************/ struct test_sprioq { SPRIOQ_ENTRY(test_sprioq, long) entries; }; static struct test_sprioq *new_sprioq(void) { struct test_sprioq *ret; ret=malloc(sizeof *ret); SPRIOQ_VALUE(ret, entries) = 42; return ret; } static int compar_sprioq(struct test_sprioq *a, struct test_sprioq *b) { return SPRIOQ_VALUE(a, entries) - SPRIOQ_VALUE(b, entries); } static void test_sprioq(void) { SPRIOQ_HEAD(, test_sprioq) head = SPRIOQ_HEAD_INITIALIZER(head); struct test_sprioq *newent, *curr; newent=new_sprioq(); SPRIOQ_INSERT(&head, newent, test_sprioq, entries, compar_sprioq); SPRIOQ_FOREACH(curr, &head, entries) { printf("%s:%u:value=%ld (%p)\n", __func__, __LINE__, SPRIOQ_VALUE(curr, entries), (void*)curr); } } /******************************************************************************/ int main(int argc, char **argv) { test_slist(); test_simpleq(); test_stailq(); test_tailq(); test_list(); test_circleq(); test_sprioq(); return 0; }