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