check_indexedList.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <stdlib.h> // EXIT_SUCCESS
  2. #include "util/ua_indexedList.h"
  3. #include "util/ua_util.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 == NULL){
  19. return FALSE;
  20. }
  21. if(*((UA_Int32*)payload) == 42){
  22. return TRUE;
  23. }
  24. return FALSE;
  25. }
  26. START_TEST(linkedList_test_destroyemptylist)
  27. {
  28. UA_indexedList_List list;
  29. UA_indexedList_init(&list);
  30. ck_assert_int_eq(list.size, 0);
  31. UA_indexedList_destroy(&list, UA_indexedList_defaultFreer);
  32. ck_assert_int_eq(list.size, 0);
  33. }
  34. END_TEST
  35. START_TEST(linkedList_test_destroysingle)
  36. {
  37. UA_indexedList_List list;
  38. UA_indexedList_init(&list);
  39. ck_assert_int_eq(list.size, 0);
  40. UA_Int32* payload = (UA_Int32*)malloc(sizeof(*payload));
  41. *payload = 10;
  42. UA_indexedList_addValue(&list, 1, payload);
  43. UA_indexedList_destroy(&list, UA_indexedList_defaultFreer);
  44. ck_assert_int_eq(list.size, 0);
  45. }
  46. END_TEST
  47. START_TEST(linkedList_test_basic)
  48. {
  49. UA_indexedList_List list;
  50. UA_indexedList_init(&list);
  51. ck_assert_int_eq(list.size, 0);
  52. UA_Int32* payload = (UA_Int32*)malloc(sizeof(*payload));
  53. *payload = 10;
  54. UA_indexedList_addValue(&list, 1, payload);
  55. payload = (UA_Int32*)malloc(sizeof(*payload));
  56. *payload = 20;
  57. UA_indexedList_addValueToFront(&list, 2, payload);
  58. payload = (UA_Int32*)malloc(sizeof(*payload));
  59. *payload = 30;
  60. UA_indexedList_addValue(&list, 3, payload);
  61. ck_assert_int_eq(list.size, 3);
  62. UA_indexedList_iterateValues(&list, visitor);
  63. ck_assert_int_eq(visit_count, 3);
  64. visit_count = 0;
  65. payload = (UA_Int32*)UA_indexedList_findValue(&list, 2);
  66. if(payload){
  67. ck_assert_int_eq(*payload, 20);
  68. }else{
  69. fail("Element 20 not found");
  70. }
  71. UA_indexedList_removeElement(&list, UA_indexedList_find(&list, 2), freer);
  72. ck_assert_int_eq(free_count, 1);
  73. free_count=0;
  74. ck_assert_int_eq(list.size, 2);
  75. payload = (UA_Int32*)UA_indexedList_findValue(&list, 2);
  76. if(payload){
  77. fail("Previously removed element 20 found");
  78. }
  79. UA_indexedList_iterateValues(&list, visitor);
  80. ck_assert_int_eq(visit_count, 2);
  81. UA_indexedList_destroy(&list, freer);
  82. ck_assert_int_eq(free_count, 2);
  83. ck_assert_int_eq(list.size, 0);
  84. }
  85. END_TEST
  86. Suite*linkedList_testSuite(void)
  87. {
  88. Suite *s = suite_create("linkedList_test");
  89. TCase *tc_core = tcase_create("Core");
  90. tcase_add_test(tc_core, linkedList_test_basic);
  91. tcase_add_test(tc_core, linkedList_test_destroyemptylist);
  92. tcase_add_test(tc_core, linkedList_test_destroysingle);
  93. suite_add_tcase(s,tc_core);
  94. return s;
  95. }
  96. int main (void)
  97. {
  98. int number_failed = 0;
  99. Suite* s = linkedList_testSuite();
  100. SRunner* sr = srunner_create(s);
  101. srunner_run_all(sr,CK_NORMAL);
  102. number_failed += srunner_ntests_failed(sr);
  103. srunner_free(sr);
  104. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  105. }