Debian Codegraph @

test/TestToolsTracker.h

raw
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 #elif MPT_COMPILER_CLANG
34 #define MyDebugBreak() __builtin_debugtrap()
35 #elif MPT_COMPILER_GCC && (MPT_ARCH_X86 || MPT_ARCH_AMD64)
36 #define MyDebugBreak() __asm__ __volatile__("int 3");
37 #else
38 #define MyDebugBreak() DebugBreak()
39 #endif
40
41
42 class mpt_test_reporter
43 : public mpt::test::silent_reporter
44 {
45 public:
46 mpt_test_reporter() = default;
47 ~mpt_test_reporter() override = default;
48 public:
49 inline void immediate_breakpoint() override {
50 MyDebugBreak();
51 }
52 };
53
54
55 // Verify that given parameters are 'equal'. Break directly into the debugger if not.
56 // The exact meaning of equality is based on operator== of the compared types.
57 #define VERIFY_EQUAL(x,y) \
58 do { \
59 if(!((x) == (y))) { \
60 MyDebugBreak(); \
61 } \
62 } while(0) \
63 /**/
64
65 // Like VERIFY_EQUAL, only differs for libopenmpt
66 #define VERIFY_EQUAL_NONCONT VERIFY_EQUAL
67
68 // Like VERIFY_EQUAL, only differs for libopenmpt
69 #define VERIFY_EQUAL_QUIET_NONCONT VERIFY_EQUAL
70
71 #define VERIFY_EQUAL_EPS(x,y,eps) \
72 do { \
73 if(std::abs((x) - (y)) > (eps)) { \
74 MyDebugBreak(); \
75 } \
76 } while(0) \
77 /**/
78
79
80 #define DO_TEST(func) \
81 do { \
82 if(IsDebuggerPresent()) { \
83 func(); \
84 } \
85 } while(0) \
86 /**/
87
88
89 } // namespace Test
90
91
92 OPENMPT_NAMESPACE_END
93
94
95 #endif // MODPLUG_TRACKER
96 #endif // ENABLE_TESTS