test: ftl: add tests for find_block()

tags/0.1.0
S.J.R. van Schaik 7 years ago
parent 73057f6f25
commit c6744d6975
  1. 1
      source/tests/Makefile
  2. 170
      source/tests/ftl/find_block.c
  3. 2
      source/tests/main.c

@ -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

@ -0,0 +1,170 @@
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <setjmp.h>
#include <cmocka.h>
#include <bitops.h>
#include <flash.h>
#include <ftl.h>
#include <macros.h>
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);
}

@ -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();

Loading…
Cancel
Save