From 74aa7fa975ca2968c4f65ed2f87dfd81bec314a6 Mon Sep 17 00:00:00 2001 From: andrej Date: Wed, 2 Nov 2022 17:48:51 +0100 Subject: [PATCH] unit-tests: add remaining interfaces hash state unit tests --- src/interfaces/tests/interfaces_utest.c | 167 ++++++++++++++++++++++-- 1 file changed, 159 insertions(+), 8 deletions(-) diff --git a/src/interfaces/tests/interfaces_utest.c b/src/interfaces/tests/interfaces_utest.c index 93aee787..4496dac9 100644 --- a/src/interfaces/tests/interfaces_utest.c +++ b/src/interfaces/tests/interfaces_utest.c @@ -25,7 +25,14 @@ static int teardown(void **state); /* tests */ /** interface hash table state **/ -static void test_hash_new_correct(void **state); +static void test_state_hash_new_correct(void **state); +static void test_state_hash_element_new_correct(void **state); +static void test_state_hash_add_correct(void **state); +static void test_state_hash_add_incorrect(void **state); +static void test_state_hash_get_correct(void **state); +static void test_state_hash_get_incorrect(void **state); +static void test_state_hash_set_name_correct(void **state); +static void test_state_hash_set_name_incorrect(void **state); /* load */ static void test_correct_load_interface(void **state); @@ -34,7 +41,14 @@ int main(void) { const struct CMUnitTest tests[] = { cmocka_unit_test(test_correct_load_interface), - cmocka_unit_test(test_hash_new_correct), + cmocka_unit_test(test_state_hash_new_correct), + cmocka_unit_test(test_state_hash_element_new_correct), + cmocka_unit_test(test_state_hash_add_correct), + cmocka_unit_test(test_state_hash_add_incorrect), + cmocka_unit_test(test_state_hash_get_correct), + cmocka_unit_test(test_state_hash_get_incorrect), + cmocka_unit_test(test_state_hash_set_name_correct), + cmocka_unit_test(test_state_hash_set_name_incorrect), }; return cmocka_run_group_tests(tests, setup, teardown); @@ -64,23 +78,160 @@ static int teardown(void **state) static void test_correct_load_interface(void **state) { - interfaces_ctx_t *ctx = *state; + (void) state; + + int rc = 0; + + assert_int_equal(rc, 0); +} + +static void test_state_hash_new_correct(void **state) +{ + (void) state; + + interfaces_interface_state_hash_element_t *state_hash; + + state_hash = interfaces_interface_state_hash_new(); + assert_null(state_hash); +} + +static void test_state_hash_element_new_correct(void **state) +{ + (void) state; + + interfaces_interface_state_hash_element_t *new_elem; + + new_elem = interfaces_interface_state_hash_element_new(); + assert_non_null(new_elem); + + interfaces_interface_state_hash_element_free(&new_elem); + assert_null(new_elem); +} + +static void test_state_hash_add_correct(void **state) +{ + (void) state; + + int rc = 0; + + interfaces_interface_state_hash_element_t *state_hash, *new_elem; + + state_hash = interfaces_interface_state_hash_new(); + assert_null(state_hash); + + new_elem = interfaces_interface_state_hash_element_new(); + + interfaces_interface_state_hash_element_set_name(&new_elem, "FOO"); + assert_int_equal(rc, 0); + + rc = interfaces_interface_state_hash_add(&state_hash, new_elem); + assert_int_equal(rc, 0); + + interfaces_interface_state_hash_free(&state_hash); +} + +static void test_state_hash_add_incorrect(void **state) +{ + (void) state; + + int rc = 0; + + interfaces_interface_state_hash_element_t *state_hash, *new_elem; + + state_hash = interfaces_interface_state_hash_new(); + assert_null(state_hash); + + new_elem = interfaces_interface_state_hash_element_new(); + + interfaces_interface_state_hash_element_set_name(&new_elem, "FOO"); + assert_int_equal(rc, 0); + + rc = interfaces_interface_state_hash_add(&state_hash, new_elem); + assert_int_equal(rc, 0); + + /* try adding the same element */ + rc = interfaces_interface_state_hash_add(&state_hash, new_elem); + assert_int_not_equal(rc, 0); + + interfaces_interface_state_hash_free(&state_hash); +} + +static void test_state_hash_get_correct(void **state) +{ + (void) state; + int rc = 0; + const char *name = "FOO"; + + interfaces_interface_state_hash_element_t *state_hash, *new_elem, *found; + state_hash = interfaces_interface_state_hash_new(); + assert_null(state_hash); + + new_elem = interfaces_interface_state_hash_element_new(); + + interfaces_interface_state_hash_element_set_name(&new_elem, name); assert_int_equal(rc, 0); + + rc = interfaces_interface_state_hash_add(&state_hash, new_elem); + assert_int_equal(rc, 0); + + found = interfaces_interface_state_hash_get(state_hash, name); + assert_non_null(found); + + interfaces_interface_state_hash_free(&state_hash); } -static void test_hash_new_correct(void **state) +static void test_state_hash_get_incorrect(void **state) { - interfaces_ctx_t *ctx = *state; + (void) state; + int rc = 0; + const char *names[] = { "FOO", "BAR" }; - interfaces_interface_state_hash_element_t *if_root; + interfaces_interface_state_hash_element_t *state_hash, *new_elem, *found; - if_root = interfaces_interface_state_hash_new(); + state_hash = interfaces_interface_state_hash_new(); + assert_null(state_hash); - assert_null(if_root); + new_elem = interfaces_interface_state_hash_element_new(); + + interfaces_interface_state_hash_element_set_name(&new_elem, names[0]); + assert_int_equal(rc, 0); + rc = interfaces_interface_state_hash_add(&state_hash, new_elem); + assert_int_equal(rc, 0); + + found = interfaces_interface_state_hash_get(state_hash, names[1]); + assert_null(found); + + interfaces_interface_state_hash_free(&state_hash); +} + +static void test_state_hash_set_name_correct(void **state) +{ + (void) state; + + int rc = 0; + + interfaces_interface_state_hash_element_t *new_elem; + + new_elem = interfaces_interface_state_hash_element_new(); + + rc = interfaces_interface_state_hash_element_set_name(&new_elem, "FOO"); assert_int_equal(rc, 0); } +static void test_state_hash_set_name_incorrect(void **state) +{ + (void) state; + + int rc = 0; + + interfaces_interface_state_hash_element_t *new_elem; + + new_elem = interfaces_interface_state_hash_element_new(); + + rc = interfaces_interface_state_hash_element_set_name(&new_elem, NULL); + assert_int_not_equal(rc, 0); +}