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