5 #ifndef OPENTITAN_SW_DEVICE_LIB_TESTING_TEST_FRAMEWORK_CHECK_H_
6 #define OPENTITAN_SW_DEVICE_LIB_TESTING_TEST_FRAMEWORK_CHECK_H_
12 #include "sw/device/lib/base/status.h"
16 #include "sw/device/lib/testing/test_framework/status.h"
23 #error "This file is C-only; it is not a polyglot header!"
34 #define CHECK_IMPL(condition, ...) \
36 status_t sts_ = OK_STATUS(); \
40 volatile bool res_ = (condition); \
46 if (OT_VA_ARGS_COUNT(_, ##__VA_ARGS__) == 0) { \
47 LOG_ERROR("CHECK-fail: " #condition); \
49 LOG_ERROR("CHECK-fail: " __VA_ARGS__); \
64 #define CHECK(condition, ...) \
66 if (status_err(CHECK_IMPL(condition, ##__VA_ARGS__))) { \
72 test_status_set(kTestStatusFailed); \
79 #define TRY_CHECK(condition, ...) TRY(CHECK_IMPL(condition, ##__VA_ARGS__))
85 #define SHOW_MISMATCH_FMT_STR_(a) _Generic((a), \
86 bool: "CHECK-fail: [%d] got: 0x%02x; want: 0x%02x", \
87 int8_t: "CHECK-fail: [%d] got: 0x%02x; want: 0x%02x", \
88 uint8_t: "CHECK-fail: [%d] got: 0x%02x; want: 0x%02x", \
89 int16_t: "CHECK-fail: [%d] got: 0x%04x; want: 0x%04x", \
90 uint16_t: "CHECK-fail: [%d] got: 0x%04x; want: 0x%04x", \
91 int32_t: "CHECK-fail: [%d] got: 0x%08x; want: 0x%08x", \
92 uint32_t: "CHECK-fail: [%d] got: 0x%08x; want: 0x%08x", \
93 int64_t: "CHECK-fail: [%d] got: 0x%016x; want: 0x%016x", \
94 uint64_t: "CHECK-fail: [%d] got: 0x%016x; want: 0x%016x")
95 #define SHOW_MATCH_FMT_STR_(a) _Generic((a), \
96 bool: "CHECK-fail: [%d] both equal: 0x%02x", \
97 int8_t: "CHECK-fail: [%d] both equal: 0x%02x", \
98 uint8_t: "CHECK-fail: [%d] both equal: 0x%02x", \
99 int16_t: "CHECK-fail: [%d] both equal: 0x%04x", \
100 uint16_t: "CHECK-fail: [%d] both equal: 0x%04x", \
101 int32_t: "CHECK-fail: [%d] both equal: 0x%08x", \
102 uint32_t: "CHECK-fail: [%d] both equal: 0x%08x", \
103 int64_t: "CHECK-fail: [%d] both equal: 0x%016x", \
104 uint64_t: "CHECK-fail: [%d] both equal: 0x%016x")
121 #define CHECK_ARRAYS_IMPL(expect_eq_, actual_, ref_, size_, ...) \
123 static_assert(sizeof(*(actual_)) == sizeof(*(ref_)), \
124 "CHECK_ARRAYS requires arguments of equal size."); \
125 status_t sts_ = OK_STATUS(); \
126 volatile bool is_eq = \
127 memcmp((actual_), (ref_), size_ * sizeof(*(actual_))) == 0; \
128 if (is_eq != expect_eq_) { \
129 if (OT_VA_ARGS_COUNT(_, ##__VA_ARGS__) == 0) { \
130 LOG_INFO("CHECK-fail: " #actual_ " %smatches " #ref_, \
131 expect_eq_ ? " un" : " "); \
133 LOG_INFO("CHECK-fail: " __VA_ARGS__); \
135 for (size_t i = 0; i < size_; ++i) { \
137 LOG_INFO(SHOW_MISMATCH_FMT_STR_((actual_)[i]), i, (actual_)[i], \
140 LOG_INFO(SHOW_MATCH_FMT_STR_((actual_)[i]), i, (actual_)[i]); \
158 #define CHECK_ARRAYS_EQ(actual_, expected_, num_items_, ...) \
160 if (status_err(CHECK_ARRAYS_IMPL(true, actual_, expected_, num_items_, \
165 test_status_set(kTestStatusFailed); \
173 #define TRY_CHECK_ARRAYS_EQ(actual_, expected_, num_items_, ...) \
174 TRY(CHECK_ARRAYS_IMPL(true, actual_, expected_, num_items_, ##__VA_ARGS__))
180 #define TRY_CHECK_ARRAYS_NE(actual_, expected_, num_items_, ...) \
181 TRY(CHECK_ARRAYS_IMPL(false, actual_, expected_, num_items_, ##__VA_ARGS__))
193 #define CHECK_ARRAYS_NE(actual_, not_expected_, num_items_, ...) \
195 if (status_err(CHECK_ARRAYS_IMPL(false, actual_, not_expected_, \
196 num_items_, ##__VA_ARGS__))) { \
200 test_status_set(kTestStatusFailed); \
211 #define CHECK_STR_EQ(actual_, expected_) \
214 const char *expected = (expected_); \
215 const char *actual = (actual_); \
217 CHECK(actual[i] == expected[i], \
218 "Strings differ at char %d, so \"%s\" != \"%s\".", i, actual, \
221 } while (actual[i] != '\0' || expected[i] != '\0'); \
233 #define CHECK_DIF_OK(dif_call, ...) \
235 dif_result_t dif_result = dif_call; \
236 if (dif_result != kDifOk) { \
241 if (OT_VA_ARGS_COUNT(_, ##__VA_ARGS__) == 0) { \
242 LOG_ERROR("DIF-fail: " #dif_call " returns %d", dif_result); \
244 LOG_ERROR("DIF-fail: " __VA_ARGS__); \
251 test_status_set(kTestStatusFailed); \
268 #define _LOG_ERROR_STATUS_DV(expr) \
270 status_t status_ = expr; \
272 char mod_id[3] = {0}; \
274 if (status_extract(status_, &msg, &line, mod_id)) { \
275 LOG_ERROR("CHECK-STATUS-fail: %c%c%c:%d = %s", mod_id[0], mod_id[1], \
276 mod_id[2], line, msg); \
278 LOG_ERROR("CHECK-STATUS-fail: 0x%08x", status_); \
288 #define UNWRAP(expr, ...) \
290 status_t status_ = expr; \
291 if (!status_ok(status_)) { \
292 if (device_log_bypass_uart_address()) { \
293 _LOG_ERROR_STATUS_DV(status_); \
295 LOG_ERROR("CHECK-STATUS-fail: %r", status_); \
297 test_status_set(kTestStatusFailed); \
309 #define CHECK_STATUS_OK(expr, ...) \
311 status_t status_ = expr; \
312 if (!status_ok(status_)) { \
313 if (device_log_bypass_uart_address()) { \
314 _LOG_ERROR_STATUS_DV(status_); \
316 LOG_ERROR("CHECK-STATUS-fail: %r", status_); \
318 test_status_set(kTestStatusFailed); \
329 #define CHECK_STATUS_NOT_OK(expr, ...) \
331 status_t status_ = expr; \
332 if (status_ok(status_)) { \
333 if (device_log_bypass_uart_address()) { \
334 _LOG_ERROR_STATUS_DV(status_); \
336 LOG_ERROR("CHECK-STATUS-fail: %r", status_); \
338 test_status_set(kTestStatusFailed); \