ztest: run clang-format on ztest_assert.h

Simple pass to make the diff easier to read for additional changes.

Signed-off-by: Yuval Peress <peress@google.com>
This commit is contained in:
Yuval Peress 2022-06-27 11:41:01 -06:00 committed by Anas Nashif
commit e26ea1b936

View file

@ -13,11 +13,12 @@
#ifndef ZEPHYR_TESTSUITE_ZTEST_ASSERT_H_ #ifndef ZEPHYR_TESTSUITE_ZTEST_ASSERT_H_
#define ZEPHYR_TESTSUITE_ZTEST_ASSERT_H_ #define ZEPHYR_TESTSUITE_ZTEST_ASSERT_H_
#include <ztest.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdbool.h>
#include <ztest.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -30,8 +31,7 @@ void ztest_test_fail(void);
static inline bool z_zassert_(bool cond, const char *file, int line) static inline bool z_zassert_(bool cond, const char *file, int line)
{ {
if (cond == false) { if (cond == false) {
PRINT("\n Assertion failed at %s:%d\n", PRINT("\n Assertion failed at %s:%d\n", ztest_relative_filename(file), line);
ztest_relative_filename(file), line);
ztest_test_fail(); ztest_test_fail();
return false; return false;
} }
@ -39,23 +39,19 @@ static inline bool z_zassert_(bool cond, const char *file, int line)
return true; return true;
} }
#define z_zassert(cond, default_msg, file, line, func, msg, ...) \ #define z_zassert(cond, default_msg, file, line, func, msg, ...) z_zassert_(cond, file, line)
z_zassert_(cond, file, line)
#else /* CONFIG_ZTEST_ASSERT_VERBOSE != 0 */ #else /* CONFIG_ZTEST_ASSERT_VERBOSE != 0 */
static inline bool z_zassert(bool cond, static inline bool z_zassert(bool cond, const char *default_msg, const char *file, int line,
const char *default_msg, const char *func, const char *msg, ...)
const char *file,
int line, const char *func,
const char *msg, ...)
{ {
if (cond == false) { if (cond == false) {
va_list vargs; va_list vargs;
va_start(vargs, msg); va_start(vargs, msg);
PRINT("\n Assertion failed at %s:%d: %s: %s\n", PRINT("\n Assertion failed at %s:%d: %s: %s\n", ztest_relative_filename(file),
ztest_relative_filename(file), line, func, default_msg); line, func, default_msg);
vprintk(msg, vargs); vprintk(msg, vargs);
printk("\n"); printk("\n");
va_end(vargs); va_end(vargs);
@ -64,8 +60,8 @@ static inline bool z_zassert(bool cond,
} }
#if CONFIG_ZTEST_ASSERT_VERBOSE == 2 #if CONFIG_ZTEST_ASSERT_VERBOSE == 2
else { else {
PRINT("\n Assertion succeeded at %s:%d (%s)\n", PRINT("\n Assertion succeeded at %s:%d (%s)\n", ztest_relative_filename(file),
ztest_relative_filename(file), line, func); line, func);
} }
#endif #endif
return true; return true;
@ -73,7 +69,6 @@ static inline bool z_zassert(bool cond,
#endif /* CONFIG_ZTEST_ASSERT_VERBOSE */ #endif /* CONFIG_ZTEST_ASSERT_VERBOSE */
/** /**
* @defgroup ztest_assert Ztest assertion macros * @defgroup ztest_assert Ztest assertion macros
* @ingroup ztest * @ingroup ztest
@ -97,66 +92,58 @@ static inline bool z_zassert(bool cond,
* @param msg Optional, can be NULL. Message to print if @a cond is false. * @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 * @param default_msg Message to print if @a cond is false
*/ */
#define zassert(cond, default_msg, msg, ...) do { \ #define zassert(cond, default_msg, msg, ...) \
bool _ret = z_zassert(cond, msg ? ("(" default_msg ")") : (default_msg), \ do { \
__FILE__, __LINE__, __func__, \ bool _ret = z_zassert(cond, msg ? ("(" default_msg ")") : (default_msg), __FILE__, \
msg ? msg : "", ##__VA_ARGS__); \ __LINE__, __func__, msg ? msg : "", ##__VA_ARGS__); \
if (!_ret) { \ if (!_ret) { \
/* If kernel but without multithreading return. */ \ /* If kernel but without multithreading return. */ \
COND_CODE_1(KERNEL, \ COND_CODE_1(KERNEL, (COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
(COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \ ()) \
()) \ } \
} \ } while (0)
} while (0)
/** /**
* @brief Assert that this function call won't be reached * @brief Assert that this function call won't be reached
* @param msg Optional message to print if the assertion fails * @param msg Optional message to print if the assertion fails
*/ */
#define zassert_unreachable(msg, ...) zassert(0, "Reached unreachable code", \ #define zassert_unreachable(msg, ...) zassert(0, "Reached unreachable code", msg, ##__VA_ARGS__)
msg, ##__VA_ARGS__)
/** /**
* @brief Assert that @a cond is true * @brief Assert that @a cond is true
* @param cond Condition to check * @param cond Condition to check
* @param msg Optional message to print if the assertion fails * @param msg Optional message to print if the assertion fails
*/ */
#define zassert_true(cond, msg, ...) zassert(cond, #cond " is false", \ #define zassert_true(cond, msg, ...) zassert(cond, #cond " is false", msg, ##__VA_ARGS__)
msg, ##__VA_ARGS__)
/** /**
* @brief Assert that @a cond is false * @brief Assert that @a cond is false
* @param cond Condition to check * @param cond Condition to check
* @param msg Optional message to print if the assertion fails * @param msg Optional message to print if the assertion fails
*/ */
#define zassert_false(cond, msg, ...) zassert(!(cond), #cond " is true", \ #define zassert_false(cond, msg, ...) zassert(!(cond), #cond " is true", msg, ##__VA_ARGS__)
msg, ##__VA_ARGS__)
/** /**
* @brief Assert that @a cond is 0 (success) * @brief Assert that @a cond is 0 (success)
* @param cond Condition to check * @param cond Condition to check
* @param msg Optional message to print if the assertion fails * @param msg Optional message to print if the assertion fails
*/ */
#define zassert_ok(cond, msg, ...) zassert(!(cond), #cond " is non-zero", \ #define zassert_ok(cond, msg, ...) zassert(!(cond), #cond " is non-zero", msg, ##__VA_ARGS__)
msg, ##__VA_ARGS__)
/** /**
* @brief Assert that @a ptr is NULL * @brief Assert that @a ptr is NULL
* @param ptr Pointer to compare * @param ptr Pointer to compare
* @param msg Optional message to print if the assertion fails * @param msg Optional message to print if the assertion fails
*/ */
#define zassert_is_null(ptr, msg, ...) zassert((ptr) == NULL, \ #define zassert_is_null(ptr, msg, ...) \
#ptr " is not NULL", \ zassert((ptr) == NULL, #ptr " is not NULL", msg, ##__VA_ARGS__)
msg, ##__VA_ARGS__)
/** /**
* @brief Assert that @a ptr is not NULL * @brief Assert that @a ptr is not NULL
* @param ptr Pointer to compare * @param ptr Pointer to compare
* @param msg Optional message to print if the assertion fails * @param msg Optional message to print if the assertion fails
*/ */
#define zassert_not_null(ptr, msg, ...) zassert((ptr) != NULL, \ #define zassert_not_null(ptr, msg, ...) zassert((ptr) != NULL, #ptr " is NULL", msg, ##__VA_ARGS__)
#ptr " is NULL", msg, \
##__VA_ARGS__)
/** /**
* @brief Assert that @a a equals @a b * @brief Assert that @a a equals @a b
@ -167,9 +154,8 @@ static inline bool z_zassert(bool cond,
* @param b Value to compare * @param b Value to compare
* @param msg Optional message to print if the assertion fails * @param msg Optional message to print if the assertion fails
*/ */
#define zassert_equal(a, b, msg, ...) zassert((a) == (b), \ #define zassert_equal(a, b, msg, ...) \
#a " not equal to " #b, \ zassert((a) == (b), #a " not equal to " #b, msg, ##__VA_ARGS__)
msg, ##__VA_ARGS__)
/** /**
* @brief Assert that @a a does not equal @a b * @brief Assert that @a a does not equal @a b
@ -180,9 +166,8 @@ static inline bool z_zassert(bool cond,
* @param b Value to compare * @param b Value to compare
* @param msg Optional message to print if the assertion fails * @param msg Optional message to print if the assertion fails
*/ */
#define zassert_not_equal(a, b, msg, ...) zassert((a) != (b), \ #define zassert_not_equal(a, b, msg, ...) \
#a " equal to " #b, \ zassert((a) != (b), #a " equal to " #b, msg, ##__VA_ARGS__)
msg, ##__VA_ARGS__)
/** /**
* @brief Assert that @a a equals @a b * @brief Assert that @a a equals @a b
@ -193,9 +178,8 @@ static inline bool z_zassert(bool cond,
* @param b Value to compare * @param b Value to compare
* @param msg Optional message to print if the assertion fails * @param msg Optional message to print if the assertion fails
*/ */
#define zassert_equal_ptr(a, b, msg, ...) \ #define zassert_equal_ptr(a, b, msg, ...) \
zassert((void *)(a) == (void *)(b), #a " not equal to " #b, \ zassert((void *)(a) == (void *)(b), #a " not equal to " #b, msg, ##__VA_ARGS__)
msg, ##__VA_ARGS__)
/** /**
* @brief Assert that @a a is within @a b with delta @a d * @brief Assert that @a a is within @a b with delta @a d
@ -205,9 +189,8 @@ static inline bool z_zassert(bool cond,
* @param d Delta * @param d Delta
* @param msg Optional message to print if the assertion fails * @param msg Optional message to print if the assertion fails
*/ */
#define zassert_within(a, b, d, msg, ...) \ #define zassert_within(a, b, d, msg, ...) \
zassert(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), \ zassert(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), #a " not within " #b " +/- " #d, \
#a " not within " #b " +/- " #d, \
msg, ##__VA_ARGS__) msg, ##__VA_ARGS__)
/** /**
@ -219,10 +202,9 @@ static inline bool z_zassert(bool cond,
* @param u Upper limit * @param u Upper limit
* @param msg Optional message to print if the assertion fails * @param msg Optional message to print if the assertion fails
*/ */
#define zassert_between_inclusive(a, l, u, msg, ...) \ #define zassert_between_inclusive(a, l, u, msg, ...) \
zassert(((a) >= (l)) && ((a) <= (u)), \ zassert(((a) >= (l)) && ((a) <= (u)), #a " not between " #l " and " #u " inclusive", msg, \
#a " not between " #l " and " #u " inclusive", \ ##__VA_ARGS__)
msg, ##__VA_ARGS__)
/** /**
* @brief Assert that 2 memory buffers have the same contents * @brief Assert that 2 memory buffers have the same contents
@ -235,8 +217,7 @@ static inline bool z_zassert(bool cond,
* @param ... Arguments, see @ref zassert_mem_equal__ * @param ... Arguments, see @ref zassert_mem_equal__
* for real arguments accepted. * for real arguments accepted.
*/ */
#define zassert_mem_equal(...) \ #define zassert_mem_equal(...) zassert_mem_equal__(__VA_ARGS__)
zassert_mem_equal__(__VA_ARGS__)
/** /**
* @brief Internal assert that 2 memory buffers have the same contents * @brief Internal assert that 2 memory buffers have the same contents
@ -249,9 +230,8 @@ static inline bool z_zassert(bool cond,
* @param size Size of buffers * @param size Size of buffers
* @param msg Optional message to print if the assertion fails * @param msg Optional message to print if the assertion fails
*/ */
#define zassert_mem_equal__(buf, exp, size, msg, ...) \ #define zassert_mem_equal__(buf, exp, size, msg, ...) \
zassert(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, \ zassert(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, msg, ##__VA_ARGS__)
msg, ##__VA_ARGS__)
/** /**
* @} * @}