/* * 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-move-head-cq.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 { int qe_item; CIRCLEQ_ENTRY(q_entry) qe_link; }; typedef CIRCLEQ_HEAD(, q_entry) q_T; typedef struct q_entry q_entry_T; static sm_ret_T chk(q_T *hd, int cnt) { q_entry_T *lp; int i, prev; prev = -1; i = 0; CIRCLEQ_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_replace_head(int elems) { int i; q_entry_T *qe; q_T head_old, head_new; qe = (q_entry_T *) sm_malloc(elems * sizeof(*qe)); SM_TEST(qe != NULL); if (qe == NULL) return; CIRCLEQ_INIT(&head_old); SM_TEST(CIRCLEQ_EMPTY(&head_old)); for (i = 0; i < elems; i++) qe[i].qe_item = i; for (i = 0; i < elems; i++) { CIRCLEQ_INSERT_TAIL(&head_old, &qe[i], qe_link); } chk(&head_old, elems); CIRCLEQ_NEW_HEAD(&head_old, &head_new, qe_link); /* if (CIRCLEQ_EMPTY(&head_old)) { CIRCLEQ_INIT(&head_new); } else { (&head_new)->qh_last = (&head_old)->qh_last; (&head_new)->qh_first = (&head_old)->qh_first; (&head_old)->qh_last->qe_link.qe_next = CIRCLEQ_END(&head_new); (&head_old)->qh_first->qe_link.qe_prev = CIRCLEQ_END(&head_new); CIRCLEQ_INIT(&head_old); } */ SM_TEST(CIRCLEQ_EMPTY(&head_old)); chk(&head_new, elems); sm_free(qe); } static void test_replace_head(void) { int j; for (j = 0; j < 5; j++) tst_replace_head(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 replace head of queue"); test_replace_head(); return sm_test_end(); }