From 6dcfd0d3226fbcfdef91c0b9a0792171eb051902 Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Wed, 20 Sep 2017 14:34:13 +0200 Subject: [PATCH] test: add tests for find_last_group() --- source/tests/Makefile | 1 + source/tests/ftl/find_last_group.c | 156 +++++++++++++++++++++++++++++++++++++ source/tests/main.c | 2 + 3 files changed, 159 insertions(+) create mode 100644 source/tests/ftl/find_last_group.c diff --git a/source/tests/Makefile b/source/tests/Makefile index b7c02b5..f59466d 100644 --- a/source/tests/Makefile +++ b/source/tests/Makefile @@ -3,6 +3,7 @@ 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/find_last_group.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_group.c b/source/tests/ftl/find_last_group.c new file mode 100644 index 0000000..022faa4 --- /dev/null +++ b/source/tests/ftl/find_last_group.c @@ -0,0 +1,156 @@ +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +uint32_t find_last_group(struct ftl_map *map, uint32_t block); + +static void test_middle(void **state) +{ + struct ftl_map map; + uint32_t ret; + + (void)state; + + map.log2_groups_per_block = ilog2(8); + + expect_value(__wrap_is_group_erased, map, &map); + expect_value(__wrap_is_group_erased, group, 32 + 3); + will_return(__wrap_is_group_erased, 0); + + expect_value(__wrap_is_group_erased, map, &map); + expect_value(__wrap_is_group_erased, group, 32 + 4); + will_return(__wrap_is_group_erased, 1); + + ret = find_last_group(&map, 4); + assert_int_equal(ret, 32 + 3); +} + +static void test_first(void **state) +{ + struct ftl_map map; + uint32_t ret; + + (void)state; + + map.log2_groups_per_block = ilog2(8); + + expect_value(__wrap_is_group_erased, map, &map); + expect_value(__wrap_is_group_erased, group, 32 + 3); + will_return(__wrap_is_group_erased, 1); + + expect_value(__wrap_is_group_erased, map, &map); + expect_value(__wrap_is_group_erased, group, 32 + 1); + will_return(__wrap_is_group_erased, 1); + + ret = find_last_group(&map, 4); + assert_int_equal(ret, 32); +} + +static void test_last(void **state) +{ + struct ftl_map map; + uint32_t ret; + + (void)state; + + map.log2_groups_per_block = ilog2(8); + + expect_value(__wrap_is_group_erased, map, &map); + expect_value(__wrap_is_group_erased, group, 32 + 3); + will_return(__wrap_is_group_erased, 0); + + expect_value(__wrap_is_group_erased, map, &map); + expect_value(__wrap_is_group_erased, group, 32 + 4); + will_return(__wrap_is_group_erased, 0); + + expect_value(__wrap_is_group_erased, map, &map); + expect_value(__wrap_is_group_erased, group, 32 + 5); + will_return(__wrap_is_group_erased, 0); + + expect_value(__wrap_is_group_erased, map, &map); + expect_value(__wrap_is_group_erased, group, 32 + 6); + will_return(__wrap_is_group_erased, 0); + + expect_value(__wrap_is_group_erased, map, &map); + expect_value(__wrap_is_group_erased, group, 32 + 6); + will_return(__wrap_is_group_erased, 0); + + expect_value(__wrap_is_group_erased, map, &map); + expect_value(__wrap_is_group_erased, group, 32 + 7); + will_return(__wrap_is_group_erased, 0); + + ret = find_last_group(&map, 4); + assert_int_equal(ret, 32 + 7); +} + +static void test_left_right(void **state) +{ + struct ftl_map map; + uint32_t ret; + + (void)state; + + map.log2_groups_per_block = ilog2(8); + + expect_value(__wrap_is_group_erased, map, &map); + expect_value(__wrap_is_group_erased, group, 32 + 3); + will_return(__wrap_is_group_erased, 1); + + expect_value(__wrap_is_group_erased, map, &map); + expect_value(__wrap_is_group_erased, group, 32 + 1); + will_return(__wrap_is_group_erased, 0); + + expect_value(__wrap_is_group_erased, map, &map); + expect_value(__wrap_is_group_erased, group, 32 + 2); + will_return(__wrap_is_group_erased, 0); + + ret = find_last_group(&map, 4); + assert_int_equal(ret, 32 + 2); +} + +static void test_right_left(void **state) +{ + struct ftl_map map; + uint32_t ret; + + (void)state; + + map.log2_groups_per_block = ilog2(8); + + expect_value(__wrap_is_group_erased, map, &map); + expect_value(__wrap_is_group_erased, group, 32 + 3); + will_return(__wrap_is_group_erased, 0); + + expect_value(__wrap_is_group_erased, map, &map); + expect_value(__wrap_is_group_erased, group, 32 + 4); + will_return(__wrap_is_group_erased, 0); + + expect_value(__wrap_is_group_erased, map, &map); + expect_value(__wrap_is_group_erased, group, 32 + 5); + will_return(__wrap_is_group_erased, 1); + + ret = find_last_group(&map, 4); + assert_int_equal(ret, 32 + 4); +} + +int test_find_last_group(void) +{ + const struct CMUnitTest tests[] = { + { "find_last_group: middle", test_middle, NULL, NULL, NULL }, + { "find_last_group: first", test_first, NULL, NULL, NULL }, + { "find_last_group: last", test_last, NULL, NULL, NULL }, + { "find_last_group: left right", test_left_right, NULL, NULL, NULL }, + { "find_last_group: right left", test_right_left, NULL, NULL, NULL }, + }; + + return cmocka_run_group_tests_name("find_last_group", tests, NULL, NULL); +} diff --git a/source/tests/main.c b/source/tests/main.c index b7ad26b..91f4452 100644 --- a/source/tests/main.c +++ b/source/tests/main.c @@ -70,6 +70,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_find_last_group(void); int test_next_upage(void); int test_read_page_group(void); int test_read_page_desc(void); @@ -83,6 +84,7 @@ int main(void) count += test_find_block(); count += test_find_block_div(); count += test_find_last_block(); + count += test_find_last_group(); count += test_next_upage(); count += test_read_page_group(); count += test_read_page_desc();