Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

tolerance (decorator)

template <typename FPT>
  tolerance(FPT eps);

template <typename FPT>
  tolerance(test_tools::fpc::percent_tolerance_t<FPT> eps)

Decorator tolerance specifies the default comparison tolerance for floating point type FTP in the decorated test unit. The default tolerance only applies to a particular type, so it makes sense to provide more than one tolerance decorator if we are comparing different floating point types. The variant with percent_tolerance uses value

eps / 100

as tolerance.

[Note] Note

For more details see the floating points comparison section.

Example: decorator tolerance

Code

#define BOOST_TEST_MODULE decorator_13
#include <boost/test/included/unit_test.hpp>

namespace utf = boost::unit_test;
namespace fpc = boost::test_tools::fpc;

BOOST_AUTO_TEST_CASE(test1, * utf::tolerance(0.0005))
{
  BOOST_TEST( 0.001 == 0.000 );
  BOOST_TEST( 1.100 == 1.101 );
}

BOOST_AUTO_TEST_CASE(test2, * utf::tolerance(0.005))
{
  BOOST_TEST( 0.001 == 0.000 );
  BOOST_TEST( 1.100 == 1.101 );
}

BOOST_AUTO_TEST_CASE(test3, * utf::tolerance(0.05F))
{
  BOOST_TEST( 0.001 == 0.000 );
  BOOST_TEST( 1.100 == 1.101 );
}

BOOST_AUTO_TEST_CASE(test4,
  * utf::tolerance(fpc::percent_tolerance(0.05)))
{
  BOOST_TEST( 0.001 == 0.000 );
  BOOST_TEST( 1.100 == 1.101 );
}

BOOST_AUTO_TEST_CASE(test5,
  * utf::tolerance(fpc::percent_tolerance(0.5)))
{
  BOOST_TEST( 0.001 == 0.000 );
  BOOST_TEST( 1.100 == 1.101 );
}

Output

> decorator_13
Running 5 test cases...
test.cpp(9): error: in "test1": check 0.001 == 0.000 has failed [0.001 != 0]. Absolute value exceeds tolerance [|0.001| > 0.0005]
test.cpp(10): error: in "test1": check 1.100 == 1.101 has failed [1.1000000000000001 != 1.101]. Relative difference exceeds tolerance [0.000908265 > 0.0005]
test.cpp(21): error: in "test3": check 0.001 == 0.000 has failed [0.001 != 0]. Absolute value exceeds tolerance [|0.001| > 0]
test.cpp(22): error: in "test3": check 1.100 == 1.101 has failed [1.1000000000000001 != 1.101]
test.cpp(28): error: in "test4": check 0.001 == 0.000 has failed [0.001 != 0]. Absolute value exceeds tolerance [|0.001| > 0.0005]
test.cpp(29): error: in "test4": check 1.100 == 1.101 has failed [1.1000000000000001 != 1.101]. Relative difference exceeds tolerance [0.000908265 > 0.0005]

*** 6 failures are detected in the test module "decorator_13"

In the above example, in test1, checks on doubles fail because they differ by more what tolerance for doubles specifies. In test2 the tolerance for doubles is greater and therefore the checks succeed. In test3, we specify only tolerance for type float, and since the checks use type double the specified tolerance does not apply. Tolerance in test4 is equivalent to that in test1, therefore its checks also fail. Tolerance in test5 is equivalent to that in test2, therefore its checks also succeed.


PrevUpHomeNext