check_indexedList.c 2.1 KB

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