Skip to content

Commit

Permalink
[C++] Add a test for evaluation of ARROW_CHECK payload
Browse files Browse the repository at this point in the history
We want to make sure that the error description payload for ARROW_CHECK is only evaluated if the check fails.
  • Loading branch information
pitrou committed Jul 11, 2023
1 parent a9035cb commit 21fb1cb
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cpp/src/arrow/util/logging_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,33 @@ TEST(PrintLogTest, LogTestWithInit) {
ArrowLog::ShutDownArrowLog();
}

struct LoggingTracer {
mutable bool was_printed = false;

friend std::ostream& operator<<(std::ostream& os, const LoggingTracer& x) {
x.was_printed = true;
return os;
}
};

TEST(ArrowCheck, PayloadNotEvaluatedOnSuccess) {
volatile bool cond = true;
LoggingTracer tracer;

ARROW_CHECK_OR_LOG(cond, WARNING) << "Some message" << tracer;
ASSERT_FALSE(tracer.was_printed);
}

TEST(ArrowCheck, PayloadEvaluatedOnFailure) {
volatile bool cond = false;
LoggingTracer tracer;

// Have to use a log level that actually gets printed, otherwise `operator<<`
// isn't called (which is good except for this test).
ARROW_CHECK_OR_LOG(cond, WARNING) << "Some message" << tracer;
ASSERT_TRUE(tracer.was_printed);
}

} // namespace util

TEST(DcheckMacros, DoNotEvaluateReleaseMode) {
Expand Down

0 comments on commit 21fb1cb

Please sign in to comment.