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)
20 #define YAFFUT_STRINGIZE(x) YAFFUT_STRINGIZE_(x)
21 #define YAFFUT_STRINGIZE_(x) #x
23 #define __YAFFUT_AT__ __FILE__ ":" YAFFUT_STRINGIZE(__LINE__)": "
25 #define __AT__ __YAFFUT_AT__
28 #define YAFFUT_EQUAL(e,a) \
29 yaffut::equal (e ,a , __YAFFUT_AT__, "EQUAL(" #e " == " #a ") failed ")
31 #define EQUAL YAFFUT_EQUAL
34 #define YAFFUT_UNEQUAL(e,a) \
35 yaffut::unequal (e, a, __YAFFUT_AT__, "UNEQUAL(" #e " != " #a ") failed ")
37 #define UNEQUAL YAFFUT_UNEQUAL
40 #define YAFFUT_CHECK(e) \
41 yaffut::check (e, __YAFFUT_AT__, "CHECK(" #e ") failed ")
43 #define CHECK YAFFUT_CHECK
46 #define YAFFUT_FAIL(s) yaffut::fail (s, __YAFFUT_AT__);
48 #define FAIL YAFFUT_FAIL
51 #define YAFFUT_ASSERT_THROW(s, e) \
55 throw yaffut::failure (__YAFFUT_AT__, #s " failed to throw"); \
59 #define ASSERT_THROW YAFFUT_ASSERT_THROW
66 std::string demangle ()
70 char *ptr = abi::__cxa_demangle (typeid (T).name (), 0, &sz, &status);
71 std::string name (ptr ? ptr : "", ptr ? strlen (ptr) : 0);
72 if (ptr) { free (ptr); }
73 std::string::size_type pos = name.rfind ("::");
74 if (pos != std::string::npos)
76 name = name.substr (pos + 2);
89 typedef ITest *(*Create_t) ();
91 typedef std::map<std::string, Create_t> Tests_t;
98 static bool EqualsSuiteName (std::string const &name, std::string const &s)
100 return name.find (':') >= name.length () - 2
101 && s.substr (0, name.length ()) == name;
104 static Factory &Instance ()
106 static Factory instance;
109 void Register (const std::string &name, Create_t create)
111 m_Tests[name] = create;
113 size_t Fail () { return m_fail; }
114 void List (const std::string &name)
116 for (Tests_t::const_iterator it = m_Tests.begin (); it != m_Tests.end (); ++it)
118 if (name.empty () || it->first == name
119 || EqualsSuiteName (name, it->first))
120 std::cout << it->first << std::endl;
123 void Run (const std::string &name)
125 for (Tests_t::const_iterator it = m_Tests.begin (); it != m_Tests.end (); ++it)
127 if ("All" == name || it->first == name
128 || EqualsSuiteName (name, it->first))
132 std::cout << std::endl << it->first << ' ' << std::flush;
134 std::auto_ptr<ITest> test (it->second ());
136 std::cout << "[OK]" << std::flush;
139 catch (const std::exception &e)
141 std::cout << "[FAIL]\n " << e.what () << std::flush;
146 std::cout << "[FAIL]\n unknown exception" << std::flush;
154 const size_t size = m_Tests.size ();
155 std::cout << std::endl;
156 std::cout << "[TOTAL](" << m_pass + m_fail << '/' << size << ")" << std::endl;
157 std::cout << "[OK](" << m_pass << '/' << size << ")" << std::endl;
159 std::cout << "[FAIL](" << m_fail << '/' << size << ")" << std::endl;
161 int Main (int argc, const char *argv[])
164 && (std::string (argv[1]) == "-h" || std::string (argv[1]) == "--help"))
166 std::cout << "Yaffut - Yet Another Framework For Unit Testing.\n\n"
167 "Usage: yaffut [OPTION] [Suite:|Suite::Test]...\n\n"
169 " -h, --help show this help\n"
170 " -l, --list list test cases" << std::endl;
174 && (std::string (argv[1]) == "-l" || std::string (argv[1]) == "--list"))
176 Factory::Instance ().List (argc > 2 ? argv[2] : "");
180 const char *all[] = {"All"};
181 const char **test = all;
189 for (int i = 0; i < num; ++i)
193 Factory::Instance ().Run (test[i]);
195 catch (const std::exception &e)
197 std::clog << e.what () << std::endl;
201 Factory::Instance ().Report ();
202 return Factory::Instance ().Fail ();
206 class failure: public std::exception
208 std::string failure_;
210 template <typename Expected, typename Actual>
211 failure (const Expected &e, Actual &a, const char *at = "", const char *expr = "")
213 std::ostringstream os;
214 os << at << expr << "\nexpected: "
215 << "(" << demangle<Expected>() << ") " << e
216 << " != actual: " << "(" << demangle<Actual>() << ") " << a;
217 failure_ = os.str ();
219 failure (const char *at = "", const char *expr = "")
221 std::ostringstream os;
223 failure_ = os.str ();
225 virtual ~failure () throw () {}
226 virtual const char *what () const throw () { return failure_.c_str (); }
229 template <typename Suite, typename Case>
234 Factory::Instance ().Register (TestName (), Create);
236 const std::string &TestName ()
238 static const std::string name (demangle<Suite>() + "::" + demangle<Case>());
241 static ITest *Create ()
247 template <typename Case>
248 struct Registrator<Case, void>
252 Factory::Instance ().Register (TestName (), Create);
254 const std::string &TestName ()
256 static const std::string name ("::" + demangle<Case>());
259 static ITest *Create ()
265 template <typename Suite, typename Case = void>
266 struct Test: public ITest, public virtual Suite
268 static Registrator<Suite, Case> s_Registrator;
271 Registrator<Suite, Case>* r = &s_Registrator;
274 template <typename E, typename T>
275 void assert_throw (void (T::*mf) (), const char *at)
279 (dynamic_cast<T *> (this)->*mf) ();
280 throw yaffut::failure (at, "statement failed to throw");
286 template <typename Suite, typename Case>
287 Registrator<Suite, Case> Test<Suite, Case>::s_Registrator;
289 template <typename Case>
290 struct Test<Case, void>: public ITest
292 static Registrator<Case, void> s_Registrator;
295 Registrator<Case, void>* r = &s_Registrator;
298 template <typename E, typename T>
299 void assert_throw (void (T::*mf) (), const char *at)
303 (dynamic_cast<T *> (this)->*mf) ();
304 throw yaffut::failure (at, "statement failed to throw");
310 template <typename Case>
311 Registrator<Case, void> Test<Case, void>::s_Registrator;
313 template <typename Expected, typename Actual>
314 void equal (const Expected &e, const Actual &a, const char *at = "", const char *expr = "")
318 throw failure (e, a, at, expr);
321 inline void equal (double e, double a, const char *at = "", const char *expr = "")
323 double max = std::abs (std::max (e, a));
324 max = max < 1.0 ? 1.0 : max;
325 if (std::abs (e - a) > std::numeric_limits<double>::epsilon () * max)
327 throw failure (e, a, at, expr);
330 inline void check (bool b, const char *at = "", const char *expr = "")
334 throw failure (at, expr);
338 template <typename Expected, typename Actual>
339 void unequal (const Expected &e, const Actual &a, const char *at = "", const char *expr = "")
343 throw failure (e, a, at, expr);
346 inline void unequal (double e, double a, const char *at = "", const char *expr = "")
348 double max = std::abs (std::max (e, a));
349 max = max < 1.0 ? 1.0 : max;
350 if (std::abs (e - a) <= std::numeric_limits<double>::epsilon () * max)
352 throw failure (e, a, at, expr);
356 template <typename T>
357 void fail (const T &expr, const char *at = "")
359 std::ostringstream os;
361 throw failure (at, os.str ().c_str ());
364 template <typename E>
365 void assert_throw (void (*pf) (), const char *at = "")
370 throw failure (at, " statement failed to throw");
377 //and for those who prefer macro obscurity over typing
378 #define TEST(Suite, Case)\
379 namespace { struct Case: public yaffut::Test<Suite, Case>{ Case(); }; } \
380 template struct yaffut::Test<Suite, Case>; Case::Case()
383 namespace { struct Case: public yaffut::Test<Case>{ Case(); }; } \
384 template struct yaffut::Test<Case>; Case::Case()
390 int main (int argc, const char *argv[])
392 std::cout << "pid(" << getpid () << ")" << std::endl;
393 return yaffut::Factory::Instance ().Main (argc, argv);
396 #endif /* YAFFUT_MAIN */
398 #define yaffut_main(argc, argv) yaffut::Factory::Instance().Main (argc, argv)