Files
Library/src/Testing/Assert.cpp
T

59 lines
1.5 KiB
C++

#include <Void/Testing/Assert.hpp>
#include <atomic>
namespace Void::Testing::Detail
{
namespace
{
std::atomic<std::size_t> g_stray_assertions{0};
} // namespace
std::size_t stray_assertion_count() noexcept
{
return g_stray_assertions.load(std::memory_order_acquire);
}
void reset_stray_assertion_count() noexcept
{
g_stray_assertions.store(0, std::memory_order_release);
}
void handle_assertion(AssertionResult result)
{
auto& ctx = TestContext::current();
if (!ctx.current_result) {
g_stray_assertions.fetch_add(1, std::memory_order_acq_rel);
return;
}
TestResult* tr = ctx.current_result;
const bool passed = result.passed;
const AssertKind kind = result.kind;
std::string abort_msg = result.message;
if (!passed) {
tr->state = TestState::Failed;
}
const std::size_t entry_bytes =
result.expression.size() +
result.expected_repr.size() +
result.actual_repr.size() +
result.message.size() + 64;
if (tr->assertions.size() < kMaxRetainedAssertionsPerTest &&
tr->diagnostic_bytes + entry_bytes <= kMaxDiagnosticBytesPerTest) {
tr->diagnostic_bytes += entry_bytes;
tr->assertions.push_back(std::move(result));
} else {
tr->assertions_truncated = true;
tr->dropped_assertions += 1;
}
if (!passed && (kind == AssertKind::Require || kind == AssertKind::Assert)) {
ctx.should_abort = true;
throw RequireFailure{abort_msg};
}
}
} // namespace Void::Testing::Detail