/* $Id$
*/
#include "test.h"
/**
* \page page_pjlib_list_test Test: Linked List
*
* This file provides implementation of \b list_test(). It tests the
* functionality of the linked-list API.
*
* \section list_test_sec Scope of the Test
*
* API tested:
* - pj_list_init()
* - pj_list_insert_before()
* - pj_list_insert_after()
* - pj_list_merge_last()
* - pj_list_empty()
* - pj_list_insert_nodes_before()
* - pj_list_erase()
* - pj_list_find_node()
* - pj_list_search()
*
*
* This file is pjlib-test/list.c
*
* \include pjlib-test/list.c
*/
#if INCLUDE_LIST_TEST
#include
typedef struct list_node
{
PJ_DECL_LIST_MEMBER(struct list_node);
int value;
} list_node;
static int compare_node(void *value, const pj_list_type *nd)
{
list_node *node = (list_node*)nd;
return ((int)value == node->value) ? 0 : -1;
}
#define PJ_SIGNED_ARRAY_SIZE(a) ((int)PJ_ARRAY_SIZE(a))
int list_test()
{
list_node nodes[4]; // must be even number of nodes
list_node list;
list_node list2;
list_node *p;
int i; // don't change to unsigned!
//
// Test insert_before().
//
list.value = (unsigned)-1;
pj_list_init(&list);
for (i=0; inext) {
pj_assert(p->value == i);
if (p->value != i) {
return -1;
}
}
//
// Test insert_after()
//
pj_list_init(&list);
for (i=PJ_SIGNED_ARRAY_SIZE(nodes)-1; i>=0; --i) {
pj_list_insert_after(&list, &nodes[i]);
}
// check.
for (i=0, p=list.next; inext) {
pj_assert(p->value == i);
if (p->value != i) {
return -1;
}
}
//
// Test merge_last()
//
// Init lists
pj_list_init(&list);
pj_list_init(&list2);
for (i=0; inext) {
pj_assert(p->value == i);
if (p->value != i) {
return -1;
}
}
// check list is empty
pj_assert( pj_list_empty(&list2) );
if (!pj_list_empty(&list2)) {
return -1;
}
//
// Check merge_first()
//
pj_list_init(&list);
pj_list_init(&list2);
for (i=0; inext) {
pj_assert(p->value == i);
if (p->value != i) {
return -1;
}
}
// check list is empty
pj_assert( pj_list_empty(&list) );
if (!pj_list_empty(&list)) {
return -1;
}
//
// Test insert_nodes_before()
//
// init list
pj_list_init(&list);
for (i=0; inext) {
pj_assert(p->value == i);
if (p->value != i) {
return -1;
}
}
// erase test.
pj_list_init(&list);
for (i=0; i=0; --i) {
int j;
pj_list_erase(&nodes[i]);
for (j=0, p=list.next; jnext) {
pj_assert(p->value == j);
if (p->value != j) {
return -1;
}
}
}
// find and search
pj_list_init(&list);
for (i=0; i