diff --git a/source/ftl/map.c b/source/ftl/map.c index 18bbb70..18f0731 100644 --- a/source/ftl/map.c +++ b/source/ftl/map.c @@ -353,7 +353,7 @@ int find_root(struct ftl_map *map, uint32_t group) * within the page group, the first user page of the next page group should be * used as that page group should not be in use. */ -static int find_head(struct ftl_map *map) +int find_head(struct ftl_map *map) { size_t log2_pages_per_block = map->log2_pages_per_group + map->log2_groups_per_block; diff --git a/source/tests/Makefile b/source/tests/Makefile index 7be1e45..9e5561c 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_head.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 diff --git a/source/tests/ftl/find_head.c b/source/tests/ftl/find_head.c new file mode 100644 index 0000000..722a4f1 --- /dev/null +++ b/source/tests/ftl/find_head.c @@ -0,0 +1,104 @@ +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +int find_head(struct ftl_map *map); + +static void test_erased(void **state) +{ + struct ftl_map map; + int ret; + + (void)state; + + map.log2_pages_per_group = ilog2(16); + map.log2_groups_per_block = ilog2(1); + map.nblocks = 4; + map.root = 4; + + expect_value(__wrap_flash_is_erased, addr, 5); + expect_value(__wrap_flash_is_erased, len, 1); + will_return(__wrap_flash_is_erased, 1); + + ret = find_head(&map); + + assert_int_equal(ret, 0); + assert_int_equal(map.head, 5); +} + +static void test_end_of_block(void **state) +{ + struct ftl_map map; + int ret; + + (void)state; + + map.log2_pages_per_group = ilog2(16); + map.log2_groups_per_block = ilog2(1); + map.nblocks = 4; + map.root = 4; + + expect_value(__wrap_flash_is_erased, addr, 5); + expect_value(__wrap_flash_is_erased, len, 1); + will_return(__wrap_flash_is_erased, 0); + + expect_value(__wrap_flash_is_erased, addr, 6); + expect_value(__wrap_flash_is_erased, len, 1); + will_return(__wrap_flash_is_erased, 0); + + expect_value(__wrap_flash_is_erased, addr, 7); + expect_value(__wrap_flash_is_erased, len, 1); + will_return(__wrap_flash_is_erased, 0); + + expect_value(__wrap_flash_is_erased, addr, 8); + expect_value(__wrap_flash_is_erased, len, 1); + will_return(__wrap_flash_is_erased, 0); + + expect_value(__wrap_flash_is_erased, addr, 9); + expect_value(__wrap_flash_is_erased, len, 1); + will_return(__wrap_flash_is_erased, 0); + + expect_value(__wrap_flash_is_erased, addr, 10); + expect_value(__wrap_flash_is_erased, len, 1); + will_return(__wrap_flash_is_erased, 0); + + expect_value(__wrap_flash_is_erased, addr, 11); + expect_value(__wrap_flash_is_erased, len, 1); + will_return(__wrap_flash_is_erased, 0); + + expect_value(__wrap_flash_is_erased, addr, 12); + expect_value(__wrap_flash_is_erased, len, 1); + will_return(__wrap_flash_is_erased, 0); + + expect_value(__wrap_flash_is_erased, addr, 13); + expect_value(__wrap_flash_is_erased, len, 1); + will_return(__wrap_flash_is_erased, 0); + + expect_value(__wrap_flash_is_erased, addr, 14); + expect_value(__wrap_flash_is_erased, len, 1); + will_return(__wrap_flash_is_erased, 0); + + ret = find_head(&map); + + assert_int_equal(ret, 0); + assert_int_equal(map.head, 16); +} + +int test_find_head(void) +{ + const struct CMUnitTest tests[] = { + { "find_head: erased", test_erased, NULL, NULL, NULL }, + { "find_head: end of block", test_end_of_block, NULL, NULL, NULL }, + }; + + return cmocka_run_group_tests_name("find_head", tests, NULL, NULL); +} diff --git a/source/tests/main.c b/source/tests/main.c index 9dc4fa7..6c660e7 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_head(void); int test_find_root(void); int test_next_upage(void); int test_read_page_group(void); @@ -86,6 +87,7 @@ int main(void) count += test_find_block_div(); count += test_find_last_block(); count += test_find_last_group(); + count += test_find_head(); count += test_find_root(); count += test_next_upage(); count += test_read_page_group();