check_list.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include <stdlib.h> // EXIT_SUCCESS
  2. #include "util/ua_util.h"
  3. #include "util/ua_list.h"
  4. #include "check.h"
  5. /* global test counters */
  6. UA_Int32 visit_count = 0;
  7. UA_Int32 free_count = 0;
  8. void elementVisitor(UA_list_Element* payload){
  9. visit_count++;
  10. }
  11. void visitor(void* payload){
  12. visit_count++;
  13. }
  14. void freer(void* payload){
  15. if(payload){
  16. free_count++;
  17. UA_free(payload);
  18. }
  19. }
  20. _Bool matcher(void* payload){
  21. if(payload == UA_NULL){
  22. return FALSE;
  23. }
  24. if(*((UA_Int32*)payload) == 42){
  25. return TRUE;
  26. }
  27. return FALSE;
  28. }
  29. _Bool comparer(void* payload, void* otherPayload) {
  30. if(payload == UA_NULL || otherPayload == UA_NULL){
  31. return UA_FALSE;
  32. } else {
  33. return ( *((UA_Int32*)payload) == *((UA_Int32*)otherPayload) );
  34. }
  35. }
  36. START_TEST(list_test_basic)
  37. {
  38. UA_list_List list;
  39. UA_list_init(&list);
  40. ck_assert_int_eq(list.size, 0);
  41. UA_Int32* payload;
  42. UA_alloc((void**)&payload, sizeof(*payload));
  43. *payload = 42;
  44. UA_list_addPayloadToFront(&list, payload);
  45. UA_alloc((void**)&payload, sizeof(*payload));
  46. *payload = 24;
  47. UA_list_addPayloadToFront(&list, payload);
  48. UA_alloc((void**)&payload, sizeof(*payload));
  49. *payload = 1;
  50. UA_list_addPayloadToBack(&list, payload);
  51. ck_assert_int_eq(*(UA_Int32*)UA_list_getFirst(&list)->payload, 24);
  52. ck_assert_int_eq(*(UA_Int32*)UA_list_getLast(&list)->payload, 1);
  53. ck_assert_int_eq(list.size, 3);
  54. UA_list_iteratePayload(&list, visitor);
  55. ck_assert_int_eq(visit_count, 3);
  56. UA_list_Element* elem = NULL;
  57. elem = UA_list_find(&list, matcher);
  58. if(elem){
  59. ck_assert_int_eq((*((UA_Int32*)elem->payload)), 42);
  60. UA_list_removeElement(elem, freer);
  61. ck_assert_int_eq(free_count, 1);
  62. free_count = 0; //reset free counter
  63. ck_assert_int_eq(list.size, 2);
  64. }else{
  65. fail("Element 42 not found");
  66. }
  67. UA_list_destroy(&list, freer);
  68. ck_assert_int_eq(free_count, 2);
  69. ck_assert_int_eq(list.size, 0);
  70. }
  71. END_TEST
  72. void myAddPayloadValueToFront(UA_list_List *list, UA_Int32 payloadValue) {
  73. UA_Int32* payload;
  74. UA_alloc((void**)&payload, sizeof(*payload));
  75. *payload = payloadValue;
  76. UA_list_addPayloadToFront(list, payload);
  77. }
  78. void myAddPayloadVectorToFront(UA_list_List *list, UA_Int32 payloadValues[], UA_Int32 n) {
  79. UA_Int32 i = 0;
  80. for (;i<n;i++) {
  81. myAddPayloadValueToFront(list,payloadValues[i]);
  82. }
  83. }
  84. START_TEST(addElementsShallResultInRespectiveSize)
  85. {
  86. // given
  87. UA_list_List list;
  88. UA_list_init(&list);
  89. // when
  90. UA_Int32 plv[] = {42,24,1};
  91. myAddPayloadVectorToFront(&list,plv,sizeof(plv)/sizeof(UA_Int32));
  92. // then
  93. ck_assert_int_eq(list.size, 3);
  94. // finally
  95. UA_list_destroy(&list, freer);
  96. }
  97. END_TEST
  98. START_TEST(findElementShallFind42)
  99. {
  100. // given
  101. UA_list_List list;
  102. UA_list_init(&list);
  103. UA_Int32 plv[] = {42,24,1};
  104. myAddPayloadVectorToFront(&list,plv,sizeof(plv)/sizeof(UA_Int32));
  105. // when
  106. UA_list_Element* e = UA_list_find(&list,matcher);
  107. // then
  108. ck_assert_ptr_ne(e, UA_NULL);
  109. ck_assert_int_eq(*(UA_Int32*)(e->payload), 42);
  110. // finally
  111. UA_list_destroy(&list, freer);
  112. }
  113. END_TEST
  114. START_TEST(searchElementShallFind24)
  115. {
  116. // given
  117. UA_list_List list;
  118. UA_list_init(&list);
  119. UA_Int32 plv[] = {42,24,1};
  120. myAddPayloadVectorToFront(&list,plv,sizeof(plv)/sizeof(UA_Int32));
  121. UA_Int32 payload = 24;
  122. // when
  123. UA_list_Element* e = UA_list_search(&list,comparer,(void*)&payload);
  124. // then
  125. ck_assert_ptr_ne(e, UA_NULL);
  126. ck_assert_int_eq(*(UA_Int32*)(e->payload), 24);
  127. // finally
  128. UA_list_destroy(&list, freer);
  129. }
  130. END_TEST
  131. START_TEST(addAndRemoveShallYieldEmptyList)
  132. {
  133. // given
  134. UA_list_List list;
  135. UA_list_init(&list);
  136. UA_Int32 plv[] = {42};
  137. myAddPayloadVectorToFront(&list,plv,sizeof(plv)/sizeof(UA_Int32));
  138. // when
  139. UA_list_Element* e = UA_list_search(&list,comparer,(void*)&plv[0]);
  140. UA_list_removeElement(e,visitor);
  141. ck_assert_int_eq(visit_count,1);
  142. visit_count = 0;
  143. UA_list_iteratePayload(&list,visitor);
  144. // then
  145. ck_assert_int_eq(list.size,0);
  146. ck_assert_int_eq(visit_count,0);
  147. // finally
  148. UA_list_destroy(&list, freer);
  149. }
  150. END_TEST
  151. START_TEST(addAndRemoveShallYieldEmptyList2)
  152. {
  153. // given
  154. UA_list_List list;
  155. UA_list_init(&list);
  156. UA_Int32 plv[] = {42};
  157. myAddPayloadVectorToFront(&list,plv,sizeof(plv)/sizeof(UA_Int32));
  158. // when
  159. UA_list_search(&list,comparer,(void*)&plv[0]);
  160. UA_list_removeLast(&list,visitor); //additionally testing removeLast explicitly
  161. ck_assert_int_eq(visit_count,1);
  162. visit_count = 0;
  163. UA_list_iterateElement(&list,elementVisitor); //additionally testing iterateElement
  164. // then
  165. ck_assert_int_eq(list.size,0);
  166. ck_assert_int_eq(visit_count,0);
  167. // finally
  168. UA_list_destroy(&list, freer);
  169. }
  170. END_TEST
  171. START_TEST(addTwiceAndRemoveFirstShallYieldListWithOneElement)
  172. {
  173. // given
  174. UA_list_List list;
  175. UA_list_init(&list);
  176. UA_Int32 plv[] = {42,24};
  177. myAddPayloadVectorToFront(&list,plv,sizeof(plv)/sizeof(UA_Int32));
  178. // when
  179. UA_list_Element* e = UA_list_search(&list,comparer,(void*)&plv[0]);
  180. UA_list_removeElement(e,UA_NULL);
  181. visit_count = 0;
  182. UA_list_iteratePayload(&list,visitor);
  183. // then
  184. ck_assert_int_eq(list.size,1);
  185. ck_assert_int_eq(visit_count,1);
  186. // finally
  187. UA_list_destroy(&list, freer);
  188. }
  189. END_TEST
  190. START_TEST(addTwiceAndRemoveLastShallYieldListWithOneElement)
  191. {
  192. // given
  193. UA_list_List list;
  194. UA_list_init(&list);
  195. UA_Int32 plv[] = {42,24};
  196. myAddPayloadVectorToFront(&list,plv,sizeof(plv)/sizeof(UA_Int32));
  197. // when
  198. UA_list_Element* e = UA_list_search(&list,comparer,(void*)&plv[1]);
  199. UA_list_removeElement(e,UA_NULL);
  200. visit_count = 0;
  201. UA_list_iteratePayload(&list,visitor);
  202. // then
  203. ck_assert_int_eq(list.size,1);
  204. ck_assert_int_eq(visit_count,1);
  205. // finally
  206. UA_list_destroy(&list, freer);
  207. }
  208. END_TEST
  209. Suite*list_testSuite(void)
  210. {
  211. Suite *s = suite_create("list_test");
  212. TCase *tc_core = tcase_create("Core");
  213. tcase_add_test(tc_core, list_test_basic);
  214. tcase_add_test(tc_core, addElementsShallResultInRespectiveSize);
  215. tcase_add_test(tc_core, findElementShallFind42);
  216. tcase_add_test(tc_core, searchElementShallFind24);
  217. tcase_add_test(tc_core, addAndRemoveShallYieldEmptyList);
  218. tcase_add_test(tc_core, addAndRemoveShallYieldEmptyList2);
  219. tcase_add_test(tc_core, addTwiceAndRemoveFirstShallYieldListWithOneElement);
  220. tcase_add_test(tc_core, addTwiceAndRemoveLastShallYieldListWithOneElement);
  221. suite_add_tcase(s,tc_core);
  222. return s;
  223. }
  224. int main (void)
  225. {
  226. int number_failed = 0;
  227. Suite* s = list_testSuite();
  228. SRunner* sr = srunner_create(s);
  229. srunner_run_all(sr,CK_NORMAL);
  230. number_failed += srunner_ntests_failed(sr);
  231. srunner_free(sr);
  232. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  233. }