skullc-peripherals/Tests/assert_ndebug.cpp

63 lines
1.1 KiB
C++

//
// Created by erki on 29.06.22.
//
#include <catch2/catch.hpp>
#ifndef NDEBUG
#define NDEBUG
#define NDEBUG_WAS_NOT_DEFINED
#endif
#include "utility_assert.hpp"
#ifdef NDEBUG_WAS_NOT_DEFINED
#undef NDEBUG
#endif
namespace
{
bool assert_flag = false;
void assertCallback(const char*, const char*, const int)
{
assert_flag = true;
}
}// namespace
TEST_CASE("Assert debug with NDEBUG is a null-opt.", "[utility],[assert]")
{
assert_flag = false;
Utility::Assert::setHandler(assertCallback);
int check_number = 0;
SKULLC_ASSERT_DEBUG(check_number++ == 0);
CHECK(check_number == 0);
CHECK(assert_flag == false);
}
TEST_CASE("Assert safe with NDEBUG gets triggered if check is false.", "[utility],[assert]")
{
assert_flag = false;
Utility::Assert::setHandler(assertCallback);
int check_number = 0;
SKULLC_ASSERT_SAFE(check_number++ == 1);
CHECK(check_number == 1);
CHECK(assert_flag == true);
}
TEST_CASE("Assert safe with NDEBUG doesn't get triggered if check is true.", "[utility],[assert]")
{
assert_flag = false;
Utility::Assert::setHandler(assertCallback);
SKULLC_ASSERT_SAFE(true);
CHECK(assert_flag == false);
}