2
0

unit1300.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curlcheck.h"
  25. #include "llist.h"
  26. static struct Curl_llist llist;
  27. static struct Curl_llist llist_destination;
  28. static void test_Curl_llist_dtor(void *key, void *value)
  29. {
  30. /* used by the llist API, does nothing here */
  31. (void)key;
  32. (void)value;
  33. }
  34. static CURLcode unit_setup(void)
  35. {
  36. Curl_llist_init(&llist, test_Curl_llist_dtor);
  37. Curl_llist_init(&llist_destination, test_Curl_llist_dtor);
  38. return CURLE_OK;
  39. }
  40. static void unit_stop(void)
  41. {
  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 *head;
  53. struct Curl_llist_element *element_next;
  54. struct Curl_llist_element *element_prev;
  55. struct Curl_llist_element *to_remove;
  56. size_t llist_size = Curl_llist_count(&llist);
  57. /**
  58. * testing llist_init
  59. * case 1:
  60. * list initiation
  61. * @assumptions:
  62. * 1: list size will be 0
  63. * 2: list head will be NULL
  64. * 3: list tail will be NULL
  65. * 4: list dtor will be NULL
  66. */
  67. fail_unless(llist.size == 0, "list initial size should be zero");
  68. fail_unless(llist.head == NULL, "list head should initiate to NULL");
  69. fail_unless(llist.tail == NULL, "list tail should initiate to NULL");
  70. fail_unless(llist.dtor == test_Curl_llist_dtor,
  71. "list dtor should initiate to test_Curl_llist_dtor");
  72. /**
  73. * testing Curl_llist_insert_next
  74. * case 1:
  75. * list is empty
  76. * @assumptions:
  77. * 1: list size will be 1
  78. * 2: list head will hold the data "unusedData_case1"
  79. * 3: list tail will be the same as list head
  80. */
  81. Curl_llist_insert_next(&llist, llist.head, &unusedData_case1, &case1_list);
  82. fail_unless(Curl_llist_count(&llist) == 1,
  83. "List size should be 1 after adding a new element");
  84. /* test that the list head data holds my unusedData */
  85. fail_unless(llist.head->ptr == &unusedData_case1,
  86. "head ptr should be first entry");
  87. /* same goes for the list tail */
  88. fail_unless(llist.tail == llist.head,
  89. "tail and head should be the same");
  90. /**
  91. * testing Curl_llist_insert_next
  92. * case 2:
  93. * list has 1 element, adding one element after the head
  94. * @assumptions:
  95. * 1: the element next to head should be our newly created element
  96. * 2: the list tail should be our newly created element
  97. */
  98. Curl_llist_insert_next(&llist, llist.head,
  99. &unusedData_case3, &case3_list);
  100. fail_unless(llist.head->next->ptr == &unusedData_case3,
  101. "the node next to head is not getting set correctly");
  102. fail_unless(llist.tail->ptr == &unusedData_case3,
  103. "the list tail is not getting set correctly");
  104. /**
  105. * testing Curl_llist_insert_next
  106. * case 3:
  107. * list has >1 element, adding one element after "NULL"
  108. * @assumptions:
  109. * 1: the element next to head should be our newly created element
  110. * 2: the list tail should different from newly created element
  111. */
  112. Curl_llist_insert_next(&llist, llist.head,
  113. &unusedData_case2, &case2_list);
  114. fail_unless(llist.head->next->ptr == &unusedData_case2,
  115. "the node next to head is not getting set correctly");
  116. /* better safe than sorry, check that the tail isn't corrupted */
  117. fail_unless(llist.tail->ptr != &unusedData_case2,
  118. "the list tail is not getting set correctly");
  119. /* unit tests for Curl_llist_remove */
  120. /**
  121. * case 1:
  122. * list has >1 element, removing head
  123. * @assumptions:
  124. * 1: list size will be decremented by one
  125. * 2: head will be the head->next
  126. * 3: "new" head's previous will be NULL
  127. */
  128. head = llist.head;
  129. abort_unless(head, "llist.head is NULL");
  130. element_next = head->next;
  131. llist_size = Curl_llist_count(&llist);
  132. Curl_llist_remove(&llist, llist.head, NULL);
  133. fail_unless(Curl_llist_count(&llist) == (llist_size-1),
  134. "llist size not decremented as expected");
  135. fail_unless(llist.head == element_next,
  136. "llist new head not modified properly");
  137. abort_unless(llist.head, "llist.head is NULL");
  138. fail_unless(llist.head->prev == NULL,
  139. "new head previous not set to null");
  140. /**
  141. * case 2:
  142. * removing non head element, with list having >=2 elements
  143. * @setup:
  144. * 1: insert another element to the list to make element >=2
  145. * @assumptions:
  146. * 1: list size will be decremented by one ; tested
  147. * 2: element->previous->next will be element->next
  148. * 3: element->next->previous will be element->previous
  149. */
  150. Curl_llist_insert_next(&llist, llist.head, &unusedData_case3,
  151. &case4_list);
  152. llist_size = Curl_llist_count(&llist);
  153. fail_unless(llist_size == 3, "should be 3 list members");
  154. to_remove = llist.head->next;
  155. abort_unless(to_remove, "to_remove is NULL");
  156. element_next = to_remove->next;
  157. element_prev = to_remove->prev;
  158. Curl_llist_remove(&llist, to_remove, NULL);
  159. fail_unless(element_prev->next == element_next,
  160. "element previous->next is not being adjusted");
  161. abort_unless(element_next, "element_next is NULL");
  162. fail_unless(element_next->prev == element_prev,
  163. "element next->previous is not being adjusted");
  164. /**
  165. * case 3:
  166. * removing the tail with list having >=1 element
  167. * @assumptions
  168. * 1: list size will be decremented by one ;tested
  169. * 2: element->previous->next will be element->next ;tested
  170. * 3: element->next->previous will be element->previous ;tested
  171. * 4: list->tail will be tail->previous
  172. */
  173. to_remove = llist.tail;
  174. element_prev = to_remove->prev;
  175. Curl_llist_remove(&llist, to_remove, NULL);
  176. fail_unless(llist.tail == element_prev,
  177. "llist tail is not being adjusted when removing tail");
  178. /**
  179. * case 4:
  180. * removing head with list having 1 element
  181. * @assumptions:
  182. * 1: list size will be decremented by one ;tested
  183. * 2: list head will be null
  184. * 3: list tail will be null
  185. */
  186. to_remove = llist.head;
  187. Curl_llist_remove(&llist, to_remove, NULL);
  188. fail_unless(llist.head == NULL,
  189. "llist head is not NULL while the llist is empty");
  190. fail_unless(llist.tail == NULL,
  191. "llist tail is not NULL while the llist is empty");
  192. /**
  193. * testing Curl_llist_append
  194. * case 1:
  195. * list is empty
  196. * @assumptions:
  197. * 1: the element next to head should be our newly created element
  198. * 2: the list tail should different from newly created element
  199. */
  200. Curl_llist_append(&llist, &unusedData_case1, &case1_list);
  201. fail_unless(Curl_llist_count(&llist) == 1,
  202. "List size should be 1 after appending a new element");
  203. /* test that the list head data holds my unusedData */
  204. fail_unless(llist.head->ptr == &unusedData_case1,
  205. "head ptr should be first entry");
  206. /* same goes for the list tail */
  207. fail_unless(llist.tail == llist.head,
  208. "tail and head should be the same");
  209. /**
  210. * testing Curl_llist_append
  211. * case 2:
  212. * list is not empty
  213. * @assumptions:
  214. * 1: the list head-next should be the newly created element
  215. * 2: the list tail should be the newly created element
  216. */
  217. Curl_llist_append(&llist, &unusedData_case2, &case2_list);
  218. fail_unless(llist.head->next->ptr == &unusedData_case2,
  219. "the node next to head is not getting set correctly");
  220. fail_unless(llist.tail->ptr == &unusedData_case2,
  221. "the list tail is not getting set correctly");
  222. /**
  223. * testing Curl_llist_append
  224. * case 3:
  225. * list is has 2 members
  226. * @assumptions:
  227. * 1: the list head-next should remain the same
  228. * 2: the list tail should be the newly created element
  229. */
  230. Curl_llist_append(&llist, &unusedData_case3, &case3_list);
  231. fail_unless(llist.head->next->ptr == &unusedData_case2,
  232. "the node next to head did not stay the same");
  233. fail_unless(llist.tail->ptr == &unusedData_case3,
  234. "the list tail is not getting set correctly");
  235. Curl_llist_destroy(&llist, NULL);
  236. Curl_llist_destroy(&llist_destination, NULL);
  237. }
  238. UNITTEST_STOP