1/*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al. 9 * 10 * This software is licensed as described in the file COPYING, which 11 * you should have received as part of this distribution. The terms 12 * are also available at http://curl.haxx.se/docs/copyright.html. 13 * 14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell 15 * copies of the Software, and permit persons to whom the Software is 16 * furnished to do so, under the terms of the COPYING file. 17 * 18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 19 * KIND, either express or implied. 20 * 21 ***************************************************************************/ 22#include "curlcheck.h" 23 24#include "llist.h" 25 26static struct curl_llist *llist; 27 28static struct curl_llist *llist_destination; 29 30static void test_curl_llist_dtor(void *key, void *value) 31{ 32 /* used by the llist API, does nothing here */ 33 (void)key; 34 (void)value; 35} 36 37static CURLcode unit_setup(void) 38{ 39 llist = Curl_llist_alloc(test_curl_llist_dtor); 40 if(!llist) 41 return CURLE_OUT_OF_MEMORY; 42 llist_destination = Curl_llist_alloc(test_curl_llist_dtor); 43 if(!llist_destination) 44 return CURLE_OUT_OF_MEMORY; 45 46 return CURLE_OK; 47} 48 49static void unit_stop(void) 50{ 51 Curl_llist_destroy(llist, NULL); 52 Curl_llist_destroy(llist_destination, NULL); 53} 54 55UNITTEST_START 56 int unusedData_case1 = 1; 57 int unusedData_case2 = 2; 58 int unusedData_case3 = 3; 59 struct curl_llist_element *head; 60 struct curl_llist_element *element_next; 61 struct curl_llist_element *element_prev; 62 struct curl_llist_element *to_remove; 63 size_t llist_size = Curl_llist_count(llist); 64 int curlErrCode = 0; 65 66 /** 67 * testing llist_init 68 * case 1: 69 * list initiation 70 * @assumptions: 71 * 1: list size will be 0 72 * 2: list head will be NULL 73 * 3: list tail will be NULL 74 * 4: list dtor will be NULL 75 */ 76 77 fail_unless(llist->size == 0, "list initial size should be zero"); 78 fail_unless(llist->head == NULL, "list head should initiate to NULL"); 79 fail_unless(llist->tail == NULL, "list tail should intiate to NULL"); 80 fail_unless(llist->dtor == test_curl_llist_dtor, 81 "list dtor shold initiate to test_curl_llist_dtor"); 82 83 /** 84 * testing Curl_llist_insert_next 85 * case 1: 86 * list is empty 87 * @assumptions: 88 * 1: list size will be 1 89 * 2: list head will hold the data "unusedData_case1" 90 * 3: list tail will be the same as list head 91 */ 92 93 curlErrCode = Curl_llist_insert_next(llist, llist->head, &unusedData_case1); 94 if(curlErrCode == 1) { 95 fail_unless(Curl_llist_count(llist) == 1, 96 "List size should be 1 after adding a new element"); 97 /*test that the list head data holds my unusedData */ 98 fail_unless(llist->head->ptr == &unusedData_case1, 99 "List size should be 1 after adding a new element"); 100 /*same goes for the list tail */ 101 fail_unless(llist->tail == llist->head, 102 "List size should be 1 after adding a new element"); 103 104 /** 105 * testing Curl_llist_insert_next 106 * case 2: 107 * list has 1 element, adding one element after the head 108 * @assumptions: 109 * 1: the element next to head should be our newly created element 110 * 2: the list tail should be our newly created element 111 */ 112 113 curlErrCode = Curl_llist_insert_next(llist, llist->head, 114 &unusedData_case3); 115 if(curlErrCode == 1) { 116 fail_unless(llist->head->next->ptr == &unusedData_case3, 117 "the node next to head is not getting set correctly"); 118 fail_unless(llist->tail->ptr == &unusedData_case3, 119 "the list tail is not getting set correctly"); 120 } 121 else { 122 printf("skipping Curl_llist_insert_next as a non " 123 "success error code was returned\n"); 124 } 125 126 /** 127 * testing Curl_llist_insert_next 128 * case 3: 129 * list has >1 element, adding one element after "NULL" 130 * @assumptions: 131 * 1: the element next to head should be our newly created element 132 * 2: the list tail should different from newly created element 133 */ 134 135 curlErrCode = Curl_llist_insert_next(llist, llist->head, 136 &unusedData_case2); 137 if(curlErrCode == 1) { 138 fail_unless(llist->head->next->ptr == &unusedData_case2, 139 "the node next to head is not getting set correctly"); 140 /* better safe than sorry, check that the tail isn't corrupted */ 141 fail_unless(llist->tail->ptr != &unusedData_case2, 142 "the list tail is not getting set correctly"); 143 } 144 else { 145 printf("skipping Curl_llist_insert_next as a non " 146 "success error code was returned\n"); 147 } 148 149 } 150 else { 151 printf("skipping Curl_llist_insert_next as a non " 152 "success error code was returned\n"); 153 } 154 155 /* unit tests for Curl_llist_remove */ 156 157 /** 158 * case 1: 159 * list has >1 element, removing head 160 * @assumptions: 161 * 1: list size will be decremented by one 162 * 2: head will be the head->next 163 * 3: "new" head's previous will be NULL 164 */ 165 166 head=llist->head; 167 abort_unless(head, "llist->head is NULL"); 168 element_next = head->next; 169 llist_size = Curl_llist_count(llist); 170 171 Curl_llist_remove(llist, llist->head, NULL); 172 173 fail_unless(Curl_llist_count(llist) == (llist_size-1), 174 "llist size not decremented as expected"); 175 fail_unless(llist->head == element_next, 176 "llist new head not modified properly"); 177 abort_unless(llist->head, "llist->head is NULL"); 178 fail_unless(llist->head->prev == NULL, 179 "new head previous not set to null"); 180 181 /** 182 * case 2: 183 * removing non head element, with list having >=2 elements 184 * @setup: 185 * 1: insert another element to the list to make element >=2 186 * @assumptions: 187 * 1: list size will be decremented by one ; tested 188 * 2: element->previous->next will be element->next 189 * 3: element->next->previous will be element->previous 190 */ 191 Curl_llist_insert_next(llist, llist->head, &unusedData_case3); 192 llist_size = Curl_llist_count(llist); 193 to_remove = llist->head->next; 194 abort_unless(to_remove, "to_remove is NULL"); 195 element_next = to_remove->next; 196 element_prev = to_remove->prev; 197 Curl_llist_remove(llist, to_remove, NULL); 198 fail_unless(element_prev->next == element_next, 199 "element previous->next is not being adjusted"); 200 abort_unless(element_next, "element_next is NULL"); 201 fail_unless(element_next->prev == element_prev, 202 "element next->previous is not being adjusted"); 203 204 /** 205 * case 3: 206 * removing the tail with list having >=1 element 207 * @assumptions 208 * 1: list size will be decremented by one ;tested 209 * 2: element->previous->next will be element->next ;tested 210 * 3: element->next->previous will be element->previous ;tested 211 * 4: list->tail will be tail->previous 212 */ 213 214 to_remove = llist->tail; 215 element_prev = to_remove->prev; 216 Curl_llist_remove(llist, to_remove, NULL); 217 fail_unless(llist->tail == element_prev, 218 "llist tail is not being adjusted when removing tail"); 219 220 /** 221 * case 4: 222 * removing head with list having 1 element 223 * @assumptions: 224 * 1: list size will be decremented by one ;tested 225 * 2: list head will be null 226 * 3: list tail will be null 227 */ 228 229 to_remove = llist->head; 230 Curl_llist_remove(llist, to_remove, NULL); 231 fail_unless(llist->head == NULL, 232 "llist head is not NULL while the llist is empty"); 233 fail_unless(llist->tail == NULL, 234 "llist tail is not NULL while the llist is empty"); 235 236 /* @testing Curl_llist_move(struct curl_llist *, 237 * struct curl_llist_element *, struct curl_llist *, 238 * struct curl_llist_element *); 239 */ 240 241 /** 242 * @case 1: 243 * moving head from an llist containg one element to an empty llist 244 * @assumptions: 245 * 1: llist size will be 0 246 * 2: llist_destination size will be 1 247 * 3: llist head will be NULL 248 * 4: llist_destination head == llist_destination tail != NULL 249 */ 250 251 /* 252 * @setup 253 * add one element to the list 254 */ 255 256 curlErrCode = Curl_llist_insert_next(llist, llist->head, &unusedData_case1); 257 /* necessary assertions */ 258 259 abort_unless(curlErrCode == 1, 260 "Curl_llist_insert_next returned an error, Can't move on with test"); 261 abort_unless(Curl_llist_count(llist) == 1, 262 "Number of list elements is not as expected, Aborting"); 263 abort_unless(Curl_llist_count(llist_destination) == 0, 264 "Number of list elements is not as expected, Aborting"); 265 266 /*actual testing code*/ 267 curlErrCode = Curl_llist_move(llist, llist->head, llist_destination, NULL); 268 abort_unless(curlErrCode == 1, 269 "Curl_llist_move returned an error, Can't move on with test"); 270 fail_unless(Curl_llist_count(llist) == 0, 271 "moving element from llist didn't decrement the size"); 272 273 fail_unless(Curl_llist_count(llist_destination) == 1, 274 "moving element to llist_destination didn't increment the size"); 275 276 fail_unless(llist->head == NULL, 277 "llist head not set to null after moving the head"); 278 279 fail_unless(llist_destination->head != NULL, 280 "llist_destination head set to null after moving an element"); 281 282 fail_unless(llist_destination->tail != NULL, 283 "llist_destination tail set to null after moving an element"); 284 285 fail_unless(llist_destination->tail == llist_destination->tail, 286 "llist_destination tail doesn't equal llist_destination head"); 287 288 289 290UNITTEST_STOP 291