#include "message.h"
-// Asserts the given condition is true. Otherwise, send a message to the
-// consumer. Accepts the following formats:
+// Asserts the given condition is true. Otherwise, sends a message to the
+// consumer and exits the problem with failure code. Accepts the following
+// formats:
//
// SPIRV_ASSERT(<message-consumer>, <condition-expression>);
// SPIRV_ASSERT(<message-consumer>, <condition-expression>, <message>);
#define SPIRV_ASSERT_1(consumer, condition) \
do { \
- if (!(condition)) \
+ if (!(condition)) { \
spvtools::Log(consumer, MessageLevel::InternalError, __FILE__, \
{__LINE__, 0, 0}, "assertion failed: " #condition); \
+ std::exit(EXIT_FAILURE); \
+ } \
} while (0)
#define SPIRV_ASSERT_2(consumer, condition, message) \
do { \
- if (!(condition)) \
+ if (!(condition)) { \
spvtools::Log(consumer, MessageLevel::InternalError, __FILE__, \
{__LINE__, 0, 0}, "assertion failed: " message); \
+ std::exit(EXIT_FAILURE); \
+ } \
} while (0)
#define SPIRV_ASSERT_more(consumer, condition, format, ...) \
do { \
- if (!(condition)) \
+ if (!(condition)) { \
spvtools::Logf(consumer, MessageLevel::InternalError, __FILE__, \
{__LINE__, 0, 0}, "assertion failed: " format, \
__VA_ARGS__); \
+ std::exit(EXIT_FAILURE); \
+ } \
} while (0)
#define SPIRV_ASSERT_3(consumer, condition, format, ...) \
using namespace spvtools;
using ::testing::MatchesRegex;
-TEST(Log, AssertStatement) {
- int invocation = 0;
- auto consumer = [&invocation](MessageLevel level, const char* source,
- const spv_position_t&, const char* message) {
- ++invocation;
- EXPECT_EQ(MessageLevel::InternalError, level);
- EXPECT_THAT(source, MatchesRegex(".*test_log.cpp$"));
- EXPECT_STREQ("assertion failed: 1 + 2 == 5", message);
- };
-
- SPIRV_ASSERT(consumer, 1 + 2 == 5);
-#if defined(NDEBUG)
- (void)consumer;
- EXPECT_EQ(0, invocation);
-#else
- EXPECT_EQ(1, invocation);
-#endif
-}
-
-TEST(Log, AssertMessage) {
- int invocation = 0;
- auto consumer = [&invocation](MessageLevel level, const char* source,
- const spv_position_t&, const char* message) {
- ++invocation;
- EXPECT_EQ(MessageLevel::InternalError, level);
- EXPECT_THAT(source, MatchesRegex(".*test_log.cpp$"));
- EXPECT_STREQ("assertion failed: happy asserting!", message);
- };
-
- SPIRV_ASSERT(consumer, 1 + 2 == 5, "happy asserting!");
-#if defined(NDEBUG)
- (void)consumer;
- EXPECT_EQ(0, invocation);
-#else
- EXPECT_EQ(1, invocation);
-#endif
-}
-
-TEST(Log, AssertFormattedMessage) {
- int invocation = 0;
- auto consumer = [&invocation](MessageLevel level, const char* source,
- const spv_position_t&, const char* message) {
- ++invocation;
- EXPECT_EQ(MessageLevel::InternalError, level);
- EXPECT_THAT(source, MatchesRegex(".*test_log.cpp$"));
- EXPECT_STREQ("assertion failed: 1 + 2 actually is 3", message);
- };
-
- SPIRV_ASSERT(consumer, 1 + 2 == 5, "1 + 2 actually is %d", 1 + 2);
-#if defined(NDEBUG)
- (void)consumer;
- EXPECT_EQ(0, invocation);
-#else
- EXPECT_EQ(1, invocation);
-#endif
-}
-
TEST(Log, Unimplemented) {
int invocation = 0;
auto consumer = [&invocation](MessageLevel level, const char* source,