CppUnit project page | FAQ | CppUnit home page

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

Defines
Making assertions

## Defines
#define | CPPUNIT_ASSERT(condition)
| Assertions that a condition is `true`.
#define | CPPUNIT_ASSERT_MESSAGE(message, condition)
| Assertion with a user specified message.
#define | CPPUNIT_FAIL(message)
| Fails with the specified message.
#define | CPPUNIT_ASSERT_EQUAL(expected, actual)
| Asserts that two values are equals.
#define | CPPUNIT_ASSERT_EQUAL_MESSAGE(message, expected, actual)
| Asserts that two values are equals, provides additional message on failure.
#define | CPPUNIT_ASSERT_DOUBLES_EQUAL(expected, actual, delta)
| Macro for primitive double value comparisons.The assertion pass if both expected and actual are finite and `fabs`( `expected` - `actual` ) <= `delta`. If either `expected` or actual are infinite (+/- inf), the assertion pass if `expected` == `actual`. If either `expected` or `actual` is a NaN (not a number), then the assertion fails.
#define | CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message, expected, actual, delta)
| Macro for primitive double value comparisons, setting a user-supplied message in case of failure.
#define | CPPUNIT_ASSERT_THROW(expression, ExceptionType)
| Asserts that the given expression throws an exception of the specified type.
#define | CPPUNIT_ASSERT_THROW_MESSAGE(message, expression, ExceptionType)
| Asserts that the given expression throws an exception of the specified type, setting a user supplied message in case of failure.
#define | CPPUNIT_ASSERT_NO_THROW(expression)
| Asserts that the given expression does not throw any exceptions.
#define | CPPUNIT_ASSERT_NO_THROW_MESSAGE(message, expression)
| Asserts that the given expression does not throw any exceptions, setting a user supplied message in case of failure.
#define | CPPUNIT_ASSERT_ASSERTION_FAIL(assertion) CPPUNIT_ASSERT_THROW( assertion, CPPUNIT_NS::Exception )
| Asserts that an assertion fail.
#define | CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE(message, assertion) CPPUNIT_ASSERT_THROW_MESSAGE( message, assertion, CPPUNIT_NS::Exception )
| Asserts that an assertion fail, with a user-supplied message in case of error.
#define | CPPUNIT_ASSERT_ASSERTION_PASS(assertion) CPPUNIT_ASSERT_NO_THROW( assertion )
| Asserts that an assertion pass.
#define | CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE(message, assertion) CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, assertion )
| Asserts that an assertion pass, with a user-supplied message in case of failure.

## Define Documentation

#define CPPUNIT_ASSERT | ( |  | condition | ) |

Value:

    ( CPPUNIT_NS::Asserter::failIf( !(condition),                                   \
                                     CPPUNIT_NS::Message( "assertion failed",         \
                                                          "Expression: " #condition), \
                                     CPPUNIT_SOURCELINE() ) )

Assertions that a condition is `true`.

#define CPPUNIT_ASSERT_ASSERTION_FAIL | ( |  | assertion | ) | CPPUNIT_ASSERT_THROW( assertion, CPPUNIT_NS::Exception )

Asserts that an assertion fail.

Use to test assertions. Example of usage:

       CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT( 1 == 2 ) );

#define CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE | ( |  | message,
|  |  | assertion
| ) |  | CPPUNIT_ASSERT_THROW_MESSAGE( message, assertion, CPPUNIT_NS::Exception )

Asserts that an assertion fail, with a user-supplied message in case of error.

Use to test assertions. Example of usage:

       CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE( "1 == 2", CPPUNIT_ASSERT( 1 == 2 ) );

#define CPPUNIT_ASSERT_ASSERTION_PASS | ( |  | assertion | ) | CPPUNIT_ASSERT_NO_THROW( assertion )

Asserts that an assertion pass.

Use to test assertions. Example of usage:

       CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( 1 == 1 ) );

