From d778e5a325ad70f497d7f0092257d5b50326bf47 Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Wed, 20 Sep 2017 14:33:54 +0200 Subject: [PATCH] test: add tests for find_last_block() --- source/tests/Makefile | 1 + source/tests/ftl/find_last_block.c | 291 +++++++++++++++++++++++++++++++++++++ source/tests/main.c | 2 + 3 files changed, 294 insertions(+) create mode 100644 source/tests/ftl/find_last_block.c diff --git a/source/tests/Makefile b/source/tests/Makefile index 0991bb2..b7c02b5 100644 --- a/source/tests/Makefile +++ b/source/tests/Makefile @@ -2,6 +2,7 @@ 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/find_last_block.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_last_block.c b/source/tests/ftl/find_last_block.c new file mode 100644 index 0000000..4f9e192 --- /dev/null +++ b/source/tests/ftl/find_last_block.c @@ -0,0 +1,291 @@ +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +uint32_t find_last_block(struct ftl_map *map, uint32_t first); + +static void test_middle(void **state) +{ + struct ftl_map map; + struct ftl_page_group group; + uint32_t ret; + + (void)state; + + memcpy(group.magic, "FTL", 3); + group.epoch = 0; + + map.nblocks = 64; + map.epoch = 0; + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 31); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 31); + will_return(__wrap_find_block, 0); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 32); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 0); + will_return(__wrap_find_block, -1); + + ret = find_last_block(&map, 0); + assert_int_equal(ret, 31); +} + +static void test_first(void **state) +{ + struct ftl_map map; + struct ftl_page_group group; + uint32_t ret; + + (void)state; + + memcpy(group.magic, "FTL", 3); + group.epoch = 0; + + map.nblocks = 64; + map.epoch = 0; + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 31); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 0); + will_return(__wrap_find_block, -1); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 15); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 0); + will_return(__wrap_find_block, -1); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 7); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 0); + will_return(__wrap_find_block, -1); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 3); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 0); + will_return(__wrap_find_block, -1); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 1); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 0); + will_return(__wrap_find_block, -1); + + ret = find_last_block(&map, 0); + assert_int_equal(ret, 0); +} + +static void test_last(void **state) +{ + struct ftl_map map; + struct ftl_page_group group; + uint32_t ret; + + (void)state; + + memcpy(group.magic, "FTL", 3); + group.epoch = 0; + + map.nblocks = 64; + map.epoch = 0; + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 31); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 31); + will_return(__wrap_find_block, 0); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 32); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 32); + will_return(__wrap_find_block, 0); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 47); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 47); + will_return(__wrap_find_block, 0); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 48); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 48); + will_return(__wrap_find_block, 0); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 55); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 55); + will_return(__wrap_find_block, 0); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 56); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 56); + will_return(__wrap_find_block, 0); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 59); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 59); + will_return(__wrap_find_block, 0); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 60); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 60); + will_return(__wrap_find_block, 0); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 61); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 61); + will_return(__wrap_find_block, 0); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 62); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 62); + will_return(__wrap_find_block, 0); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 62); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 62); + will_return(__wrap_find_block, 0); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 63); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 63); + will_return(__wrap_find_block, 0); + + ret = find_last_block(&map, 0); + assert_int_equal(ret, 63); +} + +static void test_left_right(void **state) +{ + struct ftl_map map; + struct ftl_page_group group; + uint32_t ret; + + (void)state; + + memcpy(group.magic, "FTL", 3); + group.epoch = 0; + + map.nblocks = 64; + map.epoch = 0; + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 31); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 31); + will_return(__wrap_find_block, -1); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 15); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 15); + will_return(__wrap_find_block, 0); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 16); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 16); + will_return(__wrap_find_block, 0); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 23); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 23); + will_return(__wrap_find_block, 0); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 24); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 24); + will_return(__wrap_find_block, -1); + + ret = find_last_block(&map, 0); + assert_int_equal(ret, 23); +} + +static void test_right_left(void **state) +{ + struct ftl_map map; + struct ftl_page_group group; + uint32_t ret; + + (void)state; + + memcpy(group.magic, "FTL", 3); + group.epoch = 0; + + map.nblocks = 64; + map.epoch = 0; + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 31); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 31); + will_return(__wrap_find_block, 0); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 32); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 32); + will_return(__wrap_find_block, 0); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 47); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 47); + will_return(__wrap_find_block, -1); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 39); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 39); + will_return(__wrap_find_block, 0); + + expect_value(__wrap_find_block, map, &map); + expect_value(__wrap_find_block, block, 40); + will_return(__wrap_find_block, &group); + will_return(__wrap_find_block, 40); + will_return(__wrap_find_block, -1); + + ret = find_last_block(&map, 0); + assert_int_equal(ret, 39); +} + +int test_find_last_block(void) +{ + const struct CMUnitTest tests[] = { + { "find_last_block: middle", test_middle, NULL, NULL, NULL }, + { "find_last_block: first", test_first, NULL, NULL, NULL }, + { "find_last_block: last", test_last, NULL, NULL, NULL }, + { "find_last_block: left right", test_left_right, NULL, NULL, NULL }, + { "find_last_block: right left", test_right_left, NULL, NULL, NULL }, + }; + + return cmocka_run_group_tests_name("find_last_block", tests, NULL, NULL); +} diff --git a/source/tests/main.c b/source/tests/main.c index 929a215..b7ad26b 100644 --- a/source/tests/main.c +++ b/source/tests/main.c @@ -69,6 +69,7 @@ int __wrap_flash_is_erased(struct flash_dev *dev, uint32_t addr, size_t len) int test_find_block(void); int test_find_block_div(void); +int test_find_last_block(void); int test_next_upage(void); int test_read_page_group(void); int test_read_page_desc(void); @@ -81,6 +82,7 @@ int main(void) count += test_find_block(); count += test_find_block_div(); + count += test_find_last_block(); count += test_next_upage(); count += test_read_page_group(); count += test_read_page_desc();