123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #include <stdlib.h> // EXIT_SUCCESS
- #include "util/ua_indexedList.h"
- #include "util/ua_util.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(linkedList_test_basic)
- {
- UA_indexedList_List list;
- UA_indexedList_init(&list);
- ck_assert_int_eq(list.size, 0);
- UA_Int32* payload = (UA_Int32*)malloc(sizeof(*payload));
- *payload = 10;
- UA_indexedList_addValue(&list, 1, payload);
- payload = (UA_Int32*)malloc(sizeof(*payload));
- *payload = 20;
- UA_indexedList_addValueToFront(&list, 2, payload);
- payload = (UA_Int32*)malloc(sizeof(*payload));
- *payload = 30;
- UA_indexedList_addValue(&list, 3, payload);
- ck_assert_int_eq(list.size, 3);
- UA_indexedList_iterateValues(&list, visitor);
- ck_assert_int_eq(visit_count, 3);
- visit_count = 0;
- payload = (UA_Int32*)UA_indexedList_findValue(&list, 2);
- if(payload){
- ck_assert_int_eq(*payload, 20);
- }else{
- fail("Element 20 not found");
- }
- UA_indexedList_removeElement(&list, UA_indexedList_find(&list, 2), freer);
- ck_assert_int_eq(free_count, 1);
- free_count=0;
- ck_assert_int_eq(list.size, 2);
- payload = (UA_Int32*)UA_indexedList_findValue(&list, 2);
- if(payload){
- fail("Previously removed element 20 found");
- }
- UA_indexedList_iterateValues(&list, visitor);
- ck_assert_int_eq(visit_count, 2);
- UA_indexedList_destroy(&list, freer);
- ck_assert_int_eq(free_count, 2);
- ck_assert_int_eq(list.size, 0);
- }
- END_TEST
- Suite*linkedList_testSuite(void)
- {
- Suite *s = suite_create("linkedList_test");
- TCase *tc_core = tcase_create("Core");
- tcase_add_test(tc_core, linkedList_test_basic);
- suite_add_tcase(s,tc_core);
- return s;
- }
- int main (void)
- {
- int number_failed = 0;
- Suite* s = linkedList_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;
- }
|