From: <man...@us...> - 2014-03-01 11:03:34
|
Revision: 3802 http://sourceforge.net/p/modplug/code/3802 Author: manxorist Date: 2014-03-01 11:03:23 +0000 (Sat, 01 Mar 2014) Log Message: ----------- [Ref] Test: Split tracker and library test framework tools into separate header files. [Mod] Test: Revert tracker test framework to r3782 . Revision Links: -------------- http://sourceforge.net/p/modplug/code/3782 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/test.cpp Added Paths: ----------- trunk/OpenMPT/test/TestTools.h trunk/OpenMPT/test/TestToolsLib.h trunk/OpenMPT/test/TestToolsTracker.h Modified: trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj 2014-03-01 00:21:15 UTC (rev 3801) +++ trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj 2014-03-01 11:03:23 UTC (rev 3802) @@ -242,6 +242,9 @@ <ClInclude Include="..\soundlib\WindowedFIR.h" /> <ClInclude Include="..\soundlib\XMTools.h" /> <ClInclude Include="..\test\test.h" /> + <ClInclude Include="..\test\TestTools.h" /> + <ClInclude Include="..\test\TestToolsLib.h" /> + <ClInclude Include="..\test\TestToolsTracker.h" /> <ClInclude Include="libopenmpt.h" /> <ClInclude Include="libopenmpt.hpp" /> <ClInclude Include="libopenmpt_config.h" /> Modified: trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj.filters =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj.filters 2014-03-01 00:21:15 UTC (rev 3801) +++ trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj.filters 2014-03-01 11:03:23 UTC (rev 3802) @@ -257,6 +257,15 @@ <ClInclude Include="..\soundlib\MixerInterface.h"> <Filter>Header Files\soundlib</Filter> </ClInclude> + <ClInclude Include="..\test\TestTools.h"> + <Filter>Header Files\test</Filter> + </ClInclude> + <ClInclude Include="..\test\TestToolsLib.h"> + <Filter>Header Files\test</Filter> + </ClInclude> + <ClInclude Include="..\test\TestToolsTracker.h"> + <Filter>Header Files\test</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <ClCompile Include="..\common\AudioCriticalSection.cpp"> Modified: trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj 2014-03-01 00:21:15 UTC (rev 3801) +++ trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj 2014-03-01 11:03:23 UTC (rev 3802) @@ -250,6 +250,9 @@ <ClInclude Include="..\soundlib\WindowedFIR.h" /> <ClInclude Include="..\soundlib\XMTools.h" /> <ClInclude Include="..\test\test.h" /> + <ClInclude Include="..\test\TestTools.h" /> + <ClInclude Include="..\test\TestToolsLib.h" /> + <ClInclude Include="..\test\TestToolsTracker.h" /> <ClInclude Include="libopenmpt.h" /> <ClInclude Include="libopenmpt.hpp" /> <ClInclude Include="libopenmpt_config.h" /> Modified: trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj.filters =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj.filters 2014-03-01 00:21:15 UTC (rev 3801) +++ trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj.filters 2014-03-01 11:03:23 UTC (rev 3802) @@ -263,6 +263,15 @@ <ClInclude Include="..\soundlib\MixerInterface.h"> <Filter>Header Files\soundlib</Filter> </ClInclude> + <ClInclude Include="..\test\TestTools.h"> + <Filter>Header Files\test</Filter> + </ClInclude> + <ClInclude Include="..\test\TestToolsLib.h"> + <Filter>Header Files\test</Filter> + </ClInclude> + <ClInclude Include="..\test\TestToolsTracker.h"> + <Filter>Header Files\test</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <ClCompile Include="..\common\AudioCriticalSection.cpp"> Modified: trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj 2014-03-01 00:21:15 UTC (rev 3801) +++ trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj 2014-03-01 11:03:23 UTC (rev 3802) @@ -246,6 +246,9 @@ <ClInclude Include="..\soundlib\WindowedFIR.h" /> <ClInclude Include="..\soundlib\XMTools.h" /> <ClInclude Include="..\test\test.h" /> + <ClInclude Include="..\test\TestTools.h" /> + <ClInclude Include="..\test\TestToolsLib.h" /> + <ClInclude Include="..\test\TestToolsTracker.h" /> <ClInclude Include="libopenmpt.h" /> <ClInclude Include="libopenmpt.hpp" /> <ClInclude Include="libopenmpt_config.h" /> Modified: trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj.filters =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj.filters 2014-03-01 00:21:15 UTC (rev 3801) +++ trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj.filters 2014-03-01 11:03:23 UTC (rev 3802) @@ -257,6 +257,15 @@ <ClInclude Include="..\soundlib\MixerInterface.h"> <Filter>Header Files\soundlib</Filter> </ClInclude> + <ClInclude Include="..\test\TestTools.h"> + <Filter>Header Files\test</Filter> + </ClInclude> + <ClInclude Include="..\test\TestToolsLib.h"> + <Filter>Header Files\test</Filter> + </ClInclude> + <ClInclude Include="..\test\TestToolsTracker.h"> + <Filter>Header Files\test</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <ClCompile Include="..\common\AudioCriticalSection.cpp"> Modified: trunk/OpenMPT/mptrack/mptrack_08.vcproj =================================================================== --- trunk/OpenMPT/mptrack/mptrack_08.vcproj 2014-03-01 00:21:15 UTC (rev 3801) +++ trunk/OpenMPT/mptrack/mptrack_08.vcproj 2014-03-01 11:03:23 UTC (rev 3802) @@ -1450,6 +1450,18 @@ RelativePath="..\test\test.h" > </File> + <File + RelativePath="..\test\TestTools.h" + > + </File> + <File + RelativePath="..\test\TestToolsLib.h" + > + </File> + <File + RelativePath="..\test\TestToolsTracker.h" + > + </File> </Filter> <Filter Name="Module Loaders" Modified: trunk/OpenMPT/mptrack/mptrack_10.vcxproj =================================================================== --- trunk/OpenMPT/mptrack/mptrack_10.vcxproj 2014-03-01 00:21:15 UTC (rev 3801) +++ trunk/OpenMPT/mptrack/mptrack_10.vcxproj 2014-03-01 11:03:23 UTC (rev 3802) @@ -707,6 +707,9 @@ <ClInclude Include="..\soundlib\WindowedFIR.h" /> <ClInclude Include="..\soundlib\XMTools.h" /> <ClInclude Include="..\test\test.h" /> + <ClInclude Include="..\test\TestTools.h" /> + <ClInclude Include="..\test\TestToolsLib.h" /> + <ClInclude Include="..\test\TestToolsTracker.h" /> <ClInclude Include="..\unarchiver\archive.h" /> <ClInclude Include="..\unarchiver\unarchiver.h" /> <ClInclude Include="..\unarchiver\ungzip.h" /> Modified: trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters =================================================================== --- trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters 2014-03-01 00:21:15 UTC (rev 3801) +++ trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters 2014-03-01 11:03:23 UTC (rev 3802) @@ -978,6 +978,15 @@ <ClInclude Include="..\soundlib\MixerInterface.h"> <Filter>Header Files\soundlib</Filter> </ClInclude> + <ClInclude Include="..\test\TestToolsLib.h"> + <Filter>test</Filter> + </ClInclude> + <ClInclude Include="..\test\TestToolsTracker.h"> + <Filter>test</Filter> + </ClInclude> + <ClInclude Include="..\test\TestTools.h"> + <Filter>test</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <None Include="res\bitmap1.bmp"> Added: trunk/OpenMPT/test/TestTools.h =================================================================== --- trunk/OpenMPT/test/TestTools.h (rev 0) +++ trunk/OpenMPT/test/TestTools.h 2014-03-01 11:03:23 UTC (rev 3802) @@ -0,0 +1,5 @@ + +#pragma once + +#include "TestToolsTracker.h" +#include "TestToolsLib.h" Property changes on: trunk/OpenMPT/test/TestTools.h ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/x-chdr \ No newline at end of property Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: trunk/OpenMPT/test/TestToolsLib.h =================================================================== --- trunk/OpenMPT/test/TestToolsLib.h (rev 0) +++ trunk/OpenMPT/test/TestToolsLib.h 2014-03-01 11:03:23 UTC (rev 3802) @@ -0,0 +1,191 @@ + +#pragma once + + +#ifdef ENABLE_TESTS +#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", " "); +} + +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) +{ + show_start(file, line, description); + std::cout << "..." << std::endl; +} + +static noinline void show_ok(const char * const file, const int line, const char * const description) +{ + MPT_UNREFERENCED_PARAMETER(file); + MPT_UNREFERENCED_PARAMETER(line); + MPT_UNREFERENCED_PARAMETER(description); + std::cout << "PASS" << std::endl; +} + +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) +{ + 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(); +} + +static int fail_count = 0; + +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) +{ + 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 + } +} + +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) +{ + 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) + + +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 +} + + +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) + { + TEST_FAIL_STOP(); + } else + { + TEST_OK(); + } + TEST_CATCH +} + + +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) + { + TEST_FAIL_STOP(); + } + TEST_CATCH +} + + +//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__) + + +// Like VERIFY_EQUAL, but throws exception if comparison fails. +#define VERIFY_EQUAL_NONCONT(x,y) VerifyEqualNonContImpl( (x) , (y) , #x " == " #y , THIS_FILE, __LINE__) + + +// 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__) + + +#define DO_TEST(func) \ +do{ \ + const char * description = #func ; \ + MULTI_TEST_TRY \ + MULTI_TEST_START \ + func(); \ + MULTI_TEST_END \ + MULTI_TEST_CATCH \ +}while(0) + + +} // namespace MptTest + + +#endif // !MODPLUG_TRACKER +#endif // ENABLE_TESTS Property changes on: trunk/OpenMPT/test/TestToolsLib.h ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/x-chdr \ No newline at end of property Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: trunk/OpenMPT/test/TestToolsTracker.h =================================================================== --- trunk/OpenMPT/test/TestToolsTracker.h (rev 0) +++ trunk/OpenMPT/test/TestToolsTracker.h 2014-03-01 11:03:23 UTC (rev 3802) @@ -0,0 +1,167 @@ + +#pragma once + + +#ifdef ENABLE_TESTS +#ifdef MODPLUG_TRACKER + + +namespace MptTest +{ + + +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 defined(_MSC_VER) && defined(_M_IX86) +// on x86, break directly using asm break interrupt instead of calling DebugBreak which breaks one stackframe deeper than we want +#define MyDebugBreak() do { __asm { int 3 }; } while(0) +#else +#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(THIS_FILE, __LINE__, description, false); } +#define TEST_TRY try { +#define TEST_CATCH } catch(...) { ReportExceptionAndBreak(THIS_FILE, __LINE__, description, true); } +#define TEST_START() do { } while(0) +#define TEST_OK() do { MPT_UNREFERENCED_PARAMETER(description); } while(0) +#define TEST_FAIL() ReportErrorAndBreak(THIS_FILE, __LINE__, description, false) +#define TEST_FAIL_STOP() ReportErrorAndBreak(THIS_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. +#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) + + +// 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_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) + + +} // namespace MptTest + + +#endif // MODPLUG_TRACKER +#endif // ENABLE_TESTS + Property changes on: trunk/OpenMPT/test/TestToolsTracker.h ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/x-chdr \ No newline at end of property Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Modified: trunk/OpenMPT/test/test.cpp =================================================================== --- trunk/OpenMPT/test/test.cpp 2014-03-01 00:21:15 UTC (rev 3801) +++ trunk/OpenMPT/test/test.cpp 2014-03-01 11:03:23 UTC (rev 3802) @@ -35,10 +35,6 @@ #include "../common/mptFstream.h" #endif // !MODPLUG_TRACKER #include <limits> -#ifndef MODPLUG_TRACKER -#include <memory> -#include <iostream> -#endif // !MODPLUG_TRACKER #include <istream> #include <ostream> #include <sstream> @@ -50,289 +46,26 @@ #endif #endif +#include "TestTools.h" -namespace MptTest -{ - - #ifdef THIS_FILE #undef THIS_FILE #endif #ifdef _MSC_VER -#define THIS_FILE "..\\test\\test.cpp" // __FILE__ +static const char * const THIS_FILE = "..\\test\\test.cpp"; // __FILE__ #else -#define THIS_FILE "test/test.cpp" // __FILE__ +static const char * const THIS_FILE = "test/test.cpp"; // __FILE__ #endif -#ifdef MODPLUG_TRACKER - -static noinline void ReportError(const char * file, int line, const char * description, bool abort) -//------------------------------------------------------------------------------------------------- +namespace MptTest { - 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 defined(_MSC_VER) && defined(_M_IX86) -// on x86, break directly using asm break interrupt instead of calling DebugBreak which breaks one stackframe deeper than we want -#define MyDebugBreak() do { __asm { int 3 }; } while(0) -#else -#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(THIS_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) - -#else // !MODPLUG_TRACKER - -static std::string remove_newlines(std::string str) -{ - return mpt::String::Replace(mpt::String::Replace(str, "\n", " "), "\r", " "); -} - -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) -{ - show_start(file, line, description); - std::cout << "..." << std::endl; -} - -static noinline void show_ok(const char * const file, const int line, const char * const description) -{ - MPT_UNREFERENCED_PARAMETER(file); - MPT_UNREFERENCED_PARAMETER(line); - MPT_UNREFERENCED_PARAMETER(description); - std::cout << "PASS" << std::endl; -} - -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) -{ - 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(); -} - -static int fail_count = 0; - -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) -{ - 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 - } -} - -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) -{ - 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) - -#endif // MODPLUG_TRACKER - - -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 -} - - -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) - { - TEST_FAIL_STOP(); - } else - { - TEST_OK(); - } - TEST_CATCH -} - - -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) - { - TEST_FAIL_STOP(); - } - TEST_CATCH -} - - -//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__) - - -// Like VERIFY_EQUAL, but throws exception if comparison fails. -#define VERIFY_EQUAL_NONCONT(x,y) VerifyEqualNonContImpl( (x) , (y) , #x " == " #y , THIS_FILE, __LINE__) - - -// 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__) - - -#define DO_TEST(func) \ -do{ \ - const char * description = #func ; \ - MULTI_TEST_TRY \ - MULTI_TEST_START \ - func(); \ - MULTI_TEST_END \ - MULTI_TEST_CATCH \ -}while(0) - - - - static noinline void TestVersion(); static noinline void TestTypes(); static noinline void TestMisc(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |