1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-01-30 05:39:12 +02:00

Add ASSERT() that is preserved in production builds.

This commit is contained in:
David Steele 2018-03-30 19:10:34 -04:00
parent a8721ffe11
commit 635caff573
5 changed files with 31 additions and 7 deletions

View File

@ -30,6 +30,10 @@
<release-item>
<p>Start work on C handle io object and use it to output help.</p>
</release-item>
<release-item>
<p>Add <code>ASSERT()</code> that is preserved in production builds.</p>
</release-item>
</release-development-list>
</release-core-list>

View File

@ -15,15 +15,21 @@ NDEBUG indicates to C library routines that debugging is off -- set a more reada
/***********************************************************************************************************************************
Assert Macros
Used for assertions that should only be run when debugging. Ideal for conditions that are not likely to happen in production but
could occur during development.
***********************************************************************************************************************************/
// For very important asserts that are shipped with the production code.
#define ASSERT(condition) \
{ \
if (!(condition)) \
THROW(AssertError, "assertion '%s' failed", #condition); \
}
// Used for assertions that should only be run when debugging. Ideal for conditions that are not likely to happen in production but
// could occur during development.
#ifdef DEBUG
#define ASSERT_DEBUG(condition) \
{ \
if (!(condition)) \
THROW(AssertError, "assertion '%s' failed", #condition); \
THROW(AssertError, "debug assertion '%s' failed", #condition); \
}
#else
#define ASSERT_DEBUG(condition)

View File

@ -157,7 +157,7 @@ my $oTestDef =
},
{
&TESTDEF_NAME => 'debug-on',
&TESTDEF_TOTAL => 1,
&TESTDEF_TOTAL => 2,
&TESTDEF_C => true,
&TESTDEF_COVERAGE =>
@ -167,7 +167,7 @@ my $oTestDef =
},
{
&TESTDEF_NAME => 'debug-off',
&TESTDEF_TOTAL => 1,
&TESTDEF_TOTAL => 2,
&TESTDEF_C => true,
&TESTDEF_CDEF => '-DNDEBUG -DNO_LOG',

View File

@ -8,6 +8,13 @@ Test Run
void
testRun()
{
// -----------------------------------------------------------------------------------------------------------------------------
if (testBegin("ASSERT()"))
{
TEST_RESULT_VOID(ASSERT(true), "assert true");
TEST_ERROR(ASSERT(false || false), AssertError, "assertion 'false || false' failed");
}
// -----------------------------------------------------------------------------------------------------------------------------
if (testBegin("ASSERT_DEBUG()"))
{

View File

@ -8,10 +8,17 @@ Test Run
void
testRun()
{
// -----------------------------------------------------------------------------------------------------------------------------
if (testBegin("ASSERT()"))
{
TEST_RESULT_VOID(ASSERT(true), "assert true");
TEST_ERROR(ASSERT(false || false), AssertError, "assertion 'false || false' failed");
}
// -----------------------------------------------------------------------------------------------------------------------------
if (testBegin("ASSERT_DEBUG()"))
{
TEST_RESULT_VOID(ASSERT_DEBUG(true), "assert true");
TEST_ERROR(ASSERT_DEBUG(false || false), AssertError, "assertion 'false || false' failed");
TEST_ERROR(ASSERT_DEBUG(false || false), AssertError, "debug assertion 'false || false' failed");
}
}