ztest: Added support to return data via mock

Added support to mock framework to return data via function parameters

Signed-off-by: Jan Van Winkel <jan.van_winkel@dxplore.eu>
This commit is contained in:
Jan Van Winkel 2020-12-30 17:22:51 +01:00 committed by Anas Nashif
commit 4a9a49a185
2 changed files with 60 additions and 0 deletions

View file

@ -83,6 +83,33 @@
z_ztest_check_expected_data(__func__, STRINGIFY(param), \
(void *)(param), (length))
/**
* @brief Tell function @a func to return the data @a data for @a param
*
* When using ztest_return_data(), the data pointed to by @a param should be
* same @a data in this function. Only data pointer is stored by this function,
* so it must still be valid when ztest_copy_return_data is called.
*
* @param func Function in question
* @param param Parameter for which the data should be set
* @param data pointer for the data for parameter @a param
*/
#define ztest_return_data(func, param, data) \
z_ztest_return_data(STRINGIFY(func), STRINGIFY(param), (void *)(data))
/**
* @brief Copy the data set by ztest_return_data to the memory pointed by
* @a param
*
* This will first check that @a param is not null and then copy the data.
* This must be called from the called function.
*
* @param param Parameter to return data for
* @param length Length of the data to return
*/
#define ztest_copy_return_data(param, length) \
z_ztest_copy_return_data(__func__, STRINGIFY(param), \
(void *)(param), (length))
/**
* @brief Tell @a func that it should return @a value
*
@ -136,6 +163,10 @@ void z_ztest_expect_data(const char *fn, const char *name, void *val);
void z_ztest_check_expected_data(const char *fn, const char *name, void *data,
uint32_t length);
void z_ztest_return_data(const char *fn, const char *name, void *val);
void z_ztest_copy_return_data(const char *fn, const char *name, void *data,
uint32_t length);
void z_ztest_returns_value(const char *fn, uintptr_t value);
uintptr_t z_ztest_get_return_value(const char *fn);

View file

@ -250,6 +250,35 @@ void z_ztest_check_expected_data(const char *fn, const char *name, void *data,
}
}
void z_ztest_return_data(const char *fn, const char *name, void *val)
{
insert_value(&parameter_list, fn, name, (uintptr_t)val);
}
void z_ztest_copy_return_data(const char *fn, const char *name, void *data,
uint32_t length)
{
struct parameter *param;
void *return_data;
if (data == NULL) {
PRINT("%s:%s received null pointer\n", fn, name);
ztest_test_fail();
return;
}
param = find_and_delete_value(&parameter_list, fn, name);
if (!param) {
PRINT("Failed to find parameter %s for %s\n", name, fn);
memset(data, 0, length);
ztest_test_fail();
} else {
return_data = (void *)param->value;
free_parameter(param);
memcpy(data, return_data, length);
}
}
void z_ztest_returns_value(const char *fn, uintptr_t value)
{
insert_value(&return_value_list, fn, "", value);