blob: 2a2dafb83c8f767466dd4a8144ac75c41f20761a [file] [log] [blame]
Benny Prijonoe7224612005-11-13 19:40:44 +00001/* $Id$
2 */
3/*
4 * PJLIB - PJ Foundation Library
5 * (C)2003-2005 Benny Prijono <bennylp@bulukucing.org>
6 *
7 * Author:
8 * Benny Prijono <bennylp@bulukucing.org>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24#include "test.h"
25
26/**
27 * \page page_pjlib_list_test Test: Linked List
28 *
29 * This file provides implementation of \b list_test(). It tests the
30 * functionality of the linked-list API.
31 *
32 * \section list_test_sec Scope of the Test
33 *
34 * API tested:
35 * - pj_list_init()
36 * - pj_list_insert_before()
37 * - pj_list_insert_after()
38 * - pj_list_merge_last()
39 * - pj_list_empty()
40 * - pj_list_insert_nodes_before()
41 * - pj_list_erase()
42 * - pj_list_find_node()
43 * - pj_list_search()
44 *
45 *
46 * This file is <b>pjlib-test/list.c</b>
47 *
48 * \include pjlib-test/list.c
49 */
50
51#if INCLUDE_LIST_TEST
52
53#include <pjlib.h>
54
55typedef struct list_node
56{
57 PJ_DECL_LIST_MEMBER(struct list_node);
58 int value;
59} list_node;
60
61static int compare_node(void *value, const pj_list_type *nd)
62{
63 list_node *node = (list_node*)nd;
64 return ((long)value == node->value) ? 0 : -1;
65}
66
67#define PJ_SIGNED_ARRAY_SIZE(a) ((int)PJ_ARRAY_SIZE(a))
68
69int list_test()
70{
71 list_node nodes[4]; // must be even number of nodes
72 list_node list;
73 list_node list2;
74 list_node *p;
75 int i; // don't change to unsigned!
76
77 //
78 // Test insert_before().
79 //
80 list.value = (unsigned)-1;
81 pj_list_init(&list);
82 for (i=0; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i) {
83 nodes[i].value = i;
84 pj_list_insert_before(&list, &nodes[i]);
85 }
86 // check.
87 for (i=0, p=list.next; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i, p=p->next) {
88 pj_assert(p->value == i);
89 if (p->value != i) {
90 return -1;
91 }
92 }
93
94 //
95 // Test insert_after()
96 //
97 pj_list_init(&list);
98 for (i=PJ_SIGNED_ARRAY_SIZE(nodes)-1; i>=0; --i) {
99 pj_list_insert_after(&list, &nodes[i]);
100 }
101 // check.
102 for (i=0, p=list.next; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i, p=p->next) {
103 pj_assert(p->value == i);
104 if (p->value != i) {
105 return -1;
106 }
107 }
108
109 //
110 // Test merge_last()
111 //
112 // Init lists
113 pj_list_init(&list);
114 pj_list_init(&list2);
115 for (i=0; i<PJ_SIGNED_ARRAY_SIZE(nodes)/2; ++i) {
116 pj_list_insert_before(&list, &nodes[i]);
117 }
118 for (i=PJ_SIGNED_ARRAY_SIZE(nodes)/2; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i) {
119 pj_list_insert_before(&list2, &nodes[i]);
120 }
121 // merge
122 pj_list_merge_last(&list, &list2);
123 // check.
124 for (i=0, p=list.next; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i, p=p->next) {
125 pj_assert(p->value == i);
126 if (p->value != i) {
127 return -1;
128 }
129 }
130 // check list is empty
131 pj_assert( pj_list_empty(&list2) );
132 if (!pj_list_empty(&list2)) {
133 return -1;
134 }
135
136 //
137 // Check merge_first()
138 //
139 pj_list_init(&list);
140 pj_list_init(&list2);
141 for (i=0; i<PJ_SIGNED_ARRAY_SIZE(nodes)/2; ++i) {
142 pj_list_insert_before(&list, &nodes[i]);
143 }
144 for (i=PJ_SIGNED_ARRAY_SIZE(nodes)/2; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i) {
145 pj_list_insert_before(&list2, &nodes[i]);
146 }
147 // merge
148 pj_list_merge_first(&list2, &list);
149 // check (list2).
150 for (i=0, p=list2.next; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i, p=p->next) {
151 pj_assert(p->value == i);
152 if (p->value != i) {
153 return -1;
154 }
155 }
156 // check list is empty
157 pj_assert( pj_list_empty(&list) );
158 if (!pj_list_empty(&list)) {
159 return -1;
160 }
161
162 //
163 // Test insert_nodes_before()
164 //
165 // init list
166 pj_list_init(&list);
167 for (i=0; i<PJ_SIGNED_ARRAY_SIZE(nodes)/2; ++i) {
168 pj_list_insert_before(&list, &nodes[i]);
169 }
170 // chain remaining nodes
171 pj_list_init(&nodes[PJ_SIGNED_ARRAY_SIZE(nodes)/2]);
172 for (i=PJ_SIGNED_ARRAY_SIZE(nodes)/2+1; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i) {
173 pj_list_insert_before(&nodes[PJ_SIGNED_ARRAY_SIZE(nodes)/2], &nodes[i]);
174 }
175 // insert nodes
176 pj_list_insert_nodes_before(&list, &nodes[PJ_SIGNED_ARRAY_SIZE(nodes)/2]);
177 // check
178 for (i=0, p=list.next; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i, p=p->next) {
179 pj_assert(p->value == i);
180 if (p->value != i) {
181 return -1;
182 }
183 }
184
185 // erase test.
186 pj_list_init(&list);
187 for (i=0; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i) {
188 nodes[i].value = i;
189 pj_list_insert_before(&list, &nodes[i]);
190 }
191 for (i=PJ_SIGNED_ARRAY_SIZE(nodes)-1; i>=0; --i) {
192 int j;
193 pj_list_erase(&nodes[i]);
194 for (j=0, p=list.next; j<i; ++j, p=p->next) {
195 pj_assert(p->value == j);
196 if (p->value != j) {
197 return -1;
198 }
199 }
200 }
201
202 // find and search
203 pj_list_init(&list);
204 for (i=0; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i) {
205 nodes[i].value = i;
206 pj_list_insert_before(&list, &nodes[i]);
207 }
208 for (i=0; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i) {
209 p = (list_node*) pj_list_find_node(&list, &nodes[i]);
210 pj_assert( p == &nodes[i] );
211 if (p != &nodes[i]) {
212 return -1;
213 }
214 p = (list_node*) pj_list_search(&list, (void*)(long)i, &compare_node);
215 pj_assert( p == &nodes[i] );
216 if (p != &nodes[i]) {
217 return -1;
218 }
219 }
220 return 0;
221}
222
223#else
224/* To prevent warning about "translation unit is empty"
225 * when this test is disabled.
226 */
227int dummy_list_test;
228#endif /* INCLUDE_LIST_TEST */
229
230