28 #ifndef PT_UNIT_ASSERTION_H
29 #define PT_UNIT_ASSERTION_H
31 #include <Pt/Unit/Api.h>
32 #include "Pt/SourceInfo.h"
97 const char* what()
const {
return _what.c_str(); }
104 namespace {
bool testCond =
false; }
106 inline bool getFalse()
118 #define PT_UNIT_ASSERT(cond) \
121 throw Pt::Unit::Assertion(#cond, PT_SOURCEINFO); \
122 } while (::Pt::Unit::testCond)
134 #define PT_UNIT_ASSERT_MSG(cond, what) \
138 std::ostringstream _pt_msg; \
140 throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
142 } while (::Pt::Unit::testCond)
149 #define PT_UNIT_ASSERT_EQUALS(value1, value2) \
151 if( ! ((value1) == (value2)) ) \
153 std::ostringstream _pt_msg; \
154 _pt_msg << "not equal: value1 (" #value1 ")=<" << value1 << "> value2 (" #value2 ")=<" << value2 << '>'; \
155 throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
157 } while (::Pt::Unit::testCond)
169 #define PT_UNIT_ASSERT_EQUAL(value1, value2) \
171 if( ! ((value1) == (value2)) ) \
173 std::ostringstream _pt_msg; \
174 _pt_msg << "not equal: (" #value1 ")=<" << value1 << ">, (" #value2 ")=<" << value2 << '>'; \
175 throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
177 } while (::Pt::Unit::testCond)
190 #define PT_UNIT_ASSERT_NEAR(value1, value2) \
192 const double _pt_v1 = static_cast<double>(value1); \
193 const double _pt_v2 = static_cast<double>(value2); \
194 const double _pt_rel_eps = std::sqrt(std::numeric_limits<double>::epsilon()); \
195 const double _pt_abs_eps = std::numeric_limits<double>::min(); \
196 const double _pt_scale = std::fmax(std::fabs(_pt_v1), std::fabs(_pt_v2)); \
197 const double _pt_eps = std::fmax(_pt_rel_eps * _pt_scale, _pt_abs_eps); \
198 if( std::fabs(_pt_v1 - _pt_v2) > _pt_eps ) \
200 std::ostringstream _pt_msg; \
201 _pt_msg << "not near: (" #value1 ")=<" << _pt_v1 \
202 << ">, (" #value2 ")=<" << _pt_v2 \
203 << ">, eps=<" << _pt_eps \
204 << ">, diff=<" << std::fabs(_pt_v1 - _pt_v2) << '>'; \
205 throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
207 } while (::Pt::Unit::testCond)
219 #define PT_UNIT_ASSERT_THROW(cond, EX) \
227 catch(const _pt_ex &) \
229 std::ostringstream _pt_msg; \
230 _pt_msg << "exception of type " #EX " expected in " #cond; \
231 throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
235 } while (::Pt::Unit::testCond)
245 #define PT_UNIT_ASSERT_NOTHROW(cond) \
251 catch(const std::exception& e) \
253 throw Pt::Unit::Assertion( \
254 std::string("unexpected exception of type ") + typeid(e).name() + ": " + e.what(), \
259 throw Pt::Unit::Assertion("unexpected exception." , PT_SOURCEINFO); \
261 } while (::Pt::Unit::testCond)
271 #define PT_UNIT_FAIL(what) \
273 std::ostringstream _pt_msg; \
275 throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
276 } while (::Pt::Unit::testCond)
282 #endif // include guard
Core module.
Definition: Allocator.h:33
const Pt::SourceInfo & sourceInfo() const
Information where assertion failed.
Assertion(const std::string &what, const SourceInfo &si)
Construct from a message and source info.
Test Assertion exception
Definition: Assertion.h:79
Source code info class.
Definition: SourceInfo.h:101