X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=flower%2Finclude%2Fyaffut.hh;h=aa54fa66168363eec4fe87484103c1001297f2d6;hb=32a34dcef0c0041c6d62677487a380b5c8b85712;hp=e324922b69738c60abbc17c7ab88b46ef3b789b8;hpb=f41973ff763d5972a85995b6d40c864281ec6714;p=lilypond.git diff --git a/flower/include/yaffut.hh b/flower/include/yaffut.hh index e324922b69..aa54fa6616 100644 --- a/flower/include/yaffut.hh +++ b/flower/include/yaffut.hh @@ -6,7 +6,12 @@ #ifndef __YAFFUT_H__ #define __YAFFUT_H__ +#include "config.hh" +#if HAVE_CXA_DEMANGLE #include +#else +#include +#endif #include #include @@ -59,146 +64,151 @@ #define ASSERT_THROW YAFFUT_ASSERT_THROW #endif -namespace yaffut { +namespace yaffut +{ template -std::string demangle() +std::string demangle () { +#if HAVE_CXA_DEMANGLE size_t sz; int status; - char* ptr = abi::__cxa_demangle(typeid(T).name(), 0, &sz, &status); - std::string name(ptr ? ptr : "", ptr ? strlen(ptr) : 0); - if(ptr){ free(ptr); } - std::string::size_type pos = name.rfind("::"); - if(pos != std::string::npos) - { - name = name.substr(pos + 2); - } + char *ptr = abi::__cxa_demangle (typeid (T).name (), 0, &sz, &status); + std::string name (ptr ? ptr : "", ptr ? strlen (ptr) : 0); + if (ptr) { free (ptr); } + std::string::size_type pos = name.rfind ("::"); + if (pos != std::string::npos) + { + name = name.substr (pos + 2); + } return name; +#else + return typeid (T).name (); +#endif } struct ITest { - virtual ~ITest(){} + virtual ~ITest () {} }; class Factory { public: - typedef ITest* (*Create_t) (); + typedef ITest *(*Create_t) (); private: typedef std::map Tests_t; Tests_t m_Tests; size_t m_fail; size_t m_pass; private: - Factory(){} - ~Factory(){} - static bool EqualsSuiteName (std::string const &name, std::string const& s) + Factory () {} + ~Factory () {} + static bool EqualsSuiteName (std::string const &name, std::string const &s) { return name.find (':') >= name.length () - 2 - && s.substr (0, name.length ()) == name; + && s.substr (0, name.length ()) == name; } public: - static Factory& Instance() + static Factory &Instance () { static Factory instance; return instance; } - void Register(const std::string& name, Create_t create) + void Register (const std::string &name, Create_t create) { m_Tests[name] = create; } size_t Fail () { return m_fail; } - void List(const std::string& name) + void List (const std::string &name) { - for(Tests_t::const_iterator it = m_Tests.begin(); it != m_Tests.end(); ++it) - { - if(name.empty () || it->first == name - || EqualsSuiteName (name, it->first)) - std::cout << it->first << std::endl; - } + for (Tests_t::const_iterator it = m_Tests.begin (); it != m_Tests.end (); ++it) + { + if (name.empty () || it->first == name + || EqualsSuiteName (name, it->first)) + std::cout << it->first << std::endl; + } } - void Run(const std::string& name) + void Run (const std::string &name) { - for(Tests_t::const_iterator it = m_Tests.begin(); it != m_Tests.end(); ++it) - { - if("All" == name || it->first == name - || EqualsSuiteName (name, it->first)) + for (Tests_t::const_iterator it = m_Tests.begin (); it != m_Tests.end (); ++it) { - try - { - std::cout << std::endl << it->first << ' ' << std::flush; + if ("All" == name || it->first == name + || EqualsSuiteName (name, it->first)) { - std::auto_ptr test(it->second()); + try + { + std::cout << std::endl << it->first << ' ' << std::flush; + { + std::auto_ptr test (it->second ()); + } + std::cout << "[OK]" << std::flush; + ++m_pass; + } + catch (const std::exception &e) + { + std::cout << "[FAIL]\n " << e.what () << std::flush; + ++m_fail; + } + catch (...) + { + std::cout << "[FAIL]\n unknown exception" << std::flush; + ++m_fail; + } } - std::cout << "[OK]" << std::flush; - ++m_pass; - } - catch(const std::exception& e) - { - std::cout << "[FAIL]\n " << e.what() << std::flush; - ++m_fail; - } - catch(...) - { - std::cout << "[FAIL]\n unknown exception" << std::flush; - ++m_fail; - } } - } } void Report () { - const size_t size = m_Tests.size(); + const size_t size = m_Tests.size (); std::cout << std::endl; std::cout << "[TOTAL](" << m_pass + m_fail << '/' << size << ")" << std::endl; std::cout << "[OK](" << m_pass << '/' << size << ")" << std::endl; if (m_fail) std::cout << "[FAIL](" << m_fail << '/' << size << ")" << std::endl; } - int Main (int argc, const char* argv[]) + int Main (int argc, const char *argv[]) { - if(argc > 1 - && (std::string(argv[1]) == "-h" || std::string(argv[1]) == "--help")) - { - std::cout << "Yaffut - Yet Another Framework For Unit Testing.\n\n" - "Usage: yaffut [OPTION] [Suite:|Suite::Test]...\n\n" - "Options:\n" - " -h, --help show this help\n" - " -l, --list list test cases" << std::endl; - return 0; - } - if(argc > 1 - && (std::string(argv[1]) == "-l" || std::string(argv[1]) == "--list")) - { - Factory::Instance().List(argc > 2 ? argv[2] : ""); - return 0; - } + if (argc > 1 + && (std::string (argv[1]) == "-h" || std::string (argv[1]) == "--help")) + { + std::cout << "Yaffut - Yet Another Framework For Unit Testing.\n\n" + "Usage: yaffut [OPTION] [Suite:|Suite::Test]...\n\n" + "Options:\n" + " -h, --help show this help\n" + " -l, --list list test cases" << std::endl; + return 0; + } + if (argc > 1 + && (std::string (argv[1]) == "-l" || std::string (argv[1]) == "--list")) + { + Factory::Instance ().List (argc > 2 ? argv[2] : ""); + return 0; + } - const char* all[] = {"All"}; - const char** test = all; + const char *all[] = {"All"}; + const char **test = all; int num = 1; - if(1 < argc) - { - test = argv; - num = argc; - } - - for(int i = 0; i < num; ++i) - { - try + if (1 < argc) { - Factory::Instance().Run(test[i]); + test = argv; + num = argc; } - catch(const std::exception& e) + + for (int i = 0; i < num; ++i) { - std::clog << e.what() << std::endl; + try + { + Factory::Instance ().Run (test[i]); + } + catch (const std::exception &e) + { + std::clog << e.what () << std::endl; + } } - } - Factory::Instance().Report (); - return Factory::Instance().Fail (); + Factory::Instance ().Report (); + return Factory::Instance ().Fail (); } }; @@ -207,37 +217,37 @@ class failure: public std::exception std::string failure_; public: template - failure(const Expected& e, Actual& a, const char* at = "", const char* expr = "") + failure (const Expected &e, Actual &a, const char *at = "", const char *expr = "") { std::ostringstream os; os << at << expr << "\nexpected: " << "(" << demangle() << ") " << e << " != actual: " << "(" << demangle() << ") " << a; - failure_ = os.str(); + failure_ = os.str (); } - failure(const char* at = "", const char* expr = "") + failure (const char *at = "", const char *expr = "") { std::ostringstream os; os << at << expr; - failure_ = os.str(); + failure_ = os.str (); } - virtual ~failure() throw() {} - virtual const char* what() const throw() { return failure_.c_str(); } + virtual ~failure () throw () {} + virtual const char *what () const throw () { return failure_.c_str (); } }; template struct Registrator { - Registrator() + Registrator () { - Factory::Instance().Register(TestName(), Create); + Factory::Instance ().Register (TestName (), Create); } - const std::string& TestName() + const std::string &TestName () { - static const std::string name(demangle() + "::" + demangle()); + static const std::string name (demangle() + "::" + demangle()); return name; } - static ITest* Create() + static ITest *Create () { return new Case; } @@ -246,133 +256,129 @@ struct Registrator template struct Registrator { - Registrator() + Registrator () { - Factory::Instance().Register(TestName(), Create); + Factory::Instance ().Register (TestName (), Create); } - const std::string& TestName() + const std::string &TestName () { static const std::string name ("::" + demangle()); return name; } - static ITest* Create() + static ITest *Create () { return new Case; } }; - template struct Test: public ITest, public virtual Suite { static Registrator s_Registrator; - Test(): Suite() + Test (): Suite () { Registrator* r = &s_Registrator; - r = 0; + (void)r; } template - void assert_throw(void(T::*mf)(), const char* at) + void assert_throw (void (T::*mf) (), const char *at) { try - { - (dynamic_cast (this)->*mf)(); - throw yaffut::failure (at, "statement failed to throw"); - } - catch(const E&){} + { + (dynamic_cast (this)->*mf) (); + throw yaffut::failure (at, "statement failed to throw"); + } + catch (const E &) {} } }; template Registrator Test::s_Registrator; - template struct Test: public ITest { static Registrator s_Registrator; - Test() + Test () { Registrator* r = &s_Registrator; - r = 0; + (void)r; } template - void assert_throw(void(T::*mf)(), const char* at) + void assert_throw (void (T::*mf) (), const char *at) { try - { - (dynamic_cast (this)->*mf)(); - throw yaffut::failure (at, "statement failed to throw"); - } - catch(const E&){} + { + (dynamic_cast (this)->*mf) (); + throw yaffut::failure (at, "statement failed to throw"); + } + catch (const E &) {} } }; template Registrator Test::s_Registrator; - - template -void equal(const Expected& e, const Actual& a, const char* at = "", const char* expr = "") +void equal (const Expected &e, const Actual &a, const char *at = "", const char *expr = "") { - if(e != a) - { - throw failure(e, a, at, expr); - } + if (e != a) + { + throw failure (e, a, at, expr); + } } -inline void equal(double e, double a, const char* at = "", const char* expr = "") +inline void equal (double e, double a, const char *at = "", const char *expr = "") { - double max = std::abs(std::max(e, a)); + double max = std::abs (std::max (e, a)); max = max < 1.0 ? 1.0 : max; - if(std::abs(e - a) > std::numeric_limits::epsilon() * max) - { - throw failure(e, a, at, expr); - } + if (std::abs (e - a) > std::numeric_limits::epsilon () * max) + { + throw failure (e, a, at, expr); + } } -inline void check(bool b, const char* at = "", const char* expr = "") -{ - if(!b) - { - throw failure(at, expr); - } +inline void check (bool b, const char *at = "", const char *expr = "") +{ + if (!b) + { + throw failure (at, expr); + } } template -void unequal(const Expected& e, const Actual& a, const char* at = "", const char* expr = "") +void unequal (const Expected &e, const Actual &a, const char *at = "", const char *expr = "") { - if(e == a) - { - throw failure(e, a, at, expr); - } + if (e == a) + { + throw failure (e, a, at, expr); + } } -inline void unequal(double e, double a, const char* at = "", const char* expr = "") +inline void unequal (double e, double a, const char *at = "", const char *expr = "") { - double max = std::abs(std::max(e, a)); + double max = std::abs (std::max (e, a)); max = max < 1.0 ? 1.0 : max; - if(std::abs(e - a) <= std::numeric_limits::epsilon() * max) - { - throw failure(e, a, at, expr); - } + if (std::abs (e - a) <= std::numeric_limits::epsilon () * max) + { + throw failure (e, a, at, expr); + } } template -void fail(const T& expr, const char* at = "") +void fail (const T &expr, const char *at = "") { std::ostringstream os; os << expr; - throw failure(at, os.str().c_str()); + throw failure (at, os.str ().c_str ()); } template -void assert_throw(void(*pf)(), const char* at = "") +void assert_throw (void (*pf) (), const char *at = "") { try - { - (*pf)(); - throw failure (at, " statement failed to throw"); - } - catch(const E&){} + { + (*pf) (); + throw failure (at, " statement failed to throw"); + } + catch (const E &) {} } } @@ -389,11 +395,13 @@ void assert_throw(void(*pf)(), const char* at = "") #ifdef YAFFUT_MAIN #include +#include +#include -int main(int argc, const char* argv[]) +int main (int argc, const char *argv[]) { - std::cout << "pid(" << getpid() << ")" << std::endl; - return yaffut::Factory::Instance().Main (argc, argv); + std::cout << "pid(" << getpid () << ")" << std::endl; + return yaffut::Factory::Instance ().Main (argc, argv); }; #endif /* YAFFUT_MAIN */