test: add tests for find_block_div() and read_page_desc()

tags/0.1.0
S.J.R. van Schaik 7 years ago
parent b29e0b945d
commit bbc799f805
  1. 2
      source/ftl/map.c
  2. 1
      source/tests/Makefile
  3. 246
      source/tests/main.c

@ -170,7 +170,7 @@ int write_upage(struct ftl_map *map, const void *page,
* can also be determined, as a single page group may not cover a whole erase * can also be determined, as a single page group may not cover a whole erase
* block. * block.
*/ */
static int find_block_div(struct ftl_map *map) int find_block_div(struct ftl_map *map)
{ {
size_t log2_pages_per_block = map->log2_block_size - map->log2_page_size; size_t log2_pages_per_block = map->log2_block_size - map->log2_page_size;
size_t nbytes_avail = (1 << map->log2_page_size) - size_t nbytes_avail = (1 << map->log2_page_size) -

@ -0,0 +1 @@
test-obj-y += source/tests/main.o

@ -0,0 +1,246 @@
#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>
size_t __wrap_flash_read(struct flash_dev *dev, uint32_t addr,
void *data, size_t len) __attribute__((used));
int find_block_div(struct ftl_map *map);
int read_page_desc(struct ftl_map *map,
struct ftl_page_desc *page_desc, uint32_t upage);
size_t __wrap_flash_read(struct flash_dev *dev, uint32_t addr,
void *data, size_t len)
{
(void)dev;
check_expected(addr);
check_expected(len);
memcpy(data, mock_ptr_type(void *), len);
return mock_type(size_t);
}
static void test_find_block_div1(void **state)
{
struct ftl_map map;
(void)state;
map.log2_block_size = ilog2(64 * KIB);
map.log2_page_size = ilog2(4 * KIB);
find_block_div(&map);
assert_int_equal(1 << map.log2_pages_per_group, 16);
assert_int_equal(1 << map.log2_groups_per_block, 1);
}
static void test_find_block_div2(void **state)
{
struct ftl_map map;
(void)state;
map.log2_block_size = ilog2(16 * KIB);
map.log2_page_size = ilog2(4 * KIB);
find_block_div(&map);
assert_int_equal(1 << map.log2_pages_per_group, 4);
assert_int_equal(1 << map.log2_groups_per_block, 1);
}
static void test_find_block_div3(void **state)
{
struct ftl_map map;
(void)state;
map.log2_block_size = ilog2(64 * KIB);
map.log2_page_size = ilog2(1 * KIB);
find_block_div(&map);
assert_int_equal(1 << map.log2_pages_per_group, 8);
assert_int_equal(1 << map.log2_groups_per_block, 8);
}
static void test_read_page_desc1(void **state)
{
struct ftl_map map;
struct ftl_page_desc page_desc, ret_page_desc;
size_t ret;
(void)state;
memcpy(&page_desc.magic, "page", 4);
expect_value(__wrap_flash_read, addr, (16 - 1) * 4 * KIB +
sizeof(struct ftl_page_group));
expect_value(__wrap_flash_read, len, sizeof page_desc);
will_return(__wrap_flash_read, &page_desc);
will_return(__wrap_flash_read, sizeof page_desc);
map.log2_pages_per_group = ilog2(16);
map.log2_page_size = ilog2(4 * KIB);
ret = read_page_desc(&map, &ret_page_desc, 0);
assert_int_equal(ret, 0);
}
static void test_read_page_desc2(void **state)
{
struct ftl_map map;
struct ftl_page_desc page_desc, ret_page_desc;
size_t ret;
(void)state;
memcpy(&page_desc.magic, "page", 4);
expect_value(__wrap_flash_read, addr, (16 - 1) * 4 * KIB +
sizeof(struct ftl_page_group) + 14 * sizeof(struct ftl_page_desc));
expect_value(__wrap_flash_read, len, sizeof page_desc);
will_return(__wrap_flash_read, &page_desc);
will_return(__wrap_flash_read, sizeof page_desc);
map.log2_pages_per_group = ilog2(16);
map.log2_page_size = ilog2(4 * KIB);
ret = read_page_desc(&map, &ret_page_desc, 14);
assert_int_equal(ret, 0);
}
static void test_read_page_desc3(void **state)
{
struct ftl_map map;
struct ftl_page_desc ret_page_desc;
size_t ret;
(void)state;
map.log2_pages_per_group = ilog2(16);
map.log2_page_size = ilog2(4 * KIB);
ret = read_page_desc(&map, &ret_page_desc, 15);
assert_int_equal(ret, -1);
}
static void test_read_page_desc4(void **state)
{
struct ftl_map map;
struct ftl_page_desc page_desc, ret_page_desc;
size_t ret;
(void)state;
memcpy(&page_desc.magic, "page", 4);
expect_value(__wrap_flash_read, addr, (32 - 1) * 4 * KIB +
sizeof(struct ftl_page_group));
expect_value(__wrap_flash_read, len, sizeof page_desc);
will_return(__wrap_flash_read, &page_desc);
will_return(__wrap_flash_read, sizeof page_desc);
map.log2_pages_per_group = ilog2(16);
map.log2_page_size = ilog2(4 * KIB);
ret = read_page_desc(&map, &ret_page_desc, 16);
assert_int_equal(ret, 0);
}
static void test_read_page_desc5(void **state)
{
struct ftl_map map;
struct ftl_page_desc page_desc, ret_page_desc;
size_t ret;
(void)state;
memcpy(&page_desc.magic, "page", 4);
expect_value(__wrap_flash_read, addr, (32 - 1) * 4 * KIB +
sizeof(struct ftl_page_group) + 14 * sizeof(struct ftl_page_desc));
expect_value(__wrap_flash_read, len, sizeof page_desc);
will_return(__wrap_flash_read, &page_desc);
will_return(__wrap_flash_read, sizeof page_desc);
map.log2_pages_per_group = ilog2(16);
map.log2_page_size = ilog2(4 * KIB);
ret = read_page_desc(&map, &ret_page_desc, 30);
assert_int_equal(ret, 0);
}
static void test_read_page_desc6(void **state)
{
struct ftl_map map;
struct ftl_page_desc ret_page_desc;
size_t ret;
(void)state;
map.log2_pages_per_group = ilog2(16);
map.log2_page_size = ilog2(4 * KIB);
ret = read_page_desc(&map, &ret_page_desc, 31);
assert_int_equal(ret, -1);
}
static void test_read_page_desc7(void **state)
{
struct ftl_map map;
struct ftl_page_desc page_desc, ret_page_desc;
size_t ret;
(void)state;
memcpy(&page_desc.magic, "page", 4);
expect_value(__wrap_flash_read, addr, (48 - 1) * 4 * KIB +
sizeof(struct ftl_page_group));
expect_value(__wrap_flash_read, len, sizeof page_desc);
will_return(__wrap_flash_read, &page_desc);
will_return(__wrap_flash_read, sizeof page_desc);
map.log2_pages_per_group = ilog2(16);
map.log2_page_size = ilog2(4 * KIB);
ret = read_page_desc(&map, &ret_page_desc, 32);
assert_int_equal(ret, 0);
}
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_find_block_div1),
cmocka_unit_test(test_find_block_div2),
cmocka_unit_test(test_find_block_div3),
cmocka_unit_test(test_read_page_desc1),
cmocka_unit_test(test_read_page_desc2),
cmocka_unit_test(test_read_page_desc3),
cmocka_unit_test(test_read_page_desc4),
cmocka_unit_test(test_read_page_desc5),
cmocka_unit_test(test_read_page_desc6),
cmocka_unit_test(test_read_page_desc7),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
Loading…
Cancel
Save