1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2018, 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 https://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 Curl_llist_init(&llist, test_curl_llist_dtor);
40 Curl_llist_init(&llist_destination, test_curl_llist_dtor);
41 return CURLE_OK;
42}
43
44static void unit_stop(void)
45{
46}
47
48UNITTEST_START
49{
50 int unusedData_case1 = 1;
51 int unusedData_case2 = 2;
52 int unusedData_case3 = 3;
53 struct curl_llist_element case1_list;
54 struct curl_llist_element case2_list;
55 struct curl_llist_element case3_list;
56 struct curl_llist_element case4_list;
57 struct curl_llist_element case5_list;
58 struct curl_llist_element *head;
59 struct curl_llist_element *element_next;
60 struct curl_llist_element *element_prev;
61 struct curl_llist_element *to_remove;
62 size_t llist_size = Curl_llist_count(&llist);
63
64 /**
65 * testing llist_init
66 * case 1:
67 * list initiation
68 * @assumptions:
69 * 1: list size will be 0
70 * 2: list head will be NULL
71 * 3: list tail will be NULL
72 * 4: list dtor will be NULL
73 */
74
75 fail_unless(llist.size == 0, "list initial size should be zero");
76 fail_unless(llist.head == NULL, "list head should initiate to NULL");
77 fail_unless(llist.tail == NULL, "list tail should intiate to NULL");
78 fail_unless(llist.dtor == test_curl_llist_dtor,
79 "list dtor shold initiate to test_curl_llist_dtor");
80
81 /**
82 * testing Curl_llist_insert_next
83 * case 1:
84 * list is empty
85 * @assumptions:
86 * 1: list size will be 1
87 * 2: list head will hold the data "unusedData_case1"
88 * 3: list tail will be the same as list head
89 */
90
91 Curl_llist_insert_next(&llist, llist.head, &unusedData_case1, &case1_list);
92
93 fail_unless(Curl_llist_count(&llist) == 1,
94 "List size should be 1 after adding a new element");
95 /*test that the list head data holds my unusedData */
96 fail_unless(llist.head->ptr == &unusedData_case1,
97 "head ptr should be first entry");
98 /*same goes for the list tail */
99 fail_unless(llist.tail == llist.head,
100 "tail and head should be the same");
101
102 /**
103 * testing Curl_llist_insert_next
104 * case 2:
105 * list has 1 element, adding one element after the head
106 * @assumptions:
107 * 1: the element next to head should be our newly created element
108 * 2: the list tail should be our newly created element
109 */
110
111 Curl_llist_insert_next(&llist, llist.head,
112 &unusedData_case3, &case3_list);
113 fail_unless(llist.head->next->ptr == &unusedData_case3,
114 "the node next to head is not getting set correctly");
115 fail_unless(llist.tail->ptr == &unusedData_case3,
116 "the list tail is not getting set correctly");
117
118 /**
119 * testing Curl_llist_insert_next
120 * case 3:
121 * list has >1 element, adding one element after "NULL"
122 * @assumptions:
123 * 1: the element next to head should be our newly created element
124 * 2: the list tail should different from newly created element
125 */
126
127 Curl_llist_insert_next(&llist, llist.head,
128 &unusedData_case2, &case2_list);
129 fail_unless(llist.head->next->ptr == &unusedData_case2,
130 "the node next to head is not getting set correctly");
131 /* better safe than sorry, check that the tail isn't corrupted */
132 fail_unless(llist.tail->ptr != &unusedData_case2,
133 "the list tail is not getting set correctly");
134
135 /* unit tests for Curl_llist_remove */
136
137 /**
138 * case 1:
139 * list has >1 element, removing head
140 * @assumptions:
141 * 1: list size will be decremented by one
142 * 2: head will be the head->next
143 * 3: "new" head's previous will be NULL
144 */
145
146 head = llist.head;
147 abort_unless(head, "llist.head is NULL");
148 element_next = head->next;
149 llist_size = Curl_llist_count(&llist);
150
151 Curl_llist_remove(&llist, llist.head, NULL);
152
153 fail_unless(Curl_llist_count(&llist) == (llist_size-1),
154 "llist size not decremented as expected");
155 fail_unless(llist.head == element_next,
156 "llist new head not modified properly");
157 abort_unless(llist.head, "llist.head is NULL");
158 fail_unless(llist.head->prev == NULL,
159 "new head previous not set to null");
160
161 /**
162 * case 2:
163 * removing non head element, with list having >=2 elements
164 * @setup:
165 * 1: insert another element to the list to make element >=2
166 * @assumptions:
167 * 1: list size will be decremented by one ; tested
168 * 2: element->previous->next will be element->next
169 * 3: element->next->previous will be element->previous
170 */
171 Curl_llist_insert_next(&llist, llist.head, &unusedData_case3,
172 &case4_list);
173 llist_size = Curl_llist_count(&llist);
174 fail_unless(llist_size == 3, "should be 3 list members");
175
176 to_remove = llist.head->next;
177 abort_unless(to_remove, "to_remove is NULL");
178 element_next = to_remove->next;
179 element_prev = to_remove->prev;
180 Curl_llist_remove(&llist, to_remove, NULL);
181 fail_unless(element_prev->next == element_next,
182 "element previous->next is not being adjusted");
183 abort_unless(element_next, "element_next is NULL");
184 fail_unless(element_next->prev == element_prev,
185 "element next->previous is not being adjusted");
186
187 /**
188 * case 3:
189 * removing the tail with list having >=1 element
190 * @assumptions
191 * 1: list size will be decremented by one ;tested
192 * 2: element->previous->next will be element->next ;tested
193 * 3: element->next->previous will be element->previous ;tested
194 * 4: list->tail will be tail->previous
195 */
196
197 to_remove = llist.tail;
198 element_prev = to_remove->prev;
199 Curl_llist_remove(&llist, to_remove, NULL);
200 fail_unless(llist.tail == element_prev,
201 "llist tail is not being adjusted when removing tail");
202
203 /**
204 * case 4:
205 * removing head with list having 1 element
206 * @assumptions:
207 * 1: list size will be decremented by one ;tested
208 * 2: list head will be null
209 * 3: list tail will be null
210 */
211
212 to_remove = llist.head;
213 Curl_llist_remove(&llist, to_remove, NULL);
214 fail_unless(llist.head == NULL,
215 "llist head is not NULL while the llist is empty");
216 fail_unless(llist.tail == NULL,
217 "llist tail is not NULL while the llist is empty");
218
219 /* @testing Curl_llist_move(struct curl_llist *,
220 * struct curl_llist_element *, struct curl_llist *,
221 * struct curl_llist_element *);
222 */
223
224 /**
225 * @case 1:
226 * moving head from an llist containing one element to an empty llist
227 * @assumptions:
228 * 1: llist size will be 0
229 * 2: llist_destination size will be 1
230 * 3: llist head will be NULL
231 * 4: llist_destination head == llist_destination tail != NULL
232 */
233
234 /*
235 * @setup
236 * add one element to the list
237 */
238
239 Curl_llist_insert_next(&llist, llist.head, &unusedData_case1,
240 &case5_list);
241 /* necessary assertions */
242
243 abort_unless(Curl_llist_count(&llist) == 1,
244 "Number of list elements is not as expected, Aborting");
245 abort_unless(Curl_llist_count(&llist_destination) == 0,
246 "Number of list elements is not as expected, Aborting");
247
248 /*actual testing code*/
249 Curl_llist_move(&llist, llist.head, &llist_destination, NULL);
250 fail_unless(Curl_llist_count(&llist) == 0,
251 "moving element from llist didn't decrement the size");
252
253 fail_unless(Curl_llist_count(&llist_destination) == 1,
254 "moving element to llist_destination didn't increment the size");
255
256 fail_unless(llist.head == NULL,
257 "llist head not set to null after moving the head");
258
259 fail_unless(llist_destination.head != NULL,
260 "llist_destination head set to null after moving an element");
261
262 fail_unless(llist_destination.tail != NULL,
263 "llist_destination tail set to null after moving an element");
264
265 fail_unless(llist_destination.tail == llist_destination.head,
266 "llist_destination tail doesn't equal llist_destination head");
267
268 Curl_llist_destroy(&llist, NULL);
269 Curl_llist_destroy(&llist_destination, NULL);
270}
271UNITTEST_STOP
272