Utility: add mini-assert library
This commit is contained in:
parent
e554d30bf6
commit
c0a622c5e4
@ -24,7 +24,11 @@ add_executable(tests
|
|||||||
rand.cpp
|
rand.cpp
|
||||||
fixedpoint.cpp
|
fixedpoint.cpp
|
||||||
pixelbuffer.cpp
|
pixelbuffer.cpp
|
||||||
pixelbuffer_effects.cpp function.cpp)
|
pixelbuffer_effects.cpp
|
||||||
|
function.cpp
|
||||||
|
assert_ndebug.cpp
|
||||||
|
assert.cpp
|
||||||
|
)
|
||||||
|
|
||||||
target_link_libraries(tests
|
target_link_libraries(tests
|
||||||
PUBLIC
|
PUBLIC
|
||||||
|
|||||||
68
Tests/assert.cpp
Normal file
68
Tests/assert.cpp
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
//
|
||||||
|
// Created by erki on 29.06.22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <catch2/catch.hpp>
|
||||||
|
|
||||||
|
#ifdef NDEBUG
|
||||||
|
# undef NDEBUG
|
||||||
|
# define NDEBUG_WAS_SET
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "utility_assert.hpp"
|
||||||
|
|
||||||
|
#ifdef NDEBUG_WAS_SET
|
||||||
|
# define NDEBUG
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
bool assert_flag = false;
|
||||||
|
|
||||||
|
void assertCallback(const char*, const int)
|
||||||
|
{
|
||||||
|
assert_flag = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Assert debug without NDEBUG gets triggered if check is false.", "[utility],[assert]")
|
||||||
|
{
|
||||||
|
assert_flag = false;
|
||||||
|
Utility::Assert::setHandler(assertCallback);
|
||||||
|
|
||||||
|
SKULLC_ASSERT_DEBUG(false);
|
||||||
|
|
||||||
|
CHECK(assert_flag == true);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Assert debug without NDEBUG doesn't get triggered if check is true.", "[utility],[assert]")
|
||||||
|
{
|
||||||
|
assert_flag = false;
|
||||||
|
Utility::Assert::setHandler(assertCallback);
|
||||||
|
|
||||||
|
SKULLC_ASSERT_DEBUG(true);
|
||||||
|
|
||||||
|
CHECK(assert_flag == false);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Assert safe without NDEBUG gets triggered if check is false.", "[utility],[assert]")
|
||||||
|
{
|
||||||
|
assert_flag = false;
|
||||||
|
Utility::Assert::setHandler(assertCallback);
|
||||||
|
|
||||||
|
SKULLC_ASSERT_SAFE(false);
|
||||||
|
|
||||||
|
CHECK(assert_flag == true);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Assert safe without 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);
|
||||||
|
}
|
||||||
62
Tests/assert_ndebug.cpp
Normal file
62
Tests/assert_ndebug.cpp
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
//
|
||||||
|
// 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 int)
|
||||||
|
{
|
||||||
|
assert_flag = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
|
|||||||
add_library(utility STATIC
|
add_library(utility STATIC
|
||||||
Src/utility_logging.cpp
|
Src/utility_logging.cpp
|
||||||
Src/utility_rand.cpp
|
Src/utility_rand.cpp
|
||||||
|
Src/utility_assert.cpp
|
||||||
${additional_sources}
|
${additional_sources}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
33
Utility/Inc/utility_assert.hpp
Normal file
33
Utility/Inc/utility_assert.hpp
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
//
|
||||||
|
// Created by erki on 29.06.22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef SKULLC_UTILITY_ASSERT_HPP_
|
||||||
|
#define SKULLC_UTILITY_ASSERT_HPP_
|
||||||
|
|
||||||
|
namespace Utility::Assert::Detail
|
||||||
|
{
|
||||||
|
|
||||||
|
void assertImpl(const bool expr, const char* file, const int line);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef NDEBUG
|
||||||
|
# define SKULLC_ASSERT_DEBUG(e) Utility::Assert::Detail::assertImpl(e, __FILE__, __LINE__)
|
||||||
|
#else
|
||||||
|
# define SKULLC_ASSERT_DEBUG(e)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define SKULLC_ASSERT_SAFE(e) Utility::Assert::Detail::assertImpl(e, __FILE__, __LINE__)
|
||||||
|
|
||||||
|
namespace Utility::Assert
|
||||||
|
{
|
||||||
|
|
||||||
|
using assert_cb = void (*)(const char* file, const int line);
|
||||||
|
|
||||||
|
void setHandler(assert_cb callback);
|
||||||
|
assert_cb getHandler();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // SKULLC_UTILITY_ASSERT_HPP_
|
||||||
@ -9,4 +9,4 @@
|
|||||||
#define SKULLC_CONCAT(x, y) SKULLC_CONCAT_IMPL(x, y)
|
#define SKULLC_CONCAT(x, y) SKULLC_CONCAT_IMPL(x, y)
|
||||||
#define SKULLC_TAG struct SKULLC_CONCAT(SkullCTag_, __COUNTER__)
|
#define SKULLC_TAG struct SKULLC_CONCAT(SkullCTag_, __COUNTER__)
|
||||||
|
|
||||||
#endif// SKULLC_UTILITY_TAG_HPP_
|
#endif // SKULLC_UTILITY_TAG_HPP_
|
||||||
|
|||||||
45
Utility/Src/utility_assert.cpp
Normal file
45
Utility/Src/utility_assert.cpp
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
//
|
||||||
|
// Created by erki on 29.06.22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "utility_assert.hpp"
|
||||||
|
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
Utility::Assert::assert_cb INSTALLED_HANDLER = nullptr;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace Utility::Assert
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace Detail
|
||||||
|
{
|
||||||
|
|
||||||
|
void assertImpl(const bool expr, const char* file, const int line)
|
||||||
|
{
|
||||||
|
if (!expr)
|
||||||
|
{
|
||||||
|
if (INSTALLED_HANDLER)
|
||||||
|
INSTALLED_HANDLER(file, line);
|
||||||
|
else
|
||||||
|
std::terminate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void setHandler(assert_cb callback)
|
||||||
|
{
|
||||||
|
INSTALLED_HANDLER = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_cb getHandler()
|
||||||
|
{
|
||||||
|
return INSTALLED_HANDLER;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user