/
tut_assert.hpp
320 lines (278 loc) · 7.07 KB
/
tut_assert.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#ifndef TUT_ASSERT_H_GUARD
#define TUT_ASSERT_H_GUARD
#include <tut/tut_config.hpp>
#include <limits>
#include <iomanip>
#include <iterator>
#include <cassert>
#include <cmath>
#if defined(TUT_USE_POSIX)
#include <errno.h>
#include <cstring>
#endif
#include "tut_exception.hpp"
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-function"
#elif defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#endif
namespace tut
{
namespace detail
{
template<typename M>
std::ostringstream &msg_prefix(std::ostringstream &str, const M &msg)
{
std::ostringstream ss;
ss << msg;
if(!ss.str().empty())
{
str << msg << ": ";
}
return str;
}
}
namespace
{
/**
* Tests provided condition.
* Throws if false.
*/
void ensure(bool cond)
{
if (!cond)
{
// TODO: default ctor?
throw failure("");
}
}
/**
* Tests provided condition.
* Throws if true.
*/
void ensure_not(bool cond)
{
ensure(!cond);
}
/**
* Tests provided condition.
* Throws if false.
*/
template <typename M>
void ensure(const M& msg, bool cond)
{
if (!cond)
{
throw failure(msg);
}
}
/**
* Tests provided condition.
* Throws if true.
*/
template <typename M>
void ensure_not(const M& msg, bool cond)
{
ensure(msg, !cond);
}
/**
* Tests two objects for being equal.
* Throws if false.
*
* NB: both LHS and RHS must have operator << defined somewhere, or
* client code will not compile at all!
*/
template <typename M, typename LHS, typename RHS>
void ensure_equals(const M& msg, const LHS& actual, const RHS& expected)
{
if (expected != actual)
{
std::ostringstream ss;
detail::msg_prefix(ss,msg)
<< "expected `"
<< expected
<< "` actual `"
<< actual
<< "`";
throw failure(ss.str());
}
}
/**
* Tests two pointers for being equal.
* Throws if false.
*
* NB: both T and Q must have operator << defined somewhere, or
* client code will not compile at all!
*/
template <typename M, typename LHS, typename RHS>
void ensure_equals(const M& msg, const LHS * const actual, const RHS * const expected)
{
if (expected != actual)
{
std::ostringstream ss;
detail::msg_prefix(ss,msg)
<< "expected `"
<< (void*)expected
<< "` actual `"
<< (void*)actual
<< "`";
throw failure(ss.str());
}
}
template<typename M>
void ensure_equals(const M& msg, const double& actual, const double& expected, const double& epsilon)
{
const double diff = actual - expected;
if ( !((diff <= epsilon) && (diff >= -epsilon )) )
{
std::ostringstream ss;
detail::msg_prefix(ss,msg)
<< std::scientific
<< std::showpoint
<< std::setprecision(16)
<< "expected `" << expected
<< "` actual `" << actual
<< "` with precision `" << epsilon << "`";
throw failure(ss.str());
}
}
template<typename M>
void ensure_equals(const M& msg, const double& actual, const double& expected)
{
ensure_equals(msg, actual, expected, std::numeric_limits<double>::epsilon());
}
template <typename LHS, typename RHS>
void ensure_equals(const LHS& actual, const RHS& expected)
{
ensure_equals("Values are not equal", actual, expected);
}
template<typename LhsIterator, typename RhsIterator>
void ensure_equals(const std::string &msg,
const LhsIterator &lhs_begin, const LhsIterator &lhs_end,
const RhsIterator &rhs_begin, const RhsIterator &rhs_end)
{
typename std::iterator_traits<LhsIterator>::difference_type lhs_size = std::distance(lhs_begin, lhs_end);
typename std::iterator_traits<RhsIterator>::difference_type rhs_size = std::distance(rhs_begin, rhs_end);
if(lhs_size < rhs_size)
{
ensure_equals(msg + ": range is too short", lhs_size, rhs_size);
}
if(lhs_size > rhs_size)
{
ensure_equals(msg + ": range is too long", lhs_size, rhs_size);
}
assert(lhs_size == rhs_size);
LhsIterator lhs_i = lhs_begin;
RhsIterator rhs_i = rhs_begin;
while( (lhs_i != lhs_end) && (rhs_i != rhs_end) )
{
if(*lhs_i != *rhs_i)
{
std::ostringstream ss;
detail::msg_prefix(ss,msg)
<< "expected `" << *rhs_i
<< "` actual `" << *lhs_i
<< "` at offset " << std::distance(lhs_begin, lhs_i);
throw failure(ss.str());
}
lhs_i++;
rhs_i++;
}
assert(lhs_i == lhs_end);
assert(rhs_i == rhs_end);
}
template<typename LhsIterator, typename RhsIterator>
void ensure_equals(const LhsIterator &lhs_begin, const LhsIterator &lhs_end,
const RhsIterator &rhs_begin, const RhsIterator &rhs_end)
{
ensure_equals("Ranges are not equal", lhs_begin, lhs_end, rhs_begin, rhs_end);
}
template<typename LhsType, typename RhsType>
void ensure_equals(const LhsType *lhs_begin, const LhsType *lhs_end,
const RhsType *rhs_begin, const RhsType *rhs_end)
{
ensure_equals("Ranges are not equal", lhs_begin, lhs_end, rhs_begin, rhs_end);
}
/**
* Tests two objects for being at most in given distance one from another.
* Borders are excluded.
* Throws if false.
*
* NB: T must have operator << defined somewhere, or
* client code will not compile at all! Also, T shall have
* operators + and -, and be comparable.
*
* TODO: domains are wrong, T - T might not yield T, but Q
*/
template <typename M, class T>
void ensure_distance(const M& msg, const T& actual, const T& expected, const T& distance)
{
if (expected-distance >= actual || expected+distance <= actual)
{
std::ostringstream ss;
detail::msg_prefix(ss,msg)
<< " expected `"
<< expected-distance
<< "` - `"
<< expected+distance
<< "` actual `"
<< actual
<< "`";
throw failure(ss.str());
}
}
template <class T>
void ensure_distance(const T& actual, const T& expected, const T& distance)
{
ensure_distance<>("Distance is wrong", actual, expected, distance);
}
template<typename M>
void ensure_errno(const M& msg, bool cond)
{
if(!cond)
{
#if defined(TUT_USE_POSIX)
char e[512];
std::ostringstream ss;
detail::msg_prefix(ss,msg)
<< strerror_r(errno, e, sizeof(e));
throw failure(ss.str());
#else
throw failure(msg);
#endif
}
}
/**
* Unconditionally fails with message.
*/
void fail(const char* msg = "")
{
throw failure(msg);
}
template<typename M>
void fail(const M& msg)
{
throw failure(msg);
}
/**
* Mark test case as known failure and skip execution.
*/
void skip(const char* msg = "")
{
throw skipped(msg);
}
template<typename M>
void skip(const M& msg)
{
throw skipped(msg);
}
} // end of namespace
}
#ifdef __clang__
#pragma clang diagnostic pop
#elif defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
#endif