mirror of
https://github.com/catchorg/Catch2.git
synced 2025-05-24 21:49:24 +00:00
Reformatting
This commit is contained in:
parent
6acb36a996
commit
2efc1146bf
11 changed files with 211 additions and 545 deletions
|
@ -1,13 +1,9 @@
|
|||
/*
|
||||
* 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
|
||||
|
@ -17,140 +13,78 @@
|
|||
#include <cmath>
|
||||
#include <limits>
|
||||
|
||||
namespace Catch
|
||||
{
|
||||
namespace Detail
|
||||
{
|
||||
class Approx
|
||||
{
|
||||
public:
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
explicit Approx
|
||||
(
|
||||
double value
|
||||
)
|
||||
: m_epsilon( std::numeric_limits<float>::epsilon()*100 ),
|
||||
m_scale( 1.0 ),
|
||||
m_value( value )
|
||||
{
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
Approx
|
||||
(
|
||||
const Approx& other
|
||||
)
|
||||
: m_epsilon( other.m_epsilon ),
|
||||
m_scale( other.m_scale ),
|
||||
m_value( other.m_value )
|
||||
{
|
||||
}
|
||||
namespace Catch {
|
||||
namespace Detail {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
static Approx custom
|
||||
()
|
||||
{
|
||||
return Approx( 0 );
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
Approx operator()
|
||||
(
|
||||
double value
|
||||
)
|
||||
{
|
||||
Approx approx( value );
|
||||
approx.epsilon( m_epsilon );
|
||||
approx.scale( m_scale );
|
||||
return approx;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
friend bool operator ==
|
||||
(
|
||||
double lhs,
|
||||
const Approx& rhs
|
||||
)
|
||||
{
|
||||
// Thanks to Richard Harris for his help refining this formula
|
||||
return fabs( lhs - rhs.m_value ) < rhs.m_epsilon * (rhs.m_scale + (std::max)( fabs(lhs), fabs(rhs.m_value) ) );
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
friend bool operator ==
|
||||
(
|
||||
const Approx& lhs,
|
||||
double rhs
|
||||
)
|
||||
{
|
||||
return operator==( rhs, lhs );
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
friend bool operator !=
|
||||
(
|
||||
double lhs,
|
||||
const Approx& rhs
|
||||
)
|
||||
{
|
||||
return !operator==( lhs, rhs );
|
||||
}
|
||||
class Approx {
|
||||
public:
|
||||
explicit Approx ( double value )
|
||||
: m_epsilon( std::numeric_limits<float>::epsilon()*100 ),
|
||||
m_scale( 1.0 ),
|
||||
m_value( value )
|
||||
{}
|
||||
|
||||
Approx( const Approx& other )
|
||||
: m_epsilon( other.m_epsilon ),
|
||||
m_scale( other.m_scale ),
|
||||
m_value( other.m_value )
|
||||
{}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
friend bool operator !=
|
||||
(
|
||||
const Approx& lhs,
|
||||
double rhs
|
||||
)
|
||||
{
|
||||
return !operator==( rhs, lhs );
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
Approx& epsilon
|
||||
(
|
||||
double newEpsilon
|
||||
)
|
||||
{
|
||||
m_epsilon = newEpsilon;
|
||||
return *this;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
Approx& scale
|
||||
(
|
||||
double newScale
|
||||
)
|
||||
{
|
||||
m_scale = newScale;
|
||||
return *this;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
std::string toString() const
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "Approx( " << m_value << ")";
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
private:
|
||||
double m_epsilon;
|
||||
double m_scale;
|
||||
double m_value;
|
||||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template<>
|
||||
inline std::string toString<Detail::Approx>
|
||||
(
|
||||
const Detail::Approx& value
|
||||
)
|
||||
{
|
||||
return value.toString();
|
||||
}
|
||||
static Approx custom() {
|
||||
return Approx( 0 );
|
||||
}
|
||||
|
||||
Approx operator()( double value ) {
|
||||
Approx approx( value );
|
||||
approx.epsilon( m_epsilon );
|
||||
approx.scale( m_scale );
|
||||
return approx;
|
||||
}
|
||||
|
||||
friend bool operator == ( double lhs, const Approx& rhs ) {
|
||||
// Thanks to Richard Harris for his help refining this formula
|
||||
return fabs( lhs - rhs.m_value ) < rhs.m_epsilon * (rhs.m_scale + (std::max)( fabs(lhs), fabs(rhs.m_value) ) );
|
||||
}
|
||||
|
||||
friend bool operator == ( const Approx& lhs, double rhs ) {
|
||||
return operator==( rhs, lhs );
|
||||
}
|
||||
|
||||
friend bool operator != ( double lhs, const Approx& rhs ) {
|
||||
return !operator==( lhs, rhs );
|
||||
}
|
||||
|
||||
friend bool operator != ( const Approx& lhs, double rhs ) {
|
||||
return !operator==( rhs, lhs );
|
||||
}
|
||||
|
||||
Approx& epsilon( double newEpsilon ) {
|
||||
m_epsilon = newEpsilon;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Approx& scale( double newScale ) {
|
||||
m_scale = newScale;
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::string toString() const {
|
||||
std::ostringstream oss;
|
||||
oss << "Approx( " << m_value << ")";
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
private:
|
||||
double m_epsilon;
|
||||
double m_scale;
|
||||
double m_value;
|
||||
};
|
||||
}
|
||||
|
||||
template<>
|
||||
inline std::string toString<Detail::Approx>( const Detail::Approx& value ) {
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
} // end namespace Catch
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue