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)
25 #define YAFFUT_STRINGIZE(x) YAFFUT_STRINGIZE_(x)
26 #define YAFFUT_STRINGIZE_(x) #x
28 #define __YAFFUT_AT__ __FILE__ ":" YAFFUT_STRINGIZE(__LINE__)": "
30 #define __AT__ __YAFFUT_AT__
33 #define YAFFUT_EQUAL(e,a) \
34 yaffut::equal (e ,a , __YAFFUT_AT__, "EQUAL(" #e " == " #a ") failed ")
36 #define EQUAL YAFFUT_EQUAL
39 #define YAFFUT_UNEQUAL(e,a) \
40 yaffut::unequal (e, a, __YAFFUT_AT__, "UNEQUAL(" #e " != " #a ") failed ")
42 #define UNEQUAL YAFFUT_UNEQUAL
45 #define YAFFUT_CHECK(e) \
46 yaffut::check (e, __YAFFUT_AT__, "CHECK(" #e ") failed ")
48 #define CHECK YAFFUT_CHECK
51 #define YAFFUT_FAIL(s) yaffut::fail (s, __YAFFUT_AT__);
53 #define FAIL YAFFUT_FAIL
56 #define YAFFUT_ASSERT_THROW(s, e) \
60 throw yaffut::failure (__YAFFUT_AT__, #s " failed to throw"); \
64 #define ASSERT_THROW YAFFUT_ASSERT_THROW
71 std::string demangle ()
76 char *ptr = abi::__cxa_demangle (typeid (T).name (), 0, &sz, &status);
77 std::string name (ptr ? ptr : "", ptr ? strlen (ptr) : 0);
78 if (ptr) { free (ptr); }
79 std::string::size_type pos = name.rfind ("::");
80 if (pos != std::string::npos)
82 name = name.substr (pos + 2);
86 return typeid (T).name ();
98 typedef ITest *(*Create_t) ();
100 typedef std::map<std::string, Create_t> Tests_t;
107 static bool EqualsSuiteName (std::string const &name, std::string const &s)
109 return name.find (':') >= name.length () - 2
110 && s.substr (0, name.length ()) == name;
113 static Factory &Instance ()
115 static Factory instance;
118 void Register (const std::string &name, Create_t create)
120 m_Tests[name] = create;
122 size_t Fail () { return m_fail; }
123 void List (const std::string &name)
125 for (Tests_t::const_iterator it = m_Tests.begin (); it != m_Tests.end (); ++it)
127 if (name.empty () || it->first == name
128 || EqualsSuiteName (name, it->first))
129 std::cout << it->first << std::endl;
132 void Run (const std::string &name)
134 for (Tests_t::const_iterator it = m_Tests.begin (); it != m_Tests.end (); ++it)
136 if ("All" == name || it->first == name
137 || EqualsSuiteName (name, it->first))
141 std::cout << std::endl << it->first << ' ' << std::flush;
143 std::auto_ptr<ITest> test (it->second ());
145 std::cout << "[OK]" << std::flush;
148 catch (const std::exception &e)
150 std::cout << "[FAIL]\n " << e.what () << std::flush;
155 std::cout << "[FAIL]\n unknown exception" << std::flush;
163 const size_t size = m_Tests.size ();
164 std::cout << std::endl;
165 std::cout << "[TOTAL](" << m_pass + m_fail << '/' << size << ")" << std::endl;
166 std::cout << "[OK](" << m_pass << '/' << size << ")" << std::endl;
168 std::cout << "[FAIL](" << m_fail << '/' << size << ")" << std::endl;
170 int Main (int argc, const char *argv[])
173 && (std::string (argv[1]) == "-h" || std::string (argv[1]) == "--help"))
175 std::cout << "Yaffut - Yet Another Framework For Unit Testing.\n\n"
176 "Usage: yaffut [OPTION] [Suite:|Suite::Test]...\n\n"
178 " -h, --help show this help\n"
179 " -l, --list list test cases" << std::endl;
183 && (std::string (argv[1]) == "-l" || std::string (argv[1]) == "--list"))
185 Factory::Instance ().List (argc > 2 ? argv[2] : "");
189 const char *all[] = {"All"};
190 const char **test = all;
198 for (int i = 0; i < num; ++i)
202 Factory::Instance ().Run (test[i]);
204 catch (const std::exception &e)
206 std::clog << e.what () << std::endl;
210 Factory::Instance ().Report ();
211 return Factory::Instance ().Fail ();
215 class failure: public std::exception
217 std::string failure_;
219 template <typename Expected, typename Actual>
220 failure (const Expected &e, Actual &a, const char *at = "", const char *expr = "")
222 std::ostringstream os;
223 os << at << expr << "\nexpected: "
224 << "(" << demangle<Expected>() << ") " << e
225 << " != actual: " << "(" << demangle<Actual>() << ") " << a;
226 failure_ = os.str ();
228 failure (const char *at = "", const char *expr = "")
230 std::ostringstream os;
232 failure_ = os.str ();
234 virtual ~failure () throw () {}
235 virtual const char *what () const throw () { return failure_.c_str (); }
238 template <typename Suite, typename Case>
243 Factory::Instance ().Register (TestName (), Create);
245 const std::string &TestName ()
247 static const std::string name (demangle<Suite>() + "::" + demangle<Case>());
250 static ITest *Create ()
256 template <typename Case>
257 struct Registrator<Case, void>
261 Factory::Instance ().Register (TestName (), Create);
263 const std::string &TestName ()
265 static const std::string name ("::" + demangle<Case>());
268 static ITest *Create ()
274 template <typename Suite, typename Case = void>
275 struct Test: public ITest, public virtual Suite
277 static Registrator<Suite, Case> s_Registrator;
280 Registrator<Suite, Case>* r = &s_Registrator;
283 template <typename E, typename T>
284 void assert_throw (void (T::*mf) (), const char *at)
288 (dynamic_cast<T *> (this)->*mf) ();
289 throw yaffut::failure (at, "statement failed to throw");
295 template <typename Suite, typename Case>
296 Registrator<Suite, Case> Test<Suite, Case>::s_Registrator;
298 template <typename Case>
299 struct Test<Case, void>: public ITest
301 static Registrator<Case, void> s_Registrator;
304 Registrator<Case, void>* r = &s_Registrator;
307 template <typename E, typename T>
308 void assert_throw (void (T::*mf) (), const char *at)
312 (dynamic_cast<T *> (this)->*mf) ();
313 throw yaffut::failure (at, "statement failed to throw");
319 template <typename Case>
320 Registrator<Case, void> Test<Case, void>::s_Registrator;
322 template <typename Expected, typename Actual>
323 void equal (const Expected &e, const Actual &a, const char *at = "", const char *expr = "")
327 throw failure (e, a, at, expr);
330 inline void equal (double e, double a, const char *at = "", const char *expr = "")
332 double max = std::abs (std::max (e, a));
333 max = max < 1.0 ? 1.0 : max;
334 if (std::abs (e - a) > std::numeric_limits<double>::epsilon () * max)
336 throw failure (e, a, at, expr);
339 inline void check (bool b, const char *at = "", const char *expr = "")
343 throw failure (at, expr);
347 template <typename Expected, typename Actual>
348 void unequal (const Expected &e, const Actual &a, const char *at = "", const char *expr = "")
352 throw failure (e, a, at, expr);
355 inline void unequal (double e, double a, const char *at = "", const char *expr = "")
357 double max = std::abs (std::max (e, a));
358 max = max < 1.0 ? 1.0 : max;
359 if (std::abs (e - a) <= std::numeric_limits<double>::epsilon () * max)
361 throw failure (e, a, at, expr);
365 template <typename T>
366 void fail (const T &expr, const char *at = "")
368 std::ostringstream os;
370 throw failure (at, os.str ().c_str ());
373 template <typename E>
374 void assert_throw (void (*pf) (), const char *at = "")
379 throw failure (at, " statement failed to throw");
386 //and for those who prefer macro obscurity over typing
387 #define TEST(Suite, Case)\
388 namespace { struct Case: public yaffut::Test<Suite, Case>{ Case(); }; } \
389 template struct yaffut::Test<Suite, Case>; Case::Case()
392 namespace { struct Case: public yaffut::Test<Case>{ Case(); }; } \
393 template struct yaffut::Test<Case>; Case::Case()
399 int main (int argc, const char *argv[])
401 std::cout << "pid(" << getpid () << ")" << std::endl;
402 return yaffut::Factory::Instance ().Main (argc, argv);
405 #endif /* YAFFUT_MAIN */
407 #define yaffut_main(argc, argv) yaffut::Factory::Instance().Main (argc, argv)