1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, 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.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
26 static struct Curl_llist llist;
27
28 static struct Curl_llist llist_destination;
29
test_Curl_llist_dtor(void * key,void * value)30 static 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
unit_setup(void)37 static 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
unit_stop(void)44 static void unit_stop(void)
45 {
46 }
47
48 UNITTEST_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 *head;
58 struct Curl_llist_element *element_next;
59 struct Curl_llist_element *element_prev;
60 struct Curl_llist_element *to_remove;
61 size_t llist_size = Curl_llist_count(&llist);
62
63 /**
64 * testing llist_init
65 * case 1:
66 * list initiation
67 * @assumptions:
68 * 1: list size will be 0
69 * 2: list head will be NULL
70 * 3: list tail will be NULL
71 * 4: list dtor will be NULL
72 */
73
74 fail_unless(llist.size == 0, "list initial size should be zero");
75 fail_unless(llist.head == NULL, "list head should initiate to NULL");
76 fail_unless(llist.tail == NULL, "list tail should intiate to NULL");
77 fail_unless(llist.dtor == test_Curl_llist_dtor,
78 "list dtor should initiate to test_Curl_llist_dtor");
79
80 /**
81 * testing Curl_llist_insert_next
82 * case 1:
83 * list is empty
84 * @assumptions:
85 * 1: list size will be 1
86 * 2: list head will hold the data "unusedData_case1"
87 * 3: list tail will be the same as list head
88 */
89
90 Curl_llist_insert_next(&llist, llist.head, &unusedData_case1, &case1_list);
91
92 fail_unless(Curl_llist_count(&llist) == 1,
93 "List size should be 1 after adding a new element");
94 /*test that the list head data holds my unusedData */
95 fail_unless(llist.head->ptr == &unusedData_case1,
96 "head ptr should be first entry");
97 /*same goes for the list tail */
98 fail_unless(llist.tail == llist.head,
99 "tail and head should be the same");
100
101 /**
102 * testing Curl_llist_insert_next
103 * case 2:
104 * list has 1 element, adding one element after the head
105 * @assumptions:
106 * 1: the element next to head should be our newly created element
107 * 2: the list tail should be our newly created element
108 */
109
110 Curl_llist_insert_next(&llist, llist.head,
111 &unusedData_case3, &case3_list);
112 fail_unless(llist.head->next->ptr == &unusedData_case3,
113 "the node next to head is not getting set correctly");
114 fail_unless(llist.tail->ptr == &unusedData_case3,
115 "the list tail is not getting set correctly");
116
117 /**
118 * testing Curl_llist_insert_next
119 * case 3:
120 * list has >1 element, adding one element after "NULL"
121 * @assumptions:
122 * 1: the element next to head should be our newly created element
123 * 2: the list tail should different from newly created element
124 */
125
126 Curl_llist_insert_next(&llist, llist.head,
127 &unusedData_case2, &case2_list);
128 fail_unless(llist.head->next->ptr == &unusedData_case2,
129 "the node next to head is not getting set correctly");
130 /* better safe than sorry, check that the tail isn't corrupted */
131 fail_unless(llist.tail->ptr != &unusedData_case2,
132 "the list tail is not getting set correctly");
133
134 /* unit tests for Curl_llist_remove */
135
136 /**
137 * case 1:
138 * list has >1 element, removing head
139 * @assumptions:
140 * 1: list size will be decremented by one
141 * 2: head will be the head->next
142 * 3: "new" head's previous will be NULL
143 */
144
145 head = llist.head;
146 abort_unless(head, "llist.head is NULL");
147 element_next = head->next;
148 llist_size = Curl_llist_count(&llist);
149
150 Curl_llist_remove(&llist, llist.head, NULL);
151
152 fail_unless(Curl_llist_count(&llist) == (llist_size-1),
153 "llist size not decremented as expected");
154 fail_unless(llist.head == element_next,
155 "llist new head not modified properly");
156 abort_unless(llist.head, "llist.head is NULL");
157 fail_unless(llist.head->prev == NULL,
158 "new head previous not set to null");
159
160 /**
161 * case 2:
162 * removing non head element, with list having >=2 elements
163 * @setup:
164 * 1: insert another element to the list to make element >=2
165 * @assumptions:
166 * 1: list size will be decremented by one ; tested
167 * 2: element->previous->next will be element->next
168 * 3: element->next->previous will be element->previous
169 */
170 Curl_llist_insert_next(&llist, llist.head, &unusedData_case3,
171 &case4_list);
172 llist_size = Curl_llist_count(&llist);
173 fail_unless(llist_size == 3, "should be 3 list members");
174
175 to_remove = llist.head->next;
176 abort_unless(to_remove, "to_remove is NULL");
177 element_next = to_remove->next;
178 element_prev = to_remove->prev;
179 Curl_llist_remove(&llist, to_remove, NULL);
180 fail_unless(element_prev->next == element_next,
181 "element previous->next is not being adjusted");
182 abort_unless(element_next, "element_next is NULL");
183 fail_unless(element_next->prev == element_prev,
184 "element next->previous is not being adjusted");
185
186 /**
187 * case 3:
188 * removing the tail with list having >=1 element
189 * @assumptions
190 * 1: list size will be decremented by one ;tested
191 * 2: element->previous->next will be element->next ;tested
192 * 3: element->next->previous will be element->previous ;tested
193 * 4: list->tail will be tail->previous
194 */
195
196 to_remove = llist.tail;
197 element_prev = to_remove->prev;
198 Curl_llist_remove(&llist, to_remove, NULL);
199 fail_unless(llist.tail == element_prev,
200 "llist tail is not being adjusted when removing tail");
201
202 /**
203 * case 4:
204 * removing head with list having 1 element
205 * @assumptions:
206 * 1: list size will be decremented by one ;tested
207 * 2: list head will be null
208 * 3: list tail will be null
209 */
210
211 to_remove = llist.head;
212 Curl_llist_remove(&llist, to_remove, NULL);
213 fail_unless(llist.head == NULL,
214 "llist head is not NULL while the llist is empty");
215 fail_unless(llist.tail == NULL,
216 "llist tail is not NULL while the llist is empty");
217
218 Curl_llist_destroy(&llist, NULL);
219 Curl_llist_destroy(&llist_destination, NULL);
220 }
221 UNITTEST_STOP
222