Utility: add mini-assert library
All checks were successful
continuous-integration/drone/push Build is passing
gitea/skullc-peripherals/pipeline/head This commit looks good

This commit is contained in:
Erki 2022-06-29 01:00:45 +03:00
parent e554d30bf6
commit c0a622c5e4
7 changed files with 215 additions and 2 deletions

View File

@ -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
View 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
View 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);
}

View File

@ -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}
) )

View 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_

View File

@ -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_

View 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;
}
}