📄 testassert.h
字号:
#ifndef CPPUNIT_TESTASSERT_H#define CPPUNIT_TESTASSERT_H#include <cppunit/Portability.h>#include <cppunit/Exception.h>#include <cppunit/Asserter.h>#include <cppunit/portability/Stream.h>#include <stdio.h>#include <float.h> // For struct assertion_traits<double>CPPUNIT_NS_BEGIN/*! \brief Traits used by CPPUNIT_ASSERT_EQUAL(). * * Here is an example of specialising these traits: * * \code * template<> * struct assertion_traits<std::string> // specialization for the std::string type * { * static bool equal( const std::string& x, const std::string& y ) * { * return x == y; * } * * static std::string toString( const std::string& x ) * { * std::string text = '"' + x + '"'; // adds quote around the string to see whitespace * OStringStream ost; * ost << text; * return ost.str(); * } * }; * \endcode */template <class T>struct assertion_traits { static bool equal( const T& x, const T& y ) { return x == y; } static std::string toString( const T& x ) { OStringStream ost; ost << x; return ost.str(); }};/*! \brief Traits used by CPPUNIT_ASSERT_DOUBLES_EQUAL(). * * This specialisation from @c struct @c assertion_traits<> ensures that * doubles are converted in full, instead of being rounded to the default * 6 digits of precision. Use the system defined ISO C99 macro DBL_DIG * within float.h is available to define the maximum precision, otherwise * use the hard-coded maximum precision of 15. */template <>struct assertion_traits<double>{ static bool equal( double x, double y ) { return x == y; } static std::string toString( double x ) {#ifdef DBL_DIG const int precision = DBL_DIG;#else const int precision = 15;#endif // #ifdef DBL_DIG char buffer[128];#ifdef __STDC_SECURE_LIB__ // Use secure version with visual studio 2005 to avoid warning. sprintf_s(buffer, sizeof(buffer), "%.*g", precision, x); #else sprintf(buffer, "%.*g", precision, x); #endif return buffer; }};/*! \brief (Implementation) Asserts that two objects of the same type are equals. * Use CPPUNIT_ASSERT_EQUAL instead of this function. * \sa assertion_traits, Asserter::failNotEqual(). */template <class T>void assertEquals( const T& expected, const T& actual, SourceLine sourceLine, const std::string &message ){ if ( !assertion_traits<T>::equal(expected,actual) ) // lazy toString conversion... { Asserter::failNotEqual( assertion_traits<T>::toString(expected), assertion_traits<T>::toString(actual), sourceLine, message ); }}/*! \brief (Implementation) Asserts that two double are equals given a tolerance. * Use CPPUNIT_ASSERT_DOUBLES_EQUAL instead of this function. * \sa Asserter::failNotEqual(). */void CPPUNIT_API assertDoubleEquals( double expected, double actual, double delta, SourceLine sourceLine, const std::string &message );/* A set of macros which allow us to get the line number * and file name at the point of an error. * Just goes to show that preprocessors do have some * redeeming qualities. */#if CPPUNIT_HAVE_CPP_SOURCE_ANNOTATION/** Assertions that a condition is \c true. * \ingroup Assertions */#define CPPUNIT_ASSERT(condition) \ ( CPPUNIT_NS::Asserter::failIf( !(condition), \ CPPUNIT_NS::Message( "assertion failed", \ "Expression: " #condition), \ CPPUNIT_SOURCELINE() ) )#else#define CPPUNIT_ASSERT(condition) \ ( CPPUNIT_NS::Asserter::failIf( !(condition), \ CPPUNIT_NS::Message( "assertion failed" ), \ CPPUNIT_SOURCELINE() ) )#endif/** Assertion with a user specified message. * \ingroup Assertions * \param message Message reported in diagnostic if \a condition evaluates * to \c false. * \param condition If this condition evaluates to \c false then the * test failed. */#define CPPUNIT_ASSERT_MESSAGE(message,condition) \ ( CPPUNIT_NS::Asserter::failIf( !(condition), \ CPPUNIT_NS::Message( "assertion failed", \ "Expression: " \ #condition, \ message ), \ CPPUNIT_SOURCELINE() ) )/** Fails with the specified message. * \ingroup Assertions * \param message Message reported in diagnostic. */#define CPPUNIT_FAIL( message ) \ ( CPPUNIT_NS::Asserter::fail( CPPUNIT_NS::Message( "forced failure", \ message ), \ CPPUNIT_SOURCELINE() ) )#ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED/// Generalized macro for primitive value comparisons#define CPPUNIT_ASSERT_EQUAL(expected,actual) \ ( CPPUNIT_NS::assertEquals( (expected), \ (actual), \ __LINE__, __FILE__ ) )#else/** Asserts that two values are equals. * \ingroup Assertions * * Equality and string representation can be defined with * an appropriate CppUnit::assertion_traits class. * * A diagnostic is printed if actual and expected values disagree. * * Requirement for \a expected and \a actual parameters: * - They are exactly of the same type * - They are serializable into a std::strstream using operator <<. * - They can be compared using operator ==. * * The last two requirements (serialization and comparison) can be * removed by specializing the CppUnit::assertion_traits. */#define CPPUNIT_ASSERT_EQUAL(expected,actual) \ ( CPPUNIT_NS::assertEquals( (expected), \ (actual), \ CPPUNIT_SOURCELINE(), \ "" ) )/** Asserts that two values are equals, provides additional message on failure. * \ingroup Assertions * * Equality and string representation can be defined with * an appropriate assertion_traits class. * * A diagnostic is printed if actual and expected values disagree. * The message is printed in addition to the expected and actual value * to provide additional information. * * Requirement for \a expected and \a actual parameters: * - They are exactly of the same type * - They are serializable into a std::strstream using operator <<. * - They can be compared using operator ==. * * The last two requirements (serialization and comparison) can be * removed by specializing the CppUnit::assertion_traits.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -