From 920a59499b7453588a04155a4b53b28a102aacd2 Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Wed, 20 Sep 2017 15:41:41 +0200 Subject: [PATCH] test: ftl: add tests for find_root() --- source/ftl/map.c | 2 +- source/tests/Makefile | 1 + source/tests/ftl/find_root.c | 153 +++++++++++++++++++++++++++++++++++++++++++ source/tests/main.c | 2 + 4 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 source/tests/ftl/find_root.c diff --git a/source/ftl/map.c b/source/ftl/map.c index 575ff56..18bbb70 100644 --- a/source/ftl/map.c +++ b/source/ftl/map.c @@ -331,7 +331,7 @@ uint32_t find_last_group(struct ftl_map *map, uint32_t block) return low; } -static int find_root(struct ftl_map *map, uint32_t group) +int find_root(struct ftl_map *map, uint32_t group) { struct ftl_page_desc page_desc; uint32_t upage; diff --git a/source/tests/Makefile b/source/tests/Makefile index 5d9868c..7be1e45 100644 --- a/source/tests/Makefile +++ b/source/tests/Makefile @@ -4,6 +4,7 @@ test-obj-y += source/tests/ftl/find_block.o test-obj-y += source/tests/ftl/find_block_div.o test-obj-y += source/tests/ftl/find_last_block.o test-obj-y += source/tests/ftl/find_last_group.o +test-obj-y += source/tests/ftl/find_root.o test-obj-y += source/tests/ftl/next_upage.o test-obj-y += source/tests/ftl/read_page_group.o test-obj-y += source/tests/ftl/read_page_desc.o diff --git a/source/tests/ftl/find_root.c b/source/tests/ftl/find_root.c new file mode 100644 index 0000000..3d5af60 --- /dev/null +++ b/source/tests/ftl/find_root.c @@ -0,0 +1,153 @@ +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +int find_root(struct ftl_map *map, uint32_t group); + +static void test_group0_upage0(void **state) +{ + struct ftl_map map; + struct ftl_page_desc page_desc; + int ret; + + (void)state; + + map.log2_pages_per_group = ilog2(16); + map.log2_groups_per_block = ilog2(1); + map.nblocks = 4; + + expect_value(__wrap_read_page_desc, map, &map); + expect_value(__wrap_read_page_desc, upage, 0); + will_return(__wrap_read_page_desc, &page_desc); + will_return(__wrap_read_page_desc, -1); + + ret = find_root(&map, 0); + + assert_int_equal(ret, -1); +} + +static void test_group0_upage5(void **state) +{ + struct ftl_map map; + struct ftl_page_desc page_desc; + int ret; + + (void)state; + + map.log2_pages_per_group = ilog2(16); + map.log2_groups_per_block = ilog2(1); + map.nblocks = 4; + + expect_value(__wrap_read_page_desc, map, &map); + expect_value(__wrap_read_page_desc, upage, 0); + will_return(__wrap_read_page_desc, &page_desc); + will_return(__wrap_read_page_desc, 0); + + expect_value(__wrap_read_page_desc, map, &map); + expect_value(__wrap_read_page_desc, upage, 1); + will_return(__wrap_read_page_desc, &page_desc); + will_return(__wrap_read_page_desc, 0); + + expect_value(__wrap_read_page_desc, map, &map); + expect_value(__wrap_read_page_desc, upage, 2); + will_return(__wrap_read_page_desc, &page_desc); + will_return(__wrap_read_page_desc, 0); + + expect_value(__wrap_read_page_desc, map, &map); + expect_value(__wrap_read_page_desc, upage, 3); + will_return(__wrap_read_page_desc, &page_desc); + will_return(__wrap_read_page_desc, 0); + + expect_value(__wrap_read_page_desc, map, &map); + expect_value(__wrap_read_page_desc, upage, 4); + will_return(__wrap_read_page_desc, &page_desc); + will_return(__wrap_read_page_desc, 0); + + expect_value(__wrap_read_page_desc, map, &map); + expect_value(__wrap_read_page_desc, upage, 5); + will_return(__wrap_read_page_desc, &page_desc); + will_return(__wrap_read_page_desc, 0); + + expect_value(__wrap_read_page_desc, map, &map); + expect_value(__wrap_read_page_desc, upage, 6); + will_return(__wrap_read_page_desc, &page_desc); + will_return(__wrap_read_page_desc, -1); + + ret = find_root(&map, 0); + + assert_int_equal(ret, 0); + assert_int_equal(map.root, 5); +} + +static void test_group1_upage5(void **state) +{ + struct ftl_map map; + struct ftl_page_desc page_desc; + int ret; + + (void)state; + + map.log2_pages_per_group = ilog2(16); + map.log2_groups_per_block = ilog2(1); + map.nblocks = 4; + + expect_value(__wrap_read_page_desc, map, &map); + expect_value(__wrap_read_page_desc, upage, 16); + will_return(__wrap_read_page_desc, &page_desc); + will_return(__wrap_read_page_desc, 0); + + expect_value(__wrap_read_page_desc, map, &map); + expect_value(__wrap_read_page_desc, upage, 16 + 1); + will_return(__wrap_read_page_desc, &page_desc); + will_return(__wrap_read_page_desc, 0); + + expect_value(__wrap_read_page_desc, map, &map); + expect_value(__wrap_read_page_desc, upage, 16 + 2); + will_return(__wrap_read_page_desc, &page_desc); + will_return(__wrap_read_page_desc, 0); + + expect_value(__wrap_read_page_desc, map, &map); + expect_value(__wrap_read_page_desc, upage, 16 + 3); + will_return(__wrap_read_page_desc, &page_desc); + will_return(__wrap_read_page_desc, 0); + + expect_value(__wrap_read_page_desc, map, &map); + expect_value(__wrap_read_page_desc, upage, 16 + 4); + will_return(__wrap_read_page_desc, &page_desc); + will_return(__wrap_read_page_desc, 0); + + expect_value(__wrap_read_page_desc, map, &map); + expect_value(__wrap_read_page_desc, upage, 16 + 5); + will_return(__wrap_read_page_desc, &page_desc); + will_return(__wrap_read_page_desc, 0); + + expect_value(__wrap_read_page_desc, map, &map); + expect_value(__wrap_read_page_desc, upage, 16 + 6); + will_return(__wrap_read_page_desc, &page_desc); + will_return(__wrap_read_page_desc, -1); + + ret = find_root(&map, 1); + + assert_int_equal(ret, 0); + assert_int_equal(map.root, 16 + 5); +} + +int test_find_root(void) +{ + const struct CMUnitTest tests[] = { + { "find_root: group=0, upage=0 (no root)", test_group0_upage0, NULL, NULL, NULL }, + { "find_root: group=0, upage=5", test_group0_upage5, NULL, NULL, NULL }, + { "find_root: group=1, upage=5", test_group1_upage5, NULL, NULL, NULL }, + }; + + return cmocka_run_group_tests_name("find_root", tests, NULL, NULL); +} diff --git a/source/tests/main.c b/source/tests/main.c index 91f4452..9dc4fa7 100644 --- a/source/tests/main.c +++ b/source/tests/main.c @@ -71,6 +71,7 @@ int test_find_block(void); int test_find_block_div(void); int test_find_last_block(void); int test_find_last_group(void); +int test_find_root(void); int test_next_upage(void); int test_read_page_group(void); int test_read_page_desc(void); @@ -85,6 +86,7 @@ int main(void) count += test_find_block_div(); count += test_find_last_block(); count += test_find_last_group(); + count += test_find_root(); count += test_next_upage(); count += test_read_page_group(); count += test_read_page_desc();