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