Skip to content

Commit

Permalink
tests: ztest: Add formatting in assert APIs
Browse files Browse the repository at this point in the history
All ztest_* apis now support format specifier which will
evetually help to pass variable arguments to ztest assert
apis in different test cases while showing error messages.

Signed-off-by: Punit Vara <[email protected]>
Signed-off-by: Anas Nashif <[email protected]>
  • Loading branch information
punitvara authored and Anas Nashif committed Oct 28, 2017
1 parent 50d7ca1 commit 273c0fd
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions tests/ztest/include/ztest_assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
#define __ZTEST_ASSERT_H__

#include <ztest.h>
#include <stdarg.h>
#include <stdio.h>

void ztest_test_fail(void);

#if CONFIG_ZTEST_ASSERT_VERBOSE == 0

static inline void _zassert_(int cond, const char *file, int line)
Expand All @@ -29,17 +30,29 @@ static inline void _zassert_(int cond, const char *file, int line)
}
}

#define _zassert(cond, msg, default_msg, file, line, func) \
#define _zassert(cond, default_msg, file, line, func, msg, ...) \
_zassert_(cond, file, line)

#else /* CONFIG_ZTEST_ASSERT_VERBOSE != 0 */

static inline void _zassert(int cond, const char *msg, const char *default_msg,
const char *file, int line, const char *func)
static inline void _zassert(int cond,
const char *default_msg,
const char *file,
int line, const char *func,
const char *msg, ...)
{
if (!(cond)) {
PRINT("\n Assertion failed at %s:%d: %s: %s %s\n",
file, line, func, msg, default_msg);
va_list vargs;

va_start(vargs, msg);
PRINT("\n Assertion failed at %s:%d: %s: %s\n",
file, line, func, default_msg);
#if defined(CONFIG_STDOUT_CONSOLE)
vprintf(msg, vargs);
#else
vprintk(msg, vargs);
#endif
va_end(vargs);
ztest_test_fail();
}
#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
Expand All @@ -52,6 +65,7 @@ static inline void _zassert(int cond, const char *msg, const char *default_msg,

#endif /* CONFIG_ZTEST_ASSERT_VERBOSE */


/**
* @defgroup ztest_assert Ztest assertion macros
* @ingroup ztest
Expand All @@ -72,43 +86,50 @@ static inline void _zassert(int cond, const char *msg, const char *default_msg,
* @param default_msg Message to print if @a cond is false
*/

#define zassert(cond, msg, default_msg) \
_zassert(cond, msg ? msg : "", msg ? ("(" default_msg ")") : (default_msg), \
__FILE__, __LINE__, __func__)
#define zassert(cond, default_msg, msg, ...) \
_zassert(cond, msg ? ("(" default_msg ")") : (default_msg), \
__FILE__, __LINE__, __func__, msg ? msg : "", ##__VA_ARGS__)

/**
* @brief Assert that this function call won't be reached
* @param msg Optional message to print if the assertion fails
*/
#define zassert_unreachable(msg) zassert(0, msg, "Reached unreachable code")
#define zassert_unreachable(msg, ...) zassert(0, "Reached unreachable code", \
msg, ##__VA_ARGS__)

/**
* @brief Assert that @a cond is true
* @param cond Condition to check
* @param msg Optional message to print if the assertion fails
*/
#define zassert_true(cond, msg) zassert(cond, msg, #cond " is false")
#define zassert_true(cond, msg, ...) zassert(cond, #cond " is false", \
msg, ##__VA_ARGS__)

/**
* @brief Assert that @a cond is false
* @param cond Condition to check
* @param msg Optional message to print if the assertion fails
*/
#define zassert_false(cond, msg) zassert(!(cond), msg, #cond " is true")
#define zassert_false(cond, msg, ...) zassert(!(cond), #cond " is true", \
msg, ##__VA_ARGS__)

/**
* @brief Assert that @a ptr is NULL
* @param ptr Pointer to compare
* @param msg Optional message to print if the assertion fails
*/
#define zassert_is_null(ptr, msg) zassert((ptr) == NULL, msg, #ptr " is not NULL")
#define zassert_is_null(ptr, msg, ...) zassert((ptr) == NULL, \
#ptr " is not NULL", \
msg, ##__VA_ARGS__)

/**
* @brief Assert that @a ptr is not NULL
* @param ptr Pointer to compare
* @param msg Optional message to print if the assertion fails
*/
#define zassert_not_null(ptr, msg) zassert((ptr) != NULL, msg, #ptr " is NULL")
#define zassert_not_null(ptr, msg, ...) zassert((ptr) != NULL, \
#ptr " is NULL", msg, \
##__VA_ARGS__)

/**
* @brief Assert that @a a equals @a b
Expand All @@ -119,7 +140,9 @@ static inline void _zassert(int cond, const char *msg, const char *default_msg,
* @param b Value to compare
* @param msg Optional message to print if the assertion fails
*/
#define zassert_equal(a, b, msg) zassert((a) == (b), msg, #a " not equal to " #b)
#define zassert_equal(a, b, msg, ...) zassert((a) == (b), \
#a " not equal to " #b, \
msg, ##__VA_ARGS__)

/**
* @brief Assert that @a a does not equal @a b
Expand All @@ -130,7 +153,9 @@ static inline void _zassert(int cond, const char *msg, const char *default_msg,
* @param b Value to compare
* @param msg Optional message to print if the assertion fails
*/
#define zassert_not_equal(a, b, msg) zassert((a) != (b), msg, #a " equal to " #b)
#define zassert_not_equal(a, b, msg, ...) zassert((a) != (b), \
#a " equal to " #b, \
msg, ##__VA_ARGS__)

/**
* @brief Assert that @a a equals @a b
Expand All @@ -141,8 +166,9 @@ static inline void _zassert(int cond, const char *msg, const char *default_msg,
* @param b Value to compare
* @param msg Optional message to print if the assertion fails
*/
#define zassert_equal_ptr(a, b, msg) \
zassert((void *)(a) == (void *)(b), msg, #a " not equal to " #b)
#define zassert_equal_ptr(a, b, msg, ...) \
zassert((void *)(a) == (void *)(b), #a " not equal to " #b, \
msg, ##__VA_ARGS__)

/**
* @}
Expand Down

0 comments on commit 273c0fd

Please sign in to comment.