check_list.c 5.4 KB

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