test: ftl: add tests for ftl_read()

tags/0.1.0
S.J.R. van Schaik 7 years ago
parent 0d1ffa71fb
commit bf1f0f4bee
  1. 1
      source/tests/Makefile
  2. 4
      source/tests/flash/mock.c
  3. 80
      source/tests/ftl/ftl_read.c
  4. 2
      source/tests/ftl/main.c

@ -18,6 +18,7 @@ test-obj-y += source/tests/ftl/write_upage.o
test-obj-y += source/tests/ftl/trace_path.o
test-obj-y += source/tests/ftl/ftl_is_mapped.o
test-obj-y += source/tests/ftl/ftl_read.o
TEST_CFLAGS += -Dflash_read=__wrap_flash_read
TEST_CFLAGS += -Dflash_write=__wrap_flash_write

@ -28,7 +28,7 @@ size_t __wrap_flash_read(struct flash_dev *dev, uint32_t addr,
if (len > ret_len)
len = ret_len;
if (len)
if (len && ret_data)
memcpy(data, ret_data, len);
return len;
@ -59,6 +59,8 @@ size_t __wrap_flash_write(struct flash_dev *dev, uint32_t addr,
size_t __wrap_flash_write0(struct flash_dev *dev, uint32_t addr, size_t len)
{
(void)dev;
check_expected(addr);
check_expected(len);

@ -0,0 +1,80 @@
#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>
static void test_basic_read(void **state)
{
char data[24];
struct ftl_map map;
int ret;
(void)state;
map.log2_page_size = ilog2(4 * KIB);
map.outstanding.flags = 0;
expect_value(__wrap_trace_path, map, &map);
expect_value(__wrap_trace_path, new_page_desc, NULL);
expect_not_value(__wrap_trace_path, page, NULL);
expect_value(__wrap_trace_path, va, 4);
will_return(__wrap_trace_path, NULL);
will_return(__wrap_trace_path, 42);
will_return(__wrap_trace_path, 0);
expect_value(__wrap_flash_read, addr, 42 << 12 | 0x321);
expect_value(__wrap_flash_read, len, 24);
will_return(__wrap_flash_read, 24);
will_return(__wrap_flash_read, NULL);
ret = ftl_read(&map, data, 24, 0x4321);
assert_int_equal(ret, 24);
}
static void test_boundary(void **state)
{
char data[32];
struct ftl_map map;
int ret;
(void)state;
map.log2_page_size = ilog2(4 * KIB);
map.outstanding.flags = 0;
expect_value(__wrap_trace_path, map, &map);
expect_value(__wrap_trace_path, new_page_desc, NULL);
expect_not_value(__wrap_trace_path, page, NULL);
expect_value(__wrap_trace_path, va, 4);
will_return(__wrap_trace_path, NULL);
will_return(__wrap_trace_path, 42);
will_return(__wrap_trace_path, 0);
expect_value(__wrap_flash_read, addr, 42 << 12 | 0xff0);
expect_value(__wrap_flash_read, len, 16);
will_return(__wrap_flash_read, 16);
will_return(__wrap_flash_read, NULL);
ret = ftl_read(&map, data, 32, 0x4ff0);
assert_int_equal(ret, 16);
}
int test_ftl_read(void)
{
const struct CMUnitTest tests[] = {
{ "ftl_read: basic read", test_basic_read, NULL, NULL, NULL },
{ "ftl_read: boundary", test_boundary, NULL, NULL, NULL },
};
return cmocka_run_group_tests_name("ftl_read", tests, NULL, NULL);
}

@ -25,6 +25,7 @@ int test_write_upage(void);
int test_trace_path(void);
int test_ftl_is_mapped(void);
int test_ftl_read(void);
int test_ftl(void)
{
@ -44,6 +45,7 @@ int test_ftl(void)
count += test_trace_path();
count += test_ftl_is_mapped();
count += test_ftl_read();
return count;
}

Loading…
Cancel
Save