From ed49402e3b31c81a3c8c82716cf802ac5c25d027 Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Tue, 19 Sep 2017 16:18:46 +0200 Subject: [PATCH] test: ftl: add tests for read_page_group() --- source/tests/Makefile | 1 + source/tests/ftl/read_page_group.c | 97 ++++++++++++++++++++++++++++++++++++++ source/tests/main.c | 2 + 3 files changed, 100 insertions(+) create mode 100644 source/tests/ftl/read_page_group.c diff --git a/source/tests/Makefile b/source/tests/Makefile index 4c90592..a9c90d3 100644 --- a/source/tests/Makefile +++ b/source/tests/Makefile @@ -1,6 +1,7 @@ test-obj-y += source/tests/main.o test-obj-y += source/tests/ftl/find_block_div.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 test-obj-y += source/tests/ftl/write_page_desc.o diff --git a/source/tests/ftl/read_page_group.c b/source/tests/ftl/read_page_group.c new file mode 100644 index 0000000..e51fceb --- /dev/null +++ b/source/tests/ftl/read_page_group.c @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +size_t __wrap_flash_read(struct flash_dev *dev, uint32_t addr, + void *data, size_t len) __attribute__((used)); +int read_page_group(struct ftl_map *map, + struct ftl_page_group *group, uint32_t upage); + +static void test_magic(void **state) +{ + struct ftl_map map; + struct ftl_page_group group, ret_group; + size_t ret; + + (void)state; + + memcpy(&group.magic, "FtL", 3); + + expect_value(__wrap_flash_read, addr, (16 - 1) * 4 * KIB); + expect_value(__wrap_flash_read, len, sizeof group); + will_return(__wrap_flash_read, sizeof group); + will_return(__wrap_flash_read, &group); + + map.log2_pages_per_group = ilog2(16); + map.log2_page_size = ilog2(4 * KIB); + + ret = read_page_group(&map, &ret_group, 0); + + assert_int_equal(ret, -1); +} + +static void test_group0(void **state) +{ + struct ftl_map map; + struct ftl_page_group group, ret_group; + size_t ret; + + (void)state; + + memcpy(&group.magic, "FTL", 3); + + expect_value(__wrap_flash_read, addr, (16 - 1) * 4 * KIB); + expect_value(__wrap_flash_read, len, sizeof group); + will_return(__wrap_flash_read, sizeof group); + will_return(__wrap_flash_read, &group); + + map.log2_pages_per_group = ilog2(16); + map.log2_page_size = ilog2(4 * KIB); + + ret = read_page_group(&map, &ret_group, 0); + + assert_int_equal(ret, 0); +} + +static void test_group1(void **state) +{ + struct ftl_map map; + struct ftl_page_group group, ret_group; + size_t ret; + + (void)state; + + memcpy(&group.magic, "FTL", 3); + + expect_value(__wrap_flash_read, addr, (2 * 16 - 1) * 4 * KIB); + expect_value(__wrap_flash_read, len, sizeof group); + will_return(__wrap_flash_read, sizeof group); + will_return(__wrap_flash_read, &group); + + map.log2_pages_per_group = ilog2(16); + map.log2_page_size = ilog2(4 * KIB); + + ret = read_page_group(&map, &ret_group, 1); + + assert_int_equal(ret, 0); +} + +int test_read_page_group(void) +{ + const struct CMUnitTest tests[] = { + { "read_page_group: invalid magic", test_magic, NULL, NULL, NULL }, + { "read_page_group: group=0", test_group0, NULL, NULL, NULL }, + { "read_page_group: group=1", test_group1, NULL, NULL, NULL }, + }; + + return cmocka_run_group_tests_name("read_page_group", tests, NULL, NULL); +} diff --git a/source/tests/main.c b/source/tests/main.c index fa8b1cf..7c577c1 100644 --- a/source/tests/main.c +++ b/source/tests/main.c @@ -74,6 +74,7 @@ int __wrap_flash_is_erased(struct flash_dev *dev, uint32_t addr, size_t len) int test_find_block_div(void); int test_next_upage(void); +int test_read_page_group(void); int test_read_page_desc(void); int test_write_page_desc(void); @@ -83,6 +84,7 @@ int main(void) count += test_find_block_div(); count += test_next_upage(); + count += test_read_page_group(); count += test_read_page_desc(); count += test_write_page_desc();