1 /*
2  * TestToolsTracker.h
3  * ------------------
4  * Purpose: Unit test framework for OpenMPT.
5  * Notes  : Really basic functionality that relies on a debugger that catches
6  *          exceptions and breaks right at the spot where it gets thrown.
7  * Authors: OpenMPT Devs
8  * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
9  */
10 
11 
12 #pragma once
13 
14 #include "openmpt/all/BuildSettings.hpp"
15 
16 
17 #ifdef ENABLE_TESTS
18 #ifdef MODPLUG_TRACKER
19 
20 
21 #include "mpt/test/test.hpp"
22 
23 
24 OPENMPT_NAMESPACE_BEGIN
25 
26 
27 namespace Test {
28 
29 
30 #if MPT_COMPILER_MSVC
31 // With MSVC, break directly using __debugbreak intrinsic instead of calling DebugBreak which breaks one stackframe deeper than we want
32 #define MyDebugBreak() __debugbreak()
33 #else
34 #define MyDebugBreak() DebugBreak()
35 #endif
36 
37 
38 class mpt_test_reporter
39 	: public mpt::test::silent_reporter
40 {
41 public:
42 	mpt_test_reporter() = default;
43 	~mpt_test_reporter() override = default;
44 public:
immediate_breakpoint()45 	inline void immediate_breakpoint() override {
46 		MyDebugBreak();
47 	}
48 };
49 
50 
51 // Verify that given parameters are 'equal'. Break directly into the debugger if not.
52 // The exact meaning of equality is based on operator== of the compared types.
53 #define VERIFY_EQUAL(x,y)	\
54 	do { \
55 		if(!((x) == (y))) { \
56 			MyDebugBreak(); \
57 		} \
58 	} while(0) \
59 /**/
60 
61 // Like VERIFY_EQUAL, only differs for libopenmpt
62 #define VERIFY_EQUAL_NONCONT VERIFY_EQUAL
63 
64 // Like VERIFY_EQUAL, only differs for libopenmpt
65 #define VERIFY_EQUAL_QUIET_NONCONT VERIFY_EQUAL
66 
67 #define VERIFY_EQUAL_EPS(x,y,eps)	\
68 	do { \
69 		if(std::abs((x) - (y)) > (eps)) { \
70 			MyDebugBreak(); \
71 		} \
72 	} while(0) \
73 /**/
74 
75 
76 #define DO_TEST(func) \
77 	do { \
78 		if(IsDebuggerPresent()) { \
79 			func(); \
80 		} \
81 	} while(0) \
82 /**/
83 
84 
85 } // namespace Test
86 
87 
88 OPENMPT_NAMESPACE_END
89 
90 
91 #endif // MODPLUG_TRACKER
92 #endif // ENABLE_TESTS
93