123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- #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_destroyemptylist)
- {
- UA_indexedList_List list;
- UA_indexedList_init(&list);
- ck_assert_int_eq(list.size, 0);
- UA_indexedList_destroy(&list, UA_indexedList_defaultFreer);
- ck_assert_int_eq(list.size, 0);
- }
- END_TEST
- START_TEST(linkedList_test_destroysingle)
- {
- 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);
- UA_indexedList_destroy(&list, UA_indexedList_defaultFreer);
- ck_assert_int_eq(list.size, 0);
- }
- END_TEST
-
- START_TEST(linkedList_test_basic)
- {
- ck_assert_int_eq(UA_indexedList_addValue(UA_NULL, 0, UA_NULL), UA_ERROR);
- ck_assert_int_eq(UA_indexedList_addValueToFront(UA_NULL, 0, UA_NULL), UA_ERROR);
- ck_assert_int_eq(UA_indexedList_destroy(UA_NULL, UA_NULL), UA_ERROR);
- ck_assert_int_eq(UA_indexedList_find(UA_NULL, 0), UA_NULL);
- ck_assert_int_eq(UA_indexedList_iterateValues(UA_NULL, UA_NULL), UA_ERROR);
- ck_assert_int_eq(UA_indexedList_removeElement(UA_NULL, 0, UA_NULL), UA_ERROR);
- 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);
- tcase_add_test(tc_core, linkedList_test_destroyemptylist);
- tcase_add_test(tc_core, linkedList_test_destroysingle);
- 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;
- }
|