check_list.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 == NULL){
  18. return FALSE;
  19. }
  20. if(*((UA_Int32*)payload) == 42){
  21. return TRUE;
  22. }
  23. return FALSE;
  24. }
  25. START_TEST(list_test_basic)
  26. {
  27. UA_list_List list;
  28. UA_list_init(&list);
  29. ck_assert_int_eq(list.size, 0);
  30. UA_Int32* payload;
  31. UA_alloc((void**)&payload, sizeof(*payload));
  32. *payload = 42;
  33. UA_list_addPayloadToFront(&list, payload);
  34. UA_alloc((void**)&payload, sizeof(*payload));
  35. *payload = 24;
  36. UA_list_addPayloadToFront(&list, payload);
  37. UA_alloc((void**)&payload, sizeof(*payload));
  38. *payload = 1;
  39. UA_list_addPayloadToBack(&list, payload);
  40. ck_assert_int_eq(list.size, 3);
  41. UA_list_iteratePayload(&list, visitor);
  42. ck_assert_int_eq(visit_count, 3);
  43. UA_list_Element* elem = NULL;
  44. elem = UA_list_find(&list, matcher);
  45. if(elem){
  46. ck_assert_int_eq((*((UA_Int32*)elem->payload)), 42);
  47. UA_list_removeElement(elem, freer);
  48. ck_assert_int_eq(free_count, 1);
  49. free_count = 0; //reset free counter
  50. ck_assert_int_eq(list.size, 2);
  51. }else{
  52. fail("Element 42 not found");
  53. }
  54. UA_list_destroy(&list, freer);
  55. ck_assert_int_eq(free_count, 2);
  56. ck_assert_int_eq(list.size, 0);
  57. }
  58. END_TEST
  59. Suite*list_testSuite(void)
  60. {
  61. Suite *s = suite_create("list_test");
  62. TCase *tc_core = tcase_create("Core");
  63. tcase_add_test(tc_core, list_test_basic);
  64. suite_add_tcase(s,tc_core);
  65. return s;
  66. }
  67. int main (void)
  68. {
  69. int number_failed = 0;
  70. Suite* s = list_testSuite();
  71. SRunner* sr = srunner_create(s);
  72. srunner_run_all(sr,CK_NORMAL);
  73. number_failed += srunner_ntests_failed(sr);
  74. srunner_free(sr);
  75. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  76. }