BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsls_fuzztestpreconditionexception

Detailed Description

Outline

Purpose

Provide an exception type for handling failed preconditions.

Classes

See also
bsls_fuzztest

Description

This component implements an exception class, bsls::FuzzTestPreconditionException, that provides a mechanism to convey context information from a failing precondition to a test handler. The context that is captured consists of the program source of the failing expression, the name of the file containing the assertion, the line number within that file where the asserted expression may be found, and the level of the assertion that has failed.

Usage

First we write a macro to act as a precondition testing assert facility that will throw an exception of type bsls::FuzzTestPreconditionException if the asserted expression fails. The thrown exception will capture the source-code of the expression, the filename and line number of the failing expression.

#define TEST_PRECONDITION(EXPRESSION) \$
if (!(EXPRESSION)) { \$
bsls::AssertViolation violation(#EXPRESSION, \$
__FILE__, \$
__LINE__, \$
"LEVEL"); \$
}
Definition bsls_assert.h:1929
Definition bsls_fuzztestpreconditionexception.h:118

Next we use the macro inside a try-block, so that we can catch the exception thrown if the tested expression fails.

try {
void *p = NULL;
TEST_PRECONDITION(0 != p);
}

If the assertion fails, catch the exception and confirm that it correctly recorded the context of where the assertion failed.

catch (const bsls::FuzzTestPreconditionException& exception) {
assert(0 == strcmp("0 != p",
exception.assertViolation().comment()));
assert(0 == strcmp(__FILE__,
exception.assertViolation().fileName()));
assert(11 == __LINE__ - exception.assertViolation().lineNumber());
assert(0 == strcmp("LEVEL",
exception.assertViolation().assertLevel()));
}
int lineNumber() const
Return the lineNumber attribute of this object.
Definition bsls_assert.h:2354
const char * comment() const
Return the comment attribute of this object.
Definition bsls_assert.h:2342
const char * assertLevel() const
Return the assertLevel attribute of this object.
Definition bsls_assert.h:2336
const char * fileName() const
Return the fileName attribute of this object.
Definition bsls_assert.h:2348
const AssertViolation & assertViolation() const
Definition bsls_fuzztestpreconditionexception.h:182