check_indexedList.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_basic)
  27. {
  28. UA_indexedList_List list;
  29. UA_indexedList_init(&list);
  30. ck_assert_int_eq(list.size, 0);
  31. UA_Int32* payload = (UA_Int32*)malloc(sizeof(*payload));
  32. *payload = 10;
  33. UA_indexedList_addValue(&list, 1, payload);
  34. payload = (UA_Int32*)malloc(sizeof(*payload));
  35. *payload = 20;
  36. UA_indexedList_addValueToFront(&list, 2, payload);
  37. payload = (UA_Int32*)malloc(sizeof(*payload));
  38. *payload = 30;
  39. UA_indexedList_addValue(&list, 3, payload);
  40. ck_assert_int_eq(list.size, 3);
  41. UA_indexedList_iterateValues(&list, visitor);
  42. ck_assert_int_eq(visit_count, 3);
  43. visit_count = 0;
  44. payload = (UA_Int32*)UA_indexedList_findValue(&list, 2);
  45. if(payload){
  46. ck_assert_int_eq(*payload, 20);
  47. }else{
  48. fail("Element 20 not found");
  49. }
  50. UA_indexedList_removeElement(&list, UA_indexedList_find(&list, 2), freer);
  51. ck_assert_int_eq(free_count, 1);
  52. free_count=0;
  53. ck_assert_int_eq(list.size, 2);
  54. payload = (UA_Int32*)UA_indexedList_findValue(&list, 2);
  55. if(payload){
  56. fail("Previously removed element 20 found");
  57. }
  58. UA_indexedList_iterateValues(&list, visitor);
  59. ck_assert_int_eq(visit_count, 2);
  60. UA_indexedList_destroy(&list, freer);
  61. ck_assert_int_eq(free_count, 2);
  62. ck_assert_int_eq(list.size, 0);
  63. }
  64. END_TEST
  65. Suite*linkedList_testSuite(void)
  66. {
  67. Suite *s = suite_create("linkedList_test");
  68. TCase *tc_core = tcase_create("Core");
  69. tcase_add_test(tc_core, linkedList_test_basic);
  70. suite_add_tcase(s,tc_core);
  71. return s;
  72. }
  73. int main (void)
  74. {
  75. int number_failed = 0;
  76. Suite* s = linkedList_testSuite();
  77. SRunner* sr = srunner_create(s);
  78. srunner_run_all(sr,CK_NORMAL);
  79. number_failed += srunner_ntests_failed(sr);
  80. srunner_free(sr);
  81. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  82. }