1 #ifndef TUT_ASSERT_H_GUARD
2 #define TUT_ASSERT_H_GUARD
4 #include "tut_exception.hpp"
8 #if defined(TUT_USE_POSIX)
19 std::ostream &msg_prefix(std::ostream &str, const M &msg)
26 str << ss.rdbuf() << ": ";
38 * Tests provided condition.
41 void ensure(bool cond)
45 // TODO: default ctor?
51 * Tests provided condition.
54 void ensure_not(bool cond)
60 * Tests provided condition.
64 void ensure(const M& msg, bool cond)
73 * Tests provided condition.
77 void ensure_not(const M& msg, bool cond)
83 * Tests two objects for being equal.
86 * NB: both T and Q must have operator << defined somewhere, or
87 * client code will not compile at all!
89 template <typename M, typename LHS, typename RHS>
90 void ensure_equals(const M& msg, const LHS& actual, const RHS& expected)
92 if (expected != actual)
95 detail::msg_prefix(ss,msg)
101 throw failure(ss.str());
105 template <typename LHS, typename RHS>
106 void ensure_equals(const LHS& actual, const RHS& expected)
108 ensure_equals("Values are not equal", actual, expected);
112 void ensure_equals(const M& msg, const double& actual, const double& expected,
113 const double& epsilon = std::numeric_limits<double>::epsilon())
115 const double diff = actual - expected;
117 if ( !((diff <= epsilon) && (diff >= -epsilon )) )
119 std::stringstream ss;
120 detail::msg_prefix(ss,msg)
123 << std::setprecision(16)
124 << "expected " << expected
125 << " actual " << actual
126 << " with precision " << epsilon;
127 throw failure(ss.str());
131 * Tests two objects for being at most in given distance one from another.
132 * Borders are excluded.
135 * NB: T must have operator << defined somewhere, or
136 * client code will not compile at all! Also, T shall have
137 * operators + and -, and be comparable.
139 * TODO: domains are wrong, T - T might not yield T, but Q
141 template <typename M, class T>
142 void ensure_distance(const M& msg, const T& actual, const T& expected, const T& distance)
144 if (expected-distance >= actual || expected+distance <= actual)
146 std::stringstream ss;
147 detail::msg_prefix(ss,msg)
155 throw failure(ss.str());
160 void ensure_distance(const T& actual, const T& expected, const T& distance)
162 ensure_distance<>("Distance is wrong", actual, expected, distance);
166 void ensure_errno(const M& msg, bool cond)
170 #if defined(TUT_USE_POSIX)
172 std::stringstream ss;
173 detail::msg_prefix(ss,msg)
174 << strerror_r(errno, e, sizeof(e));
175 throw failure(ss.str());
183 * Unconditionally fails with message.
185 void fail(const char* msg = "")
191 void fail(const M& msg)
196 } // end of namespace