test: ftl: add tests for write_upage()

tags/0.1.0
S.J.R. van Schaik 7 years ago
parent c1a7b885f2
commit 2ff1003430
  1. 131
      source/tests/ftl/write_upage.c
  2. 2
      source/tests/main.c

@ -0,0 +1,131 @@
#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 write_upage(struct ftl_map *map, const void *page,
struct ftl_page_desc *page_desc);
static void test_prepare_head(void **state)
{
struct ftl_map map;
struct ftl_page_desc page_desc;
int ret;
(void)state;
map.log2_page_size = ilog2(4 * KIB);
map.head = 0;
will_return(__wrap_prepare_head, -1);
ret = write_upage(&map, NULL, &page_desc);
assert_int_equal(ret, -1);
}
static void test_null_page(void **state)
{
struct ftl_map map;
struct ftl_page_desc page_desc;
int ret;
(void)state;
map.log2_page_size = ilog2(4 * KIB);
map.head = 0;
will_return(__wrap_prepare_head, 0);
expect_value(__wrap_write_page_desc, map, &map);
expect_value(__wrap_write_page_desc, page_desc, &page_desc);
will_return(__wrap_write_page_desc, 0);
ret = write_upage(&map, NULL, &page_desc);
assert_int_equal(ret, 0);
}
static void test_page0(void **state)
{
struct ftl_map map;
char page[4 * KIB], ret_page[4 * KIB];
struct ftl_page_desc page_desc;
int ret;
(void)state;
memset(page, 0x5a, sizeof page);
memset(ret_page, 0, sizeof ret_page);
map.log2_page_size = ilog2(4 * KIB);
map.head = 0;
will_return(__wrap_prepare_head, 0);
expect_value(__wrap_flash_write, addr, 0);
expect_value(__wrap_flash_write, len, 4 * KIB);
will_return(__wrap_flash_write, 4 * KIB);
will_return(__wrap_flash_write, &ret_page);
expect_value(__wrap_write_page_desc, map, &map);
expect_value(__wrap_write_page_desc, page_desc, &page_desc);
will_return(__wrap_write_page_desc, 0);
ret = write_upage(&map, &page, &page_desc);
assert_int_equal(ret, 0);
assert_memory_equal(page, ret_page, sizeof page);
}
static void test_page42(void **state)
{
struct ftl_map map;
char page[4 * KIB], ret_page[4 * KIB];
struct ftl_page_desc page_desc;
int ret;
(void)state;
memset(page, 0x5a, sizeof page);
memset(ret_page, 0, sizeof ret_page);
map.log2_page_size = ilog2(4 * KIB);
map.head = 42;
will_return(__wrap_prepare_head, 0);
expect_value(__wrap_flash_write, addr, 42 * 4 * KIB);
expect_value(__wrap_flash_write, len, 4 * KIB);
will_return(__wrap_flash_write, 4 * KIB);
will_return(__wrap_flash_write, &ret_page);
expect_value(__wrap_write_page_desc, map, &map);
expect_value(__wrap_write_page_desc, page_desc, &page_desc);
will_return(__wrap_write_page_desc, 0);
ret = write_upage(&map, &page, &page_desc);
assert_int_equal(ret, 0);
assert_memory_equal(page, ret_page, sizeof page);
}
int test_write_upage(void)
{
const struct CMUnitTest tests[] = {
{ "write_upage: prepare_head() = -1", test_prepare_head, NULL, NULL, NULL },
{ "write_upage: null page", test_null_page, NULL, NULL, NULL },
{ "write_upage: head=0", test_page0, NULL, NULL, NULL },
{ "write_upage: head=42", test_page42, NULL, NULL, NULL },
};
return cmocka_run_group_tests_name("write_upage", tests, NULL, NULL);
}

@ -70,6 +70,7 @@ int test_next_upage(void);
int test_read_page_group(void); int test_read_page_group(void);
int test_read_page_desc(void); int test_read_page_desc(void);
int test_write_page_desc(void); int test_write_page_desc(void);
int test_write_upage(void);
int main(void) int main(void)
{ {
@ -80,6 +81,7 @@ int main(void)
count += test_read_page_group(); count += test_read_page_group();
count += test_read_page_desc(); count += test_read_page_desc();
count += test_write_page_desc(); count += test_write_page_desc();
count += test_write_upage();
return count; return count;
} }

Loading…
Cancel
Save