/* * Copyright (c) 2006 Claus Assmann * * By using this file, you agree to the terms and conditions set * forth in the license/LICENSE.3C file which can be found at the * top level of this source code distribution. */ #include "sm/generic.h" SM_RCSID("@(#)$Id: t-cat-tq.c,v 1.2 2006/11/20 03:34:08 ca Exp $") #include "sm/assert.h" #include "sm/error.h" #include "sm/time.h" #include "sm/heap.h" #include "sm/test.h" #include "sm/queue.h" #include #define ELEMS 5 static int Verbose = 0; struct q_entry_S { int qe_item; TAILQ_ENTRY(q_entry_S) qe_link; }; TAILQ_HEAD(q_entries_S, q_entry_S); typedef TAILQ_HEAD(, q_entry_S) q_T, *q_P; typedef struct q_entry_S q_entry_T; static sm_ret_T chk(q_T *hd, int cnt) { q_entry_T *lp; int i, prev; prev = -1; i = 0; TAILQ_FOREACH(lp, hd, qe_link) { if (prev > lp->qe_item) return SM_FAILURE; prev = lp->qe_item; ++i; } if (i != cnt) return SM_FAILURE; return SM_SUCCESS; } static void tst_crt(q_P hd, int elems, int first) { int i; q_entry_T *qe; qe = (q_entry_T *) sm_malloc(elems * sizeof(*qe)); SM_TEST(qe != NULL); if (qe == NULL) return; TAILQ_INIT(hd); SM_TEST(TAILQ_EMPTY(hd)); for (i = 0; i < elems; i++) qe[i].qe_item = i + first; for (i = 0; i < elems; i++) TAILQ_INSERT_TAIL(hd, &qe[i], qe_link); chk(hd, elems); } static void tst_cat(int el1, int el2) { q_T head_1, head_2, head_3; tst_crt(&head_1, el1, 0); tst_crt(&head_3, el1, 0); tst_crt(&head_2, el2, el1 + 1); #if 1 TAILQ_CAT(&head_1, &head_2, q_entry_S, qe_link, q_entries_S); #else if (!TAILQ_EMPTY(&head_2)) { if (TAILQ_EMPTY(&head_1)) { (&head_1)->tqh_first = (&head_2)->tqh_first; (&head_1)->tqh_last = (&head_2)->tqh_last; (&head_2)->tqh_first->qe_link.tqe_prev = &((&head_1)->tqh_first); } else { q_entry_T *first_2, *last_2; first_2 = TAILQ_FIRST(&head_2); last_2 = TAILQ_LAST(&head_2, q_entries_S); first_2->qe_link.tqe_prev = (&head_1)->tqh_last; *(&head_2)->tqh_last = (last_2); (&head_2)->tqh_last = &(last_2)->qe_link.tqe_next; } TAILQ_INIT(&head_2); } #endif chk(&head_1, el1 + el2); } static void test_cat_2(void) { int i, j; for (i = 0; i < 5; i++) for (j = 0; j < 5; j++) tst_cat(i, j); } int main(int argc, char *argv[]) { int c; while ((c = getopt(argc, argv, "V")) != -1) { switch (c) { case 'V': Verbose++; break; #if 0 default: usage(argv[0]); return EX_USAGE ; #endif /* 0 */ } } sm_test_begin(argc, argv, "test catenate two queues"); test_cat_2(); return sm_test_end(); }