1
/* $Id: list.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
* \page page_pjlib_list_test Test: Linked List
25
* This file provides implementation of \b list_test(). It tests the
26
* functionality of the linked-list API.
28
* \section list_test_sec Scope of the Test
32
* - pj_list_insert_before()
33
* - pj_list_insert_after()
34
* - pj_list_merge_last()
36
* - pj_list_insert_nodes_before()
38
* - pj_list_find_node()
42
* This file is <b>pjlib-test/list.c</b>
44
* \include pjlib-test/list.c
51
typedef struct list_node
53
PJ_DECL_LIST_MEMBER(struct list_node);
57
static int compare_node(void *value, const pj_list_type *nd)
59
list_node *node = (list_node*)nd;
60
return ((long)value == node->value) ? 0 : -1;
63
#define PJ_SIGNED_ARRAY_SIZE(a) ((int)PJ_ARRAY_SIZE(a))
67
list_node nodes[4]; // must be even number of nodes
71
int i; // don't change to unsigned!
74
// Test insert_before().
76
list.value = (unsigned)-1;
78
for (i=0; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i) {
80
pj_list_insert_before(&list, &nodes[i]);
83
for (i=0, p=list.next; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i, p=p->next) {
84
pj_assert(p->value == i);
91
// Test insert_after()
94
for (i=PJ_SIGNED_ARRAY_SIZE(nodes)-1; i>=0; --i) {
95
pj_list_insert_after(&list, &nodes[i]);
98
for (i=0, p=list.next; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i, p=p->next) {
99
pj_assert(p->value == i);
110
pj_list_init(&list2);
111
for (i=0; i<PJ_SIGNED_ARRAY_SIZE(nodes)/2; ++i) {
112
pj_list_insert_before(&list, &nodes[i]);
114
for (i=PJ_SIGNED_ARRAY_SIZE(nodes)/2; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i) {
115
pj_list_insert_before(&list2, &nodes[i]);
118
pj_list_merge_last(&list, &list2);
120
for (i=0, p=list.next; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i, p=p->next) {
121
pj_assert(p->value == i);
126
// check list is empty
127
pj_assert( pj_list_empty(&list2) );
128
if (!pj_list_empty(&list2)) {
133
// Check merge_first()
136
pj_list_init(&list2);
137
for (i=0; i<PJ_SIGNED_ARRAY_SIZE(nodes)/2; ++i) {
138
pj_list_insert_before(&list, &nodes[i]);
140
for (i=PJ_SIGNED_ARRAY_SIZE(nodes)/2; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i) {
141
pj_list_insert_before(&list2, &nodes[i]);
144
pj_list_merge_first(&list2, &list);
146
for (i=0, p=list2.next; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i, p=p->next) {
147
pj_assert(p->value == i);
152
// check list is empty
153
pj_assert( pj_list_empty(&list) );
154
if (!pj_list_empty(&list)) {
159
// Test insert_nodes_before()
163
for (i=0; i<PJ_SIGNED_ARRAY_SIZE(nodes)/2; ++i) {
164
pj_list_insert_before(&list, &nodes[i]);
166
// chain remaining nodes
167
pj_list_init(&nodes[PJ_SIGNED_ARRAY_SIZE(nodes)/2]);
168
for (i=PJ_SIGNED_ARRAY_SIZE(nodes)/2+1; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i) {
169
pj_list_insert_before(&nodes[PJ_SIGNED_ARRAY_SIZE(nodes)/2], &nodes[i]);
172
pj_list_insert_nodes_before(&list, &nodes[PJ_SIGNED_ARRAY_SIZE(nodes)/2]);
174
for (i=0, p=list.next; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i, p=p->next) {
175
pj_assert(p->value == i);
183
for (i=0; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i) {
185
pj_list_insert_before(&list, &nodes[i]);
187
for (i=PJ_SIGNED_ARRAY_SIZE(nodes)-1; i>=0; --i) {
189
pj_list_erase(&nodes[i]);
190
for (j=0, p=list.next; j<i; ++j, p=p->next) {
191
pj_assert(p->value == j);
200
for (i=0; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i) {
202
pj_list_insert_before(&list, &nodes[i]);
204
for (i=0; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i) {
205
p = (list_node*) pj_list_find_node(&list, &nodes[i]);
206
pj_assert( p == &nodes[i] );
207
if (p != &nodes[i]) {
210
p = (list_node*) pj_list_search(&list, (void*)(long)i, &compare_node);
211
pj_assert( p == &nodes[i] );
212
if (p != &nodes[i]) {
220
/* To prevent warning about "translation unit is empty"
221
* when this test is disabled.
224
#endif /* INCLUDE_LIST_TEST */