check_list.c 7.3 KB

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