You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
4.4 KiB
185 lines
4.4 KiB
7 years ago
|
#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 read_page_desc(struct ftl_map *map,
|
||
|
struct ftl_page_desc *page_desc, uint32_t upage);
|
||
|
|
||
|
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 test_read_page_desc(void)
|
||
|
{
|
||
|
const struct CMUnitTest tests[] = {
|
||
|
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_name("read_page_desc", tests, NULL, NULL);
|
||
|
}
|