lightweight_test

| Home | Libraries | People | FAQ | More

## lightweight_test

### Authors

- Peter Dimov
- Beman Dawes

### Header <boost/core/lightweight_test.hpp>

The header `<boost/core/lightweight_test.hpp>` is a lightweight test framework. It's useful for writing Boost regression tests for components that are dependencies of Boost.Test.

When using `lightweight_test.hpp`, do not forget to `return boost::report_errors()` from `main`.

#### Synopsis

    #define BOOST_TEST(expression) /*unspecified*/
    #define BOOST_ERROR(message) /*unspecified*/
    #define BOOST_TEST_EQ(expr1, expr2) /*unspecified*/
    #define BOOST_TEST_NE(expr1, expr2) /*unspecified*/
    #define BOOST_TEST_THROWS(expr, excep) /*unspecified*/

    namespace boost
    {
        int report_errors();
    }

#### BOOST_TEST

    BOOST_TEST(expression)

If expression is false increases the error count and outputs a message containing `expression`.

#### BOOST_ERROR

    BOOST_ERROR(message)

Increases error count and outputs a message containing `message`.

#### BOOST_TEST_EQ

    BOOST_TEST_EQ(expr1, expr2)

If `expr1 != expr2` increases the error count and outputs a message containing both expressions.

#### BOOST_TEST_NE

    BOOST_TEST_NE(expr1, expr2)

If `expr1 == expr2` increases the error count and outputs a message containing both expressions.

#### BOOST_TEST_THROWS

    BOOST_TEST_THROWS(expr, excep)

If `BOOST_NO_EXCEPTIONS` is not defined and if `expr` does not throw an exception of type `excep`, increases the error count and outputs a message containing the expression.

If `BOOST_NO_EXCEPTIONS` is defined, this macro expands to nothing and `expr` is not evaluated.

#### report_errors

    int boost::report_errors()

Return the error count from `main`.

#### Example

    #include <boost/core/lightweight_test.hpp>

    int sqr( int x )
    {
        return x * x;
    }

    int main()
    {
        BOOST_TEST( sqr(2) == 4 );
        BOOST_TEST_EQ( sqr(-3), 9 );

        return boost::report_errors();
    }

### Header <boost/core/lightweight_test_trait.hpp>

The header `<boost/core/lightweight_test_trait.hpp>` defines a couple of extra macros for testing compile-time traits that return a boolean value.

#### Synopsis

    #define BOOST_TEST_TRAIT_TRUE((Trait)) /*unspecified*/
    #define BOOST_TEST_TRAIT_FALSE((Trait)) /*unspecified*/

#### BOOST_TEST_TRAIT_TRUE

    BOOST_TEST_TRAIT_TRUE((Trait))

If `Trait::value != true` increases the error count and outputs a message containing `Trait`. Note the double set of parentheses; these enable `Trait` to contain a comma, which is common for templates.

#### BOOST_TEST_TRAIT_FALSE

    BOOST_TEST_TRAIT_FALSE((Trait))

If `Trait::value != false` increases the error count and outputs a message containing `Trait`. Note the double set of parentheses.

#### Example

    #include <boost/core/lightweight_test_trait.hpp>
    #include <boost/core/is_same.hpp>

    template<class T, class U> struct X
    {
        typedef T type;
    };

    using boost::core::is_same;

    int main()
    {
        BOOST_TEST_TRAIT_TRUE(( is_same<X<int, long>::type, int> ));

        return boost::report_errors();
    }