check_list.c 5.4 KB

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