e37f62767abc21656b1820135f04d3f0acfce5a4
[platform/upstream/curl.git] / tests / unit / unit1300.c
1 #include <stdlib.h>
2 #include "curl_config.h"
3 #include "setup.h"
4
5 #include "llist.h"
6 #include "curlcheck.h"
7
8 struct curl_llist *llist;
9
10 static void test_curl_llist_dtor(void *key, void *value)
11 {
12   /* used by the llist API, does nothing here */
13   (void)key;
14   (void)value;
15 }
16
17 static CURLcode unit_setup(void)
18 {
19   llist = Curl_llist_alloc(test_curl_llist_dtor);
20   if (!llist)
21     return CURLE_OUT_OF_MEMORY;
22   return CURLE_OK;
23 }
24
25 static void unit_stop(void)
26 {
27   Curl_llist_destroy(llist, NULL);
28 }
29
30 UNITTEST_START
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);
39   int curlErrCode = 0;
40
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");
46
47   /**
48    * testing Curl_llist_insert_next
49    * case 1:
50    * list is empty
51    * @assumptions:
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
55    */
56
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");
67
68     /**
69      * testing Curl_llist_insert_next
70      * case 2:
71      * list has 1 element, adding one element after the head
72      * @assumptions:
73      * 1: the element next to head should be our newly created element
74      * 2: the list tail should be our newly created element
75      */
76
77     curlErrCode = Curl_llist_insert_next(llist, llist->head,
78                                          &unusedData_case3);
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");
84     }
85     else {
86       printf("skipping Curl_llist_insert_next as a non "
87              "success error code was returned\n");
88     }
89
90     /**
91      * testing Curl_llist_insert_next
92      * case 3:
93      * list has >1 element, adding one element after "NULL"
94      * @assumptions:
95      * 1: the element next to head should be our newly created element
96      * 2: the list tail should different from newly created element
97      */
98
99     curlErrCode = Curl_llist_insert_next(llist, llist->head,
100                                          &unusedData_case2);
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");
107     }
108     else {
109       printf("skipping Curl_llist_insert_next as a non "
110              "success error code was returned\n");
111     }
112
113   }
114   else {
115     printf("skipping Curl_llist_insert_next as a non "
116            "success error code was returned\n");
117   }
118
119   /* unit tests for Curl_llist_remove */
120
121   /**
122    * case 1:
123    * list has >1 element, removing head
124    * @assumptions:
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
128    */
129
130   head=llist->head;
131   abort_unless(head, "llist->head is NULL");
132   element_next = head->next;
133   llist_size = Curl_llist_count(llist);
134
135   Curl_llist_remove(llist, llist->head, NULL);
136
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");
144
145   /**
146    * case 2:
147    * removing non head element, with list having >=2 elements
148    * @setup:
149    * 1: insert another element to the list to make element >=2
150    * @assumptions:
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
154    */
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");
167
168   /**
169    * case 3:
170    * removing the tail with list having >=1 element
171    * @assumptions
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
176    */
177
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");
183
184   /**
185    * case 4:
186    * removing head with list having 1 element
187    * @assumptions:
188    * 1: list size will be decremented by one ;tested
189    * 2: list head will be null
190    * 3: list tail will be null
191    */
192
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");
199
200 UNITTEST_STOP