#define CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE | ( |  | message,
|  |  | assertion
| ) |  | CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, assertion )

Asserts that an assertion pass, with a user-supplied message in case of failure.

Use to test assertions. Example of usage:

       CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE( "1 != 1", CPPUNIT_ASSERT( 1 == 1 ) );

#define CPPUNIT_ASSERT_DOUBLES_EQUAL | ( |  | expected,
|  |  | actual,
|  |  | delta
| ) |  |

Value:

    ( CPPUNIT_NS::assertDoubleEquals( (expected),            \
                                        (actual),              \
                                        (delta),               \
                                        CPPUNIT_SOURCELINE(),  \
                                        "" ) )

Macro for primitive double value comparisons.The assertion pass if both expected and actual are finite and `fabs`( `expected` - `actual` ) <= `delta`. If either `expected` or actual are infinite (+/- inf), the assertion pass if `expected` == `actual`. If either `expected` or `actual` is a NaN (not a number), then the assertion fails.

#define CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE | ( |  | message,
|  |  | expected,
|  |  | actual,
|  |  | delta
| ) |  |

Value:

    ( CPPUNIT_NS::assertDoubleEquals( (expected),            \
                                        (actual),              \
                                        (delta),               \
                                        CPPUNIT_SOURCELINE(),  \
                                        (message) ) )

Macro for primitive double value comparisons, setting a user-supplied message in case of failure.

See also:
  CPPUNIT_ASSERT_DOUBLES_EQUAL for detailed semantic of the assertion.

#define CPPUNIT_ASSERT_EQUAL | ( |  | expected,
|  |  | actual
| ) |  |

Value:

    ( CPPUNIT_NS::assertEquals( (expected),              \
                                  (actual),                \
                                  CPPUNIT_SOURCELINE(),    \
                                  "" ) )

Asserts that two values are equals.

Equality and string representation can be defined with an appropriate CppUnit::assertion_traits class.

A diagnostic is printed if actual and expected values disagree.

Requirement for expected and actual parameters:

- They are exactly of the same type
- They are serializable into a std::strstream using operator <<.
- They can be compared using operator ==.

The last two requirements (serialization and comparison) can be removed by specializing the CppUnit::assertion_traits.

#define CPPUNIT_ASSERT_EQUAL_MESSAGE | ( |  | message,
|  |  | expected,
|  |  | actual
| ) |  |

Value:

    ( CPPUNIT_NS::assertEquals( (expected),              \
                                  (actual),                \
                                  CPPUNIT_SOURCELINE(),    \
                                  (message) ) )

Asserts that two values are equals, provides additional message on failure.

Equality and string representation can be defined with an appropriate assertion_traits class.

A diagnostic is printed if actual and expected values disagree. The message is printed in addition to the expected and actual value to provide additional information.

Requirement for expected and actual parameters:

- They are exactly of the same type
- They are serializable into a std::strstream using operator <<.
- They can be compared using operator ==.

The last two requirements (serialization and comparison) can be removed by specializing the CppUnit::assertion_traits.

#define CPPUNIT_ASSERT_MESSAGE | ( |  | message,
|  |  | condition
| ) |  |

Value:

    ( CPPUNIT_NS::Asserter::failIf( !(condition),                            \
                                      CPPUNIT_NS::Message( "assertion failed", \
                                                           "Expression: "      \
                                                           #condition,         \
                                                           message ),          \
                                      CPPUNIT_SOURCELINE() ) )

Assertion with a user specified message.

Parameters:

  message | Message reported in diagnostic if condition evaluates to `false`.
  condition | If this condition evaluates to `false` then the test failed.

#define CPPUNIT_ASSERT_NO_THROW | ( |  | expression | ) |

Value:

    CPPUNIT_ASSERT_NO_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(),       \
                                        expression )

Asserts that the given expression does not throw any exceptions.

Example of usage:

       std::vector<int> v;
       v.push_back( 10 );
      CPPUNIT_ASSERT_NO_THROW( v.at( 0 ) );

