/*
* 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-sl.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;
SLIST_ENTRY(q_entry) qe_link;
};
typedef SLIST_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;
SLIST_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;
SLIST_INIT(&head_old);
SM_TEST(SLIST_EMPTY(&head_old));
for (i = 0; i < elems; i++)
qe[i].qe_item = elems + 1 - i;
for (i = 0; i < elems; i++)
{
SLIST_INSERT_HEAD(&head_old, &qe[i], qe_link);
}
chk(&head_old, elems);
#if 1
SLIST_NEW_HEAD(&head_old, &head_new, qe_link);
#else
(&head_new)->slh_first = (&head_old)->slh_first;
SLIST_INIT(&head_old);
#endif
SM_TEST(SLIST_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