2 #include "curl_config.h"
8 struct curl_llist *llist;
10 static void test_curl_llist_dtor(void *key, void *value)
12 /* used by the llist API, does nothing here */
17 static CURLcode unit_setup(void)
19 llist = Curl_llist_alloc(test_curl_llist_dtor);
21 return CURLE_OUT_OF_MEMORY;
25 static void unit_stop(void)
27 Curl_llist_destroy(llist, NULL);
31 int unusedData_case1 = 1;
32 int unusedData_case2 = 2;
33 int unusedData_case3 = 3;
34 struct curl_llist_element *head;
35 struct curl_llist_element *element_next;
36 struct curl_llist_element *element_prev;
37 struct curl_llist_element *to_remove;
38 size_t llist_size = Curl_llist_count(llist);
41 fail_unless(llist->size == 0, "list initial size should be zero");
42 fail_unless(llist->head == NULL, "list head should initiate to NULL");
43 fail_unless(llist->tail == NULL, "list tail should intiate to NULL");
44 fail_unless(llist->dtor == test_curl_llist_dtor,
45 "list dtor shold initiate to test_curl_llist_dtor");
48 * testing Curl_llist_insert_next
52 * 1: list size will be 1
53 * 2: list head will hold the data "unusedData_case1"
54 * 3: list tail will be the same as list head
57 curlErrCode = Curl_llist_insert_next(llist, llist->head, &unusedData_case1);
58 if(curlErrCode == 1) {
59 fail_unless(Curl_llist_count(llist) == 1,
60 "List size should be 1 after adding a new element");
61 /*test that the list head data holds my unusedData */
62 fail_unless(llist->head->ptr == &unusedData_case1,
63 "List size should be 1 after adding a new element");
64 /*same goes for the list tail */
65 fail_unless(llist->tail == llist->head,
66 "List size should be 1 after adding a new element");
69 * testing Curl_llist_insert_next
71 * list has 1 element, adding one element after the head
73 * 1: the element next to head should be our newly created element
74 * 2: the list tail should be our newly created element
77 curlErrCode = Curl_llist_insert_next(llist, llist->head,
79 if(curlErrCode == 1) {
80 fail_unless(llist->head->next->ptr == &unusedData_case3,
81 "the node next to head is not getting set correctly");
82 fail_unless(llist->tail->ptr == &unusedData_case3,
83 "the list tail is not getting set correctly");
86 printf("skipping Curl_llist_insert_next as a non "
87 "success error code was returned\n");
91 * testing Curl_llist_insert_next
93 * list has >1 element, adding one element after "NULL"
95 * 1: the element next to head should be our newly created element
96 * 2: the list tail should different from newly created element
99 curlErrCode = Curl_llist_insert_next(llist, llist->head,
101 if(curlErrCode == 1) {
102 fail_unless(llist->head->next->ptr == &unusedData_case2,
103 "the node next to head is not getting set correctly");
104 /* better safe than sorry, check that the tail isn't corrupted */
105 fail_unless(llist->tail->ptr != &unusedData_case2,
106 "the list tail is not getting set correctly");
109 printf("skipping Curl_llist_insert_next as a non "
110 "success error code was returned\n");
115 printf("skipping Curl_llist_insert_next as a non "
116 "success error code was returned\n");
119 /* unit tests for Curl_llist_remove */
123 * list has >1 element, removing head
125 * 1: list size will be decremented by one
126 * 2: head will be the head->next
127 * 3: "new" head's previous will be NULL
131 abort_unless(head, "llist->head is NULL");
132 element_next = head->next;
133 llist_size = Curl_llist_count(llist);
135 Curl_llist_remove(llist, llist->head, NULL);
137 fail_unless(Curl_llist_count(llist) == (llist_size-1),
138 "llist size not decremented as expected");
139 fail_unless(llist->head == element_next,
140 "llist new head not modified properly");
141 abort_unless(llist->head, "llist->head is NULL");
142 fail_unless(llist->head->prev == NULL,
143 "new head previous not set to null");
147 * removing non head element, with list having >=2 elements
149 * 1: insert another element to the list to make element >=2
151 * 1: list size will be decremented by one ; tested
152 * 2: element->previous->next will be element->next
153 * 3: element->next->previous will be element->previous
155 Curl_llist_insert_next(llist, llist->head, &unusedData_case3);
156 llist_size = Curl_llist_count(llist);
157 to_remove = llist->head->next;
158 abort_unless(to_remove, "to_remove is NULL");
159 element_next = to_remove->next;
160 element_prev = to_remove->prev;
161 Curl_llist_remove(llist, to_remove, NULL);
162 fail_unless(element_prev->next == element_next,
163 "element previous->next is not being adjusted");
164 abort_unless(element_next, "element_next is NULL");
165 fail_unless(element_next->prev == element_prev,
166 "element next->previous is not being adjusted");
170 * removing the tail with list having >=1 element
172 * 1: list size will be decremented by one ;tested
173 * 2: element->previous->next will be element->next ;tested
174 * 3: element->next->previous will be element->previous ;tested
175 * 4: list->tail will be tail->previous
178 to_remove = llist->tail;
179 element_prev = to_remove->prev;
180 Curl_llist_remove(llist, to_remove, NULL);
181 fail_unless(llist->tail == element_prev,
182 "llist tail is not being adjusted when removing tail");
186 * removing head with list having 1 element
188 * 1: list size will be decremented by one ;tested
189 * 2: list head will be null
190 * 3: list tail will be null
193 to_remove = llist->head;
194 Curl_llist_remove(llist, to_remove, NULL);
195 fail_unless(llist->head == NULL,
196 "llist head is not NULL while the llist is empty");
197 fail_unless(llist->tail == NULL,
198 "llist tail is not NULL while the llist is empty");