Skip to content

Commit

Permalink
Add ASSERT() that is preserved in production builds.
Browse files Browse the repository at this point in the history
  • Loading branch information
dwsteele committed Mar 30, 2018
1 parent a8721ff commit 635caff
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
4 changes: 4 additions & 0 deletions doc/xml/release.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
14 changes: 10 additions & 4 deletions src/common/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions test/lib/pgBackRestTest/Common/DefineTest.pm
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ my $oTestDef =
},
{
&TESTDEF_NAME => 'debug-on',
&TESTDEF_TOTAL => 1,
&TESTDEF_TOTAL => 2,
&TESTDEF_C => true,

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

Expand Down
7 changes: 7 additions & 0 deletions test/src/module/common/debugOffTest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()"))
{
Expand Down
9 changes: 8 additions & 1 deletion test/src/module/common/debugOnTest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit 635caff

Please sign in to comment.