Go to the documentation of this file.
44 #ifndef _TUSB_ASSERTION_H_
45 #define _TUSB_ASSERTION_H_
61 #define STATIC_ASSERT static_assert
63 #if defined __COUNTER__ && __COUNTER__ != __COUNTER__
64 #define _ASSERT_COUNTER __COUNTER__
66 #define _ASSERT_COUNTER __LINE__
69 #define STATIC_ASSERT(const_expr, message) enum { XSTRING_CONCAT_(static_assert_, _ASSERT_COUNTER) = 1/(!!(const_expr)) }
73 #if TUSB_CFG_DEBUG == 3
74 #define _PRINTF(...) printf(__VA_ARGS__) // PRINTF
83 #define ASSERT_MESSAGE(format, ...)\
84 _PRINTF("Assert at %s: %s: %d: " format "\n", __BASE_FILE__, __func__ , __LINE__, __VA_ARGS__)
86 #define ASSERT_MESSAGE(format, ...)\
87 _PRINTF("%d:note: Assert " format "\n", __LINE__, __VA_ARGS__)
91 #define ASSERT_ERROR_HANDLER(x, para) return x
93 #define ASSERT_ERROR_HANDLER(x, para) Throw(x)
96 #define ASSERT_DEFINE_WITH_HANDLER(error_handler, handler_para, setup_statement, condition, error, format, ...) \
100 if (hal_debugger_is_attached()){\
101 hal_debugger_breakpoint();\
103 ASSERT_MESSAGE(format, __VA_ARGS__);\
105 error_handler(error, handler_para);\
109 #define ASSERT_DEFINE(...) ASSERT_DEFINE_WITH_HANDLER(ASSERT_ERROR_HANDLER, NULL, __VA_ARGS__)
114 #define ASSERT_STATUS_MESSAGE(sts, message) \
115 ASSERT_DEFINE(tusb_error_t status = (tusb_error_t)(sts),\
116 TUSB_ERROR_NONE == status, status, "%s: %s", TUSB_ErrorStr[status], message)
118 #define ASSERT_STATUS(sts) \
119 ASSERT_DEFINE(tusb_error_t status = (tusb_error_t)(sts),\
120 TUSB_ERROR_NONE == status, status, "%s", TUSB_ErrorStr[status])
125 #define ASSERT(...) ASSERT_TRUE(__VA_ARGS__)
126 #define ASSERT_TRUE(condition , error) ASSERT_DEFINE( , (condition), error, "%s", "evaluated to false")
127 #define ASSERT_FALSE(condition , error) ASSERT_DEFINE( ,!(condition), error, "%s", "evaluated to true")
132 #define ASSERT_PTR(...) ASSERT_PTR_NOT_NULL(__VA_ARGS__)
133 #define ASSERT_PTR_NOT_NULL(pointer, error) ASSERT_DEFINE( , NULL != (pointer), error, "%s", "pointer is NULL")
134 #define ASSERT_PTR_NULL(pointer, error) ASSERT_DEFINE( , NULL == (pointer), error, "%s", "pointer is not NULL")
139 #define ASSERT_XXX_EQUAL(type_format, expected, actual, error) \
141 uint32_t exp = (expected); uint32_t act = (actual),\
144 "expected " type_format ", actual " type_format, exp, act)
146 #define ASSERT_XXX_WITHIN(type_format, lower, upper, actual, error) \
148 uint32_t low = (lower); uint32_t up = (upper); uint32_t act = (actual),\
149 (low <= act) && (act <= up),\
151 "expected within " type_format " - " type_format ", actual " type_format, low, up, act)
156 #define ASSERT_INT(...) ASSERT_INT_EQUAL(__VA_ARGS__)
157 #define ASSERT_INT_EQUAL(...) ASSERT_XXX_EQUAL("%d", __VA_ARGS__)
158 #define ASSERT_INT_WITHIN(...) ASSERT_XXX_WITHIN("%d", __VA_ARGS__)
163 #define ASSERT_HEX(...) ASSERT_HEX_EQUAL(__VA_ARGS__)
164 #define ASSERT_HEX_EQUAL(...) ASSERT_XXX_EQUAL("0x%x", __VA_ARGS__)
165 #define ASSERT_HEX_WITHIN(...) ASSERT_XXX_WITHIN("0x%x", __VA_ARGS__)
170 #define BIN8_PRINTF_PATTERN "%d%d%d%d%d%d%d%d"
171 #define BIN8_PRINTF_CONVERT(byte) \
172 ((byte) & 0x80 ? 1 : 0), \
173 ((byte) & 0x40 ? 1 : 0), \
174 ((byte) & 0x20 ? 1 : 0), \
175 ((byte) & 0x10 ? 1 : 0), \
176 ((byte) & 0x08 ? 1 : 0), \
177 ((byte) & 0x04 ? 1 : 0), \
178 ((byte) & 0x02 ? 1 : 0), \
179 ((byte) & 0x01 ? 1 : 0)
181 #define ASSERT_BIN8(...) ASSERT_BIN8_EQUAL(__VA_ARGS__)
182 #define ASSERT_BIN8_EQUAL(expected, actual, error)\
184 uint8_t exp = (expected); uint8_t act = (actual),\
187 "expected " BIN8_PRINTF_PATTERN ", actual " BIN8_PRINTF_PATTERN, BIN8_PRINTF_CONVERT(exp), BIN8_PRINTF_CONVERT(act) )