unit1300.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. #include "llist.h"
  24. static struct curl_llist *llist;
  25. static struct curl_llist *llist_destination;
  26. static void test_curl_llist_dtor(void *key, void *value)
  27. {
  28. /* used by the llist API, does nothing here */
  29. (void)key;
  30. (void)value;
  31. }
  32. static CURLcode unit_setup(void)
  33. {
  34. llist = Curl_llist_alloc(test_curl_llist_dtor);
  35. if(!llist)
  36. return CURLE_OUT_OF_MEMORY;
  37. llist_destination = Curl_llist_alloc(test_curl_llist_dtor);
  38. if(!llist_destination) {
  39. Curl_llist_destroy(llist, NULL);
  40. return CURLE_OUT_OF_MEMORY;
  41. }
  42. return CURLE_OK;
  43. }
  44. static void unit_stop(void)
  45. {
  46. Curl_llist_destroy(llist, NULL);
  47. Curl_llist_destroy(llist_destination, NULL);
  48. }
  49. UNITTEST_START
  50. int unusedData_case1 = 1;
  51. int unusedData_case2 = 2;
  52. int unusedData_case3 = 3;
  53. struct curl_llist_element *head;
  54. struct curl_llist_element *element_next;
  55. struct curl_llist_element *element_prev;
  56. struct curl_llist_element *to_remove;
  57. size_t llist_size = Curl_llist_count(llist);
  58. int curlErrCode = 0;
  59. /**
  60. * testing llist_init
  61. * case 1:
  62. * list initiation
  63. * @assumptions:
  64. * 1: list size will be 0
  65. * 2: list head will be NULL
  66. * 3: list tail will be NULL
  67. * 4: list dtor will be NULL
  68. */
  69. fail_unless(llist->size == 0, "list initial size should be zero");
  70. fail_unless(llist->head == NULL, "list head should initiate to NULL");
  71. fail_unless(llist->tail == NULL, "list tail should intiate to NULL");
  72. fail_unless(llist->dtor == test_curl_llist_dtor,
  73. "list dtor shold initiate to test_curl_llist_dtor");
  74. /**
  75. * testing Curl_llist_insert_next
  76. * case 1:
  77. * list is empty
  78. * @assumptions:
  79. * 1: list size will be 1
  80. * 2: list head will hold the data "unusedData_case1"
  81. * 3: list tail will be the same as list head
  82. */
  83. curlErrCode = Curl_llist_insert_next(llist, llist->head, &unusedData_case1);
  84. if(curlErrCode == 1) {
  85. fail_unless(Curl_llist_count(llist) == 1,
  86. "List size should be 1 after adding a new element");
  87. /*test that the list head data holds my unusedData */
  88. fail_unless(llist->head->ptr == &unusedData_case1,
  89. "List size should be 1 after adding a new element");
  90. /*same goes for the list tail */
  91. fail_unless(llist->tail == llist->head,
  92. "List size should be 1 after adding a new element");
  93. /**
  94. * testing Curl_llist_insert_next
  95. * case 2:
  96. * list has 1 element, adding one element after the head
  97. * @assumptions:
  98. * 1: the element next to head should be our newly created element
  99. * 2: the list tail should be our newly created element
  100. */
  101. curlErrCode = Curl_llist_insert_next(llist, llist->head,
  102. &unusedData_case3);
  103. if(curlErrCode == 1) {
  104. fail_unless(llist->head->next->ptr == &unusedData_case3,
  105. "the node next to head is not getting set correctly");
  106. fail_unless(llist->tail->ptr == &unusedData_case3,
  107. "the list tail is not getting set correctly");
  108. }
  109. else {
  110. printf("skipping Curl_llist_insert_next as a non "
  111. "success error code was returned\n");
  112. }
  113. /**
  114. * testing Curl_llist_insert_next
  115. * case 3:
  116. * list has >1 element, adding one element after "NULL"
  117. * @assumptions:
  118. * 1: the element next to head should be our newly created element
  119. * 2: the list tail should different from newly created element
  120. */
  121. curlErrCode = Curl_llist_insert_next(llist, llist->head,
  122. &unusedData_case2);
  123. if(curlErrCode == 1) {
  124. fail_unless(llist->head->next->ptr == &unusedData_case2,
  125. "the node next to head is not getting set correctly");
  126. /* better safe than sorry, check that the tail isn't corrupted */
  127. fail_unless(llist->tail->ptr != &unusedData_case2,
  128. "the list tail is not getting set correctly");
  129. }
  130. else {
  131. printf("skipping Curl_llist_insert_next as a non "
  132. "success error code was returned\n");
  133. }
  134. }
  135. else {
  136. printf("skipping Curl_llist_insert_next as a non "
  137. "success error code was returned\n");
  138. }
  139. /* unit tests for Curl_llist_remove */
  140. /**
  141. * case 1:
  142. * list has >1 element, removing head
  143. * @assumptions:
  144. * 1: list size will be decremented by one
  145. * 2: head will be the head->next
  146. * 3: "new" head's previous will be NULL
  147. */
  148. head=llist->head;
  149. abort_unless(head, "llist->head is NULL");
  150. element_next = head->next;
  151. llist_size = Curl_llist_count(llist);
  152. Curl_llist_remove(llist, llist->head, NULL);
  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. * 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. llist_size = Curl_llist_count(llist);
  172. to_remove = llist->head->next;
  173. abort_unless(to_remove, "to_remove is NULL");
  174. element_next = to_remove->next;
  175. element_prev = to_remove->prev;
  176. Curl_llist_remove(llist, to_remove, NULL);
  177. fail_unless(element_prev->next == element_next,
  178. "element previous->next is not being adjusted");
  179. abort_unless(element_next, "element_next is NULL");
  180. fail_unless(element_next->prev == element_prev,
  181. "element next->previous is not being adjusted");
  182. /**
  183. * case 3:
  184. * removing the tail with list having >=1 element
  185. * @assumptions
  186. * 1: list size will be decremented by one ;tested
  187. * 2: element->previous->next will be element->next ;tested
  188. * 3: element->next->previous will be element->previous ;tested
  189. * 4: list->tail will be tail->previous
  190. */
  191. to_remove = llist->tail;
  192. element_prev = to_remove->prev;
  193. Curl_llist_remove(llist, to_remove, NULL);
  194. fail_unless(llist->tail == element_prev,
  195. "llist tail is not being adjusted when removing tail");
  196. /**
  197. * case 4:
  198. * removing head with list having 1 element
  199. * @assumptions:
  200. * 1: list size will be decremented by one ;tested
  201. * 2: list head will be null
  202. * 3: list tail will be null
  203. */
  204. to_remove = llist->head;
  205. Curl_llist_remove(llist, to_remove, NULL);
  206. fail_unless(llist->head == NULL,
  207. "llist head is not NULL while the llist is empty");
  208. fail_unless(llist->tail == NULL,
  209. "llist tail is not NULL while the llist is empty");
  210. /* @testing Curl_llist_move(struct curl_llist *,
  211. * struct curl_llist_element *, struct curl_llist *,
  212. * struct curl_llist_element *);
  213. */
  214. /**
  215. * @case 1:
  216. * moving head from an llist containg one element to an empty llist
  217. * @assumptions:
  218. * 1: llist size will be 0
  219. * 2: llist_destination size will be 1
  220. * 3: llist head will be NULL
  221. * 4: llist_destination head == llist_destination tail != NULL
  222. */
  223. /*
  224. * @setup
  225. * add one element to the list
  226. */
  227. curlErrCode = Curl_llist_insert_next(llist, llist->head, &unusedData_case1);
  228. /* necessary assertions */
  229. abort_unless(curlErrCode == 1,
  230. "Curl_llist_insert_next returned an error, Can't move on with test");
  231. abort_unless(Curl_llist_count(llist) == 1,
  232. "Number of list elements is not as expected, Aborting");
  233. abort_unless(Curl_llist_count(llist_destination) == 0,
  234. "Number of list elements is not as expected, Aborting");
  235. /*actual testing code*/
  236. curlErrCode = Curl_llist_move(llist, llist->head, llist_destination, NULL);
  237. abort_unless(curlErrCode == 1,
  238. "Curl_llist_move returned an error, Can't move on with test");
  239. fail_unless(Curl_llist_count(llist) == 0,
  240. "moving element from llist didn't decrement the size");
  241. fail_unless(Curl_llist_count(llist_destination) == 1,
  242. "moving element to llist_destination didn't increment the size");
  243. fail_unless(llist->head == NULL,
  244. "llist head not set to null after moving the head");
  245. fail_unless(llist_destination->head != NULL,
  246. "llist_destination head set to null after moving an element");
  247. fail_unless(llist_destination->tail != NULL,
  248. "llist_destination tail set to null after moving an element");
  249. fail_unless(llist_destination->tail == llist_destination->tail,
  250. "llist_destination tail doesn't equal llist_destination head");
  251. UNITTEST_STOP