Alexandre Lision | 8af73cb | 2013-12-10 14:11:20 -0500 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
| 19 | */ |
| 20 | #include "test.h" |
| 21 | |
| 22 | /** |
| 23 | * \page page_pjlib_list_test Test: Linked List |
| 24 | * |
| 25 | * This file provides implementation of \b list_test(). It tests the |
| 26 | * functionality of the linked-list API. |
| 27 | * |
| 28 | * \section list_test_sec Scope of the Test |
| 29 | * |
| 30 | * API tested: |
| 31 | * - pj_list_init() |
| 32 | * - pj_list_insert_before() |
| 33 | * - pj_list_insert_after() |
| 34 | * - pj_list_merge_last() |
| 35 | * - pj_list_empty() |
| 36 | * - pj_list_insert_nodes_before() |
| 37 | * - pj_list_erase() |
| 38 | * - pj_list_find_node() |
| 39 | * - pj_list_search() |
| 40 | * |
| 41 | * |
| 42 | * This file is <b>pjlib-test/list.c</b> |
| 43 | * |
| 44 | * \include pjlib-test/list.c |
| 45 | */ |
| 46 | |
| 47 | #if INCLUDE_LIST_TEST |
| 48 | |
| 49 | #include <pjlib.h> |
| 50 | |
| 51 | typedef struct list_node |
| 52 | { |
| 53 | PJ_DECL_LIST_MEMBER(struct list_node); |
| 54 | int value; |
| 55 | } list_node; |
| 56 | |
| 57 | static int compare_node(void *value, const pj_list_type *nd) |
| 58 | { |
| 59 | list_node *node = (list_node*)nd; |
| 60 | return ((long)(pj_ssize_t)value == node->value) ? 0 : -1; |
| 61 | } |
| 62 | |
| 63 | #define PJ_SIGNED_ARRAY_SIZE(a) ((int)PJ_ARRAY_SIZE(a)) |
| 64 | |
| 65 | int list_test() |
| 66 | { |
| 67 | list_node nodes[4]; // must be even number of nodes |
| 68 | list_node list; |
| 69 | list_node list2; |
| 70 | list_node *p; |
| 71 | int i; // don't change to unsigned! |
| 72 | |
| 73 | // |
| 74 | // Test insert_before(). |
| 75 | // |
| 76 | list.value = (unsigned)-1; |
| 77 | pj_list_init(&list); |
| 78 | for (i=0; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i) { |
| 79 | nodes[i].value = i; |
| 80 | pj_list_insert_before(&list, &nodes[i]); |
| 81 | } |
| 82 | // check. |
| 83 | for (i=0, p=list.next; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i, p=p->next) { |
| 84 | pj_assert(p->value == i); |
| 85 | if (p->value != i) { |
| 86 | return -1; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // |
| 91 | // Test insert_after() |
| 92 | // |
| 93 | pj_list_init(&list); |
| 94 | for (i=PJ_SIGNED_ARRAY_SIZE(nodes)-1; i>=0; --i) { |
| 95 | pj_list_insert_after(&list, &nodes[i]); |
| 96 | } |
| 97 | // check. |
| 98 | for (i=0, p=list.next; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i, p=p->next) { |
| 99 | pj_assert(p->value == i); |
| 100 | if (p->value != i) { |
| 101 | return -1; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // |
| 106 | // Test merge_last() |
| 107 | // |
| 108 | // Init lists |
| 109 | pj_list_init(&list); |
| 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]); |
| 113 | } |
| 114 | for (i=PJ_SIGNED_ARRAY_SIZE(nodes)/2; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i) { |
| 115 | pj_list_insert_before(&list2, &nodes[i]); |
| 116 | } |
| 117 | // merge |
| 118 | pj_list_merge_last(&list, &list2); |
| 119 | // check. |
| 120 | for (i=0, p=list.next; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i, p=p->next) { |
| 121 | pj_assert(p->value == i); |
| 122 | if (p->value != i) { |
| 123 | return -1; |
| 124 | } |
| 125 | } |
| 126 | // check list is empty |
| 127 | pj_assert( pj_list_empty(&list2) ); |
| 128 | if (!pj_list_empty(&list2)) { |
| 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | // |
| 133 | // Check merge_first() |
| 134 | // |
| 135 | pj_list_init(&list); |
| 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]); |
| 139 | } |
| 140 | for (i=PJ_SIGNED_ARRAY_SIZE(nodes)/2; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i) { |
| 141 | pj_list_insert_before(&list2, &nodes[i]); |
| 142 | } |
| 143 | // merge |
| 144 | pj_list_merge_first(&list2, &list); |
| 145 | // check (list2). |
| 146 | for (i=0, p=list2.next; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i, p=p->next) { |
| 147 | pj_assert(p->value == i); |
| 148 | if (p->value != i) { |
| 149 | return -1; |
| 150 | } |
| 151 | } |
| 152 | // check list is empty |
| 153 | pj_assert( pj_list_empty(&list) ); |
| 154 | if (!pj_list_empty(&list)) { |
| 155 | return -1; |
| 156 | } |
| 157 | |
| 158 | // |
| 159 | // Test insert_nodes_before() |
| 160 | // |
| 161 | // init list |
| 162 | pj_list_init(&list); |
| 163 | for (i=0; i<PJ_SIGNED_ARRAY_SIZE(nodes)/2; ++i) { |
| 164 | pj_list_insert_before(&list, &nodes[i]); |
| 165 | } |
| 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]); |
| 170 | } |
| 171 | // insert nodes |
| 172 | pj_list_insert_nodes_before(&list, &nodes[PJ_SIGNED_ARRAY_SIZE(nodes)/2]); |
| 173 | // check |
| 174 | for (i=0, p=list.next; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i, p=p->next) { |
| 175 | pj_assert(p->value == i); |
| 176 | if (p->value != i) { |
| 177 | return -1; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // erase test. |
| 182 | pj_list_init(&list); |
| 183 | for (i=0; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i) { |
| 184 | nodes[i].value = i; |
| 185 | pj_list_insert_before(&list, &nodes[i]); |
| 186 | } |
| 187 | for (i=PJ_SIGNED_ARRAY_SIZE(nodes)-1; i>=0; --i) { |
| 188 | int j; |
| 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); |
| 192 | if (p->value != j) { |
| 193 | return -1; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // find and search |
| 199 | pj_list_init(&list); |
| 200 | for (i=0; i<PJ_SIGNED_ARRAY_SIZE(nodes); ++i) { |
| 201 | nodes[i].value = i; |
| 202 | pj_list_insert_before(&list, &nodes[i]); |
| 203 | } |
| 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]) { |
| 208 | return -1; |
| 209 | } |
| 210 | p = (list_node*) pj_list_search(&list, (void*)(pj_ssize_t)i, |
| 211 | &compare_node); |
| 212 | pj_assert( p == &nodes[i] ); |
| 213 | if (p != &nodes[i]) { |
| 214 | return -1; |
| 215 | } |
| 216 | } |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | #else |
| 221 | /* To prevent warning about "translation unit is empty" |
| 222 | * when this test is disabled. |
| 223 | */ |
| 224 | int dummy_list_test; |
| 225 | #endif /* INCLUDE_LIST_TEST */ |
| 226 | |
| 227 | |