mirror of
https://github.com/KhronosGroup/SPIRV-Tools
synced 2024-10-18 19:20:05 +00:00
Let SPIRV_ASSERT() exit the program if the assertion fails.
This commit is contained in:
parent
114af21994
commit
869440ebd4
@ -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, ...) \
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user