1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- #include <stdlib.h> // EXIT_SUCCESS
- #include "UA_list.h"
- #include "check.h"
- /* global test counters */
- UA_Int32 visit_count = 0;
- UA_Int32 free_count = 0;
- void visitor(void* payload){
- visit_count++;
- }
- void freer(void* payload){
- if(payload){
- free_count++;
- UA_free(payload);
- }
- }
- _Bool matcher(void* payload){
- if(payload == NULL){
- return FALSE;
- }
- if(*((UA_Int32*)payload) == 42){
- return TRUE;
- }
- return FALSE;
- }
- START_TEST(list_test_basic)
- {
- UA_list_List list;
- UA_list_init(&list);
- ck_assert_int_eq(list.size, 0);
- UA_Int32* payload;
- UA_alloc((void**)&payload, sizeof(*payload));
- *payload = 42;
- UA_list_addPayloadToFront(&list, payload);
- UA_alloc((void**)&payload, sizeof(*payload));
- *payload = 24;
- UA_list_addPayloadToFront(&list, payload);
- UA_alloc((void**)&payload, sizeof(*payload));
- *payload = 1;
- UA_list_addPayloadToBack(&list, payload);
- ck_assert_int_eq(list.size, 3);
- UA_list_iteratePayload(&list, visitor);
- ck_assert_int_eq(visit_count, 3);
- UA_list_Element* elem = NULL;
- elem = UA_list_find(&list, matcher);
- if(elem){
- ck_assert_int_eq((*((UA_Int32*)elem->payload)), 42);
- UA_list_removeElement(elem, freer);
- ck_assert_int_eq(free_count, 1);
- free_count = 0; //reset free counter
- ck_assert_int_eq(list.size, 2);
- }else{
- fail("Element 42 not found");
- }
- UA_list_destroy(&list, freer);
- ck_assert_int_eq(free_count, 2);
- ck_assert_int_eq(list.size, 0);
- }
- END_TEST
- Suite*list_testSuite(void)
- {
- Suite *s = suite_create("list_test");
- TCase *tc_core = tcase_create("Core");
- tcase_add_test(tc_core, list_test_basic);
- suite_add_tcase(s,tc_core);
- return s;
- }
- int main (void)
- {
- int number_failed = 0;
- Suite* s = list_testSuite();
- SRunner* sr = srunner_create(s);
- srunner_run_all(sr,CK_NORMAL);
- number_failed += srunner_ntests_failed(sr);
- srunner_free(sr);
- return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
- }
|