From f3eb5df644191fa81b81a3bcd4de0a3ce125775d Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Thu, 21 Sep 2017 17:27:15 +0200 Subject: [PATCH] test: ftl: add tests for ftl_is_mapped() --- source/ftl/map.c | 5 ++++ source/tests/Makefile | 3 ++ source/tests/ftl/ftl_is_mapped.c | 62 ++++++++++++++++++++++++++++++++++++++++ source/tests/ftl/main.c | 4 +++ source/tests/ftl/mock.c | 24 ++++++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 source/tests/ftl/ftl_is_mapped.c diff --git a/source/ftl/map.c b/source/ftl/map.c index 18f0731..6575fad 100644 --- a/source/ftl/map.c +++ b/source/ftl/map.c @@ -447,6 +447,11 @@ int ftl_resume_map(struct ftl_map *map) * depth until we have either found that there is no further subtree to * traverse or until we have found the actual user page. */ +#ifdef trace_path +#undef trace_path +#define trace_path __real_trace_path +#endif + int trace_path(struct ftl_map *map, struct ftl_page_desc *new_page_desc, uint32_t *page, uint32_t va) { diff --git a/source/tests/Makefile b/source/tests/Makefile index 506ba4f..8c98a10 100644 --- a/source/tests/Makefile +++ b/source/tests/Makefile @@ -16,6 +16,8 @@ test-obj-y += source/tests/ftl/read_page_desc.o test-obj-y += source/tests/ftl/write_page_desc.o test-obj-y += source/tests/ftl/write_upage.o +test-obj-y += source/tests/ftl/ftl_is_mapped.o + TEST_CFLAGS += -Dflash_read=__wrap_flash_read TEST_CFLAGS += -Dflash_write=__wrap_flash_write TEST_CFLAGS += -Dflash_is_erased=__wrap_flash_is_erased @@ -25,3 +27,4 @@ TEST_CFLAGS += -Dfind_block=__wrap_find_block TEST_CFLAGS += -Dprepare_head=__wrap_prepare_head TEST_CFLAGS += -Dread_page_desc=__wrap_read_page_desc TEST_CFLAGS += -Dwrite_page_desc=__wrap_write_page_desc +TEST_CFLAGS += -Dtrace_path=__wrap_trace_path diff --git a/source/tests/ftl/ftl_is_mapped.c b/source/tests/ftl/ftl_is_mapped.c new file mode 100644 index 0000000..42680b6 --- /dev/null +++ b/source/tests/ftl/ftl_is_mapped.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +static void test_unmapped(void **state) +{ + struct ftl_map map; + int ret; + + (void)state; + + expect_value(__wrap_trace_path, map, &map); + expect_value(__wrap_trace_path, new_page_desc, NULL); + expect_value(__wrap_trace_path, page, NULL); + expect_value(__wrap_trace_path, va, 42); + will_return(__wrap_trace_path, NULL); + will_return(__wrap_trace_path, 0); + will_return(__wrap_trace_path, -1); + + ret = ftl_is_mapped(&map, 42); + + assert_int_equal(ret, 0); +} + +static void test_mapped(void **state) +{ + struct ftl_map map; + int ret; + + (void)state; + + expect_value(__wrap_trace_path, map, &map); + expect_value(__wrap_trace_path, new_page_desc, NULL); + expect_value(__wrap_trace_path, page, NULL); + expect_value(__wrap_trace_path, va, 42); + will_return(__wrap_trace_path, NULL); + will_return(__wrap_trace_path, 0); + will_return(__wrap_trace_path, 0); + + ret = ftl_is_mapped(&map, 42); + + assert_int_equal(ret, 1); +} + +int test_ftl_is_mapped(void) +{ + const struct CMUnitTest tests[] = { + { "ftl_is_mapped: unmapped", test_unmapped, NULL, NULL, NULL }, + { "ftl_is_mapped: mapped", test_mapped, NULL, NULL, NULL }, + }; + + return cmocka_run_group_tests_name("ftl_is_unmapped", tests, NULL, NULL); +} diff --git a/source/tests/ftl/main.c b/source/tests/ftl/main.c index ea4f031..16d41da 100644 --- a/source/tests/ftl/main.c +++ b/source/tests/ftl/main.c @@ -23,6 +23,8 @@ int test_read_page_desc(void); int test_write_page_desc(void); int test_write_upage(void); +int test_ftl_is_mapped(void); + int test_ftl(void) { int count = 0; @@ -39,5 +41,7 @@ int test_ftl(void) count += test_write_page_desc(); count += test_write_upage(); + count += test_ftl_is_mapped(); + return count; } diff --git a/source/tests/ftl/mock.c b/source/tests/ftl/mock.c index 80f3ba2..c526e56 100644 --- a/source/tests/ftl/mock.c +++ b/source/tests/ftl/mock.c @@ -61,3 +61,27 @@ int __wrap_write_page_desc(struct ftl_map *map, return mock_type(int); } + +int __wrap_trace_path(struct ftl_map *map, struct ftl_page_desc *new_page_desc, + uint32_t *page, uint32_t va) +{ + struct ftl_page_desc *ret_page_desc; + uint32_t ret_page; + + check_expected_ptr(map); + check_expected_ptr(new_page_desc); + check_expected_ptr(page); + check_expected(va); + + ret_page_desc = mock_type(struct ftl_page_desc *); + + if (new_page_desc) + memcpy(new_page_desc, ret_page_desc, sizeof *new_page_desc); + + ret_page = mock_type(uint32_t); + + if (page) + *page = ret_page; + + return mock_type(int); +}