...one of the most highly
regarded and expertly designed C++ library projects in the
world. — Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
Howiguously.
is_validresult in proper result code.
is_validinvocation, the program will crash.
The Unit Test Framework solves all these issues. To integrate with it above program needs to be changed to:
#include <my_class.hpp> #define
BOOST_TEST_MODULEMyTest #include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE( my_test ) { my_class test_object( "qwerty" ); BOOST_TEST( test_object.is_valid() ); }
Now, you not only receive uniform result code, even in case of exception,
but also nicely formatted output from
BOOST_TEST tool, would you choose
to see it. Is there any other ways to perform checks? The following example
test program shows several different ways to detect and report an error
in the
add()
function.
#define
BOOST_TEST_MODULEMyTest #include <boost/test/unit_test.hpp> int add( int i, int j ) { return i + j; }
BOOST_AUTO_TEST_CASE(my_test) { // six ways to detect and report the same error: // continues on error
BOOST_TEST( add(2, 2) == 4 );
// throws on error// throws on error
BOOST_TEST_REQUIRE( add(2, 2) == 4 );
//continues on error if (add(2, 2) != 4)//continues on error if (add(2, 2) != 4)
BOOST_ERROR( "Ouch..." );
// throws on error if (add(2, 2) != 4)// throws on error if (add(2, 2) != 4)
BOOST_FAIL( "Ouch..." );
// throws on error if (add(2, 2) != 4) throw "Ouch...";// throws on error if (add(2, 2) != 4) throw "Ouch...";
// continues on error// continues on error
BOOST_TEST( add(2, 2) == 4,
"2 plus 2 is not 4 but " << add(2, 2)); }"2 plus 2 is not 4 but " << add(2, 2)); }