Debian Codegraph @

test/TestToolsLib.h

raw
1 /*
2 * TestToolsLib.h
3 * --------------
4 * Purpose: Unit test framework for libopenmpt.
5 * Notes : This is more complex than the OpenMPT version because we cannot
6 * rely on a debugger and have to deal with exceptions ourselves.
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 #ifndef MODPLUG_TRACKER
19
20
21 //#define MPT_TEST_CXX11
22
23
24 #include "mpt/test/test.hpp"
25
26 #include <type_traits>
27
28 #include "mpt/base/bit.hpp"
29 #include "openmpt/base/FlagSet.hpp"
30 #include "../soundlib/Snd_defs.h"
31
32
33 OPENMPT_NAMESPACE_BEGIN
34
35
36 namespace Test {
37
38
39
40 class mpt_test_reporter
41 : public mpt::test::silent_reporter
42 {
43 public:
44 mpt_test_reporter() = default;
45 ~mpt_test_reporter() override = default;
46 public:
47 void case_run(const mpt::source_location & loc) override;
48 void case_run(const mpt::source_location & loc, const char * text_e) override;
49 void case_run(const mpt::source_location & loc, const char * text_ex, const char * text_e) override;
50 void case_run(const mpt::source_location & loc, const char * text_a, const char * text_cmp, const char * text_b) override;
51 void case_result(const mpt::source_location & loc, const mpt::test::result & result) override;
52 };
53
54
55
56 extern int fail_count;
57
58
59 enum Verbosity
60 {
61 VerbosityQuiet,
62 VerbosityNormal,
63 VerbosityVerbose,
64 };
65
66 enum Fatality
67 {
68 FatalityContinue,
69 FatalityStop
70 };
71
72
73 struct TestFailed
74 {
75 std::string values;
76 TestFailed(const std::string &values) : values(values) { }
77 TestFailed() { }
78 };
79
80 } // namespace Test
81
82 template<typename T>
83 struct ToStringHelper
84 {
85 std::string operator () (const T &x)
86 {
87 return mpt::afmt::val(x);
88 }
89 };
90
91 #ifdef MPT_TEST_CXX11
92
93 template<>
94 struct ToStringHelper<mpt::endian>
95 {
96 std::string operator () (const mpt::endian &x)
97 {
98 if(x == mpt::endian::big) return "big";
99 if(x == mpt::endian::little) return "little";
100 return "unknown";
101 }
102 };
103
104 template<typename enum_t, typename store_t>
105 struct ToStringHelper<FlagSet<enum_t, store_t> >
106 {
107 std::string operator () (const FlagSet<enum_t, store_t> &x)
108 {
109 return mpt::afmt::val(x.GetRaw());
110 }
111 };
112
113 template<typename enum_t>
114 struct ToStringHelper<enum_value_type<enum_t> >
115 {
116 std::string operator () (const enum_value_type<enum_t> &x)
117 {
118 return mpt::afmt::val(x.as_bits());
119 }
120 };
121
122 template<typename Ta, typename Tb>
123 struct ToStringHelper<std::pair<Ta, Tb> >
124 {
125 std::string operator () (const std::pair<Ta, Tb> &x)
126 {
127 return std::string("{") + mpt::afmt::val(x.first) + std::string(",") + mpt::afmt::val(x.second) + std::string("}");
128 }
129 };
130
131 template<std::size_t FRACT, typename T>
132 struct ToStringHelper<FPInt<FRACT, T> >
133 {
134 std::string operator () (const FPInt<FRACT, T> &x)
135 {
136 return std::string("FPInt<") + mpt::afmt::val(FRACT) + std::string(",") + mpt::afmt::val(typeid(T).name()) + std::string(">{") + mpt::afmt::val(x.GetInt()) + std::string(".") + mpt::afmt::val(x.GetFract()) + std::string("}");
137 }
138 };
139
140 template<>
141 struct ToStringHelper<SamplePosition>
142 {
143 std::string operator () (const SamplePosition &x)
144 {
145 return mpt::afmt::val(x.GetInt()) + std::string(".") + std::string("0x") + mpt::afmt::hex0<8>(x.GetFract());
146 }
147 };
148
149 #endif // MPT_TEST_CXX11
150
151
152 namespace Test {
153
154 class Testcase
155 {
156
157 private:
158
159 Fatality const fatality;
160 Verbosity const verbosity;
161 const char * const desc;
162 mpt::source_location const loc;
163
164 public:
165
166 Testcase(Fatality fatality, Verbosity verbosity, const char * const desc, const mpt::source_location &loc);
167
168 public:
169
170 std::string AsString() const;
171
172 void ShowStart() const;
173 void ShowProgress(const char * text) const;
174 void ShowPass() const;
175 void ShowFail(bool exception = false, const char * const text = nullptr) const;
176
177 void ReportPassed();
178 void ReportFailed();
179
180 void ReportException();
181
182 private:
183
184 template <typename Tx, typename Ty>
185 inline bool IsEqual(const Tx &x, const Ty &y, std::false_type, std::false_type)
186 {
187 return (x == y);
188 }
189
190 template <typename Tx, typename Ty>
191 inline bool IsEqual(const Tx &x, const Ty &y, std::false_type, std::true_type)
192 {
193 return (x == y);
194 }
195
196 template <typename Tx, typename Ty>
197 inline bool IsEqual(const Tx &x, const Ty &y, std::true_type, std::false_type)
198 {
199 return (x == y);
200 }
201
202 template <typename Tx, typename Ty>
203 inline bool IsEqual(const Tx &x, const Ty &y, std::true_type /* is_integer */, std::true_type /* is_integer */ )
204 {
205 // Avoid signed-unsigned-comparison warnings and test equivalence in case of either type conversion direction.
206 return ((x == static_cast<Tx>(y)) && (static_cast<Ty>(x) == y));
207 }
208
209 template <typename Tx, typename Ty, typename Teps>
210 inline bool IsEqualEpsilon(const Tx &x, const Ty &y, const Teps &eps)
211 {
212 return std::abs(x - y) <= eps;
213 }
214
215 public:
216
217 #ifdef MPT_TEST_CXX11
218
219 private:
220
221 template <typename Tx, typename Ty>
222 MPT_NOINLINE void TypeCompareHelper(const Tx &x, const Ty &y)
223 {
224 if(!IsEqual(x, y, std::is_integral<Tx>(), std::is_integral<Ty>()))
225 {
226 throw TestFailed(MPT_AFORMAT("{} != {}")(ToStringHelper<Tx>()(x), ToStringHelper<Ty>()(y)));
227 //throw TestFailed();
228 }
229 }
230
231 template <typename Tx, typename Ty, typename Teps>
232 MPT_NOINLINE void TypeCompareHelper(const Tx &x, const Ty &y, const Teps &eps)
233 {
234 if(!IsEqualEpsilon(x, y, eps))
235 {
236 throw TestFailed(MPT_AFORMAT("{} != {}")(ToStringHelper<Tx>()(x), ToStringHelper<Ty>()(y)));
237 //throw TestFailed();
238 }
239 }
240
241 public:
242
243 template <typename Tfx, typename Tfy>
244 MPT_NOINLINE void operator () (const Tfx &fx, const Tfy &fy)
245 {
246 ShowStart();
247 try
248 {
249 ShowProgress("Calculate x ...");
250 const auto x = fx();
251 ShowProgress("Calculate y ...");
252 const auto y = fy();
253 ShowProgress("Compare ...");
254 TypeCompareHelper(x, y);
255 ReportPassed();
256 } catch(...)
257 {
258 ReportFailed();
259 }
260 }
261
262 template <typename Tfx, typename Tfy, typename Teps>
263 MPT_NOINLINE void operator () (const Tfx &fx, const Tfy &fy, const Teps &eps)
264 {
265 ShowStart();
266 try
267 {
268 ShowProgress("Calculate x ...");
269 const auto x = fx();
270 ShowProgress("Calculate y ...");
271 const auto y = fy();
272 ShowProgress("Compare ...");
273 TypeCompareHelper(x, y, eps);
274 ReportPassed();
275 } catch(...)
276 {
277 ReportFailed();
278 }
279 }
280
281 #define VERIFY_EQUAL(x,y) Test::Testcase(Test::FatalityContinue, Test::VerbosityNormal, #x " == " #y , MPT_SOURCE_LOCATION_CURRENT() )( [&](){return (x) ;}, [&](){return (y) ;} )
282 #define VERIFY_EQUAL_NONCONT(x,y) Test::Testcase(Test::FatalityStop , Test::VerbosityNormal, #x " == " #y , MPT_SOURCE_LOCATION_CURRENT() )( [&](){return (x) ;}, [&](){return (y) ;} )
283 #define VERIFY_EQUAL_QUIET_NONCONT(x,y) Test::Testcase(Test::FatalityStop , Test::VerbosityQuiet , #x " == " #y , MPT_SOURCE_LOCATION_CURRENT() )( [&](){return (x) ;}, [&](){return (y) ;} )
284
285 #define VERIFY_EQUAL_EPS(x,y,eps) Test::Testcase(Test::FatalityContinue, Test::VerbosityNormal, #x " == " #y , MPT_SOURCE_LOCATION_CURRENT() )( [&](){return (x) ;}, [&](){return (y) ;}, (eps) )
286
287 #else
288
289 public:
290
291 template <typename Tx, typename Ty>
292 MPT_NOINLINE void operator () (const Tx &x, const Ty &y)
293 {
294 ShowStart();
295 try
296 {
297 if(!IsEqual(x, y, std::is_integral<Tx>(), std::is_integral<Ty>()))
298 {
299 //throw TestFailed(MPT_AFORMAT("{} != {}")(x, y));
300 throw TestFailed();
301 }
302 ReportPassed();
303 } catch(...)
304 {
305 ReportFailed();
306 }
307 }
308
309 template <typename Tx, typename Ty, typename Teps>
310 MPT_NOINLINE void operator () (const Tx &x, const Ty &y, const Teps &eps)
311 {
312 ShowStart();
313 try
314 {
315 if(!IsEqualEpsilon(x, y, eps))
316 {
317 //throw TestFailed(MPT_AFORMAT("{} != {}")(x, y));
318 throw TestFailed();
319 }
320 ReportPassed();
321 } catch(...)
322 {
323 ReportFailed();
324 }
325 }
326
327 #define VERIFY_EQUAL(x,y) Test::Testcase(Test::FatalityContinue, Test::VerbosityNormal, #x " == " #y , MPT_SOURCE_LOCATION_CURRENT() )( (x) , (y) )
328 #define VERIFY_EQUAL_NONCONT(x,y) Test::Testcase(Test::FatalityStop , Test::VerbosityNormal, #x " == " #y , MPT_SOURCE_LOCATION_CURRENT() )( (x) , (y) )
329 #define VERIFY_EQUAL_QUIET_NONCONT(x,y) Test::Testcase(Test::FatalityStop , Test::VerbosityQuiet , #x " == " #y , MPT_SOURCE_LOCATION_CURRENT() )( (x) , (y) )
330
331 #define VERIFY_EQUAL_EPS(x,y,eps) Test::Testcase(Test::FatalityContinue, Test::VerbosityNormal, #x " == " #y , MPT_SOURCE_LOCATION_CURRENT() )( (x) , (y), (eps) )
332
333 #endif
334
335 };
336
337
338 #define DO_TEST(func) \
339 do { \
340 Test::Testcase test(Test::FatalityStop, Test::VerbosityVerbose, #func , MPT_SOURCE_LOCATION_CURRENT() ); \
341 try { \
342 test.ShowStart(); \
343 fail_count = 0; \
344 func(); \
345 if(fail_count > 0) { \
346 throw Test::TestFailed(); \
347 } \
348 test.ReportPassed(); \
349 } catch(...) { \
350 test.ReportException(); \
351 } \
352 } while(0)
353
354
355 } // namespace Test
356
357
358 OPENMPT_NAMESPACE_END
359
360
361 #endif // !MODPLUG_TRACKER
362 #endif // ENABLE_TESTS