catch2/include/internal/catch_approx.hpp

81 lines
2.1 KiB
C++
Raw Normal View History

2011-04-28 09:03:28 +02:00
/*
* catch_approx.hpp
* Catch
*
* Created by Phil on 28/04/2011.
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
*
* 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)
*
*/
#ifndef TWOBLUECUBES_CATCH_APPROX_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_APPROX_HPP_INCLUDED
#include "catch_capture.hpp"
#include <cmath>
namespace Catch
{
namespace Detail
{
class Approx
{
public:
///////////////////////////////////////////////////////////////////////////
explicit Approx
(
double d
)
: m_epsilon( 1e-10 ),
m_scale( 1.0 ),
m_d( d )
2011-04-28 09:03:28 +02:00
{
}
///////////////////////////////////////////////////////////////////////////
template<typename T>
friend bool operator ==
(
const T& lhs,
const Approx& rhs
)
2011-04-28 09:03:28 +02:00
{
// Thanks to Richard Harris for his help refining this formula
return fabs( lhs - rhs.m_d ) < rhs.m_epsilon * (rhs.m_scale + (std::max)( fabs(lhs), fabs(rhs.m_d) ) );
2011-04-28 09:03:28 +02:00
}
///////////////////////////////////////////////////////////////////////////
template<typename T>
friend bool operator !=
(
const T& lhs,
const Approx& rhs
)
2011-04-28 09:03:28 +02:00
{
return !operator==( lhs, rhs );
2011-04-28 09:03:28 +02:00
}
double m_epsilon;
double m_scale;
2011-04-28 09:03:28 +02:00
double m_d;
};
}
///////////////////////////////////////////////////////////////////////////////
template<>
inline std::string toString<Detail::Approx>
(
const Detail::Approx& value
)
{
std::ostringstream oss;
oss << "Approx( " << value.m_d << ")";
return oss.str();
}
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_APPROX_HPP_INCLUDED