]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/test/example/unit_test_example_10.cpp
1 // (C) Copyright Gennadiy Rozental 2001-2014.
2 // (C) Copyright Gennadiy Rozental & Ullrich Koethe 2001.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/test for the library home page.
10 #include <boost/test/tools/floating_point_comparison.hpp>
11 #include <boost/test/unit_test.hpp>
12 using namespace boost::unit_test
;
13 using boost::math::fpc::close_at_tolerance
;
14 using boost::math::fpc::percent_tolerance
;
17 #include <boost/lexical_cast.hpp>
26 //____________________________________________________________________________//
33 void deposit(double amount
) { m_amount
+= amount
; }
34 void withdraw(double amount
)
38 throw std::logic_error("You don't have that much money!");
42 double balance() const { return m_amount
; }
48 //____________________________________________________________________________//
51 account_test( double init_value
) { m_account
.deposit( init_value
); }
53 account m_account
; // a very simple fixture
57 // different kinds of non-critical tests
58 // they report the error and continue
61 // reports 'error in "account_test::test_init": test m_account.balance() >= 0.0 failed' on error
62 BOOST_CHECK( m_account
.balance() >= 0.0 );
64 // customized assertion
65 // reports 'error in "account_test::test_init": Initial balance should be more then 1, was actual_value' on error
66 BOOST_CHECK_MESSAGE( m_account
.balance() > 1.0,
67 "Initial balance should be more then 1, was " << m_account
.balance() );
69 // equality assertion (not very wise idea use equality check on floating point values)
70 // reports 'error in "account_test::test_init": test m_account.balance() == 5.0 failed [actual_value != 5]' on error
71 BOOST_CHECK_EQUAL( m_account
.balance(), 5.0 );
73 // closeness assertion for floating-point numbers (symbol (==) used to mark closeness, (!=) to mark non closeness )
74 // reports 'error in "account_test::test_init": test m_account.balance() (==) 10.0 failed [actual_value (!=) 10 (1e-010)]' on error
75 BOOST_CHECK_CLOSE( m_account
.balance(), 10.0, /* tolerance */ 1e-10 );
80 // these 2 statements just to show that usage manipulators doesn't hurt Boost.Test output
81 std::cout
<< "Current balance: " << std::hex
<< (int)m_account
.balance() << std::endl
;
82 std::cerr
<< "Current balance: " << std::hex
<< (int)m_account
.balance() << std::endl
;
84 float curr_ballance
= (float)m_account
.balance();
87 std::cout
<< "Enter deposit value:\n";
88 std::cin
>> deposit_value
;
90 m_account
.deposit( deposit_value
);
92 // correct result validation; could fail due to rounding errors; use BOOST_CHECK_CLOSE instead
93 // reports "test m_account.balance() == curr_ballance + deposit_value failed" on error
94 BOOST_CHECK( m_account
.balance() == curr_ballance
+ deposit_value
);
96 // different kinds of critical tests
98 // reports 'fatal error in "account_test::test_deposit": test m_account.balance() >= 100.0 failed' on error
99 BOOST_REQUIRE( m_account
.balance() >= 100.0 );
101 // reports 'fatal error in "account_test::test_deposit": Balance should be more than 500.1, was actual_value' on error
102 BOOST_REQUIRE_MESSAGE( m_account
.balance() > 500.1,
103 "Balance should be more than 500.1, was " << m_account
.balance());
105 // reports 'fatal error in "account_test::test_deposit": test std::not_equal_to<double>()(m_account.balance(), 999.9) failed
106 // for (999.9, 999.9)' on error
107 BOOST_REQUIRE_PREDICATE( std::not_equal_to
<double>(), (m_account
.balance())(999.9) );
109 // reports 'fatal error in "account_test::test_deposit": test close_at_tolerance<double>( 1e-9 )( m_account.balance(), 605.5)
110 // failed for (actual_value, 605.5)
111 BOOST_REQUIRE_PREDICATE( close_at_tolerance
<double>( percent_tolerance( 1e-9 ) ),
112 (m_account
.balance())(605.5) );
117 float curr_ballance
= (float)m_account
.balance();
119 m_account
.withdraw(2.5);
121 // correct result validation; could fail due to rounding errors; use BOOST_CHECK_CLOSE instead
122 // reports "test m_account.balance() == curr_ballance - 2.5 failed" on error
123 BOOST_CHECK( m_account
.balance() == curr_ballance
- 2.5 );
125 // reports 'error in "account_test::test_withdraw": exception std::runtime_error is expected' on error
126 BOOST_CHECK_THROW( m_account
.withdraw( m_account
.balance() + 1 ), std::runtime_error
);
131 //____________________________________________________________________________//
133 struct account_test_suite
: public test_suite
{
134 account_test_suite( double init_value
) : test_suite("account_test_suite") {
135 // add member function test cases to a test suite
136 boost::shared_ptr
<account_test
> instance( new account_test( init_value
) );
138 test_case
* init_test_case
= BOOST_CLASS_TEST_CASE( &account_test::test_init
, instance
);
139 test_case
* deposit_test_case
= BOOST_CLASS_TEST_CASE( &account_test::test_deposit
, instance
);
140 test_case
* withdraw_test_case
= BOOST_CLASS_TEST_CASE( &account_test::test_withdraw
, instance
);
142 deposit_test_case
->depends_on( init_test_case
);
143 withdraw_test_case
->depends_on( deposit_test_case
);
145 add( init_test_case
, 1 );
146 add( deposit_test_case
, 1 );
147 add( withdraw_test_case
);
151 //____________________________________________________________________________//
154 init_unit_test_suite( int argc
, char * argv
[] ) {
155 framework::master_test_suite().p_name
.value
= "Unit test example 10";
159 throw std::logic_error( "Initial deposit expected" );
161 framework::master_test_suite().add( new account_test_suite( boost::lexical_cast
<double>( argv
[1] ) ) );
163 catch( boost::bad_lexical_cast
const& ) {
164 throw std::logic_error( "Initial deposit value should match format of double" );
170 //____________________________________________________________________________//