1
#ifndef TUT_ASSERT_H_GUARD
2
#define TUT_ASSERT_H_GUARD
3
#include <tut/tut_config.hpp>
11
#if defined(TUT_USE_POSIX)
16
#include "tut_exception.hpp"
24
std::ostringstream &msg_prefix(std::ostringstream &str, const M &msg)
26
std::ostringstream ss;
43
* Tests provided condition.
46
void ensure(bool cond)
50
// TODO: default ctor?
56
* Tests provided condition.
59
void ensure_not(bool cond)
65
* Tests provided condition.
69
void ensure(const M& msg, bool cond)
78
* Tests provided condition.
82
void ensure_not(const M& msg, bool cond)
88
* Tests two objects for being equal.
91
* NB: both LHS and RHS must have operator << defined somewhere, or
92
* client code will not compile at all!
94
template <typename M, typename LHS, typename RHS>
95
void ensure_equals(const M& msg, const LHS& actual, const RHS& expected)
97
if (expected != actual)
99
std::ostringstream ss;
100
detail::msg_prefix(ss,msg)
106
throw failure(ss.str());
111
* Tests two pointers for being equal.
114
* NB: both T and Q must have operator << defined somewhere, or
115
* client code will not compile at all!
117
template <typename M, typename LHS, typename RHS>
118
void ensure_equals(const M& msg, const LHS * const actual, const RHS * const expected)
120
if (expected != actual)
122
std::ostringstream ss;
123
detail::msg_prefix(ss,msg)
129
throw failure(ss.str());
134
void ensure_equals(const M& msg, const double& actual, const double& expected, const double& epsilon)
136
const double diff = actual - expected;
138
if ( !((diff <= epsilon) && (diff >= -epsilon )) )
140
std::ostringstream ss;
141
detail::msg_prefix(ss,msg)
144
<< std::setprecision(16)
145
<< "expected `" << expected
146
<< "` actual `" << actual
147
<< "` with precision `" << epsilon << "`";
148
throw failure(ss.str());
153
void ensure_equals(const M& msg, const double& actual, const double& expected)
155
ensure_equals(msg, actual, expected, std::numeric_limits<double>::epsilon());
158
template <typename LHS, typename RHS>
159
void ensure_equals(const LHS& actual, const RHS& expected)
161
ensure_equals("Values are not equal", actual, expected);
165
template<typename LhsIterator, typename RhsIterator>
166
void ensure_equals(const std::string &msg,
167
const LhsIterator &lhs_begin, const LhsIterator &lhs_end,
168
const RhsIterator &rhs_begin, const RhsIterator &rhs_end)
170
typename std::iterator_traits<LhsIterator>::difference_type lhs_size = std::distance(lhs_begin, lhs_end);
171
typename std::iterator_traits<RhsIterator>::difference_type rhs_size = std::distance(rhs_begin, rhs_end);
173
if(lhs_size < rhs_size)
175
ensure_equals(msg + ": range is too short", lhs_size, rhs_size);
178
if(lhs_size > rhs_size)
180
ensure_equals(msg + ": range is too long", lhs_size, rhs_size);
183
assert(lhs_size == rhs_size);
185
LhsIterator lhs_i = lhs_begin;
186
RhsIterator rhs_i = rhs_begin;
187
while( (lhs_i != lhs_end) && (rhs_i != rhs_end) )
191
std::ostringstream ss;
192
detail::msg_prefix(ss,msg)
193
<< "expected `" << *rhs_i
194
<< "` actual `" << *lhs_i
195
<< "` at offset " << std::distance(lhs_begin, lhs_i);
196
throw failure(ss.str());
203
assert(lhs_i == lhs_end);
204
assert(rhs_i == rhs_end);
207
template<typename LhsIterator, typename RhsIterator>
208
void ensure_equals(const LhsIterator &lhs_begin, const LhsIterator &lhs_end,
209
const RhsIterator &rhs_begin, const RhsIterator &rhs_end)
211
ensure_equals("Ranges are not equal", lhs_begin, lhs_end, rhs_begin, rhs_end);
214
template<typename LhsType, typename RhsType>
215
void ensure_equals(const LhsType *lhs_begin, const LhsType *lhs_end,
216
const RhsType *rhs_begin, const RhsType *rhs_end)
218
ensure_equals("Ranges are not equal", lhs_begin, lhs_end, rhs_begin, rhs_end);
222
* Tests two objects for being at most in given distance one from another.
223
* Borders are excluded.
226
* NB: T must have operator << defined somewhere, or
227
* client code will not compile at all! Also, T shall have
228
* operators + and -, and be comparable.
230
* TODO: domains are wrong, T - T might not yield T, but Q
232
template <typename M, class T>
233
void ensure_distance(const M& msg, const T& actual, const T& expected, const T& distance)
235
if (expected-distance >= actual || expected+distance <= actual)
237
std::ostringstream ss;
238
detail::msg_prefix(ss,msg)
246
throw failure(ss.str());
251
void ensure_distance(const T& actual, const T& expected, const T& distance)
253
ensure_distance<>("Distance is wrong", actual, expected, distance);
257
void ensure_errno(const M& msg, bool cond)
261
#if defined(TUT_USE_POSIX)
263
std::ostringstream ss;
264
detail::msg_prefix(ss,msg)
265
<< strerror_r(errno, e, sizeof(e));
266
throw failure(ss.str());
274
* Unconditionally fails with message.
276
void fail(const char* msg = "")
282
void fail(const M& msg)
288
* Mark test case as known failure and skip execution.
290
void skip(const char* msg = "")
296
void skip(const M& msg)
301
} // end of namespace