Skip to content

Commit

Permalink
unit-tests: add remaining interfaces hash state unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrej committed Nov 2, 2022
1 parent d55c8ca commit 74aa7fa
Showing 1 changed file with 159 additions and 8 deletions.
167 changes: 159 additions & 8 deletions src/interfaces/tests/interfaces_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}

0 comments on commit 74aa7fa

Please sign in to comment.