1 //          Copyright Joakim Karlsson & Kim Gräsman 2010-2012.
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 #ifndef SNOWHOUSE_EQUALSWITHDELTACONSTRAINT_H
7 #define SNOWHOUSE_EQUALSWITHDELTACONSTRAINT_H
8 
9 #include "expressions/expression.h"
10 
11 namespace snowhouse
12 {
13   template<typename ExpectedType, typename DeltaType>
14   struct EqualsWithDeltaConstraint : Expression<EqualsWithDeltaConstraint<ExpectedType, DeltaType> >
15   {
EqualsWithDeltaConstraintEqualsWithDeltaConstraint16     EqualsWithDeltaConstraint(const ExpectedType& expected, const DeltaType& delta)
17         : m_expected(expected), m_delta(delta)
18     {
19     }
20 
21     template<typename ActualType>
operatorEqualsWithDeltaConstraint22     bool operator()(const ActualType& actual) const
23     {
24       return ((m_expected <= (actual + m_delta)) && (m_expected >= (actual - m_delta)));
25     }
26 
27     ExpectedType m_expected;
28     DeltaType m_delta;
29   };
30 
31   template<typename ExpectedType, typename DeltaType>
EqualsWithDelta(const ExpectedType & expected,const DeltaType & delta)32   inline EqualsWithDeltaConstraint<ExpectedType, DeltaType> EqualsWithDelta(const ExpectedType& expected, const DeltaType& delta)
33   {
34     return EqualsWithDeltaConstraint<ExpectedType, DeltaType>(expected, delta);
35   }
36 
37   template<typename ExpectedType, typename DeltaType>
38   struct Stringizer<EqualsWithDeltaConstraint<ExpectedType, DeltaType> >
39   {
40     static std::string ToString(const EqualsWithDeltaConstraint<ExpectedType, DeltaType>& constraint)
41     {
42       std::ostringstream builder;
43       builder << "equal to " << snowhouse::Stringize(constraint.m_expected) << " (+/- " << snowhouse::Stringize(constraint.m_delta) << ")";
44 
45       return builder.str();
46     }
47   };
48 }
49 
50 #endif
51