From c6744d6975e78eae8e8c22e0c6897815333e5603 Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Wed, 20 Sep 2017 14:31:54 +0200 Subject: [PATCH] test: ftl: add tests for find_block() --- source/tests/Makefile | 1 + source/tests/ftl/find_block.c | 170 ++++++++++++++++++++++++++++++++++++++++++ source/tests/main.c | 2 + 3 files changed, 173 insertions(+) create mode 100644 source/tests/ftl/find_block.c diff --git a/source/tests/Makefile b/source/tests/Makefile index e8de216..0991bb2 100644 --- a/source/tests/Makefile +++ b/source/tests/Makefile @@ -1,5 +1,6 @@ test-obj-y += source/tests/main.o test-obj-y += source/tests/ftl/mock.o +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/next_upage.o test-obj-y += source/tests/ftl/read_page_group.o diff --git a/source/tests/ftl/find_block.c b/source/tests/ftl/find_block.c new file mode 100644 index 0000000..7be485c --- /dev/null +++ b/source/tests/ftl/find_block.c @@ -0,0 +1,170 @@ +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +int __real_find_block(struct ftl_map *map, struct ftl_page_group *group, + uint32_t *where, uint32_t block); + +static void test_block0(void **state) +{ + struct ftl_map map; + struct ftl_page_group group, ret_group; + int ret; + uint32_t where; + + (void)state; + + memcpy(group.magic, "FTL", 3); + + map.log2_block_size = ilog2(64 * KIB); + map.log2_page_size = ilog2(1 * KIB); + map.log2_pages_per_group = ilog2(8); + map.nblocks = 64; + + expect_value(__wrap_flash_read, addr, (8 - 1) * 1 * KIB); + expect_value(__wrap_flash_read, len, sizeof group); + will_return(__wrap_flash_read, sizeof group); + will_return(__wrap_flash_read, &group); + + ret = __real_find_block(&map, &ret_group, &where, 0); + assert_int_equal(ret, 0); + assert_int_equal(where, 0); +} + +static void test_block3(void **state) +{ + struct ftl_map map; + struct ftl_page_group group, ret_group; + int ret; + uint32_t where; + + (void)state; + + memcpy(group.magic, "FTL", 3); + + map.log2_block_size = ilog2(64 * KIB); + map.log2_page_size = ilog2(1 * KIB); + map.log2_pages_per_group = ilog2(8); + map.nblocks = 64; + + expect_value(__wrap_flash_read, addr, (8 - 1) * 1 * KIB); + expect_value(__wrap_flash_read, len, sizeof group); + will_return(__wrap_flash_read, 0); + will_return(__wrap_flash_read, NULL); + + expect_value(__wrap_flash_read, addr, (1 * 64 + 8 - 1) * 1 * KIB); + expect_value(__wrap_flash_read, len, sizeof group); + will_return(__wrap_flash_read, 0); + will_return(__wrap_flash_read, NULL); + + expect_value(__wrap_flash_read, addr, (2 * 64 + 8 - 1) * 1 * KIB); + expect_value(__wrap_flash_read, len, sizeof group); + will_return(__wrap_flash_read, 0); + will_return(__wrap_flash_read, NULL); + + expect_value(__wrap_flash_read, addr, (3 * 64 + 8 - 1) * 1 * KIB); + expect_value(__wrap_flash_read, len, sizeof group); + will_return(__wrap_flash_read, sizeof group); + will_return(__wrap_flash_read, &group); + + ret = __real_find_block(&map, &ret_group, &where, 0); + assert_int_equal(ret, 0); + assert_int_equal(where, 3); +} + +static void test_block8_offset5(void **state) +{ + struct ftl_map map; + struct ftl_page_group group, ret_group; + int ret; + uint32_t where; + + (void)state; + + memcpy(group.magic, "FTL", 3); + + map.log2_block_size = ilog2(64 * KIB); + map.log2_page_size = ilog2(1 * KIB); + map.log2_pages_per_group = ilog2(8); + map.nblocks = 64; + + expect_value(__wrap_flash_read, addr, (5 * 64 + 8 - 1) * 1 * KIB); + expect_value(__wrap_flash_read, len, sizeof group); + will_return(__wrap_flash_read, 0); + will_return(__wrap_flash_read, NULL); + + expect_value(__wrap_flash_read, addr, (6 * 64 + 8 - 1) * 1 * KIB); + expect_value(__wrap_flash_read, len, sizeof group); + will_return(__wrap_flash_read, 0); + will_return(__wrap_flash_read, NULL); + + expect_value(__wrap_flash_read, addr, (7 * 64 + 8 - 1) * 1 * KIB); + expect_value(__wrap_flash_read, len, sizeof group); + will_return(__wrap_flash_read, 0); + will_return(__wrap_flash_read, NULL); + + expect_value(__wrap_flash_read, addr, (8 * 64 + 8 - 1) * 1 * KIB); + expect_value(__wrap_flash_read, len, sizeof group); + will_return(__wrap_flash_read, sizeof group); + will_return(__wrap_flash_read, &group); + + ret = __real_find_block(&map, &ret_group, &where, 5); + assert_int_equal(ret, 0); + assert_int_equal(where, 8); +} + +static void test_no_block(void **state) +{ + struct ftl_map map; + struct ftl_page_group group, ret_group; + int ret; + uint32_t where; + + (void)state; + + memcpy(group.magic, "FTL", 3); + + map.log2_block_size = ilog2(64 * KIB); + map.log2_page_size = ilog2(1 * KIB); + map.log2_pages_per_group = ilog2(8); + map.nblocks = 64; + + expect_value(__wrap_flash_read, addr, (61 * 64 + 8 - 1) * 1 * KIB); + expect_value(__wrap_flash_read, len, sizeof group); + will_return(__wrap_flash_read, 0); + will_return(__wrap_flash_read, NULL); + + expect_value(__wrap_flash_read, addr, (62 * 64 + 8 - 1) * 1 * KIB); + expect_value(__wrap_flash_read, len, sizeof group); + will_return(__wrap_flash_read, 0); + will_return(__wrap_flash_read, NULL); + + expect_value(__wrap_flash_read, addr, (63 * 64 + 8 - 1) * 1 * KIB); + expect_value(__wrap_flash_read, len, sizeof group); + will_return(__wrap_flash_read, 0); + will_return(__wrap_flash_read, NULL); + + ret = __real_find_block(&map, &ret_group, &where, 61); + assert_int_equal(ret, -1); +} + +int test_find_block(void) +{ + const struct CMUnitTest tests[] = { + { "find_block: block=0, offset=0", test_block0, NULL, NULL, NULL }, + { "find_block: block=3, offset=0", test_block3, NULL, NULL, NULL }, + { "find_block: block=8, offset=5", test_block8_offset5, NULL, NULL, NULL }, + { "find_block: no block, offset=61", test_no_block, NULL, NULL, NULL }, + }; + + return cmocka_run_group_tests_name("find_block", tests, NULL, NULL); +} diff --git a/source/tests/main.c b/source/tests/main.c index d4ee68a..929a215 100644 --- a/source/tests/main.c +++ b/source/tests/main.c @@ -67,6 +67,7 @@ int __wrap_flash_is_erased(struct flash_dev *dev, uint32_t addr, size_t len) return mock_type(int); } +int test_find_block(void); int test_find_block_div(void); int test_next_upage(void); int test_read_page_group(void); @@ -78,6 +79,7 @@ int main(void) { int count = 0; + count += test_find_block(); count += test_find_block_div(); count += test_next_upage(); count += test_read_page_group();