/*
 * 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-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 <stdio.h>

#define ELEMS	5

static int Verbose = 0;

struct q_entry
{
	int                     qe_item;
	TAILQ_ENTRY(q_entry)	qe_link;
};

typedef TAILQ_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;
	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_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;

	TAILQ_INIT(&head_old);
	SM_TEST(TAILQ_EMPTY(&head_old));
	for (i = 0; i < elems; i++)
		qe[i].qe_item = i;

	for (i = 0; i < elems; i++)
	{
		TAILQ_INSERT_TAIL(&head_old, &qe[i], qe_link);
	}
	chk(&head_old, elems);

#if 1
	TAILQ_NEW_HEAD(&head_old, &head_new, qe_link);
#else
	if (TAILQ_EMPTY(&head_old)) {
		TAILQ_INIT(&head_new);
	} else {
		(&head_new)->tqh_first = (&head_old)->tqh_first;
		(&head_new)->tqh_last = (&head_old)->tqh_last;
		(&head_old)->tqh_first->qe_link.tqe_prev = &((&head_new)->tqh_first);
		TAILQ_INIT(&head_old);
	}
#endif

	SM_TEST(TAILQ_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();
}


syntax highlighted by Code2HTML, v. 0.9.1