From 3d15ec911fa90241bf027172cedea90af584805b Mon Sep 17 00:00:00 2001 From: Gennadiy Rozental Date: Wed, 14 Dec 2005 06:06:14 +0000 Subject: [PATCH] unit test examples reworked [SVN r32050] --- example/Jamfile | 54 ++++--- example/auto_unit_test_example4.cpp | 25 --- example/auto_unit_test_example5.cpp | 21 --- example/auto_unit_test_example6.cpp | 19 --- example/est_example1.cpp | 56 +++++++ example/est_example2.cpp | 80 ++++++++++ example/exec_mon_example.cpp | 3 +- example/logged_exp_example.cpp | 150 ++++++++++++++++++ example/prg_exec_example.cpp | 2 + example/test_exec_example.cpp | 2 +- example/unit_test_example1.cpp | 33 ---- example/unit_test_example2.cpp | 42 ----- example/unit_test_example_01.cpp | 39 +++++ example/unit_test_example_02.cpp | 44 +++++ example/unit_test_example_03.cpp | 57 +++++++ ..._example2.cpp => unit_test_example_04.cpp} | 12 +- ..._example3.cpp => unit_test_example_05.cpp} | 17 +- example/unit_test_example_06.cpp | 44 +++++ example/unit_test_example_07.cpp | 43 +++++ ..._example1.cpp => unit_test_example_08.cpp} | 20 +-- example/unit_test_example_09_1.cpp | 34 ++++ example/unit_test_example_09_2.cpp | 34 ++++ ..._example3.cpp => unit_test_example_10.cpp} | 29 ++-- ..._example4.cpp => unit_test_example_11.cpp} | 17 +- ..._example5.cpp => unit_test_example_12.cpp} | 40 ++--- ...mple5.input => unit_test_example_12.input} | 0 26 files changed, 696 insertions(+), 221 deletions(-) delete mode 100755 example/auto_unit_test_example4.cpp delete mode 100755 example/auto_unit_test_example5.cpp delete mode 100755 example/auto_unit_test_example6.cpp create mode 100644 example/est_example1.cpp create mode 100644 example/est_example2.cpp create mode 100644 example/logged_exp_example.cpp delete mode 100644 example/unit_test_example1.cpp delete mode 100644 example/unit_test_example2.cpp create mode 100644 example/unit_test_example_01.cpp create mode 100644 example/unit_test_example_02.cpp create mode 100644 example/unit_test_example_03.cpp rename example/{auto_unit_test_example2.cpp => unit_test_example_04.cpp} (80%) mode change 100755 => 100644 rename example/{auto_unit_test_example3.cpp => unit_test_example_05.cpp} (55%) mode change 100755 => 100644 create mode 100644 example/unit_test_example_06.cpp create mode 100644 example/unit_test_example_07.cpp rename example/{auto_unit_test_example1.cpp => unit_test_example_08.cpp} (71%) mode change 100755 => 100644 create mode 100644 example/unit_test_example_09_1.cpp create mode 100644 example/unit_test_example_09_2.cpp rename example/{unit_test_example3.cpp => unit_test_example_10.cpp} (84%) rename example/{unit_test_example4.cpp => unit_test_example_11.cpp} (55%) rename example/{unit_test_example5.cpp => unit_test_example_12.cpp} (84%) rename example/{unit_test_example5.input => unit_test_example_12.input} (100%) diff --git a/example/Jamfile b/example/Jamfile index 7785ba52..a90a5f2c 100644 --- a/example/Jamfile +++ b/example/Jamfile @@ -13,37 +13,49 @@ import testing ; # Make tests run by default. DEPENDS all : test ; -rule boost-test-example ( example-name : lib-name ) +rule boost-test-example ( example-name : test-rule : lib-name ? : source_files * : use-dll ? ) { - return [ compile $(example-name).cpp ../build/$(lib-name) - : on <*>-w-8080 + source_files ?= $(example-name).cpp ; + local library = [ cond $(use-dll) : ../build/$(lib-name) : ../build/$(lib-name) ] ; + + return [ $(test-rule) $(source_files) [ cond $(lib-name) : $(library) ] + : #args + : #input-files + : #requirements + on + <*>-w-8080 + [ cond $(use-dll) : BOOST_TEST_DYN_LINK=1 ] : $(example-name) ] ; } test-suite "boost_test_examples" -: [ boost-test-example exec_mon_example : boost_prg_exec_monitor ] - [ boost-test-example prg_exec_example : boost_prg_exec_monitor ] +: [ boost-test-example exec_mon_example : run : boost_prg_exec_monitor ] + [ boost-test-example prg_exec_example : run : boost_prg_exec_monitor ] - [ boost-test-example test_exec_example : boost_test_exec_monitor ] + [ boost-test-example test_exec_example : run : boost_test_exec_monitor ] - [ boost-test-example unit_test_example1 : boost_unit_test_framework ] - [ boost-test-example unit_test_example2 : boost_unit_test_framework ] - [ boost-test-example unit_test_example3 : boost_unit_test_framework ] - [ boost-test-example unit_test_example4 : boost_unit_test_framework ] - [ boost-test-example unit_test_example5 : boost_unit_test_framework ] + [ boost-test-example test_case_template_example : run : boost_unit_test_framework ] - [ boost-test-example test_case_template_example : boost_unit_test_framework ] + [ boost-test-example unit_test_example_01 : run-fail : boost_unit_test_framework ] + [ boost-test-example unit_test_example_02 : run : boost_unit_test_framework ] + [ boost-test-example unit_test_example_03 : run : boost_unit_test_framework ] + [ boost-test-example unit_test_example_04 : run-fail : boost_unit_test_framework ] + [ boost-test-example unit_test_example_05 : run-fail : boost_unit_test_framework ] + [ boost-test-example unit_test_example_06 : run-fail : boost_unit_test_framework ] + [ boost-test-example unit_test_example_07 : run : boost_unit_test_framework ] + [ boost-test-example unit_test_example_08 : run : boost_unit_test_framework ] + [ boost-test-example unit_test_example_09 : run : boost_unit_test_framework : unit_test_example_09_1.cpp unit_test_example_09_2.cpp ] + [ boost-test-example unit_test_example_10 : run-fail : boost_unit_test_framework ] + [ boost-test-example unit_test_example_11 : run-fail : boost_unit_test_framework ] +# [ boost-test-example unit_test_example_12 : run : boost_unit_test_framework ] - [ boost-test-example named_param_example : boost_unit_test_framework ] + [ boost-test-example est_example1 : run : boost_unit_test_framework ] + [ boost-test-example est_example2 : run : boost_unit_test_framework ] + [ boost-test-example logged_exp_example : run : boost_unit_test_framework ] - [ boost-test-example auto_unit_test_example1 : boost_unit_test_framework ] - [ boost-test-example auto_unit_test_example2 : boost_unit_test_framework ] - [ boost-test-example auto_unit_test_example3 : boost_unit_test_framework ] - [ boost-test-example auto_unit_test_example4 : boost_unit_test_framework ] - [ boost-test-example auto_unit_test_example5 : boost_unit_test_framework ] - [ boost-test-example auto_unit_test_example6 : boost_unit_test_framework ] + [ boost-test-example named_param_example : run ] + + [ boost-test-example unit_test_example_01_dll : run-fail : boost_unit_test_framework : unit_test_example_01.cpp : 1 ] ; - - diff --git a/example/auto_unit_test_example4.cpp b/example/auto_unit_test_example4.cpp deleted file mode 100755 index c336d8ff..00000000 --- a/example/auto_unit_test_example4.cpp +++ /dev/null @@ -1,25 +0,0 @@ -// (C) Copyright Gennadiy Rozental 2005. -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) - -// See http://www.boost.org/libs/test for the library home page. - -// Boost.Test -#define BOOST_AUTO_TEST_MAIN -#include -#include -#include - -typedef boost::mpl::list test_types; - -BOOST_AUTO_TEST_CASE_TEMPLATE( test, T, test_types ) -{ - T t = 9; - - BOOST_CHECK_EQUAL( (t*t+t)/10, 9 ); -} - -//____________________________________________________________________________// - -// EOF diff --git a/example/auto_unit_test_example5.cpp b/example/auto_unit_test_example5.cpp deleted file mode 100755 index 40dbb43b..00000000 --- a/example/auto_unit_test_example5.cpp +++ /dev/null @@ -1,21 +0,0 @@ -// (C) Copyright Gennadiy Rozental 2005. -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) - -// See http://www.boost.org/libs/test for the library home page. - -// Boost.Test -#define BOOST_AUTO_TEST_MAIN -#include - -BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES( test1, 1 ) - -BOOST_AUTO_TEST_CASE( test1 ) -{ - BOOST_CHECK( 2 == 1 ); -} - -//____________________________________________________________________________// - -// EOF diff --git a/example/auto_unit_test_example6.cpp b/example/auto_unit_test_example6.cpp deleted file mode 100755 index 4ed54459..00000000 --- a/example/auto_unit_test_example6.cpp +++ /dev/null @@ -1,19 +0,0 @@ -// (C) Copyright Gennadiy Rozental 2005. -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) - -// See http://www.boost.org/libs/test for the library home page. - -// Boost.Test -#define BOOST_AUTO_TEST_MAIN "My Test" -#include - -BOOST_AUTO_TEST_CASE( test1 ) -{ - BOOST_CHECK( 2 == 1 ); -} - -//____________________________________________________________________________// - -// EOF diff --git a/example/est_example1.cpp b/example/est_example1.cpp new file mode 100644 index 00000000..d67d3253 --- /dev/null +++ b/example/est_example1.cpp @@ -0,0 +1,56 @@ +// (C) Copyright Gennadiy Rozental 2005. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Test +#define BOOST_TEST_MAIN +#include +#include +#include +using namespace boost::itest; + +// Boost +#include + +//____________________________________________________________________________// + +// Here is the function we are going to use to see all execution path variants + +template +typename Employee::string_type +validate_and_return_salary( Employee const& e, Ostr& os ) +{ + if( e.Title() == "CEO" || e.Salary() > 100000 ) + os << e.First() << " " << e.Last() << " is overpaid"; + + return e.First() + " " + e.Last(); +} + +//____________________________________________________________________________// + +// Mock pbject we are going to use for this test + +struct EmpMock : mock_object<> { + typedef mock_object<> mo_type; + typedef mo_type string_type; + + string_type const& Title() const { BOOST_ITEST_MOCK_FUNC( EmpMock::Title ); } + string_type const& First() const { BOOST_ITEST_MOCK_FUNC( EmpMock::First ); } + string_type const& Last() const { BOOST_ITEST_MOCK_FUNC( EmpMock::Last ); } + + mo_type const& Salary() const { BOOST_ITEST_MOCK_FUNC( EmpMock::Salary ); } +}; + +//____________________________________________________________________________// + +BOOST_TEST_EXCEPTION_SAFETY( test_all_exec_path ) +{ + validate_and_return_salary( EmpMock(), simple_mock ); +} + +//____________________________________________________________________________// + +// EOF diff --git a/example/est_example2.cpp b/example/est_example2.cpp new file mode 100644 index 00000000..b3216c9b --- /dev/null +++ b/example/est_example2.cpp @@ -0,0 +1,80 @@ +// (C) Copyright Gennadiy Rozental 2005. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Test +#define BOOST_TEST_MAIN +#include +#include +#include +using namespace boost::itest; + +// Boost +#include + +//____________________________________________________________________________// + +// Here is an exampt from simple (incorrect) stack implementation + +template +class stack { +public: + explicit stack( int init_capacity = 10 ) + : m_capacity( init_capacity ) + , m_size( 0 ) + , m_v( BOOST_ITEST_NEW(T)[m_capacity] ) + { + BOOST_ITEST_SCOPE( stack::stack ); + } + ~stack() + { + delete[] m_v; + } + + void push( T const& element ) + { + BOOST_ITEST_SCOPE( stack::push ); + + if( m_size == m_capacity ) { + m_capacity *= 2; + T* new_buffer = BOOST_ITEST_NEW( T )[m_capacity]; + for( unsigned i = 0; i < m_size; i++ ) { + new_buffer[i] = m_v[i]; + } + delete [] m_v; + m_v = new_buffer; + } + m_v[m_size++] = element; + } + unsigned size() { return m_size; } + +private: + unsigned m_capacity; + unsigned m_size; + T* m_v; +}; + +//____________________________________________________________________________// + +BOOST_TEST_EXCEPTION_SAFETY( test_stack_push ) +{ + stack > st( 2 ); + + for( unsigned i = 0; i < 3; ++i ) { + try { + st.push( simple_mock ); + } + catch( ... ) { + // this invariant checks that in case of failed push number of elements doesn't change + BOOST_CHECK_EQUAL( i, st.size() ); + throw; + } + } +} + +//____________________________________________________________________________// + +// EOF diff --git a/example/exec_mon_example.cpp b/example/exec_mon_example.cpp index 43de62a2..8fa385f1 100644 --- a/example/exec_mon_example.cpp +++ b/example/exec_mon_example.cpp @@ -5,6 +5,7 @@ // See http://www.boost.org/libs/test for the library home page. +#include #include #include @@ -61,7 +62,7 @@ void translate_my_exception2( my_exception2 const& ex ) } // local_namespace int -main( int argc , char *[] ) +cpp_main( int argc , char *[] ) { ::boost::execution_monitor ex_mon; diff --git a/example/logged_exp_example.cpp b/example/logged_exp_example.cpp new file mode 100644 index 00000000..9379b606 --- /dev/null +++ b/example/logged_exp_example.cpp @@ -0,0 +1,150 @@ +// (C) Copyright Gennadiy Rozental 2005. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Test +#define BOOST_TEST_MAIN +#include +#include +#include +using namespace boost::itest; + +// Boost +#include + +//____________________________________________________________________________// + +// Callaborators interfaces +class Stove { +public: + virtual void light() = 0; + virtual void set_temperature( int temp ) = 0; + virtual void set_cook_time( int time_in_sec ) = 0; + virtual void auto_clean() = 0; +}; + +class Microwave { +public: + virtual int get_max_power() = 0; + virtual void set_power_level( int l ) = 0; + virtual void set_time( int time_in_sec ) = 0; + virtual void start() = 0; +}; + +class Timer { +public: + virtual void wait( int wait_time ) = 0; +}; + +//____________________________________________________________________________// + +// Callaborators mocks +class MockStove : public ::boost::itest::mock_object<0,Stove> { +public: + virtual void light() + { + BOOST_ITEST_SCOPE( Stove::light ); + } + virtual void set_temperature( int temp ) + { + BOOST_ITEST_SCOPE( Stove::set_temperature ); + + BOOST_ITEST_DATA_FLOW( temp ); + } + virtual void set_cook_time( int time_in_sec ) + { + BOOST_ITEST_SCOPE( Stove::set_cook_time ); + + BOOST_ITEST_DATA_FLOW( time_in_sec ); + } + virtual void auto_clean() + { + BOOST_ITEST_SCOPE( Stove::auto_clean ); + } +}; + +class MockMicrowave : public ::boost::itest::mock_object<0,Microwave> { +public: + virtual int get_max_power() + { + BOOST_ITEST_SCOPE( Microwave::get_max_power ); + + return BOOST_ITEST_RETURN( int, 1000 ); + } + virtual void set_power_level( int l ) + { + BOOST_ITEST_SCOPE( Microwave::set_power_level ); + + BOOST_ITEST_DATA_FLOW( l ); + } + virtual void set_time( int time_in_sec ) + { + BOOST_ITEST_SCOPE( Microwave::set_time ); + + BOOST_ITEST_DATA_FLOW( time_in_sec ); + } + virtual void start() + { + BOOST_ITEST_SCOPE( Microwave::start ); + } +}; + +class MockTimer : public ::boost::itest::mock_object<0,Timer> { +public: + virtual void wait( int wait_time ) + { + BOOST_ITEST_SCOPE( Timer::wait ); + + BOOST_ITEST_DATA_FLOW( wait_time ); + } +}; + +//____________________________________________________________________________// + +// Class under test +class kitchen_robot { +public: + void make_grilled_chicken( Timer& t, Stove& s, Microwave& m ) + { + m.set_power_level( 600 / (m.get_max_power()/5) + 1 ); + m.set_time( 15 * 60 ); // 15 min + m.start(); // defrost + + t.wait( 15 * 60 ); + + s.set_cook_time( 2 * 60 * 60 ); // 2 hours + s.light(); + + s.set_temperature( 450 ); + t.wait( 15 * 60 ); // 15 min - preheat + + s.set_temperature( 400 ); + t.wait( 90 * 60 ); // 1 hour 30 min - cook + + s.set_temperature( 250 ); + t.wait( 90 * 60 ); // 15 min - almost done + + s.auto_clean(); // done + } +}; + +//____________________________________________________________________________// + +BOOST_TEST_LOGGED_EXPECTATIONS( test_grilled_chiken_recept ) +{ + kitchen_robot kr; + + MockTimer t; + MockStove s; + MockMicrowave m; + + + kr.make_grilled_chicken( t, s, m ); +} + +//____________________________________________________________________________// + +// EOF diff --git a/example/prg_exec_example.cpp b/example/prg_exec_example.cpp index 39e78b27..c5f77844 100644 --- a/example/prg_exec_example.cpp +++ b/example/prg_exec_example.cpp @@ -5,6 +5,8 @@ // See http://www.boost.org/libs/test for the library home page. +#include + int add( int i, int j ) { return i+j; } int cpp_main( int, char *[] ) // note the name! diff --git a/example/test_exec_example.cpp b/example/test_exec_example.cpp index 3333b0b1..71db23f7 100644 --- a/example/test_exec_example.cpp +++ b/example/test_exec_example.cpp @@ -6,7 +6,7 @@ // See http://www.boost.org/libs/test for the library home page. // Boost.Test -#include +#include int add( int i, int j ) { return i+j; } diff --git a/example/unit_test_example1.cpp b/example/unit_test_example1.cpp deleted file mode 100644 index 8c94f41a..00000000 --- a/example/unit_test_example1.cpp +++ /dev/null @@ -1,33 +0,0 @@ -// (C) Copyright Gennadiy Rozental 2002-2005. -// (C) Copyright Gennadiy Rozental & Ullrich Koethe 2001. -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) - -// See http://www.boost.org/libs/test for the library home page. - -// Boost.Test -#include -using boost::unit_test::test_suite; - -// most frequently you implement test cases as a free functions -void free_test_function() -{ - // reports 'error in "free_test_function": test 2 == 1 failed' - BOOST_CHECK(2 == 1); // non-critical test => continue after failure - - int* p = (int*)0; - *p = 0; -} - -test_suite* -init_unit_test_suite( int, char* [] ) { - test_suite* test= BOOST_TEST_SUITE( "Unit test example 1" ); - - // this example will pass cause we know ahead of time number of expected failures - test->add( BOOST_TEST_CASE( &free_test_function ), 1 /* expected one error */ ); - - return test; -} - -// EOF diff --git a/example/unit_test_example2.cpp b/example/unit_test_example2.cpp deleted file mode 100644 index ec9da837..00000000 --- a/example/unit_test_example2.cpp +++ /dev/null @@ -1,42 +0,0 @@ -// (C) Copyright Gennadiy Rozental 2002-2005. -// (C) Copyright Gennadiy Rozental & Ullrich Koethe 2001. -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) - -// See http://www.boost.org/libs/test for the library home page. - -// Boost.Test -#include -using boost::unit_test::test_suite; - -void force_division_by_zero() -{ - // unit test framework can catch operating system signals - BOOST_CHECKPOINT("About to force division by zero!"); - int i = 1, j = 0; - i = i / j; -} - -void infinite_loop() -{ - // unit test framework can break infinite loops by timeout -#ifdef __unix // don't have timeout on other platforms - BOOST_CHECKPOINT("About to enter an infinite loop!"); - while(1); -#else - BOOST_MESSAGE( "Timeout support is not implemented on your platform" ); -#endif -} - -test_suite* -init_unit_test_suite( int argc, char * argv[] ) { - test_suite* test= BOOST_TEST_SUITE( "Unit test example 2" ); - - test->add( BOOST_TEST_CASE( &force_division_by_zero ) ); - test->add( BOOST_TEST_CASE( &infinite_loop ), 0, /* timeout */ 2 ); - - return test; -} - -// EOF diff --git a/example/unit_test_example_01.cpp b/example/unit_test_example_01.cpp new file mode 100644 index 00000000..1cfa3928 --- /dev/null +++ b/example/unit_test_example_01.cpp @@ -0,0 +1,39 @@ +// (C) Copyright Gennadiy Rozental 2005. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Test + +// each test module could contain no more then one 'main' file with init function defined +// alternatively you could define init function yourself +#define BOOST_TEST_MAIN +#include + +//____________________________________________________________________________// + +// most frequently you implement test cases as a free functions with automatic registration +BOOST_AUTO_TEST_CASE( test1 ) +{ + // reports 'error in "test1": test 2 == 1 failed' + BOOST_CHECK( 2 == 1 ); +} + +//____________________________________________________________________________// + +// each test file may contain any number of test cases; each test case has to have unique name +BOOST_AUTO_TEST_CASE( test2 ) +{ + int i = 0; + + // reports 'error in "test2": check i == 2 failed [0 != 2]' + BOOST_CHECK_EQUAL( i, 2 ); + + BOOST_CHECK_EQUAL( i, 0 ); +} + +//____________________________________________________________________________// + +// EOF diff --git a/example/unit_test_example_02.cpp b/example/unit_test_example_02.cpp new file mode 100644 index 00000000..a0c2380e --- /dev/null +++ b/example/unit_test_example_02.cpp @@ -0,0 +1,44 @@ +// (C) Copyright Gennadiy Rozental 2002-2005. +// (C) Copyright Gennadiy Rozental & Ullrich Koethe 2001. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Test +#include +using namespace boost::unit_test; + +//____________________________________________________________________________// + +// you could easily implement test cases as a free functions +// this test case will need to be explecetely registered in test tree +void free_test_function() +{ + // reports 'error in "free_test_function": test 2 == 1 failed' + BOOST_CHECK(2 == 1); // non-critical test => continue after failure + + // reports 'unknown location(0): fatal error in "free_test_function": memory access violation + // d:\source code\boost\libs\test\example\unit_test_example_02.cpp(25): last checkpoint' + int* p = (int*)0; + BOOST_CHECK( *p == 0 ); +} + +//____________________________________________________________________________// + + +test_suite* +init_unit_test_suite( int, char* [] ) { + framework::master_test_suite().p_name.value = "Unit test example 02"; + + // register the test case in test tree and specify number of expected failures so + // this example will pass at runtime + framework::master_test_suite().add( BOOST_TEST_CASE( &free_test_function ), 1 /* expected one error */ ); + + return 0; +} + +//____________________________________________________________________________// + +// EOF diff --git a/example/unit_test_example_03.cpp b/example/unit_test_example_03.cpp new file mode 100644 index 00000000..20940126 --- /dev/null +++ b/example/unit_test_example_03.cpp @@ -0,0 +1,57 @@ +// (C) Copyright Gennadiy Rozental 2002-2005. +// (C) Copyright Gennadiy Rozental & Ullrich Koethe 2001. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Test +#include +using namespace boost::unit_test; + +//____________________________________________________________________________// + +// this test case is automatically registered +BOOST_AUTO_TEST_CASE( force_division_by_zero ) +{ + BOOST_CHECK( false ); + + // unit test framework can catch operating system signals + BOOST_TEST_CHECKPOINT("About to force division by zero!"); + int i = 1, j = 0; + + // reports 'unknown location(0): fatal error in "force_division_by_zero": integer divide by zero' + i = i / j; +} + +//____________________________________________________________________________// + +// this test case will have tobe registered manually +void infinite_loop() +{ + // unit test framework can break infinite loops by timeout +#ifdef __unix // don't have timeout on other platforms + BOOST_TEST_CHECKPOINT("About to enter an infinite loop!"); + while(1); +#else + BOOST_TEST_MESSAGE( "Timeout support is not implemented on your platform" ); +#endif +} + +//____________________________________________________________________________// + +test_suite* +init_unit_test_suite( int , char* [] ) +{ + framework::master_test_suite().p_name.value = "Unit test example 03"; + + // with explicit registration we could specify a test case timeout + framework::master_test_suite().add( BOOST_TEST_CASE( &infinite_loop ), 0, /* timeout */ 2 ); + + return 0; +} + +//____________________________________________________________________________// + +// EOF diff --git a/example/auto_unit_test_example2.cpp b/example/unit_test_example_04.cpp old mode 100755 new mode 100644 similarity index 80% rename from example/auto_unit_test_example2.cpp rename to example/unit_test_example_04.cpp index ceac2b3c..31c5dad1 --- a/example/auto_unit_test_example2.cpp +++ b/example/unit_test_example_04.cpp @@ -6,13 +6,14 @@ // See http://www.boost.org/libs/test for the library home page. // Boost.Test -#define BOOST_AUTO_TEST_MAIN -#include - -BOOST_AUTO_TEST_SUITE( suite1 ); +#define BOOST_TEST_MAIN +#include //____________________________________________________________________________// +// automatically registerred test cases could be organized in test suites +BOOST_AUTO_TEST_SUITE( suite1 ); + BOOST_AUTO_TEST_CASE( test1 ) { BOOST_CHECK( 2 == 1 ); @@ -20,6 +21,7 @@ BOOST_AUTO_TEST_CASE( test1 ) //____________________________________________________________________________// +// this test case belongs to suite1 test suite BOOST_AUTO_TEST_CASE( test2 ) { int i = 0; @@ -33,6 +35,7 @@ BOOST_AUTO_TEST_SUITE_END(); //____________________________________________________________________________// +// this test case belongs to master test suite BOOST_AUTO_TEST_CASE( test3 ) { int i = 0; @@ -44,6 +47,7 @@ BOOST_AUTO_TEST_CASE( test3 ) BOOST_AUTO_TEST_SUITE( suite2 ); +// this test case belongs to suite2 test suite BOOST_AUTO_TEST_CASE( test4 ) { int i = 0; diff --git a/example/auto_unit_test_example3.cpp b/example/unit_test_example_05.cpp old mode 100755 new mode 100644 similarity index 55% rename from example/auto_unit_test_example3.cpp rename to example/unit_test_example_05.cpp index 7f419bfe..33b3658a --- a/example/auto_unit_test_example3.cpp +++ b/example/unit_test_example_05.cpp @@ -6,25 +6,32 @@ // See http://www.boost.org/libs/test for the library home page. // Boost.Test -#define BOOST_AUTO_TEST_MAIN -#include +#define BOOST_TEST_MAIN +#include + +//____________________________________________________________________________// BOOST_AUTO_TEST_SUITE( suite ); struct F { - F() : i( 0 ) { BOOST_MESSAGE( "setup fixture" ); } - ~F() { BOOST_MESSAGE( "teardown fixture" ); } + F() : i( 0 ) { BOOST_TEST_MESSAGE( "setup fixture" ); } + ~F() { BOOST_TEST_MESSAGE( "teardown fixture" ); } int i; }; +//____________________________________________________________________________// + +// this test case will use struct F as fixture BOOST_FIXTURE_TEST_CASE( test1, F ) { + // you have direct access to non-private members of fixture structure BOOST_CHECK( i == 1 ); } //____________________________________________________________________________// +// you could have any number of test cases with the same fixture BOOST_FIXTURE_TEST_CASE( test2, F ) { BOOST_CHECK_EQUAL( i, 2 ); @@ -34,6 +41,6 @@ BOOST_FIXTURE_TEST_CASE( test2, F ) //____________________________________________________________________________// -BOOST_AUTO_TEST_SUITE_END(); +BOOST_AUTO_TEST_SUITE_END() // EOF diff --git a/example/unit_test_example_06.cpp b/example/unit_test_example_06.cpp new file mode 100644 index 00000000..8f8552fa --- /dev/null +++ b/example/unit_test_example_06.cpp @@ -0,0 +1,44 @@ +// (C) Copyright Gennadiy Rozental 2005. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Test +#define BOOST_TEST_MAIN +#include + +//____________________________________________________________________________// + +struct F { + F() : i( 0 ) { BOOST_TEST_MESSAGE( "setup fixture" ); } + ~F() { BOOST_TEST_MESSAGE( "teardown fixture" ); } + + int i; +}; + +//____________________________________________________________________________// + +// struct F is going to be used as a fixture for all test cases in this test suite +BOOST_FIXTURE_TEST_SUITE( s, F ) + +BOOST_AUTO_TEST_CASE( test1 ) +{ + BOOST_CHECK( i == 1 ); +} + +//____________________________________________________________________________// + +BOOST_AUTO_TEST_CASE( test2 ) +{ + BOOST_CHECK_EQUAL( i, 2 ); + + BOOST_CHECK_EQUAL( i, 0 ); +} + +//____________________________________________________________________________// + +BOOST_AUTO_TEST_SUITE_END() + +// EOF diff --git a/example/unit_test_example_07.cpp b/example/unit_test_example_07.cpp new file mode 100644 index 00000000..4be4fd9b --- /dev/null +++ b/example/unit_test_example_07.cpp @@ -0,0 +1,43 @@ +// (C) Copyright Gennadiy Rozental 2005. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Test +#define BOOST_TEST_MAIN +#include +#include +#include + +//____________________________________________________________________________// + +struct F { + F() : i( 9 ) { BOOST_TEST_MESSAGE( "setup fixture" ); } + ~F() { BOOST_TEST_MESSAGE( "teardown fixture" ); } + + int i; +}; + +//____________________________________________________________________________// + +BOOST_FIXTURE_TEST_SUITE( s, F ) + +typedef boost::mpl::list test_types; +// this test case template produce a separate test case for each type listed in test_types +// each produced test case uses strct F as a fixture +BOOST_AUTO_TEST_CASE_TEMPLATE( test, T, test_types ) +{ + T t = i; + + // usually it's a bad idea to use BOOST_CHECK_EQUAL for checking equality values of + // floating point types. This check may or may not produce an error report + BOOST_CHECK_EQUAL( (t*t+t)/10, 9 ); +} + +BOOST_AUTO_TEST_SUITE_END() + +//____________________________________________________________________________// + +// EOF diff --git a/example/auto_unit_test_example1.cpp b/example/unit_test_example_08.cpp old mode 100755 new mode 100644 similarity index 71% rename from example/auto_unit_test_example1.cpp rename to example/unit_test_example_08.cpp index 5ceab344..fa76c6fa --- a/example/auto_unit_test_example1.cpp +++ b/example/unit_test_example_08.cpp @@ -6,8 +6,13 @@ // See http://www.boost.org/libs/test for the library home page. // Boost.Test -#define BOOST_AUTO_TEST_MAIN -#include +#define BOOST_TEST_MAIN +#include + +//____________________________________________________________________________// + +// this way we could specify a number of expected failures in automatically registered test case +BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES( test1, 1 ) BOOST_AUTO_TEST_CASE( test1 ) { @@ -16,15 +21,4 @@ BOOST_AUTO_TEST_CASE( test1 ) //____________________________________________________________________________// -BOOST_AUTO_TEST_CASE( test2 ) -{ - int i = 0; - - BOOST_CHECK_EQUAL( i, 2 ); - - BOOST_CHECK_EQUAL( i, 0 ); -} - -//____________________________________________________________________________// - // EOF diff --git a/example/unit_test_example_09_1.cpp b/example/unit_test_example_09_1.cpp new file mode 100644 index 00000000..d661319d --- /dev/null +++ b/example/unit_test_example_09_1.cpp @@ -0,0 +1,34 @@ +// (C) Copyright Gennadiy Rozental 2005. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Test +#define BOOST_TEST_MAIN // this file is main +#include + +// STL +#include + +//____________________________________________________________________________// + +struct MyConfig { + MyConfig() { std::cout << "global setup part1\n"; } + ~MyConfig() { std::cout << "global teardown part1\n"; } +}; + +// structure MyConfig is used as a global fixture - it's invoked pre and post any testing is perfrmed +BOOST_GLOBAL_FIXTURE( MyConfig ) + +//____________________________________________________________________________// + +BOOST_AUTO_TEST_CASE( test1 ) +{ + BOOST_CHECK( true ); +} + +//____________________________________________________________________________// + +// EOF diff --git a/example/unit_test_example_09_2.cpp b/example/unit_test_example_09_2.cpp new file mode 100644 index 00000000..181b6072 --- /dev/null +++ b/example/unit_test_example_09_2.cpp @@ -0,0 +1,34 @@ +// (C) Copyright Gennadiy Rozental 2005. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/test for the library home page. + +// Boost.Test +// only one file should define BOOST_TEST_MAIN +#include + +// STL +#include + +//____________________________________________________________________________// + +struct MyConfig2 { + MyConfig2() { std::cout << "global setup part2\n"; } + ~MyConfig2() { std::cout << "global teardown part2\n"; } +}; + +// structure MyConfig2 is used as a global fixture. You could have any number of global fxtures +BOOST_GLOBAL_FIXTURE( MyConfig2 ) + +//____________________________________________________________________________// + +BOOST_AUTO_TEST_CASE( test2 ) +{ + BOOST_CHECK( true ); +} + +//____________________________________________________________________________// + +// EOF diff --git a/example/unit_test_example3.cpp b/example/unit_test_example_10.cpp similarity index 84% rename from example/unit_test_example3.cpp rename to example/unit_test_example_10.cpp index d7519568..1ed65304 100644 --- a/example/unit_test_example3.cpp +++ b/example/unit_test_example_10.cpp @@ -9,9 +9,9 @@ // Boost.Test #include #include -using boost::unit_test::test_suite; -using boost::unit_test::test_case; +using namespace boost::unit_test; using boost::test_tools::close_at_tolerance; +using boost::test_tools::percent_tolerance; // BOOST #include @@ -23,6 +23,8 @@ using boost::test_tools::close_at_tolerance; #include #include +//____________________________________________________________________________// + struct account { account() : m_amount(0.0) @@ -43,6 +45,8 @@ private: double m_amount; }; +//____________________________________________________________________________// + struct account_test { account_test( double init_value ) { m_account.deposit( init_value ); } @@ -104,7 +108,8 @@ struct account_test { // reports 'fatal error in "account_test::test_deposit": test close_at_tolerance( 1e-9 )( m_account.balance(), 605.5) // failed for (actual_value, 605.5) - BOOST_REQUIRE_PREDICATE( close_at_tolerance( 1e-9 ), (m_account.balance())(605.5) ); + BOOST_REQUIRE_PREDICATE( close_at_tolerance( percent_tolerance( 1e-9 ) ), + (m_account.balance())(605.5) ); } void test_withdraw() @@ -123,6 +128,8 @@ struct account_test { } }; +//____________________________________________________________________________// + struct account_test_suite : public test_suite { account_test_suite( double init_value ) : test_suite("account_test_suite") { // add member function test cases to a test suite @@ -141,21 +148,25 @@ struct account_test_suite : public test_suite { } }; +//____________________________________________________________________________// + test_suite* init_unit_test_suite( int argc, char * argv[] ) { - test_suite* test = BOOST_TEST_SUITE( "Unit test example 3" ); + framework::master_test_suite().p_name.value = "Unit test example 10"; try { if( argc < 2 ) - return (test_suite*)0; + throw std::logic_error( "Initial deposit expected" ); - test->add( new account_test_suite( boost::lexical_cast( argv[1] ) ) ); + framework::master_test_suite().add( new account_test_suite( boost::lexical_cast( argv[1] ) ) ); } - catch( boost::bad_lexical_cast& ) { - return (test_suite*)0; + catch( boost::bad_lexical_cast const& ) { + throw std::logic_error( "Initial deposit value should match format of double" ); } - return test; + return 0; } +//____________________________________________________________________________// + // EOF diff --git a/example/unit_test_example4.cpp b/example/unit_test_example_11.cpp similarity index 55% rename from example/unit_test_example4.cpp rename to example/unit_test_example_11.cpp index 2ac8f98c..3cf3f852 100644 --- a/example/unit_test_example4.cpp +++ b/example/unit_test_example_11.cpp @@ -9,27 +9,36 @@ // Boost.Test #include #include -using boost::unit_test::test_suite; +using namespace boost::unit_test; // STL #include #include +//____________________________________________________________________________// + +// this free function is invoked with all parameters specified in a list void check_string( std::string const& s ) { // reports 'error in "check_string": test s.substr( 0, 3 ) == "hdr" failed [actual_value != hdr]' BOOST_CHECK_EQUAL( s.substr( 0, 3 ), "hdr" ); } +//____________________________________________________________________________// + test_suite* init_unit_test_suite( int /*argc*/, char* /*argv*/[] ) { - test_suite* test= BOOST_TEST_SUITE( "Unit test example 4" ); + framework::master_test_suite().p_name.value = "Unit test example 11"; + // parameters have no requirements to stay alive beyong the next statement std::string const params[] = { "hdr1 ", "hdr2", "3 " }; - test->add( BOOST_PARAM_TEST_CASE( &check_string, (std::string const*)params, params+3 ) ); + framework::master_test_suite().add( + BOOST_PARAM_TEST_CASE( &check_string, (std::string const*)params, params+3 ) ); - return test; + return 0; } +//____________________________________________________________________________// + // EOF diff --git a/example/unit_test_example5.cpp b/example/unit_test_example_12.cpp similarity index 84% rename from example/unit_test_example5.cpp rename to example/unit_test_example_12.cpp index 793a99f4..dc05cfc8 100644 --- a/example/unit_test_example5.cpp +++ b/example/unit_test_example_12.cpp @@ -9,7 +9,7 @@ #include #include #include -using boost::unit_test::test_suite; +using namespace boost::unit_test; // BOOST #include @@ -29,12 +29,12 @@ using boost::unit_test::test_suite; //____________________________________________________________________________// template -struct pow10 { - BOOST_STATIC_CONSTANT( unsigned long, value = 10*pow10::value ); +struct power_of_10 { + BOOST_STATIC_CONSTANT( unsigned long, value = 10*power_of_10::value ); }; template<> -struct pow10<0> { +struct power_of_10<0> { BOOST_STATIC_CONSTANT( unsigned long, value = 1 ); }; @@ -81,18 +81,18 @@ private: if( it == m_alphabet.end() ) return true; - m_result += pow10_( it - m_alphabet.begin() ); + m_result += power_of_10_( it - m_alphabet.begin() ); return false; } - unsigned long pow10_( int i ) { + unsigned long power_of_10_( int i ) { switch( i ) { - case 0: return pow10<0>::value; - case 1: return pow10<1>::value; - case 2: return pow10<2>::value; - case 3: return pow10<3>::value; - case 4: return pow10<4>::value; + case 0: return power_of_10<0>::value; + case 1: return power_of_10<1>::value; + case 2: return power_of_10<2>::value; + case 3: return power_of_10<3>::value; + case 4: return power_of_10<4>::value; default: return 0; } } @@ -129,7 +129,7 @@ public: else if( test_data.exp_value == (unsigned long)-2 ) BOOST_CHECK_THROW( m_function_under_test( test_data.orig_string ), std::out_of_range ) else { - BOOST_MESSAGE( "Testing: " << test_data.orig_string ); + BOOST_TEST_MESSAGE( "Testing: " << test_data.orig_string ); BOOST_CHECK_EQUAL( m_function_under_test( test_data.orig_string ), test_data.exp_value ); } } @@ -141,7 +141,7 @@ private: //____________________________________________________________________________// struct massive_hash_function_test : test_suite { - massive_hash_function_test() { + massive_hash_function_test() : test_suite( "massive_hash_function_test" ) { std::string alphabet; std::cout << "Enter alphabet (4 characters without delimeters)\n"; std::cin >> alphabet; @@ -170,18 +170,12 @@ struct massive_hash_function_test : test_suite { //____________________________________________________________________________// test_suite* -init_unit_test_suite( int argc, char * argv[] ) { - test_suite* test( BOOST_TEST_SUITE( "Unit test example 5" ) ); +init_unit_test_suite( int, char* [] ) { + framework::master_test_suite().p_name.value = "Unit test example 12"; - try { - test->add( new massive_hash_function_test ); - } catch( std::logic_error const& ex ) { - std::cout << "Test suite fail to create instance of hash function: " << ex.what() << std::endl; + framework::master_test_suite().add( new massive_hash_function_test ); - return (test_suite*)0; - } - - return test; + return 0; } //____________________________________________________________________________// diff --git a/example/unit_test_example5.input b/example/unit_test_example_12.input similarity index 100% rename from example/unit_test_example5.input rename to example/unit_test_example_12.input