1// (C) Copyright Gennadiy Rozental 2001.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at
4// http://www.boost.org/LICENSE_1_0.txt)
5
6// See http://www.boost.org/libs/test for the library home page.
7//
8//! @file
9//! @brief Floating point comparison tolerance manipulators
10//!
11//! This file defines several manipulators for floating point comparison. These
12//! manipulators are intended to be used with BOOST_TEST.
13// ***************************************************************************
14
15#ifndef BOOST_TEST_TOOLS_DETAIL_TOLERANCE_MANIP_HPP_012705GER
16#define BOOST_TEST_TOOLS_DETAIL_TOLERANCE_MANIP_HPP_012705GER
17
18// Boost Test
19#include <boost/test/tools/detail/fwd.hpp>
20#include <boost/test/tools/detail/indirections.hpp>
21
22#include <boost/test/tools/fpc_tolerance.hpp>
23#include <boost/test/tools/floating_point_comparison.hpp>
24
25#include <boost/test/detail/suppress_warnings.hpp>
26
27//____________________________________________________________________________//
28
29namespace boost {
30namespace test_tools {
31namespace tt_detail {
32
33// ************************************************************************** //
34// ************** fpc tolerance manipulator ************** //
35// ************************************************************************** //
36
37template<typename FPT>
38struct tolerance_manip {
39 explicit tolerance_manip( FPT const & tol ) : m_value( tol ) {}
40
41 FPT m_value;
42};
43
44//____________________________________________________________________________//
45
46struct tolerance_manip_delay {};
47
48template<typename FPT>
49inline tolerance_manip<FPT>
50operator%( FPT v, tolerance_manip_delay const& )
51{
52 BOOST_STATIC_ASSERT_MSG( (fpc::tolerance_based<FPT>::value),
53 "tolerance should be specified using a floating points type" );
54
55 return tolerance_manip<FPT>( FPT(v / 100) );
56}
57
58//____________________________________________________________________________//
59
60template<typename E, typename FPT>
61inline assertion_result
62operator<<(assertion_evaluate_t<E> const& ae, tolerance_manip<FPT> const& tol)
63{
64 local_fpc_tolerance<FPT> lt( tol.m_value );
65
66 return ae.m_e.evaluate();
67}
68
69//____________________________________________________________________________//
70
71template<typename FPT>
72inline int
73operator<<( unit_test::lazy_ostream const&, tolerance_manip<FPT> const& ) { return 0; }
74
75//____________________________________________________________________________//
76
77template<typename FPT>
78inline check_type
79operator<<( assertion_type const& /*at*/, tolerance_manip<FPT> const& ) { return CHECK_BUILT_ASSERTION; }
80
81//____________________________________________________________________________//
82
83} // namespace tt_detail
84
85
86/*! Tolerance manipulator
87 *
88 * These functions return a manipulator that can be used in conjunction with BOOST_TEST
89 * in order to specify the tolerance with which floating point comparisons are made.
90 */
91template<typename FPT>
92inline tt_detail::tolerance_manip<FPT>
93tolerance( FPT v )
94{
95 BOOST_STATIC_ASSERT_MSG( (fpc::tolerance_based<FPT>::value),
96 "tolerance only for floating points" );
97
98 return tt_detail::tolerance_manip<FPT>( v );
99}
100
101//____________________________________________________________________________//
102
103//! @overload tolerance( FPT v )
104template<typename FPT>
105inline tt_detail::tolerance_manip<FPT>
106tolerance( fpc::percent_tolerance_t<FPT> v )
107{
108 BOOST_STATIC_ASSERT_MSG( (fpc::tolerance_based<FPT>::value),
109 "tolerance only for floating points" );
110
111 return tt_detail::tolerance_manip<FPT>( static_cast<FPT>(v.m_value / 100) );
112}
113
114//____________________________________________________________________________//
115
116//! @overload tolerance( FPT v )
117inline tt_detail::tolerance_manip_delay
118tolerance()
119{
120 return tt_detail::tolerance_manip_delay();
121}
122
123//____________________________________________________________________________//
124
125} // namespace test_tools
126} // namespace boost
127
128#include <boost/test/detail/enable_warnings.hpp>
129
130#endif // BOOST_TEST_TOOLS_DETAIL_TOLERANCE_MANIP_HPP_012705GER
131