testsuite: ztest: Add support for CONFIG_MULTITHREADING=n

Added support for no multithreading case where test cases are
called directly from main(). On failure in ztest assert macro,
macro returns from the function. It implies that ztest assert
macros can only be called in the test function.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruscinski 2021-04-14 14:37:50 +02:00 committed by Carles Cufí
commit 5e06b4add0
2 changed files with 50 additions and 21 deletions

View file

@ -27,13 +27,16 @@ const char *ztest_relative_filename(const char *file);
void ztest_test_fail(void);
#if CONFIG_ZTEST_ASSERT_VERBOSE == 0
static inline void z_zassert_(bool cond, const char *file, int line)
static inline bool z_zassert_(bool cond, const char *file, int line)
{
if (cond == false) {
PRINT("\n Assertion failed at %s:%d\n",
ztest_relative_filename(file), line);
ztest_test_fail();
return false;
}
return true;
}
#define z_zassert(cond, default_msg, file, line, func, msg, ...) \
@ -41,7 +44,7 @@ static inline void z_zassert_(bool cond, const char *file, int line)
#else /* CONFIG_ZTEST_ASSERT_VERBOSE != 0 */
static inline void z_zassert(bool cond,
static inline bool z_zassert(bool cond,
const char *default_msg,
const char *file,
int line, const char *func,
@ -57,6 +60,7 @@ static inline void z_zassert(bool cond,
printk("\n");
va_end(vargs);
ztest_test_fail();
return false;
}
#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
else {
@ -64,6 +68,7 @@ static inline void z_zassert(bool cond,
ztest_relative_filename(file), line, func);
}
#endif
return true;
}
#endif /* CONFIG_ZTEST_ASSERT_VERBOSE */
@ -84,14 +89,25 @@ static inline void z_zassert(bool cond,
* You probably don't need to call this macro directly. You should
* instead use zassert_{condition} macros below.
*
* Note that when CONFIG_MULTITHREADING=n macro returns from the function. It is
* then expected that in that case ztest asserts will be used only in the
* context of the test function.
*
* @param cond Condition to check
* @param msg Optional, can be NULL. Message to print if @a cond is false.
* @param default_msg Message to print if @a cond is false
*/
#define zassert(cond, default_msg, msg, ...) \
z_zassert(cond, msg ? ("(" default_msg ")") : (default_msg), \
__FILE__, __LINE__, __func__, msg ? msg : "", ##__VA_ARGS__)
#define zassert(cond, default_msg, msg, ...) do { \
bool _ret = z_zassert(cond, msg ? ("(" default_msg ")") : (default_msg), \
__FILE__, __LINE__, __func__, \
msg ? msg : "", ##__VA_ARGS__); \
if (!_ret) { \
/* If kernel but without multithreading return. */ \
COND_CODE_1(KERNEL, \
(COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
()) \
} \
} while (0)
/**
* @brief Assert that this function call won't be reached

View file

@ -68,7 +68,9 @@ static int cleanup_test(struct unit_test *test)
* Because we reuse the same k_thread structure this would
* causes some problems.
*/
k_thread_abort(&ztest_thread);
if (IS_ENABLED(CONFIG_MULTITHREADING)) {
k_thread_abort(&ztest_thread);
}
#endif
if (!ret && mock_status == 1) {
@ -286,25 +288,30 @@ K_THREAD_STACK_DEFINE(ztest_thread_stack, CONFIG_ZTEST_STACKSIZE +
CONFIG_TEST_EXTRA_STACKSIZE);
static ZTEST_BMEM int test_result;
static void test_finalize(void)
{
if (IS_ENABLED(CONFIG_MULTITHREADING)) {
k_thread_abort(&ztest_thread);
k_thread_abort(k_current_get());
}
}
void ztest_test_fail(void)
{
test_result = -1;
k_thread_abort(&ztest_thread);
k_thread_abort(k_current_get());
test_finalize();
}
void ztest_test_pass(void)
{
test_result = 0;
k_thread_abort(&ztest_thread);
k_thread_abort(k_current_get());
test_finalize();
}
void ztest_test_skip(void)
{
test_result = -2;
k_thread_abort(&ztest_thread);
k_thread_abort(k_current_get());
test_finalize();
}
static void init_testing(void)
@ -329,15 +336,21 @@ static int run_test(struct unit_test *test)
int ret = TC_PASS;
TC_START(test->name);
k_thread_create(&ztest_thread, ztest_thread_stack,
K_THREAD_STACK_SIZEOF(ztest_thread_stack),
(k_thread_entry_t) test_cb, (struct unit_test *)test,
NULL, NULL, CONFIG_ZTEST_THREAD_PRIORITY,
test->thread_options | K_INHERIT_PERMS,
K_NO_WAIT);
k_thread_name_set(&ztest_thread, "ztest_thread");
k_thread_join(&ztest_thread, K_FOREVER);
if (IS_ENABLED(CONFIG_MULTITHREADING)) {
k_thread_create(&ztest_thread, ztest_thread_stack,
K_THREAD_STACK_SIZEOF(ztest_thread_stack),
(k_thread_entry_t) test_cb, (struct unit_test *)test,
NULL, NULL, CONFIG_ZTEST_THREAD_PRIORITY,
test->thread_options | K_INHERIT_PERMS,
K_NO_WAIT);
k_thread_name_set(&ztest_thread, "ztest_thread");
k_thread_join(&ztest_thread, K_FOREVER);
} else {
test_result = 1;
run_test_functions(test);
}
phase = TEST_PHASE_TEARDOWN;
test->teardown();