]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/yaffut.hh
Merge with master
[lilypond.git] / flower / include / yaffut.hh
1 // Copyright 2006 Rutger E.W. van Beusekom.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5
6 #ifndef __YAFFUT_H__
7 #define __YAFFUT_H__
8
9 #include <cxxabi.h>
10
11 #include <cmath>
12 #include <iostream>
13 #include <map>
14 #include <memory>
15 #include <sstream>
16 #include <stdexcept>
17
18 #define YAFFUT_STRINGIZE(x) YAFFUT_STRINGIZE_(x)
19 #define YAFFUT_STRINGIZE_(x) #x
20
21 #define __YAFFUT_AT__ __FILE__ ":" YAFFUT_STRINGIZE(__LINE__)": "
22 #ifndef __AT__
23 #define __AT__ __YAFFUT_AT__
24 #endif
25
26 #define YAFFUT_EQUAL(e,a) \
27   yaffut::equal (e ,a , __YAFFUT_AT__, "EQUAL(" #e " == " #a ") failed ")
28 #ifndef EQUAL
29 #define EQUAL YAFFUT_EQUAL
30 #endif
31
32 #define YAFFUT_UNEQUAL(e,a) \
33   yaffut::unequal (e, a, __YAFFUT_AT__, "UNEQUAL(" #e " != " #a ") failed ")
34 #ifndef UNEQUAL
35 #define UNEQUAL YAFFUT_UNEQUAL
36 #endif
37
38 #define YAFFUT_CHECK(e) \
39   yaffut::check (e, __YAFFUT_AT__, "CHECK(" #e ") failed ")
40 #ifndef CHECK
41 #define CHECK YAFFUT_CHECK
42 #endif
43
44 #define YAFFUT_FAIL(s) yaffut::fail (s, __YAFFUT_AT__);
45 #ifndef FAIL
46 #define FAIL YAFFUT_FAIL
47 #endif
48
49 #define YAFFUT_ASSERT_THROW(s, e) \
50   try \
51   { \
52     s; \
53     throw yaffut::failure (__YAFFUT_AT__,  #s " failed to throw"); \
54   } \
55   catch(const e&){}
56 #ifndef ASSERT_THROW
57 #define ASSERT_THROW YAFFUT_ASSERT_THROW
58 #endif
59
60 namespace yaffut {
61
62 template <typename T>
63 std::string demangle()
64 {
65   size_t sz;
66   int status;
67   char* ptr = abi::__cxa_demangle(typeid(T).name(), 0, &sz, &status);
68   std::string name(ptr ? ptr : "", ptr ? strlen(ptr) : 0);
69   if(ptr){ free(ptr); }
70   std::string::size_type pos = name.rfind("::");
71   if(pos != std::string::npos)
72   {
73     name = name.substr(pos + 2);
74   }  
75   return name;
76 }
77
78 struct ITest
79 {
80   virtual ~ITest(){}
81 };
82
83 class Factory
84 {
85 public:
86   typedef ITest* (*Create_t) ();
87 private:
88   typedef std::map<std::string, Create_t> Tests_t;
89   Tests_t m_Tests;
90   size_t m_fail;
91   size_t m_pass;
92 private:
93   Factory(){}
94   ~Factory(){}
95   static bool EqualsSuiteName (std::string const &name, std::string const& s)
96   {
97     return name.find (':') >= name.length () - 2
98       && s.substr (0, name.length ()) == name;
99   }
100 public:
101   static Factory& Instance()
102   {
103     static Factory instance;
104     return instance;
105   }
106   void Register(const std::string& name, Create_t create)
107   {
108     m_Tests[name] = create;
109   }
110   size_t Fail () { return m_fail; }
111   void List(const std::string& name)
112   {
113     for(Tests_t::const_iterator it = m_Tests.begin(); it != m_Tests.end(); ++it)
114     {
115       if(name.empty () || it->first == name
116          || EqualsSuiteName (name, it->first))
117         std::cout << it->first << std::endl;
118     }
119   }
120   void Run(const std::string& name)
121   {
122     for(Tests_t::const_iterator it = m_Tests.begin(); it != m_Tests.end(); ++it)
123     {
124       if("All" == name || it->first == name
125          || EqualsSuiteName (name, it->first))
126       {
127         try
128         {
129           std::cout << std::endl << it->first << ' ' << std::flush;
130           {
131             std::auto_ptr<ITest> test(it->second());
132           }
133           std::cout << "[OK]" << std::flush;
134           ++m_pass;
135         }
136         catch(const std::exception& e)
137         {
138           std::cout << "[FAIL]\n" << e.what() << std::flush;
139           ++m_fail;
140         }
141         catch(...)
142         {
143           std::cout << "[FAIL]\nunknown exception" << std::flush;
144           ++m_fail;
145         }
146       }
147     }
148   }
149   void Report ()
150   {
151     const size_t size = m_Tests.size();
152     std::cout << std::endl;
153     std::cout << "[TOTAL](" << m_pass + m_fail << '/' << size << ")" << std::endl;
154     std::cout << "[OK](" << m_pass << '/' << size << ")" << std::endl;
155     if (m_fail)
156       std::cout << "[FAIL](" << m_fail << '/' << size << ")" << std::endl;
157   }
158   int Main (int argc, const char* argv[])
159   {
160     if(argc > 1
161        && (std::string(argv[1]) == "-h" || std::string(argv[1]) == "--help"))
162     {
163       std::cout << "Yaffut - Yet Another Framework For Unit Testing.\n\n"
164         "Usage: yaffut [OPTION] [Suite:|Suite::Test]...\n\n"
165         "Options:\n"
166         "  -h, --help  show this help\n"
167         "  -l, --list  list test cases" << std::endl;
168       return 0;
169     }
170     if(argc > 1
171        && (std::string(argv[1]) == "-l" || std::string(argv[1]) == "--list"))
172     {
173       Factory::Instance().List(argc > 2 ? argv[2] : "");
174       return 0;
175     }
176
177     const char* all[] = {"All"};
178     const char** test = all;
179     int num = 1;
180     if(1 < argc)
181     {
182       test = argv;
183       num = argc;
184     }
185     
186     for(int i = 0; i < num; ++i)
187     {
188       try
189       {
190         Factory::Instance().Run(test[i]);
191       }
192       catch(const std::exception& e)
193       {
194         std::clog << e.what() << std::endl;
195       }
196     }
197
198     Factory::Instance().Report ();
199     return Factory::Instance().Fail ();
200   }
201 };
202
203 class failure: public std::exception
204 {
205   std::string failure_;
206 public:
207   template <typename Expected, typename Actual>
208   failure(const Expected& e, Actual& a, const char* at = "", const char* expr = "")
209   {
210     std::ostringstream os;
211     os << at << expr << "\nexpected: "
212        << "(" << demangle<Expected>() << ") " << e
213        << " != actual: " << "(" << demangle<Actual>() << ") " << a;
214     failure_ = os.str();
215   }
216   failure(const char* at = "", const char* expr = "")
217   {
218     std::ostringstream os;
219     os << at << expr;
220     failure_ = os.str();
221   }
222   virtual ~failure() throw() {}
223   virtual const char* what() const throw() { return failure_.c_str(); }
224 };
225
226 template <typename Suite, typename Case>
227 struct Registrator
228 {
229   Registrator()
230   {
231     Factory::Instance().Register(TestName(), Create);
232   }
233   const std::string& TestName()
234   {
235     static const std::string name(demangle<Suite>() + "::" + demangle<Case>());
236     return name;
237   }
238   static ITest* Create()
239   {
240     return new Case;
241   }
242 };
243
244 template <typename Suite, typename Case>
245 struct Test: public ITest, public Suite
246 {
247   static Registrator<Suite, Case> s_Registrator;
248   Test()
249   : Suite()
250   {
251     Registrator<Suite, Case>* r = &s_Registrator;
252     r = 0;
253   }
254   template <typename E, typename T>
255   void assert_throw(void(T::*mf)(), const char* at)
256   {
257     try
258     {
259       (dynamic_cast<T*> (this)->*mf)();
260       throw yaffut::failure (at, "statement failed to throw");
261     }
262     catch(const E&){}
263   }
264 };
265
266 template <typename Suite, typename Case>
267 Registrator<Suite, Case> Test<Suite, Case>::s_Registrator;
268
269 template <typename Expected, typename Actual>
270 void equal(const Expected& e, const Actual& a, const char* at = "", const char* expr = "")
271 {
272   if(e != a)
273   {
274     throw failure(e, a, at, expr);
275   }
276 }
277 inline void equal(double e, double a, const char* at = "", const char* expr = "")
278 {
279   double max = std::abs(std::max(e, a));
280   max = max < 1.0 ? 1.0 : max;
281   if(std::abs(e - a) > std::numeric_limits<double>::epsilon() * max)
282   {
283     throw failure(e, a, at, expr);
284   }
285 }
286 inline void check(bool b, const char* at = "", const char* expr = "")
287
288   if(!b)
289   {
290     throw failure(at, expr);
291   }
292 }
293
294 template <typename Expected, typename Actual>
295 void unequal(const Expected& e, const Actual& a, const char* at = "", const char* expr = "")
296 {
297   if(e == a)
298   {
299     throw failure(e, a, at, expr);
300   }
301 }
302 inline void unequal(double e, double a, const char* at = "", const char* expr = "")
303 {
304   double max = std::abs(std::max(e, a));
305   max = max < 1.0 ? 1.0 : max;
306   if(std::abs(e - a) <= std::numeric_limits<double>::epsilon() * max)
307   {
308     throw failure(e, a, at, expr);
309   }
310 }
311
312 template <typename T>
313 void fail(const T& expr, const char* at = "")
314 {
315   std::ostringstream os;
316   os << expr;
317   throw failure(at, os.str().c_str());
318 }
319
320 template <typename E>
321 void assert_throw(void(*pf)(), const char* at = "")
322 {
323   try
324   {
325     (*pf)();
326     throw failure (at, " statement failed to throw");
327   }
328   catch(const E&){}
329 }
330
331 //define catch-all suite
332 struct Suite {};
333
334 }
335
336 //and for those who prefer macro obscurity over more typing
337 #define TEST(Suite, Case)\
338   namespace { struct Case: public yaffut::Test<Suite, Case>{ Case(); }; } \
339   template struct yaffut::Test<Suite, Case>; Case::Case()
340
341 #define FUNC(Case)\
342   namespace { struct Case: public yaffut::Test<yaffut::Suite, Case>{ Case(); }; } \
343   template struct yaffut::Test<yaffut::Suite, Case>; Case::Case()
344
345 #ifdef YAFFUT_MAIN
346
347 #include <iostream>
348
349 int main(int argc, const char* argv[])
350 {
351   std::cout << "pid(" << getpid() << ")" << std::endl;
352   return yaffut::Factory::Instance().Main (argc, argv);
353 };
354
355 #endif /* YAFFUT_MAIN */
356
357 #define yaffut_main(argc, argv) yaffut::Factory::Instance().Main (argc, argv)
358
359 #endif