#define CPPUNIT_ASSERT_NO_THROW_MESSAGE | ( |  | message,
|  |  | expression
| ) |  |

Value:

    do {                                                                       \
          CPPUNIT_NS::Message cpputMsg_( "unexpected exception caught" );         \
          cpputMsg_.addDetail( message );                                         \
                                                                                  \
          try {                                                                   \
             expression;                                                          \
          } catch ( const std::exception &e ) {                                   \
             cpputMsg_.addDetail( "Caught: " +                                    \
                                  CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e,             \
                                              "std::exception or derived" ) );    \
             cpputMsg_.addDetail( std::string("What(): ") + e.what() );           \
             CPPUNIT_NS::Asserter::fail( cpputMsg_,                               \
                                         CPPUNIT_SOURCELINE() );                  \
          } catch ( ... ) {                                                       \
             cpputMsg_.addDetail( "Caught: unknown." );                           \
             CPPUNIT_NS::Asserter::fail( cpputMsg_,                               \
                                         CPPUNIT_SOURCELINE() );                  \
          }                                                                       \
       } while ( false )

Asserts that the given expression does not throw any exceptions, setting a user supplied message in case of failure.

Example of usage:

       std::vector<int> v;
       v.push_back( 10 );
      CPPUNIT_ASSERT_NO_THROW( "std::vector<int> v;", v.at( 0 ) );

#define CPPUNIT_ASSERT_THROW | ( |  | expression,
|  |  | ExceptionType
| ) |  |

Value:

    CPPUNIT_ASSERT_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(),       \
                                     expression,                            \
                                     ExceptionType )

Asserts that the given expression throws an exception of the specified type.

Example of usage:

       std::vector<int> v;
      CPPUNIT_ASSERT_THROW( v.at( 50 ), std::out_of_range );

#define CPPUNIT_ASSERT_THROW_MESSAGE | ( |  | message,
|  |  | expression,
|  |  | ExceptionType
| ) |  |

Value:

    do {                                                                       \
          bool cpputCorrectExceptionThrown_ = false;                              \
          CPPUNIT_NS::Message cpputMsg_( "expected exception not thrown" );       \
          cpputMsg_.addDetail( message );                                         \
          cpputMsg_.addDetail( "Expected: "                                       \
                               CPPUNIT_GET_PARAMETER_STRING( ExceptionType ) );   \
                                                                                  \
          try {                                                                   \
             expression;                                                          \
          } catch ( const ExceptionType & ) {                                     \
             cpputCorrectExceptionThrown_ = true;                                 \
          } catch ( const std::exception &e) {                                    \
             cpputMsg_.addDetail( "Actual  : " +                                  \
                                  CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e,             \
                                              "std::exception or derived") );     \
             cpputMsg_.addDetail( std::string("What()  : ") + e.what() );         \
          } catch ( ... ) {                                                       \
             cpputMsg_.addDetail( "Actual  : unknown.");                          \
          }                                                                       \
                                                                                  \
          if ( cpputCorrectExceptionThrown_ )                                     \
             break;                                                               \
                                                                                  \
          CPPUNIT_NS::Asserter::fail( cpputMsg_,                                  \
                                      CPPUNIT_SOURCELINE() );                     \
       } while ( false )

Asserts that the given expression throws an exception of the specified type, setting a user supplied message in case of failure.

Example of usage:

       std::vector<int> v;
      CPPUNIT_ASSERT_THROW_MESSAGE( "- std::vector<int> v;", v.at( 50 ), std::out_of_range );

#define CPPUNIT_FAIL | ( |  | message | ) |

Value:

    ( CPPUNIT_NS::Asserter::fail( CPPUNIT_NS::Message( "forced failure",  \
                                                         message ),         \
                                    CPPUNIT_SOURCELINE() ) )

Fails with the specified message.

Parameters:

  message | Message reported in diagnostic.

| hosts this site. |  | Send comments to: CppUnit Developers