diff --git a/source/tests/Makefile b/source/tests/Makefile index faa972e..692f9f6 100644 --- a/source/tests/Makefile +++ b/source/tests/Makefile @@ -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 diff --git a/source/tests/flash/mock.c b/source/tests/flash/mock.c index 5974f25..c0b5ae8 100644 --- a/source/tests/flash/mock.c +++ b/source/tests/flash/mock.c @@ -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); diff --git a/source/tests/ftl/ftl_read.c b/source/tests/ftl/ftl_read.c new file mode 100644 index 0000000..0af2b76 --- /dev/null +++ b/source/tests/ftl/ftl_read.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +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); +} diff --git a/source/tests/ftl/main.c b/source/tests/ftl/main.c index c7d8d20..817adf5 100644 --- a/source/tests/ftl/main.c +++ b/source/tests/ftl/main.c @@ -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; }