1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
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 https://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "curlcheck.h"
26 static struct curl_llist llist;
28 static struct curl_llist llist_destination;
30 static void test_curl_llist_dtor(void *key, void *value)
32 /* used by the llist API, does nothing here */
37 static CURLcode unit_setup(void)
39 Curl_llist_init(&llist, test_curl_llist_dtor);
40 Curl_llist_init(&llist_destination, test_curl_llist_dtor);
44 static void unit_stop(void)
46 Curl_llist_destroy(&llist, NULL);
47 Curl_llist_destroy(&llist_destination, NULL);
52 int unusedData_case1 = 1;
53 int unusedData_case2 = 2;
54 int unusedData_case3 = 3;
55 struct curl_llist_element case1_list;
56 struct curl_llist_element case2_list;
57 struct curl_llist_element case3_list;
58 struct curl_llist_element case4_list;
59 struct curl_llist_element case5_list;
60 struct curl_llist_element *head;
61 struct curl_llist_element *element_next;
62 struct curl_llist_element *element_prev;
63 struct curl_llist_element *to_remove;
64 size_t llist_size = Curl_llist_count(&llist);
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
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");
84 * testing Curl_llist_insert_next
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
93 Curl_llist_insert_next(&llist, llist.head, &unusedData_case1, &case1_list);
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 "head ptr should be first entry");
100 /*same goes for the list tail */
101 fail_unless(llist.tail == llist.head,
102 "tail and head should be the same");
105 * testing Curl_llist_insert_next
107 * list has 1 element, adding one element after the head
109 * 1: the element next to head should be our newly created element
110 * 2: the list tail should be our newly created element
113 Curl_llist_insert_next(&llist, llist.head,
114 &unusedData_case3, &case3_list);
115 fail_unless(llist.head->next->ptr == &unusedData_case3,
116 "the node next to head is not getting set correctly");
117 fail_unless(llist.tail->ptr == &unusedData_case3,
118 "the list tail is not getting set correctly");
121 * testing Curl_llist_insert_next
123 * list has >1 element, adding one element after "NULL"
125 * 1: the element next to head should be our newly created element
126 * 2: the list tail should different from newly created element
129 Curl_llist_insert_next(&llist, llist.head,
130 &unusedData_case2, &case2_list);
131 fail_unless(llist.head->next->ptr == &unusedData_case2,
132 "the node next to head is not getting set correctly");
133 /* better safe than sorry, check that the tail isn't corrupted */
134 fail_unless(llist.tail->ptr != &unusedData_case2,
135 "the list tail is not getting set correctly");
137 /* unit tests for Curl_llist_remove */
141 * list has >1 element, removing head
143 * 1: list size will be decremented by one
144 * 2: head will be the head->next
145 * 3: "new" head's previous will be NULL
149 abort_unless(head, "llist.head is NULL");
150 element_next = head->next;
151 llist_size = Curl_llist_count(&llist);
153 Curl_llist_remove(&llist, llist.head, NULL);
155 fail_unless(Curl_llist_count(&llist) == (llist_size-1),
156 "llist size not decremented as expected");
157 fail_unless(llist.head == element_next,
158 "llist new head not modified properly");
159 abort_unless(llist.head, "llist.head is NULL");
160 fail_unless(llist.head->prev == NULL,
161 "new head previous not set to null");
165 * removing non head element, with list having >=2 elements
167 * 1: insert another element to the list to make element >=2
169 * 1: list size will be decremented by one ; tested
170 * 2: element->previous->next will be element->next
171 * 3: element->next->previous will be element->previous
173 Curl_llist_insert_next(&llist, llist.head, &unusedData_case3,
175 llist_size = Curl_llist_count(&llist);
176 fail_unless(llist_size == 3, "should be 3 list members");
178 to_remove = llist.head->next;
179 abort_unless(to_remove, "to_remove is NULL");
180 element_next = to_remove->next;
181 element_prev = to_remove->prev;
182 Curl_llist_remove(&llist, to_remove, NULL);
183 fail_unless(element_prev->next == element_next,
184 "element previous->next is not being adjusted");
185 abort_unless(element_next, "element_next is NULL");
186 fail_unless(element_next->prev == element_prev,
187 "element next->previous is not being adjusted");
191 * removing the tail with list having >=1 element
193 * 1: list size will be decremented by one ;tested
194 * 2: element->previous->next will be element->next ;tested
195 * 3: element->next->previous will be element->previous ;tested
196 * 4: list->tail will be tail->previous
199 to_remove = llist.tail;
200 element_prev = to_remove->prev;
201 Curl_llist_remove(&llist, to_remove, NULL);
202 fail_unless(llist.tail == element_prev,
203 "llist tail is not being adjusted when removing tail");
207 * removing head with list having 1 element
209 * 1: list size will be decremented by one ;tested
210 * 2: list head will be null
211 * 3: list tail will be null
214 to_remove = llist.head;
215 Curl_llist_remove(&llist, to_remove, NULL);
216 fail_unless(llist.head == NULL,
217 "llist head is not NULL while the llist is empty");
218 fail_unless(llist.tail == NULL,
219 "llist tail is not NULL while the llist is empty");
221 /* @testing Curl_llist_move(struct curl_llist *,
222 * struct curl_llist_element *, struct curl_llist *,
223 * struct curl_llist_element *);
228 * moving head from an llist containing one element to an empty llist
230 * 1: llist size will be 0
231 * 2: llist_destination size will be 1
232 * 3: llist head will be NULL
233 * 4: llist_destination head == llist_destination tail != NULL
238 * add one element to the list
241 Curl_llist_insert_next(&llist, llist.head, &unusedData_case1,
243 /* necessary assertions */
245 abort_unless(Curl_llist_count(&llist) == 1,
246 "Number of list elements is not as expected, Aborting");
247 abort_unless(Curl_llist_count(&llist_destination) == 0,
248 "Number of list elements is not as expected, Aborting");
250 /*actual testing code*/
251 Curl_llist_move(&llist, llist.head, &llist_destination, NULL);
252 fail_unless(Curl_llist_count(&llist) == 0,
253 "moving element from llist didn't decrement the size");
255 fail_unless(Curl_llist_count(&llist_destination) == 1,
256 "moving element to llist_destination didn't increment the size");
258 fail_unless(llist.head == NULL,
259 "llist head not set to null after moving the head");
261 fail_unless(llist_destination.head != NULL,
262 "llist_destination head set to null after moving an element");
264 fail_unless(llist_destination.tail != NULL,
265 "llist_destination tail set to null after moving an element");
267 fail_unless(llist_destination.tail == llist_destination.tail,
268 "llist_destination tail doesn't equal llist_destination head");