From 8a6edd531e33b8d3cccec98c92f7c9505f57b4e7 Mon Sep 17 00:00:00 2001 From: mat007 Date: Mon, 20 May 2013 10:49:09 +0000 Subject: [PATCH] Added floating point comparison constraints git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@659 860be788-9bd5-4423-9f1e-828f051e677b --- build/boost/doc/changelog.qbk | 1 + build/boost/doc/reference.qbk | 4 +++ test/test_constraints.cpp | 46 ++++++++++++++++++++++++++--------- turtle/constraints.hpp | 16 ++++++++++++ 4 files changed, 56 insertions(+), 11 deletions(-) diff --git a/build/boost/doc/changelog.qbk b/build/boost/doc/changelog.qbk index 4018b20..d6602a4 100644 --- a/build/boost/doc/changelog.qbk +++ b/build/boost/doc/changelog.qbk @@ -8,6 +8,7 @@ Not yet released * Officially documented MOCK_UNARY_CONSTRAINT and MOCK_BINARY_CONSTRAINT * Added support for several sequences in 'in' * Added support for nullptr as constraint +* Added mock::close, mock::close_fraction, mock::small and mock::near constraints [endsect] diff --git a/build/boost/doc/reference.qbk b/build/boost/doc/reference.qbk index 5a51372..4d4111c 100644 --- a/build/boost/doc/reference.qbk +++ b/build/boost/doc/reference.qbk @@ -323,6 +323,10 @@ Constraints : [[mock::greater( ['expected] )] [['actual] > ['expected]] [compares ['actual] to ['expected] using operator >]] [[mock::less_equal( ['expected] )] [['actual] <= ['expected]] [compares ['actual] to ['expected] using operator <=]] [[mock::greater_equal( ['expected] )] [['actual] >= ['expected]] [compares ['actual] to ['expected] using operator >=]] + [[mock::near] [std::abs( ['actual] - ['expected] ) < ['arg]] [checks whether ['actual] is near ['expected]]] + [[mock::close] [boost::test_tools::check_is_close( ['actual], ['expected], boost::test_tools::percent_tolerance( ['arg] ) )] [checks whether ['actual] is close to ['expected], see [@http://www.boost.org/libs/test/doc/html/utf/testing-tools/floating_point_comparison.html Floating-point comparison algorithms] ]] + [[mock::close_fraction] [boost::test_tools::check_is_close( ['actual], ['expected], boost::test_tools::fraction_tolerance( ['arg] ) )] [checks whether ['actual] is close to ['expected], see [@http://www.boost.org/libs/test/doc/html/utf/testing-tools/floating_point_comparison.html Floating-point comparison algorithms] ]] + [[mock::small] [boost::test_tools::check_is_small( ['actual], ['expected] ) )] [checks whether ['actual] is small with a tolerance of ['expected], see [@http://www.boost.org/libs/test/doc/html/utf/testing-tools/floating_point_comparison.html Floating-point comparison algorithms] ]] [[mock::call( ['expected] )] [['expected]( ['actual] )] [calls ['expected] as a functor returning a ['bool] and accepting ['actual] as parameter]] [[mock::same( ['expected] )] [&['actual] == &['expected]] [compares ['actual] to ['expected] by comparing their pointers]] [[mock::assign( ['expected] )] [['actual] = ['expected], true diff --git a/test/test_constraints.cpp b/test/test_constraints.cpp index a433f0d..dc945d8 100644 --- a/test/test_constraints.cpp +++ b/test/test_constraints.cpp @@ -41,7 +41,7 @@ BOOST_AUTO_TEST_CASE( constraints_can_be_combined_using_the_and_operator ) mock::less( 0 ) && mock::greater( 0 ); } -BOOST_AUTO_TEST_CASE( equal ) +BOOST_AUTO_TEST_CASE( equal_constraint ) { BOOST_CHECK( mock::equal( std::string( "string" ) ).c_( "string" ) ); BOOST_CHECK( ! mock::equal( std::string( "string" ) ).c_( "not string" ) ); @@ -57,7 +57,7 @@ BOOST_AUTO_TEST_CASE( equal ) } } -BOOST_AUTO_TEST_CASE( same ) +BOOST_AUTO_TEST_CASE( same_constraint ) { { int i = 0; @@ -86,7 +86,7 @@ BOOST_AUTO_TEST_CASE( same ) #endif } -BOOST_AUTO_TEST_CASE( assign ) +BOOST_AUTO_TEST_CASE( assign_constraint ) { { int i = 0; @@ -149,7 +149,7 @@ BOOST_AUTO_TEST_CASE( assign ) } } -BOOST_AUTO_TEST_CASE( retrieve ) +BOOST_AUTO_TEST_CASE( retrieve_constraint ) { { int i = 0; @@ -235,14 +235,14 @@ namespace }; } -BOOST_AUTO_TEST_CASE( retrieve_uses_assignment_operator ) +BOOST_AUTO_TEST_CASE( retrieve_constraint_uses_assignment_operator ) { B b; const A a = A(); mock::retrieve( b ).c_( a ); } -BOOST_AUTO_TEST_CASE( affirm ) +BOOST_AUTO_TEST_CASE( affirm_constraint ) { int* i = 0; int j; @@ -250,7 +250,7 @@ BOOST_AUTO_TEST_CASE( affirm ) BOOST_CHECK( mock::affirm.c_( &j ) ); } -BOOST_AUTO_TEST_CASE( negate ) +BOOST_AUTO_TEST_CASE( negate_constraint ) { int* i = 0; int j; @@ -270,19 +270,19 @@ namespace } } -BOOST_AUTO_TEST_CASE( call ) +BOOST_AUTO_TEST_CASE( call_constraint ) { BOOST_CHECK( mock::call( &return_true ).c_() ); BOOST_CHECK( ! mock::call( &return_false ).c_() ); } -BOOST_AUTO_TEST_CASE( evaluate ) +BOOST_AUTO_TEST_CASE( evaluate_constraint ) { BOOST_CHECK( mock::evaluate.c_( &return_true ) ); BOOST_CHECK( ! mock::evaluate.c_( &return_false ) ); } -BOOST_AUTO_TEST_CASE( contain_with_const_char_ptr ) +BOOST_AUTO_TEST_CASE( contain_constraint_with_const_char_ptr ) { BOOST_CHECK( mock::contain( "string" ).c_( "this is a string" ) ); BOOST_CHECK( mock::contain( "string" ).c_( std::string( "this is a string" ) ) ); @@ -304,7 +304,7 @@ BOOST_AUTO_TEST_CASE( contain_with_const_char_ptr ) } } -BOOST_AUTO_TEST_CASE( contain_with_strings ) +BOOST_AUTO_TEST_CASE( contain_constraint_with_strings ) { BOOST_CHECK( mock::contain( std::string( "string" ) ).c_( "this is a string" ) ); BOOST_CHECK( mock::contain( std::string( "string" ) ).c_( std::string( "this is a string" ) ) ); @@ -343,3 +343,27 @@ BOOST_AUTO_TEST_CASE( type_with_overloaded_address_operator_can_be_used_in_const type_with_overloaded_address_operator* pt; mock::retrieve( pt ).c_( t ); } + +BOOST_AUTO_TEST_CASE( close_constraint ) +{ + BOOST_CHECK( mock::close( 12.0, 0.0001 ).c_( 12 ) ); + BOOST_CHECK( ! mock::close( 12.0, 0.0001 ).c_( 13 ) ); +} + +BOOST_AUTO_TEST_CASE( close_fraction_constraint ) +{ + BOOST_CHECK( mock::close_fraction( 12.0, 0.0001 ).c_( 12 ) ); + BOOST_CHECK( ! mock::close_fraction( 12.0, 0.0001 ).c_( 13 ) ); +} + +BOOST_AUTO_TEST_CASE( small_constraint ) +{ + BOOST_CHECK( mock::small( 0.0001 ).c_( 0. ) ); + BOOST_CHECK( ! mock::small( 0.0001 ).c_( 12. ) ); +} + +BOOST_AUTO_TEST_CASE( near_constraint ) +{ + BOOST_CHECK( mock::near( 12.0, 0.0001 ).c_( 12 ) ); + BOOST_CHECK( ! mock::near( 12.0, 0.0001 ).c_( 13 ) ); +} diff --git a/turtle/constraints.hpp b/turtle/constraints.hpp index 111e461..7f2a241 100644 --- a/turtle/constraints.hpp +++ b/turtle/constraints.hpp @@ -16,6 +16,7 @@ #include #include #include +#include namespace mock { @@ -30,6 +31,21 @@ namespace mock MOCK_BINARY_CONSTRAINT( less_equal, actual <= expected ) MOCK_BINARY_CONSTRAINT( greater_equal, actual >= expected ) + MOCK_BINARY_CONSTRAINT( small, \ + ( boost::test_tools::check_is_small( actual, expected ) ) ); + + MOCK_TERNARY_CONSTRAINT( close, \ + ( boost::test_tools::check_is_close( \ + actual, expected, \ + boost::test_tools::percent_tolerance( arg ) ) ) ); + + MOCK_TERNARY_CONSTRAINT( close_fraction, \ + ( boost::test_tools::check_is_close( \ + actual, expected, \ + boost::test_tools::fraction_tolerance( arg ) ) ) ); + + MOCK_TERNARY_CONSTRAINT( near, std::abs( actual - expected ) < arg ); + namespace detail { template< typename Expected >