|
32 | 32 | #endif |
33 | 33 |
|
34 | 34 | #include <os.hpp> |
35 | | -inline void __expect_fail(const char *expr, const char *file, int line, const char *func){ |
| 35 | +inline void __expect_emit_failure(std::string_view msg, std::string_view panic_text) { |
36 | 36 | #ifndef UNITTESTS |
37 | 37 | #ifdef INCLUDEOS_SMP_ENABLE |
38 | 38 | SMP::global_lock(); |
39 | 39 | #endif |
40 | | - fprintf(stderr, "%s:%i:%s %s \n",file, line, func, expr); |
| 40 | + std::fprintf(stderr, "%.*s\n", int(msg.size()), msg.data()); |
41 | 41 | fflush(NULL); |
42 | 42 | #ifdef INCLUDEOS_SMP_ENABLE |
43 | 43 | SMP::global_unlock(); |
44 | 44 | #endif |
45 | | - os::panic(expr); |
| 45 | + os::panic(std::string(panic_text).c_str()); |
46 | 46 | #else // TEST |
| 47 | + (void) panic_text; |
47 | 48 | // throw here to allow tests to capture the error |
48 | 49 | #include <stdexcept> |
49 | | - #include <format> |
50 | | - auto msg = std::format("{}:{}:{} {}",file, line, func, expr); |
51 | | - throw std::runtime_error(msg); |
| 50 | + throw std::runtime_error(std::string(msg)); |
52 | 51 | #endif |
53 | 52 | } |
54 | 53 |
|
55 | | -#define Expects(x) ((void)((x) || (__expect_fail("Expects failed: "#x, __FILE__, __LINE__, __func__),0))) |
56 | | -#define Ensures(x) ((void)((x) || (__expect_fail("Ensures failed: "#x, __FILE__, __LINE__, __func__),0))) |
| 54 | +template <class... Args> |
| 55 | +inline void __expect_failf(const char *err_prefix, const char * /*cond*/, const char *file, int line, const char *func, std::format_string<Args...> fmt, Args&&... args){ |
| 56 | + auto reason_msg = std::format(fmt, std::forward<Args>(args)...); |
| 57 | + auto error_msg = std::format("{}:{}:{}: {}: {}", file, line, func, err_prefix, reason_msg); |
| 58 | + __expect_emit_failure(error_msg, reason_msg); |
| 59 | +} |
| 60 | + |
| 61 | +inline void __expect_failf(const char *err_prefix, const char *cond, const char *file, int line, const char *func){ |
| 62 | + auto reason_msg = std::format("{}: {}", err_prefix, cond); |
| 63 | + auto error_msg = std::format("{}:{}:{}: {}", file, line, func, err_prefix); |
| 64 | + __expect_emit_failure(error_msg, reason_msg); |
| 65 | +} |
| 66 | + |
| 67 | +#define Expects(cond) ((void)((cond) || (__expect_failf("Expects failed", #cond, __FILE__, __LINE__, __func__),0))) |
| 68 | +#define Ensures(cond) ((void)((cond) || (__expect_failf("Ensures failed", #cond, __FILE__, __LINE__, __func__),0))) |
| 69 | + |
| 70 | +#define Expectsf(cond, fmt, ...) ((void)((cond) || (__expect_failf("Expects failed", #cond, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__),0))) |
| 71 | +#define Ensuresf(cond, fmt, ...) ((void)((cond) || (__expect_failf("Ensures failed", #cond, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__),0))) |
57 | 72 |
|
58 | 73 | namespace os { |
59 | 74 | // parameter for noexcept specifier when bypassing noexcept for testing |
|
0 commit comments