check_indexedList.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. ck_assert_int_eq(UA_indexedList_addValue(UA_NULL, 0, UA_NULL), UA_ERROR);
  50. ck_assert_int_eq(UA_indexedList_addValueToFront(UA_NULL, 0, UA_NULL), UA_ERROR);
  51. ck_assert_int_eq(UA_indexedList_destroy(UA_NULL, UA_NULL), UA_ERROR);
  52. ck_assert_int_eq(UA_indexedList_find(UA_NULL, 0), UA_NULL);
  53. ck_assert_int_eq(UA_indexedList_iterateValues(UA_NULL, UA_NULL), UA_ERROR);
  54. ck_assert_int_eq(UA_indexedList_removeElement(UA_NULL, 0, UA_NULL), UA_ERROR);
  55. UA_indexedList_List list;
  56. UA_indexedList_init(&list);
  57. ck_assert_int_eq(list.size, 0);
  58. UA_Int32* payload = (UA_Int32*)malloc(sizeof(*payload));
  59. *payload = 10;
  60. UA_indexedList_addValue(&list, 1, payload);
  61. payload = (UA_Int32*)malloc(sizeof(*payload));
  62. *payload = 20;
  63. UA_indexedList_addValueToFront(&list, 2, payload);
  64. payload = (UA_Int32*)malloc(sizeof(*payload));
  65. *payload = 30;
  66. UA_indexedList_addValue(&list, 3, payload);
  67. ck_assert_int_eq(list.size, 3);
  68. UA_indexedList_iterateValues(&list, visitor);
  69. ck_assert_int_eq(visit_count, 3);
  70. visit_count = 0;
  71. payload = (UA_Int32*)UA_indexedList_findValue(&list, 2);
  72. if(payload){
  73. ck_assert_int_eq(*payload, 20);
  74. }else{
  75. fail("Element 20 not found");
  76. }
  77. UA_indexedList_removeElement(&list, UA_indexedList_find(&list, 2), freer);
  78. ck_assert_int_eq(free_count, 1);
  79. free_count=0;
  80. ck_assert_int_eq(list.size, 2);
  81. payload = (UA_Int32*)UA_indexedList_findValue(&list, 2);
  82. if(payload){
  83. fail("Previously removed element 20 found");
  84. }
  85. UA_indexedList_iterateValues(&list, visitor);
  86. ck_assert_int_eq(visit_count, 2);
  87. UA_indexedList_destroy(&list, freer);
  88. ck_assert_int_eq(free_count, 2);
  89. ck_assert_int_eq(list.size, 0);
  90. }
  91. END_TEST
  92. Suite*linkedList_testSuite(void)
  93. {
  94. Suite *s = suite_create("linkedList_test");
  95. TCase *tc_core = tcase_create("Core");
  96. tcase_add_test(tc_core, linkedList_test_basic);
  97. tcase_add_test(tc_core, linkedList_test_destroyemptylist);
  98. tcase_add_test(tc_core, linkedList_test_destroysingle);
  99. suite_add_tcase(s,tc_core);
  100. return s;
  101. }
  102. int main (void)
  103. {
  104. int number_failed = 0;
  105. Suite* s = linkedList_testSuite();
  106. SRunner* sr = srunner_create(s);
  107. srunner_run_all(sr,CK_NORMAL);
  108. number_failed += srunner_ntests_failed(sr);
  109. srunner_free(sr);
  110. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  111. }