Let SPIRV_ASSERT() exit the program if the assertion fails.

This commit is contained in:
Lei Zhang 2016-09-16 12:10:47 -04:00
parent 114af21994
commit 869440ebd4
2 changed files with 12 additions and 62 deletions

View File

@ -22,8 +22,9 @@
#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>);
@ -133,24 +134,30 @@ void Logf(const MessageConsumer& consumer, MessageLevel level, const char* file,
#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, ...) \

View File

@ -23,63 +23,6 @@ namespace {
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,