/* -*- mode: c ; c-file-style: "canonware-c-style" -*-
 ******************************************************************************
 *
 * Copyright (C) 1996-2004 Jason Evans <jasone@canonware.com>.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice(s), this list of conditions and the following disclaimer
 *    unmodified other than the allowable addition of one or more
 *    copyright notices.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice(s), this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************
 *
 * Version: Onyx 5.1.1
 *
 ******************************************************************************/

#include "../include/libonyx/libonyx.h"

/* Number of queue entries (<= 26, preferrably). */
#define NENTRIES 8

typedef struct list_s list_t;

struct list_s
{
    ql_elm(list_t) link;
    char id;
};

int
main()
{
    uint32_t i;
    list_t entries[NENTRIES], *t;
    ql_head(list_t) head;

    libonyx_init(0, NULL, NULL);
    fprintf(stderr, "Test begin\n");

    /* Initialize entries. */
    fprintf(stderr, "ql_new()\n");
    ql_new(&head);
    for (i = 0; i < NENTRIES; i++)
    {
	entries[i].id = 'a' + i;
	ql_elm_new(&entries[i], link);
    }
    if (ql_first(&head) != NULL)
    {
	fprintf(stderr, "  ql_first(): %c\n", ql_first(&head)->id);
    }
    else
    {
	fprintf(stderr, "  ql_first(): NULL\n");
    }
    if (ql_last(&head, link) != NULL)
    {
	fprintf(stderr, "  ql_last(): %c\n", ql_last(&head,
						     link)->id);
    }
    else
    {
	fprintf(stderr, "  ql_last(): NULL\n");
    }
    fprintf(stderr, "  ql_foreach():");
    ql_foreach(t, &head, link)
    {
	fprintf(stderr, " %c", t->id);
    }
    fprintf(stderr, "\n");
    fprintf(stderr, "  ql_reverse_foreach():");
    ql_reverse_foreach(t, &head, link)
    {
	fprintf(stderr, " %c", t->id);
    }
    fprintf(stderr, "\n");
    ql_foreach(t, &head, link)
    {
	if (ql_next(&head, t, link) != NULL)
	{
	    fprintf(stderr, "  ql_next(%c): %c\n", t->id,
		    ql_next(&head, t, link)->id);
	}
	else
	{
	    fprintf(stderr, "  ql_next(%c): NULL\n", t->id);
	}
    }
    ql_reverse_foreach(t, &head, link)
    {
	if (ql_prev(&head, t, link) != NULL)
	{
	    fprintf(stderr, "  ql_prev(%c): %c\n", t->id,
		    ql_prev(&head, t, link)->id);
	}
	else
	{
	    fprintf(stderr, "  ql_prev(%c): NULL\n", t->id);
	}
    }

    /* Link the entries together. */
    fprintf(stderr, "ql_tail_insert()\n");
    for (i = 0; i < NENTRIES; i++)
    {
	ql_tail_insert(&head, &entries[i], link);
    }
    if (ql_first(&head) != NULL)
    {
	fprintf(stderr, "  ql_first(): %c\n", ql_first(&head)->id);
    }
    else
    {
	fprintf(stderr, "  ql_first(): NULL\n");
    }
    if (ql_last(&head, link) != NULL)
    {
	fprintf(stderr, "  ql_last(): %c\n", ql_last(&head, link)->id);
    }
    else
    {
	fprintf(stderr, "  ql_last(): NULL\n");
    }
    fprintf(stderr, "  ql_foreach():");
    ql_foreach(t, &head, link)
    {
	fprintf(stderr, " %c", t->id);
    }
    fprintf(stderr, "\n");
    fprintf(stderr, "  ql_reverse_foreach():");
    ql_reverse_foreach(t, &head, link)
    {
	fprintf(stderr, " %c", t->id);
    }
    fprintf(stderr, "\n");
    ql_foreach(t, &head, link)
    {
	if (ql_next(&head, t, link) != NULL)
	{
	    fprintf(stderr, "  ql_next(%c): %c\n", t->id,
		    ql_next(&head, t, link)->id);
	}
	else
	{
	    fprintf(stderr, "  ql_next(%c): NULL\n", t->id);
	}
    }
    ql_reverse_foreach(t, &head, link)
	{
	if (ql_prev(&head, t, link) != NULL)
	{
	    fprintf(stderr, "  ql_prev(%c): %c\n", t->id,
		    ql_prev(&head, t, link)->id);
	}
	else
	{
	    fprintf(stderr, "  ql_prev(%c): NULL\n", t->id);
	}
    }

    fprintf(stderr, "ql_tail_remove()\n");
    for (i = 0; i < NENTRIES; i++)
    {
	fprintf(stderr, "  --> Iteration %u\n", i);
	if (ql_first(&head) != NULL)
	{
	    fprintf(stderr, "  ql_first(): %c\n", ql_first(&head)->id);
	}
	else
	{
	    fprintf(stderr, "  ql_first(): NULL\n");
	}
	if (ql_last(&head, link) != NULL)
	{
	    fprintf(stderr, "  ql_last(): %c\n", ql_last(&head, link)->id);
	}
	else
	{
	    fprintf(stderr, "  ql_last(): NULL\n");
	}
	fprintf(stderr, "  ql_foreach():");
	ql_foreach(t, &head, link)
	{
	    fprintf(stderr, " %c", t->id);
	}
	fprintf(stderr, "\n");
	ql_tail_remove(&head, list_t, link);
    }
    if (ql_first(&head) != NULL)
    {
	fprintf(stderr, "  ql_first(): %c\n", ql_first(&head)->id);
    }
    else
    {
	fprintf(stderr, "  ql_first(): NULL\n");
    }
    if (ql_last(&head, link) != NULL)
    {
	fprintf(stderr, "  ql_last(): %c\n", ql_last(&head, link)->id);
    }
    else
    {
	fprintf(stderr, "  ql_last(): NULL\n");
    }
    fprintf(stderr, "  ql_foreach():");
    ql_foreach(t, &head, link)
    {
	fprintf(stderr, " %c", t->id);
    }
    fprintf(stderr, "\n");
    fprintf(stderr, "  ql_reverse_foreach():");
    ql_reverse_foreach(t, &head, link)
    {
	fprintf(stderr, " %c", t->id);
    }
    fprintf(stderr, "\n");
    ql_foreach(t, &head, link)
    {
	if (ql_next(&head, t, link) != NULL)
	{
	    fprintf(stderr, "  ql_next(%c): %c\n", t->id,
		    ql_next(&head, t, link)->id);
	}
	else
	{
	    fprintf(stderr, "  ql_next(%c): NULL\n", t->id);
	}
    }
    ql_reverse_foreach(t, &head, link)
    {
	if (ql_prev(&head, t, link) != NULL)
	{
	    fprintf(stderr, "  ql_prev(%c): %c\n", t->id,
		    ql_prev(&head, t, link)->id);
	}
	else
	{
	    fprintf(stderr, "  ql_prev(%c): NULL\n", t->id);
	}
    }

    /* Link the entries together. */
    fprintf(stderr, "ql_head_insert()\n");
    for (i = 0; i < NENTRIES; i++)
    {
	ql_head_insert(&head, &entries[i], link);
    }
    if (ql_first(&head) != NULL)
    {
	fprintf(stderr, "  ql_first(): %c\n", ql_first(&head)->id);
    }
    else
    {
	fprintf(stderr, "  ql_first(): NULL\n");
    }
    if (ql_last(&head, link) != NULL)
    {
	fprintf(stderr, "  ql_last(): %c\n", ql_last(&head, link)->id);
    }
    else
    {
	fprintf(stderr, "  ql_last(): NULL\n");
    }
    fprintf(stderr, "  ql_foreach():");
    ql_foreach(t, &head, link)
    {
	fprintf(stderr, " %c", t->id);
    }
    fprintf(stderr, "\n");
    fprintf(stderr, "  ql_reverse_foreach():");
    ql_reverse_foreach(t, &head, link)
    {
	fprintf(stderr, " %c", t->id);
    }
    fprintf(stderr, "\n");
    ql_foreach(t, &head, link)
    {
	if (ql_next(&head, t, link) != NULL)
	{
	    fprintf(stderr, "  ql_next(%c): %c\n", t->id,
		    ql_next(&head, t, link)->id);
	}
	else
	{
	    fprintf(stderr, "  ql_next(%c): NULL\n", t->id);
	}
    }
    ql_reverse_foreach(t, &head, link)
    {
	if (ql_prev(&head, t, link) != NULL)
	{
	    fprintf(stderr, "  ql_prev(%c): %c\n", t->id,
		    ql_prev(&head, t, link)->id);
	}
	else
	{
	    fprintf(stderr, "  ql_prev(%c): NULL\n", t->id);
	}
    }

    fprintf(stderr, "ql_head_remove()\n");
    for (i = 0; i < NENTRIES; i++)
    {
	fprintf(stderr, "  --> Iteration %u\n", i);
	if (ql_first(&head) != NULL)
	{
	    fprintf(stderr, "  ql_first(): %c\n", ql_first(&head)->id);
	}
	else
	{
	    fprintf(stderr, "  ql_first(): NULL\n");
	}
	if (ql_last(&head, link) != NULL)
	{
	    fprintf(stderr, "  ql_last(): %c\n", ql_last(&head, link)->id);
	}
	else
	{
	    fprintf(stderr, "  ql_last(): NULL\n");
	}
	fprintf(stderr, "  ql_foreach():");
	ql_foreach(t, &head, link)
	{
	    fprintf(stderr, " %c", t->id);
	}
	fprintf(stderr, "\n");
	ql_head_remove(&head, list_t, link);
    }
    if (ql_first(&head) != NULL)
    {
	fprintf(stderr, "  ql_first(): %c\n", ql_first(&head)->id);
    }
    else
    {
	fprintf(stderr, "  ql_first(): NULL\n");
    }
    if (ql_last(&head, link) != NULL)
    {
	fprintf(stderr, "  ql_last(): %c\n", ql_last(&head, link)->id);
    }
    else
    {
	fprintf(stderr, "  ql_last(): NULL\n");
    }
    fprintf(stderr, "  ql_foreach():");
    ql_foreach(t, &head, link)
    {
	fprintf(stderr, " %c", t->id);
    }
    fprintf(stderr, "\n");
    fprintf(stderr, "  ql_reverse_foreach():");
    ql_reverse_foreach(t, &head, link)
    {
	fprintf(stderr, " %c", t->id);
    }
    fprintf(stderr, "\n");
    ql_foreach(t, &head, link)
    {
	if (ql_next(&head, t, link) != NULL)
	{
	    fprintf(stderr, "  ql_next(%c): %c\n", t->id,
		    ql_next(&head, t, link)->id);
	}
	else
	{
	    fprintf(stderr, "  ql_next(%c): NULL\n", t->id);
	}
    }
    ql_reverse_foreach(t, &head, link)
    {
	if (ql_prev(&head, t, link) != NULL)
	{
	    fprintf(stderr, "  ql_prev(%c): %c\n", t->id,
		    ql_prev(&head, t, link)->id);
	}
	else
	{
	    fprintf(stderr, "  ql_prev(%c): NULL\n", t->id);
	}
    }

    /* ql_remove(), ql_before_insert(), and ql_after_insert() are used
     * internally by other macros that are already tested, so there's no need to
     * test them completely.  However, insertion/deletion from the middle of
     * lists is not tested, so do that here. */
    fprintf(stderr, "ql_tail_insert(), ql_before_insert(),"
	    " ql_after_insert()\n");
    ql_tail_insert(&head, &entries[0], link);
    ql_before_insert(&head, &entries[0], &entries[1], link);
    ql_before_insert(&head, &entries[0], &entries[2], link);
    ql_after_insert(&entries[0], &entries[3], link);
    ql_after_insert(&entries[0], &entries[4], link);
    ql_before_insert(&head, &entries[1], &entries[5], link);
    ql_after_insert(&entries[2], &entries[6], link);
    ql_before_insert(&head, &entries[3], &entries[7], link);

    if (ql_first(&head) != NULL)
    {
	fprintf(stderr, "  ql_first(): %c\n", ql_first(&head)->id);
    }
    else
    {
	fprintf(stderr, "  ql_first(): NULL\n");
    }
    if (ql_last(&head, link) != NULL)
    {
	fprintf(stderr, "  ql_last(): %c\n", ql_last(&head, link)->id);
    }
    else
    {
	fprintf(stderr, "  ql_last(): NULL\n");
    }
    fprintf(stderr, "  ql_foreach():");
    ql_foreach(t, &head, link)
    {
	fprintf(stderr, " %c", t->id);
    }
    fprintf(stderr, "\n");
    fprintf(stderr, "  ql_reverse_foreach():");
    ql_reverse_foreach(t, &head, link)
    {
	fprintf(stderr, " %c", t->id);
    }
    fprintf(stderr, "\n");
    ql_foreach(t, &head, link)
    {
	if (ql_next(&head, t, link) != NULL)
	{
	    fprintf(stderr, "  ql_next(%c): %c\n", t->id,
		    ql_next(&head, t, link)->id);
	}
	else
	{
	    fprintf(stderr, "  ql_next(%c): NULL\n", t->id);
	}
    }
    ql_reverse_foreach(t, &head, link)
    {
	if (ql_prev(&head, t, link) != NULL)
	{
	    fprintf(stderr, "  ql_prev(%c): %c\n", t->id,
		    ql_prev(&head, t, link)->id);
	}
	else
	{
	    fprintf(stderr, "  ql_prev(%c): NULL\n", t->id);
	}
    }

    fprintf(stderr, "ql_remove()\n");
    for (i = 0; i < NENTRIES; i++)
    {
	fprintf(stderr, "  --> Iteration %u\n", i);
	ql_remove(&head, &entries[i], link);
	if (ql_first(&head) != NULL)
	{
	    fprintf(stderr, "  ql_first(): %c\n", ql_first(&head)->id);
	}
	else
	{
	    fprintf(stderr, "  ql_first(): NULL\n");
	}
	if (ql_last(&head, link) != NULL)
	{
	    fprintf(stderr, "  ql_last(): %c\n", ql_last(&head, link)->id);
	}
	else
	{
	    fprintf(stderr, "  ql_last(): NULL\n");
	}
	fprintf(stderr, "  ql_foreach():");
	ql_foreach(t, &head, link)
	{
	    fprintf(stderr, " %c", t->id);
	}
	fprintf(stderr, "\n");
    }

    fprintf(stderr, "Test end\n");
    libonyx_shutdown();
    return 0;
}
