28 #ifndef PT_UNIT_ASSERTION_H
29 #define PT_UNIT_ASSERTION_H
31 #include <Pt/Unit/Api.h>
32 #include "Pt/SourceInfo.h"
98 const char* what()
const {
return _what.c_str(); }
105 namespace {
bool testCond =
false; }
107 inline bool getFalse()
119 #define PT_UNIT_ASSERT(cond) \
122 throw Pt::Unit::Assertion(#cond, PT_SOURCEINFO); \
123 } while (::Pt::Unit::testCond)
135 #define PT_UNIT_ASSERT_MSG(cond, what) \
139 std::ostringstream _pt_msg; \
141 throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
143 } while (::Pt::Unit::testCond)
150 #define PT_UNIT_ASSERT_EQUALS(value1, value2) \
152 if( ! ((value1) == (value2)) ) \
154 std::ostringstream _pt_msg; \
155 _pt_msg << "not equal: value1 (" #value1 ")=<" << value1 << "> value2 (" #value2 ")=<" << value2 << '>'; \
156 throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
158 } while (::Pt::Unit::testCond)
170 #define PT_UNIT_ASSERT_EQUAL(value1, value2) \
172 if( ! ((value1) == (value2)) ) \
174 std::ostringstream _pt_msg; \
175 _pt_msg << "not equal: (" #value1 ")=<" << value1 << ">, (" #value2 ")=<" << value2 << '>'; \
176 throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
178 } while (::Pt::Unit::testCond)
191 #define PT_UNIT_ASSERT_NEAR(value1, value2) \
193 const double _pt_v1 = static_cast<double>(value1); \
194 const double _pt_v2 = static_cast<double>(value2); \
195 const double _pt_rel_eps = std::sqrt(std::numeric_limits<double>::epsilon()); \
196 const double _pt_abs_eps = std::numeric_limits<double>::min(); \
197 const double _pt_scale = std::fmax(std::fabs(_pt_v1), std::fabs(_pt_v2)); \
198 const double _pt_eps = std::fmax(_pt_rel_eps * _pt_scale, _pt_abs_eps); \
199 if( std::fabs(_pt_v1 - _pt_v2) > _pt_eps ) \
201 std::ostringstream _pt_msg; \
202 _pt_msg << "not near: (" #value1 ")=<" << _pt_v1 \
203 << ">, (" #value2 ")=<" << _pt_v2 \
204 << ">, eps=<" << _pt_eps \
205 << ">, diff=<" << std::fabs(_pt_v1 - _pt_v2) << '>'; \
206 throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
208 } while (::Pt::Unit::testCond)
220 #define PT_UNIT_ASSERT_THROW(cond, EX) \
228 catch(const _pt_ex &) \
230 std::ostringstream _pt_msg; \
231 _pt_msg << "exception of type " #EX " expected in " #cond; \
232 throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
236 } while (::Pt::Unit::testCond)
246 #define PT_UNIT_ASSERT_NOTHROW(cond) \
252 catch(const std::exception& e) \
254 throw Pt::Unit::Assertion( \
255 std::string("unexpected exception of type ") + typeid(e).name() + ": " + e.what(), \
260 throw Pt::Unit::Assertion("unexpected exception." , PT_SOURCEINFO); \
262 } while (::Pt::Unit::testCond)
272 #define PT_UNIT_FAIL(what) \
274 std::ostringstream _pt_msg; \
276 throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
277 } while (::Pt::Unit::testCond)
283 #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:80
Source code info class.
Definition: SourceInfo.h:101