From: <man...@us...> - 2014-03-24 18:03:23
|
Revision: 3959 http://sourceforge.net/p/modplug/code/3959 Author: manxorist Date: 2014-03-24 18:03:15 +0000 (Mon, 24 Mar 2014) Log Message: ----------- [Ref] Test: Rewrite and simplify the tracker test framework: Avoid catching any exceptions and do not try to display any fancy message boxes. Instead, totally rely on a present debugger to break at the right spot (when a test fails or exactly at the point it throws an exception, where the debugger break due to the exception being uncaught). If no debugger is present, do not even bother to run the test cases at all. [Ref] Test: Rename namespace MptTest to namespace mpt::Test. Modified Paths: -------------- trunk/OpenMPT/libopenmpt/libopenmpt_impl.cpp trunk/OpenMPT/mptrack/Mptrack.cpp trunk/OpenMPT/test/TestTools.h trunk/OpenMPT/test/TestToolsLib.cpp trunk/OpenMPT/test/TestToolsLib.h trunk/OpenMPT/test/TestToolsTracker.h trunk/OpenMPT/test/test.cpp trunk/OpenMPT/test/test.h Modified: trunk/OpenMPT/libopenmpt/libopenmpt_impl.cpp =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmpt_impl.cpp 2014-03-24 17:20:07 UTC (rev 3958) +++ trunk/OpenMPT/libopenmpt/libopenmpt_impl.cpp 2014-03-24 18:03:15 UTC (rev 3959) @@ -33,7 +33,7 @@ #if defined( LIBOPENMPT_BUILD_TEST ) void run_tests() { - MptTest::DoTests(); + mpt::Test::DoTests(); } #endif // LIBOPENMPT_BUILD_TEST Modified: trunk/OpenMPT/mptrack/Mptrack.cpp =================================================================== --- trunk/OpenMPT/mptrack/Mptrack.cpp 2014-03-24 17:20:07 UTC (rev 3958) +++ trunk/OpenMPT/mptrack/Mptrack.cpp 2014-03-24 18:03:15 UTC (rev 3959) @@ -970,9 +970,7 @@ EndWaitCursor(); -#ifdef ENABLE_TESTS - MptTest::DoTests(); -#endif + mpt::Test::DoTests(); if(TrackerSettings::Instance().m_SoundSettingsOpenDeviceAtStartup) { Modified: trunk/OpenMPT/test/TestTools.h =================================================================== --- trunk/OpenMPT/test/TestTools.h 2014-03-24 17:20:07 UTC (rev 3958) +++ trunk/OpenMPT/test/TestTools.h 2014-03-24 18:03:15 UTC (rev 3959) @@ -2,7 +2,7 @@ * TestTools.h * ----------- * Purpose: Unit test framework. - * Notes : Currently somewhat unreadable :/ + * Notes : * Authors: OpenMPT Devs * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. */ Modified: trunk/OpenMPT/test/TestToolsLib.cpp =================================================================== --- trunk/OpenMPT/test/TestToolsLib.cpp 2014-03-24 17:20:07 UTC (rev 3958) +++ trunk/OpenMPT/test/TestToolsLib.cpp 2014-03-24 18:03:15 UTC (rev 3959) @@ -20,8 +20,7 @@ #include <iostream> -namespace MptTest -{ +namespace mpt { namespace Test { int fail_count = 0; @@ -171,7 +170,7 @@ } -} // namespace MptTest +} } // namespace mpt::Test #endif // !MODPLUG_TRACKER Modified: trunk/OpenMPT/test/TestToolsLib.h =================================================================== --- trunk/OpenMPT/test/TestToolsLib.h 2014-03-24 17:20:07 UTC (rev 3958) +++ trunk/OpenMPT/test/TestToolsLib.h 2014-03-24 18:03:15 UTC (rev 3959) @@ -2,7 +2,8 @@ * TestToolsLib.h * -------------- * Purpose: Unit test framework for libopenmpt. - * Notes : Currently somewhat unreadable :/ + * Notes : This is more complex than the OpenMPT version because we cannot + * rely on a debugger and have to deal with exceptions ourselves. * Authors: OpenMPT Devs * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. */ @@ -18,8 +19,7 @@ #include "../common/FlagSet.h" -namespace MptTest -{ +namespace mpt { namespace Test { extern int fail_count; @@ -49,7 +49,7 @@ Context(const Context &c); }; -#define MPT_TEST_CONTEXT_CURRENT() (::MptTest::Context( __FILE__ , __LINE__ )) +#define MPT_TEST_CONTEXT_CURRENT() (::mpt::Test::Context( __FILE__ , __LINE__ )) struct TestFailed @@ -59,7 +59,7 @@ TestFailed() { } }; -} // namespace MptTest +} } // namespace mpt::Test template<typename T> struct ToStringHelper @@ -79,8 +79,7 @@ } }; -namespace MptTest -{ +namespace mpt { namespace Test { class Test { @@ -147,9 +146,9 @@ } } - #define VERIFY_EQUAL(x,y) ::MptTest::Test(::MptTest::FatalityContinue, ::MptTest::VerbosityNormal, #x " == " #y , MPT_TEST_CONTEXT_CURRENT() )( [&](){return (x) ;}, [&](){return (y) ;} ) - #define VERIFY_EQUAL_NONCONT(x,y) ::MptTest::Test(::MptTest::FatalityStop, ::MptTest::VerbosityNormal, #x " == " #y , MPT_TEST_CONTEXT_CURRENT() )( [&](){return (x) ;}, [&](){return (y) ;} ) - #define VERIFY_EQUAL_QUIET_NONCONT(x,y) ::MptTest::Test(::MptTest::FatalityStop, ::MptTest::VerbosityQuiet, #x " == " #y , MPT_TEST_CONTEXT_CURRENT() )( [&](){return (x) ;}, [&](){return (y) ;} ) + #define VERIFY_EQUAL(x,y) ::mpt::Test::Test(::mpt::Test::FatalityContinue, ::mpt::Test::VerbosityNormal, #x " == " #y , MPT_TEST_CONTEXT_CURRENT() )( [&](){return (x) ;}, [&](){return (y) ;} ) + #define VERIFY_EQUAL_NONCONT(x,y) ::mpt::Test::Test(::mpt::Test::FatalityStop, ::mpt::Test::VerbosityNormal, #x " == " #y , MPT_TEST_CONTEXT_CURRENT() )( [&](){return (x) ;}, [&](){return (y) ;} ) + #define VERIFY_EQUAL_QUIET_NONCONT(x,y) ::mpt::Test::Test(::mpt::Test::FatalityStop, ::mpt::Test::VerbosityQuiet, #x " == " #y , MPT_TEST_CONTEXT_CURRENT() )( [&](){return (x) ;}, [&](){return (y) ;} ) #else @@ -173,9 +172,9 @@ } } - #define VERIFY_EQUAL(x,y) ::MptTest::Test(::MptTest::FatalityContinue, ::MptTest::VerbosityNormal, #x " == " #y , MPT_TEST_CONTEXT_CURRENT() )( (x) , (y) ) - #define VERIFY_EQUAL_NONCONT(x,y) ::MptTest::Test(::MptTest::FatalityStop, ::MptTest::VerbosityNormal, #x " == " #y , MPT_TEST_CONTEXT_CURRENT() )( (x) , (y) ) - #define VERIFY_EQUAL_QUIET_NONCONT(x,y) ::MptTest::Test(::MptTest::FatalityStop, ::MptTest::VerbosityQuiet, #x " == " #y , MPT_TEST_CONTEXT_CURRENT() )( (x) , (y) ) + #define VERIFY_EQUAL(x,y) ::mpt::Test::Test(::mpt::Test::FatalityContinue, ::mpt::Test::VerbosityNormal, #x " == " #y , MPT_TEST_CONTEXT_CURRENT() )( (x) , (y) ) + #define VERIFY_EQUAL_NONCONT(x,y) ::mpt::Test::Test(::mpt::Test::FatalityStop, ::mpt::Test::VerbosityNormal, #x " == " #y , MPT_TEST_CONTEXT_CURRENT() )( (x) , (y) ) + #define VERIFY_EQUAL_QUIET_NONCONT(x,y) ::mpt::Test::Test(::mpt::Test::FatalityStop, ::mpt::Test::VerbosityQuiet, #x " == " #y , MPT_TEST_CONTEXT_CURRENT() )( (x) , (y) ) #endif @@ -184,13 +183,13 @@ #define DO_TEST(func) \ do{ \ - ::MptTest::Test test(::MptTest::FatalityStop, ::MptTest::VerbosityNormal, #func , MPT_TEST_CONTEXT_CURRENT() ); \ + ::mpt::Test::Test test(::mpt::Test::FatalityStop, ::mpt::Test::VerbosityNormal, #func , MPT_TEST_CONTEXT_CURRENT() ); \ try { \ test.ShowStart(); \ fail_count = 0; \ func(); \ if(fail_count > 0) { \ - throw ::MptTest::TestFailed(); \ + throw ::mpt::Test::TestFailed(); \ } \ test.ReportPassed(); \ } catch(...) { \ @@ -199,7 +198,7 @@ }while(0) -} // namespace MptTest +} } // namespace mpt::Test #endif // !MODPLUG_TRACKER Modified: trunk/OpenMPT/test/TestToolsTracker.h =================================================================== --- trunk/OpenMPT/test/TestToolsTracker.h 2014-03-24 17:20:07 UTC (rev 3958) +++ trunk/OpenMPT/test/TestToolsTracker.h 2014-03-24 18:03:15 UTC (rev 3959) @@ -2,7 +2,8 @@ * TestToolsTracker.h * ------------------ * Purpose: Unit test framework for OpenMPT. - * Notes : Currently somewhat unreadable :/ + * Notes : Really basic functionality that relies on a debugger that catches + * exceptions and breaks right at the spot where it gets thrown. * Authors: OpenMPT Devs * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. */ @@ -15,76 +16,9 @@ #ifdef MODPLUG_TRACKER -namespace MptTest -{ +namespace mpt { namespace Test { -static noinline void ReportError(const char * file, int line, const char * description, bool abort) -//------------------------------------------------------------------------------------------------- -{ - std::string pos = std::string() + file + "(" + Stringify(line) + "):\n"; - std::string message = pos + "Test failed: '" + description + "'"; - if(IsDebuggerPresent()) - { - message += "\n"; - OutputDebugString(message.c_str()); - } else - { - if(abort) - { - throw std::runtime_error(message); - } else - { - Reporting::Error(message.c_str(), "OpenMPT TestSuite"); - } - } -} - -static noinline void ReportExceptionError(const char * file, int line, const char * description, const char * exception_what) -//--------------------------------------------------------------------------------------------------------------------------- -{ - std::string pos = std::string() + file + "(" + Stringify(line) + "):\n"; - std::string message; - if(exception_what) - { - message = pos + "Test '" + description + "' threw an exception, message:\n" + exception_what; - } else - { - message = pos + "Test '" + description + "' threw an unknown exception."; - } - if(IsDebuggerPresent()) - { - message += "\n"; - OutputDebugString(message.c_str()); - } else - { - Reporting::Error(message.c_str(), "OpenMPT TestSuite"); - } -} - -static noinline void ReportException(const char * const file, const int line, const char * const description, bool rethrow) -//------------------------------------------------------------------------------------------------------------------------- -{ - try - { - throw; // get the exception - } catch(std::exception &e) - { - ReportExceptionError(file, line, description, e.what()); - if(rethrow) - { - throw; - } - } catch(...) - { - ReportExceptionError(file, line, description, nullptr); - if(rethrow) - { - throw; - } - } -} - #if MPT_COMPILER_MSVC // With MSVC, break directly using __debugbreak intrinsic instead of calling DebugBreak which breaks one stackframe deeper than we want #define MyDebugBreak() __debugbreak() @@ -92,83 +26,34 @@ #define MyDebugBreak() DebugBreak() #endif -#define ReportExceptionAndBreak(file, line, description, rethrow) do { ReportException(file, line, description, rethrow); if(IsDebuggerPresent()) MyDebugBreak(); } while(0) -#define ReportErrorAndBreak(file, line, description, progress) do { ReportError(file, line, description, progress); if(IsDebuggerPresent()) MyDebugBreak(); } while(0) -#define MULTI_TEST_TRY try { -#define MULTI_TEST_START -#define MULTI_TEST_END -#define MULTI_TEST_CATCH } catch(...) { ReportExceptionAndBreak(__FILE__, __LINE__, description, false); } -#define TEST_TRY try { -#define TEST_CATCH } catch(...) { ReportExceptionAndBreak(__FILE__, __LINE__, description, true); } -#define TEST_START() do { } while(0) -#define TEST_OK() do { MPT_UNREFERENCED_PARAMETER(description); } while(0) -#define TEST_FAIL() ReportErrorAndBreak(__FILE__, __LINE__, description, false) -#define TEST_FAIL_STOP() ReportErrorAndBreak(__FILE__, __LINE__, description, true) - - - -//Verify that given parameters are 'equal'(show error message if not). -//The exact meaning of equality is not specified; for now using operator!=. -//The macro is active in both 'debug' and 'release' build. +// Verify that given parameters are 'equal'. Break directly into the debugger if not. +// The exact meaning of equality is based on operator!= . #define VERIFY_EQUAL(x,y) \ -do{ \ - const char * const description = #x " == " #y ; \ - TEST_TRY \ - TEST_START(); \ - if((x) != (y)) \ - { \ - TEST_FAIL(); \ - } else \ - { \ - TEST_OK(); \ - } \ - TEST_CATCH \ -}while(0) + do { \ + if((x) != (y)) { \ + MyDebugBreak(); \ + } \ + } while(0) \ +/**/ +// Like VERIFY_EQUAL, only differs for libopenmpt +#define VERIFY_EQUAL_NONCONT VERIFY_EQUAL -// Like VERIFY_EQUAL, but throws exception if comparison fails. -#define VERIFY_EQUAL_NONCONT(x,y) \ -do{ \ - const char * const description = #x " == " #y ; \ - TEST_TRY \ - TEST_START(); \ - if((x) != (y)) \ - { \ - TEST_FAIL_STOP(); \ - } else \ - { \ - TEST_OK(); \ - } \ - TEST_CATCH \ -}while(0) +// Like VERIFY_EQUAL, only differs for libopenmpt +#define VERIFY_EQUAL_QUIET_NONCONT VERIFY_EQUAL -// Like VERIFY_EQUAL_NONCONT, but do not show message if test succeeds -#define VERIFY_EQUAL_QUIET_NONCONT(x,y) \ -do{ \ - const char * const description = #x " == " #y ; \ - TEST_TRY \ - if((x) != (y)) \ - { \ - TEST_FAIL_STOP(); \ - } \ - TEST_CATCH \ -}while(0) - - #define DO_TEST(func) \ -do{ \ - const char * description = #func ; \ - MULTI_TEST_TRY \ - MULTI_TEST_START \ - func(); \ - MULTI_TEST_END \ - MULTI_TEST_CATCH \ -}while(0) + do { \ + if(IsDebuggerPresent()) { \ + func(); \ + } \ + } while(0) \ +/**/ -} // namespace MptTest +} } // namespace mpt::Test #endif // MODPLUG_TRACKER Modified: trunk/OpenMPT/test/test.cpp =================================================================== --- trunk/OpenMPT/test/test.cpp 2014-03-24 17:20:07 UTC (rev 3958) +++ trunk/OpenMPT/test/test.cpp 2014-03-24 18:03:15 UTC (rev 3959) @@ -50,8 +50,7 @@ -namespace MptTest -{ +namespace mpt { namespace Test { @@ -565,7 +564,6 @@ } - #ifdef MODPLUG_TRACKER struct CustomSettingsTestType @@ -575,30 +573,30 @@ CustomSettingsTestType(float x_ = 0.0f, float y_ = 0.0f) : x(x_), y(y_) { } }; -} // namespace MptTest +} } // namespace mpt::Test template <> -inline MptTest::CustomSettingsTestType FromSettingValue(const SettingValue &val) +inline mpt::Test::CustomSettingsTestType FromSettingValue(const SettingValue &val) { ASSERT(val.GetTypeTag() == "myType"); std::string xy = val.as<std::string>(); if(xy.empty()) { - return MptTest::CustomSettingsTestType(0.0f, 0.0f); + return mpt::Test::CustomSettingsTestType(0.0f, 0.0f); } std::size_t pos = xy.find("|"); std::string x = xy.substr(0, pos); std::string y = xy.substr(pos + 1); - return MptTest::CustomSettingsTestType(ConvertStrTo<float>(x.c_str()), ConvertStrTo<float>(y.c_str())); + return mpt::Test::CustomSettingsTestType(ConvertStrTo<float>(x.c_str()), ConvertStrTo<float>(y.c_str())); } template <> -inline SettingValue ToSettingValue(const MptTest::CustomSettingsTestType &val) +inline SettingValue ToSettingValue(const mpt::Test::CustomSettingsTestType &val) { return SettingValue(Stringify(val.x) + "|" + Stringify(val.y), "myType"); } -namespace MptTest { +namespace mpt { namespace Test { #endif // MODPLUG_TRACKER @@ -2114,14 +2112,19 @@ } -} // namespace MptTest +} } // namespace mpt::Test #else //Case: ENABLE_TESTS is not defined. -namespace MptTest +namespace mpt { namespace Test { + +void DoTests() +//------------ { - void DoTests() {} + return; } +} } // namespace mpt::Test + #endif Modified: trunk/OpenMPT/test/test.h =================================================================== --- trunk/OpenMPT/test/test.h 2014-03-24 17:20:07 UTC (rev 3958) +++ trunk/OpenMPT/test/test.h 2014-03-24 18:03:15 UTC (rev 3959) @@ -11,7 +11,8 @@ #pragma once -namespace MptTest -{ - void DoTests(); -} // namespace MptTest +namespace mpt { namespace Test { + +void DoTests(); + +} } // namespace mpt::Test This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |