unit1300.c 8.9 KB

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