From: <man...@us...> - 2014-03-02 09:59:40
|
Revision: 3806 http://sourceforge.net/p/modplug/code/3806 Author: manxorist Date: 2014-03-02 09:59:31 +0000 (Sun, 02 Mar 2014) Log Message: ----------- [Ref] Test: Rewrite test framework for libopenmpt in order to make the code more readable. [Ref] Test: Add an currently disabled alternate C++11 version of the test framework for libopenmpt. Modified Paths: -------------- trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj.filters trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj.filters trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj.filters trunk/OpenMPT/mptrack/mptrack_08.vcproj trunk/OpenMPT/mptrack/mptrack_10.vcxproj trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters trunk/OpenMPT/test/TestToolsLib.h Added Paths: ----------- trunk/OpenMPT/test/TestToolsLib.cpp Modified: trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj 2014-03-01 20:53:01 UTC (rev 3805) +++ trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj 2014-03-02 09:59:31 UTC (rev 3806) @@ -334,6 +334,7 @@ <ClCompile Include="..\soundlib\WindowedFIR.cpp" /> <ClCompile Include="..\soundlib\XMTools.cpp" /> <ClCompile Include="..\test\test.cpp" /> + <ClCompile Include="..\test\TestToolsLib.cpp" /> <ClCompile Include="libopenmpt_c.cpp" /> <ClCompile Include="libopenmpt_cxx.cpp" /> <ClCompile Include="libopenmpt_ext.cpp" /> Modified: trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj.filters =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj.filters 2014-03-01 20:53:01 UTC (rev 3805) +++ trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj.filters 2014-03-02 09:59:31 UTC (rev 3806) @@ -514,5 +514,8 @@ <ClCompile Include="..\common\Logging.cpp"> <Filter>Source Files\common</Filter> </ClCompile> + <ClCompile Include="..\test\TestToolsLib.cpp"> + <Filter>Source Files\test</Filter> + </ClCompile> </ItemGroup> </Project> \ No newline at end of file Modified: trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj 2014-03-01 20:53:01 UTC (rev 3805) +++ trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj 2014-03-02 09:59:31 UTC (rev 3806) @@ -344,6 +344,7 @@ <ClCompile Include="..\soundlib\WindowedFIR.cpp" /> <ClCompile Include="..\soundlib\XMTools.cpp" /> <ClCompile Include="..\test\test.cpp" /> + <ClCompile Include="..\test\TestToolsLib.cpp" /> <ClCompile Include="libopenmpt_c.cpp" /> <ClCompile Include="libopenmpt_cxx.cpp" /> <ClCompile Include="libopenmpt_ext.cpp" /> Modified: trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj.filters =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj.filters 2014-03-01 20:53:01 UTC (rev 3805) +++ trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj.filters 2014-03-02 09:59:31 UTC (rev 3806) @@ -520,5 +520,8 @@ <ClCompile Include="..\common\Logging.cpp"> <Filter>Source Files\common</Filter> </ClCompile> + <ClCompile Include="..\test\TestToolsLib.cpp"> + <Filter>Source Files\test</Filter> + </ClCompile> </ItemGroup> </Project> \ No newline at end of file Modified: trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj 2014-03-01 20:53:01 UTC (rev 3805) +++ trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj 2014-03-02 09:59:31 UTC (rev 3806) @@ -338,6 +338,7 @@ <ClCompile Include="..\soundlib\WindowedFIR.cpp" /> <ClCompile Include="..\soundlib\XMTools.cpp" /> <ClCompile Include="..\test\test.cpp" /> + <ClCompile Include="..\test\TestToolsLib.cpp" /> <ClCompile Include="libopenmpt_c.cpp" /> <ClCompile Include="libopenmpt_cxx.cpp" /> <ClCompile Include="libopenmpt_ext.cpp" /> Modified: trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj.filters =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj.filters 2014-03-01 20:53:01 UTC (rev 3805) +++ trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj.filters 2014-03-02 09:59:31 UTC (rev 3806) @@ -517,5 +517,8 @@ <ClCompile Include="..\common\Logging.cpp"> <Filter>Source Files\common</Filter> </ClCompile> + <ClCompile Include="..\test\TestToolsLib.cpp"> + <Filter>Source Files\test</Filter> + </ClCompile> </ItemGroup> </Project> \ No newline at end of file Modified: trunk/OpenMPT/mptrack/mptrack_08.vcproj =================================================================== --- trunk/OpenMPT/mptrack/mptrack_08.vcproj 2014-03-01 20:53:01 UTC (rev 3805) +++ trunk/OpenMPT/mptrack/mptrack_08.vcproj 2014-03-02 09:59:31 UTC (rev 3806) @@ -1259,11 +1259,11 @@ > </File> <File - RelativePath="..\common\svn_version_default\svn_version.h" + RelativePath=".\svn_version\svn_version.h" > </File> <File - RelativePath=".\svn_version\svn_version.h" + RelativePath="..\common\svn_version_default\svn_version.h" > </File> <File @@ -1455,6 +1455,10 @@ > </File> <File + RelativePath="..\test\TestToolsLib.cpp" + > + </File> + <File RelativePath="..\test\TestToolsLib.h" > </File> Modified: trunk/OpenMPT/mptrack/mptrack_10.vcxproj =================================================================== --- trunk/OpenMPT/mptrack/mptrack_10.vcxproj 2014-03-01 20:53:01 UTC (rev 3805) +++ trunk/OpenMPT/mptrack/mptrack_10.vcxproj 2014-03-02 09:59:31 UTC (rev 3806) @@ -507,6 +507,7 @@ <ClCompile Include="..\soundlib\WindowedFIR.cpp" /> <ClCompile Include="..\soundlib\XMTools.cpp" /> <ClCompile Include="..\test\test.cpp" /> + <ClCompile Include="..\test\TestToolsLib.cpp" /> <ClCompile Include="..\unarchiver\unarchiver.cpp" /> <ClCompile Include="..\unarchiver\ungzip.cpp" /> <ClCompile Include="..\unarchiver\unlha.cpp" /> Modified: trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters =================================================================== --- trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters 2014-03-01 20:53:01 UTC (rev 3805) +++ trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters 2014-03-02 09:59:31 UTC (rev 3806) @@ -502,6 +502,9 @@ <ClCompile Include="..\common\Logging.cpp"> <Filter>Source Files\common</Filter> </ClCompile> + <ClCompile Include="..\test\TestToolsLib.cpp"> + <Filter>test</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="..\soundlib\Loaders.h"> Added: trunk/OpenMPT/test/TestToolsLib.cpp =================================================================== --- trunk/OpenMPT/test/TestToolsLib.cpp (rev 0) +++ trunk/OpenMPT/test/TestToolsLib.cpp 2014-03-02 09:59:31 UTC (rev 3806) @@ -0,0 +1,178 @@ +/* + * TestToolsLib.cpp + * ---------------- + * Purpose: Unit test framework for libopenmpt. + * Notes : Currently somewhat unreadable :/ + * Authors: OpenMPT Devs + * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. + */ + + +#include "stdafx.h" +#include "TestToolsLib.h" + + +#ifdef ENABLE_TESTS +#ifndef MODPLUG_TRACKER + + +#include <exception> +#include <iostream> + + +namespace MptTest +{ + + +int fail_count = 0; + + +static std::string remove_newlines(std::string str) +{ + return mpt::String::Replace(mpt::String::Replace(str, "\n", " "), "\r", " "); +} + + +Context::Context(const char * file, int line) + : file(file) + , line(line) +{ + return; +} + + +Context::Context(const Context &c) + : file(c.file) + , line(c.line) +{ + return; +} + + +Test::Test(Fatality fatality, Verbosity verbosity, const char * const desc, const Context &context) + : fatality(fatality) + , verbosity(verbosity) + , desc(desc) + , context(context) +{ + return; +} + + +std::string Test::AsString() const +{ + return mpt::String::Print("Test: %1(%2): %3", context.file, context.line, remove_newlines(desc)); +} + + +void Test::ShowStart() const +{ + switch(verbosity) + { + case VerbosityQuiet: + break; + case VerbosityNormal: + case VerbosityVerbose: + std::cout << AsString() << ": " << std::endl; + break; + } +} + + +void Test::ShowProgress(const char * text) const +{ + switch(verbosity) + { + case VerbosityQuiet: + break; + case VerbosityNormal: + case VerbosityVerbose: + std::cout << AsString() << ": " << text << std::endl; + break; + } +} + + +void Test::ShowPass() const +{ + switch(verbosity) + { + case VerbosityQuiet: + break; + case VerbosityNormal: + case VerbosityVerbose: + std::cout << AsString() << ": PASS" << std::endl; + break; + } +} + + +void Test::ShowFail(bool exception, const char * const text) const +{ + std::cout << AsString() << ": FAIL" << std::endl; + std::cout.flush(); + if(!exception) + { + if(!text || (text && std::string(text).empty())) + { + std::cerr << "FAIL: " << AsString() << std::endl; + } else + { + std::cerr << "FAIL: " << AsString() << " : " << text << std::endl; + } + } else + { + if(!text || (text && std::string(text).empty())) + { + std::cerr << "FAIL: " << AsString() << " EXCEPTION!" << std::endl; + } else + { + std::cerr << "FAIL: " << AsString() << " EXCEPTION: " << text << std::endl; + } + } + std::cerr.flush(); +} + + +void Test::ReportPassed() +{ + ShowPass(); +} + + +void Test::ReportFailed() +{ + fail_count++; + ReportException(); +} + + +void Test::ReportException() +{ + try + { + throw; // get the exception + } catch(TestFailed & e) + { + ShowFail(false, e.values.c_str()); + if(fatality == FatalityStop) + { + throw; // rethrow + } + } catch(std::exception & e) + { + ShowFail(true, e.what()); + throw; // rethrow + } catch(...) + { + ShowFail(true); + throw; // rethrow + } +} + + +} // namespace MptTest + + +#endif // !MODPLUG_TRACKER +#endif // ENABLE_TESTS Property changes on: trunk/OpenMPT/test/TestToolsLib.cpp ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/x-c++src \ No newline at end of property Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Modified: trunk/OpenMPT/test/TestToolsLib.h =================================================================== --- trunk/OpenMPT/test/TestToolsLib.h 2014-03-01 20:53:01 UTC (rev 3805) +++ trunk/OpenMPT/test/TestToolsLib.h 2014-03-02 09:59:31 UTC (rev 3806) @@ -15,181 +15,162 @@ #ifndef MODPLUG_TRACKER -#include <memory> -#include <iostream> - - namespace MptTest { -static std::string remove_newlines(std::string str) -{ - return mpt::String::Replace(mpt::String::Replace(str, "\n", " "), "\r", " "); -} +extern int fail_count; -static noinline void show_start(const char * const file, const int line, const char * const description) -{ - std::cout << "Test: " << file << "(" << line << "): " << remove_newlines(description) << ": "; -} -static noinline void MultiTestStart(const char * const file, const int line, const char * const description) +enum Verbosity { - show_start(file, line, description); - std::cout << "..." << std::endl; -} + VerbosityQuiet, + VerbosityNormal, + VerbosityVerbose, +}; -static noinline void show_ok(const char * const file, const int line, const char * const description) +enum Fatality { - MPT_UNREFERENCED_PARAMETER(file); - MPT_UNREFERENCED_PARAMETER(line); - MPT_UNREFERENCED_PARAMETER(description); - std::cout << "PASS" << std::endl; -} + FatalityContinue, + FatalityStop +}; -static noinline void show_fail(const char * const file, const int line, const char * const description, bool exception = false, const char * const exception_text = nullptr) + +struct Context { - std::cout << "FAIL" << std::endl; - std::cout.flush(); - if(!exception) - { - std::cerr << "FAIL: " << file << "(" << line << "): " << remove_newlines(description) << std::endl; - } else if(!exception_text || (exception_text && std::string(exception_text).empty())) - { - std::cerr << "FAIL: " << file << "(" << line << "): " << remove_newlines(description) << " EXCEPTION!" << std::endl; - } else - { - std::cerr << "FAIL: " << file << "(" << line << "): " << remove_newlines(description) << " EXCEPTION: " << exception_text << std::endl; - } - std::cerr.flush(); -} +public: + const char * const file; + const int line; +public: + Context(const char * file, int line); + Context(const Context &c); +}; -static int fail_count = 0; +#define MPT_TEST_CONTEXT_CURRENT() (::MptTest::Context( __FILE__ , __LINE__ )) -static noinline void CheckFailCountOrThrow() -{ - if(fail_count > 0) - { - throw std::runtime_error("Test failed."); - } -} -static noinline void ReportException(const char * const file, const int line, const char * const description) +struct TestFailed { - try - { - throw; // get the exception - } catch(std::exception & e) - { - show_fail(file, line, description, true, e.what()); - throw; // rethrow - } catch(...) - { - show_fail(file, line, description, true); - throw; // rethrow - } -} + std::string values; + TestFailed(const std::string &values) : values(values) { } + TestFailed() { } +}; -static noinline void TestFail(const char * const file, const int line, const char * const description) -{ - show_fail(file, line, description); - fail_count++; -} -static noinline void TestFailStop(const char * const file, const int line, const char * const description) +class Test { - show_fail(file, line, description); - fail_count++; - throw std::runtime_error(std::string("Test failed: ") + description); -} -#define MULTI_TEST_TRY try { \ - fail_count = 0; -#define MULTI_TEST_START MultiTestStart(THIS_FILE, __LINE__, description); -#define MULTI_TEST_END show_start(THIS_FILE, __LINE__, description); -#define MULTI_TEST_CATCH CheckFailCountOrThrow(); \ - show_ok(THIS_FILE, __LINE__, description); \ - } catch(...) { \ - ReportException(THIS_FILE, __LINE__, description); \ - } -#define TEST_TRY try { -#define TEST_CATCH } catch(...) { \ - fail_count++; \ - ReportException(file, line, description); \ - throw; \ - } -#define TEST_START() show_start(file, line, description) -#define TEST_OK() show_ok(file, line, description) -#define TEST_FAIL() TestFail(file, line, description) -#define TEST_FAIL_STOP() TestFailStop(file, line, description) +private: + Fatality const fatality; + Verbosity const verbosity; + const char * const desc; + Context const context; -template <typename Tx, typename Ty> -noinline void VerifyEqualImpl(const Tx &x, const Ty &y, const char *const description, const char *const file, const int line) -{ - TEST_TRY - TEST_START(); - if(x != y) - { - TEST_FAIL(); - } else - { - TEST_OK(); - } - TEST_CATCH -} +public: + Test(Fatality fatality, Verbosity verbosity, const char * const desc, const Context &context); -template <typename Tx, typename Ty> -noinline void VerifyEqualNonContImpl(const Tx &x, const Ty &y, const char *const description, const char *const file, const int line) -{ - TEST_TRY - TEST_START(); - if(x != y) +public: + + std::string AsString() const; + + void ShowStart() const; + void ShowProgress(const char * text) const; + void ShowPass() const; + void ShowFail(bool exception = false, const char * const text = nullptr) const; + + void ReportPassed(); + void ReportFailed(); + + void ReportException(); + +public: + +#if 0 // C++11 version + +private: + + template <typename Tx, typename Ty> + noinline void TypeCompareHelper(const Tx &x, const Ty &y) { - TEST_FAIL_STOP(); - } else - { - TEST_OK(); + if(x != y) + { + //throw TestFailed(mpt::String::Print("%1 != %2", x, y)); + throw TestFailed(); + } } - TEST_CATCH -} +public: -template <typename Tx, typename Ty> -noinline void VerifyEqualQuietNonContImpl(const Tx &x, const Ty &y, const char *const description, const char *const file, const int line) -{ - TEST_TRY - if(x != y) + template <typename Tfx, typename Tfy> + noinline void operator () (const Tfx &fx, const Tfy &fy) { - TEST_FAIL_STOP(); + ShowStart(); + try + { + ShowProgress("Calculate x ..."); + const auto x = fx(); + ShowProgress("Calculate y ..."); + const auto y = fy(); + ShowProgress("Compare ..."); + TypeCompareHelper(x, y); + ReportPassed(); + } catch(...) + { + ReportFailed(); + } } - TEST_CATCH -} + #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) ;} ) -//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. -#define VERIFY_EQUAL(x,y) VerifyEqualImpl( (x) , (y) , #x " == " #y , THIS_FILE, __LINE__) +#else +public: -// Like VERIFY_EQUAL, but throws exception if comparison fails. -#define VERIFY_EQUAL_NONCONT(x,y) VerifyEqualNonContImpl( (x) , (y) , #x " == " #y , THIS_FILE, __LINE__) + template <typename Tx, typename Ty> + noinline void operator () (const Tx &x, const Ty &y) + { + ShowStart(); + try + { + if(x != y) + { + //throw TestFailed(mpt::String::Print("%1 != %2", x, y)); + throw TestFailed(); + } + ReportPassed(); + } catch(...) + { + ReportFailed(); + } + } + #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) ) -// Like VERIFY_EQUAL_NONCONT, but do not show message if test succeeds -#define VERIFY_EQUAL_QUIET_NONCONT(x,y) VerifyEqualQuietNonContImpl( (x) , (y) , #x " == " #y , THIS_FILE, __LINE__) +#endif +}; + #define DO_TEST(func) \ do{ \ - const char * description = #func ; \ - MULTI_TEST_TRY \ - MULTI_TEST_START \ - func(); \ - MULTI_TEST_END \ - MULTI_TEST_CATCH \ + ::MptTest::Test test(::MptTest::FatalityStop, ::MptTest::VerbosityNormal, #func , MPT_TEST_CONTEXT_CURRENT() ); \ + try { \ + test.ShowStart(); \ + fail_count = 0; \ + func(); \ + if(fail_count > 0) { \ + throw ::MptTest::TestFailed(); \ + } \ + test.ReportPassed(); \ + } catch(...) { \ + test.ReportException(); \ + } \ }while(0) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |