unit1300.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, Daniel Stenberg, <[email protected]>, 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. ***************************************************************************/
  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 *head;
  51. struct Curl_llist_element *element_next;
  52. struct Curl_llist_element *element_prev;
  53. struct Curl_llist_element *to_remove;
  54. size_t llist_size = Curl_llist_count(&llist);
  55. /**
  56. * testing llist_init
  57. * case 1:
  58. * list initiation
  59. * @assumptions:
  60. * 1: list size will be 0
  61. * 2: list head will be NULL
  62. * 3: list tail will be NULL
  63. * 4: list dtor will be NULL
  64. */
  65. fail_unless(llist.size == 0, "list initial size should be zero");
  66. fail_unless(llist.head == NULL, "list head should initiate to NULL");
  67. fail_unless(llist.tail == NULL, "list tail should intiate to NULL");
  68. fail_unless(llist.dtor == test_Curl_llist_dtor,
  69. "list dtor should initiate to test_Curl_llist_dtor");
  70. /**
  71. * testing Curl_llist_insert_next
  72. * case 1:
  73. * list is empty
  74. * @assumptions:
  75. * 1: list size will be 1
  76. * 2: list head will hold the data "unusedData_case1"
  77. * 3: list tail will be the same as list head
  78. */
  79. Curl_llist_insert_next(&llist, llist.head, &unusedData_case1, &case1_list);
  80. fail_unless(Curl_llist_count(&llist) == 1,
  81. "List size should be 1 after adding a new element");
  82. /*test that the list head data holds my unusedData */
  83. fail_unless(llist.head->ptr == &unusedData_case1,
  84. "head ptr should be first entry");
  85. /*same goes for the list tail */
  86. fail_unless(llist.tail == llist.head,
  87. "tail and head should be the same");
  88. /**
  89. * testing Curl_llist_insert_next
  90. * case 2:
  91. * list has 1 element, adding one element after the head
  92. * @assumptions:
  93. * 1: the element next to head should be our newly created element
  94. * 2: the list tail should be our newly created element
  95. */
  96. Curl_llist_insert_next(&llist, llist.head,
  97. &unusedData_case3, &case3_list);
  98. fail_unless(llist.head->next->ptr == &unusedData_case3,
  99. "the node next to head is not getting set correctly");
  100. fail_unless(llist.tail->ptr == &unusedData_case3,
  101. "the list tail is not getting set correctly");
  102. /**
  103. * testing Curl_llist_insert_next
  104. * case 3:
  105. * list has >1 element, adding one element after "NULL"
  106. * @assumptions:
  107. * 1: the element next to head should be our newly created element
  108. * 2: the list tail should different from newly created element
  109. */
  110. Curl_llist_insert_next(&llist, llist.head,
  111. &unusedData_case2, &case2_list);
  112. fail_unless(llist.head->next->ptr == &unusedData_case2,
  113. "the node next to head is not getting set correctly");
  114. /* better safe than sorry, check that the tail isn't corrupted */
  115. fail_unless(llist.tail->ptr != &unusedData_case2,
  116. "the list tail is not getting set correctly");
  117. /* unit tests for Curl_llist_remove */
  118. /**
  119. * case 1:
  120. * list has >1 element, removing head
  121. * @assumptions:
  122. * 1: list size will be decremented by one
  123. * 2: head will be the head->next
  124. * 3: "new" head's previous will be NULL
  125. */
  126. head = llist.head;
  127. abort_unless(head, "llist.head is NULL");
  128. element_next = head->next;
  129. llist_size = Curl_llist_count(&llist);
  130. Curl_llist_remove(&llist, llist.head, NULL);
  131. fail_unless(Curl_llist_count(&llist) == (llist_size-1),
  132. "llist size not decremented as expected");
  133. fail_unless(llist.head == element_next,
  134. "llist new head not modified properly");
  135. abort_unless(llist.head, "llist.head is NULL");
  136. fail_unless(llist.head->prev == NULL,
  137. "new head previous not set to null");
  138. /**
  139. * case 2:
  140. * removing non head element, with list having >=2 elements
  141. * @setup:
  142. * 1: insert another element to the list to make element >=2
  143. * @assumptions:
  144. * 1: list size will be decremented by one ; tested
  145. * 2: element->previous->next will be element->next
  146. * 3: element->next->previous will be element->previous
  147. */
  148. Curl_llist_insert_next(&llist, llist.head, &unusedData_case3,
  149. &case4_list);
  150. llist_size = Curl_llist_count(&llist);
  151. fail_unless(llist_size == 3, "should be 3 list members");
  152. to_remove = llist.head->next;
  153. abort_unless(to_remove, "to_remove is NULL");
  154. element_next = to_remove->next;
  155. element_prev = to_remove->prev;
  156. Curl_llist_remove(&llist, to_remove, NULL);
  157. fail_unless(element_prev->next == element_next,
  158. "element previous->next is not being adjusted");
  159. abort_unless(element_next, "element_next is NULL");
  160. fail_unless(element_next->prev == element_prev,
  161. "element next->previous is not being adjusted");
  162. /**
  163. * case 3:
  164. * removing the tail with list having >=1 element
  165. * @assumptions
  166. * 1: list size will be decremented by one ;tested
  167. * 2: element->previous->next will be element->next ;tested
  168. * 3: element->next->previous will be element->previous ;tested
  169. * 4: list->tail will be tail->previous
  170. */
  171. to_remove = llist.tail;
  172. element_prev = to_remove->prev;
  173. Curl_llist_remove(&llist, to_remove, NULL);
  174. fail_unless(llist.tail == element_prev,
  175. "llist tail is not being adjusted when removing tail");
  176. /**
  177. * case 4:
  178. * removing head with list having 1 element
  179. * @assumptions:
  180. * 1: list size will be decremented by one ;tested
  181. * 2: list head will be null
  182. * 3: list tail will be null
  183. */
  184. to_remove = llist.head;
  185. Curl_llist_remove(&llist, to_remove, NULL);
  186. fail_unless(llist.head == NULL,
  187. "llist head is not NULL while the llist is empty");
  188. fail_unless(llist.tail == NULL,
  189. "llist tail is not NULL while the llist is empty");
  190. Curl_llist_destroy(&llist, NULL);
  191. Curl_llist_destroy(&llist_destination, NULL);
  192. }
  193. UNITTEST_STOP