Skip to content

Commit

Permalink
More tests for memblklist
Browse files Browse the repository at this point in the history
  • Loading branch information
dargueta committed May 26, 2024
1 parent 76f3a53 commit 5911d5d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
15 changes: 10 additions & 5 deletions tests/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ import_test(memblklist, length_zero);
import_test(memblklist, add_one);
import_test(memblklist, add_many_odd);
import_test(memblklist, add_many_even);
import_test(memblklist, relindex__empty);
import_test(memblklist, relindex__basic);
import_test(reader, buffer_init_static);
import_test(reader, buffer_init_malloc);
import_test(reader, buffer_getc);
Expand All @@ -191,11 +193,14 @@ static MunitTest array_tests[] = {
define_plain_test_case(array, add_more_than_one_block),
SUITE_END_SENTINEL};

static MunitTest memblklist_tests[] = {define_plain_test_case(memblklist, length_zero),
define_plain_test_case(memblklist, add_one),
define_plain_test_case(memblklist, add_many_odd),
define_plain_test_case(memblklist, add_many_even),
SUITE_END_SENTINEL};
static MunitTest memblklist_tests[] = {
define_plain_test_case(memblklist, length_zero),
define_plain_test_case(memblklist, add_one),
define_plain_test_case(memblklist, add_many_odd),
define_plain_test_case(memblklist, add_many_even),
define_plain_test_case(memblklist, relindex__empty),
define_plain_test_case(memblklist, relindex__basic),
SUITE_END_SENTINEL};

static MunitTest reader_tests[] = {
define_plain_test_case(reader, buffer_init_static),
Expand Down
49 changes: 49 additions & 0 deletions tests/memblklist.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,55 @@ MunitResult test_memblklist__add_many_even(const MunitParameter params[], void *
return MUNIT_OK;
}

MunitResult test_memblklist__relindex__empty(const MunitParameter params[], void *udata)
{
(void)params;

MDLState *mds = (MDLState *)udata;
MDLMemBlkList list;

mdl_memblklist_init(mds, &list, 64);
munit_assert_false(mdl_memblklist_isrelindexvalid(&list, 0));
munit_assert_size(mdl_memblklist_absindex(&list, 0), ==, MDL_INVALID_INDEX);

int error = mdl_memblklist_destroy(&list);
munit_assert_int(error, ==, MDL_OK);
return MUNIT_OK;
}

MunitResult test_memblklist__relindex__basic(const MunitParameter params[], void *udata)
{
(void)params;

MDLState *mds = (MDLState *)udata;
MDLMemBlkList list;

mdl_memblklist_init(mds, &list, 32);
for (long i = 0; i < 44; i++)
{
(void)mdl_memblklist_push(&list);

munit_assert_size(mdl_memblklist_length(&list), ==, (size_t)i + 1);
munit_assert_false(mdl_memblklist_isrelindexvalid(&list, i + 1));
munit_assert_false(mdl_memblklist_isrelindexvalid(&list, -i - 2));
munit_assert_size(mdl_memblklist_absindex(&list, i + 1), ==, MDL_INVALID_INDEX);
munit_assert_size(mdl_memblklist_absindex(&list, -i - 2), ==, MDL_INVALID_INDEX);

for (long j = 0; j <= i; j++)
{
munit_assert_true(mdl_memblklist_isrelindexvalid(&list, j));
munit_assert_true(mdl_memblklist_isrelindexvalid(&list, -j - 1));
munit_assert_size((size_t)j, ==, mdl_memblklist_absindex(&list, j));
munit_assert_size((size_t)(i - j), ==,
mdl_memblklist_absindex(&list, -j - 1));
}
}

int error = mdl_memblklist_destroy(&list);
munit_assert_int(error, ==, MDL_OK);
return MUNIT_OK;
}

//////////////////////////////////////////////////////////////////////////////////////////
// Helpers

Expand Down

0 comments on commit 5911d5d

Please sign in to comment.