unit1300.c 8.9 KB

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