Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
test.h
1 /*
2  * $Id$
3  * Copyright (c) 2005, IRIT UPS.
4  *
5  * util/test.h -- facilities for performing unit test.
6  */
7 #ifndef ELM_UTIL_TEST_H
8 #define ELM_UTIL_TEST_H
9 
10 #include <elm/string.h>
11 #include <elm/io.h>
12 #include <elm/util/Initializer.h>
13 #include <elm/genstruct/SLList.h>
14 
15 namespace elm {
16 
17 // TestCase class
18 class TestCase {
19  CString _name;
20  int tests;
21  int errors;
22 public:
24  void initialize(void);
25  virtual ~TestCase(void);
26  inline cstring name(void) { return _name; }
27  void test(CString file, int line, CString text);
28  void failed(void);
29  void succeeded(void);
30  void check(CString file, int line, CString text, bool result);
31  bool require(CString file, int line, CString text, bool result);
32  template <class T> inline void check_equal(CString file, int line,
33  CString text, const T& result, const T& reference);
34  void prepare(void);
35  void complete(void);
36  void perform(void);
37  inline bool hasFailed(void) const { return errors; }
38 protected:
39  virtual void execute(void);
41 };
42 
43 class TestSet: private Initializer<TestCase> {
44 public:
45  static TestSet def;
46 
47  void perform(void);
48 
49  class Iterator: public genstruct::SLList<TestCase *>::Iterator {
50  public:
51  inline Iterator(const TestSet& set): genstruct::SLList<TestCase *>::Iterator(set.cases) { }
52  inline Iterator(const Iterator& iter): genstruct::SLList<TestCase *>::Iterator(iter) { }
53  };
54 
55 private:
56  friend class TestCase;
57  void add(TestCase *tcase);
59 };
60 
61 
62 // Inlines
63 template <class T>
64 inline void TestCase::check_equal(CString file, int line, CString text,
65 const T& result, const T& reference) {
66  check(file, line, text, result == reference);
67  if(result != reference)
68  cout << '\t' << result << " != " << reference << "\n";
69 }
70 
71 
72 // Macros
73 //#define ELM_CHECK_MAKE(name, actions) class name##Test: public { name##Test(void)
74 #define ELM_CHECK_BEGIN(name) { elm::TestCase __case(name); __case.prepare();
75 #define ELM_CHECK(tst) __case.check(__FILE__, __LINE__, #tst, tst)
76 #define ELM_CHECK_MSG(msg, res) __case.check(__FILE__, __LINE__, msg, res)
77 #define ELM_CHECK_END __case.complete(); }
78 #define ELM_REQUIRE(tst, action) if(!__case.require(__FILE__, __LINE__, #tst, tst)) action
79 #define ELM_CHECK_EQUAL(res, ref) __case.check_equal(__FILE__, __LINE__, #res " == " #ref, res, ref)
80 #define ELM_CHECK_EXCEPTION(exn, stat) { __case.test(__FILE__, __LINE__, #stat); \
81  try { stat; __case.failed(); } catch(exn) { __case.succeeded(); } }
82 #define ELM_FAIL_ON_EXCEPTION(exn, stat) { __case.test(__FILE__, __LINE__, #stat); \
83  try { stat; __case.succeeded(); } \
84  catch(exn& e) { __case.failed(); cerr << "exception = " << e.message() << elm::io::endl; } }
85 #define ELM_TEST_BEGIN(name) \
86  static class name##Test: public elm::TestCase { \
87  public: \
88  name##Test(void): elm::TestCase(#name) { } \
89  protected: \
90  virtual void execute(void) {
91 #define ELM_TEST_END \
92  } \
93  } __test;
94 
95 // shortcuts
96 #ifndef ELM_NO_SHORTCUT
97 # define CHECK_BEGIN(name) ELM_CHECK_BEGIN(name)
98 # define CHECK(tst) ELM_CHECK(tst)
99 # define CHECK_MSG(msg, res) ELM_CHECK_MSG(msg, res)
100 # define REQUIRE(tst, action) ELM_REQUIRE(tst, action)
101 # define CHECK_EQUAL(res, ref) ELM_CHECK_EQUAL(res, ref)
102 # define CHECK_END ELM_CHECK_END
103 # define CHECK_EXCEPTION(exn, stat) ELM_CHECK_EXCEPTION(exn, stat)
104 # define FAIL_ON_EXCEPTION(exn, stat) ELM_FAIL_ON_EXCEPTION(exn, stat)
105 # define TEST_BEGIN(name) ELM_TEST_BEGIN(name)
106 # define TEST_END ELM_TEST_END
107 #endif
108 
109 } // elm
110 
111 #endif // ELM_UTIL_TEST_H
TestCase & __case
Definition: test.h:40
Definition: test.h:49
void perform(void)
Definition: util_test.cpp:288
cstring name(void)
Definition: test.h:26
void set(T *target, int size, const T &v)
Definition: array.h:63
Definition: CString.h:17
void test(CString file, int line, CString text)
Definition: util_test.cpp:155
void check_equal(CString file, int line, CString text, const T &result, const T &reference)
Definition: test.h:64
Definition: SLList.h:34
virtual ~TestCase(void)
Definition: util_test.cpp:273
Iterator(const TestSet &set)
Definition: test.h:51
static TestSet def
Definition: test.h:45
void failed(void)
Definition: util_test.cpp:164
void check(CString file, int line, CString text, bool result)
Definition: util_test.cpp:193
void prepare(void)
Definition: util_test.cpp:140
Definition: Initializer.h:14
friend class SLList
Definition: SLList.h:79
Iterator(const Iterator &iter)
Definition: test.h:52
void perform(void)
Definition: util_test.cpp:262
Definition: test.h:43
void succeeded(void)
Definition: util_test.cpp:177
TestCase(CString name)
Definition: util_test.cpp:132
void initialize(void)
Definition: util_test.cpp:206
void complete(void)
Definition: util_test.cpp:214
virtual void execute(void)
Definition: util_test.cpp:255
Definition: test.h:18
bool require(CString file, int line, CString text, bool result)
Definition: util_test.cpp:240
io::Output cout
bool hasFailed(void) const
Definition: test.h:37