Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
test.h
1 /*
2  * test facilities interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2005, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_UTIL_TEST_H
22 #define ELM_UTIL_TEST_H
23 
24 #include <elm/string.h>
25 #include <elm/io.h>
26 #include <elm/util/Initializer.h>
27 #include <elm/data/List.h>
28 
29 namespace elm {
30 
31 // TestCase class
32 class TestCase {
33  CString _name;
34  int tests;
35  int errors;
36 public:
38  void initialize(void);
39  virtual ~TestCase(void);
40  inline cstring name(void) { return _name; }
41  void test(CString file, int line, CString text);
42  void failed(void);
43  void succeeded(void);
44  void check(CString file, int line, CString text, bool result);
45  bool require(CString file, int line, CString text, bool result);
46 
47  template <class T, class U> inline void check_equal(CString file, int line,
48  CString text, const T& result, const U& reference) {
49  bool test = result == reference;
50  check(file, line, text, test);
51  if(!test)
52  cout << '\t' << result << " != " << reference << "\n";
53  }
54 
55  void prepare(void);
56  void complete(void);
57  void perform(void);
58  inline bool isSuccessful(void) const { return errors == 0; }
59  inline bool hasFailed(void) const { return errors != 0; }
60  inline bool isFullPath() const { return full_path; }
61  inline void setFullPath(bool x) { full_path = x; }
62 protected:
63  virtual void execute(void);
65  bool full_path;
66 };
67 
68 class TestSet: private Initializer<TestCase> {
69 public:
70  static TestSet def;
71 
72  void perform(void);
73 
74  class Iterator: public List<TestCase *>::Iter {
75  public:
76  inline Iterator(const TestSet& set): List<TestCase *>::Iter(set.cases) { }
77  };
78 
79 private:
80  friend class TestCase;
81  void add(TestCase *tcase);
82  List<TestCase *> cases;
83 };
84 
85 
86 // Macros
87 //#define ELM_CHECK_MAKE(name, actions) class name##Test: public { name##Test(void)
88 #define ELM_CHECK_BEGIN(name) { elm::TestCase __case(name); __case.prepare();
89 #define ELM_CHECK(tst) __case.check(__FILE__, __LINE__, #tst, tst)
90 #define ELM_CHECK_MSG(msg, res) __case.check(__FILE__, __LINE__, msg, res)
91 #define ELM_CHECK_END __case.complete(); }
92 #define ELM_CHECK_RETURN __case.complete(); if(__case.isSuccessful()) return 0; else return 1; }
93 #define ELM_REQUIRE(tst, action) if(!__case.require(__FILE__, __LINE__, #tst, tst)) action
94 #define ELM_CHECK_EQUAL(res, ref) __case.check_equal(__FILE__, __LINE__, #res " == " #ref, res, ref)
95 #define ELM_CHECK_EXCEPTION(exn, stat) { __case.test(__FILE__, __LINE__, #stat); \
96  try { stat; __case.failed(); } catch(const exn&) { __case.succeeded(); } }
97 #define ELM_FAIL_ON_EXCEPTION(exn, stat) { __case.test(__FILE__, __LINE__, #stat); \
98  try { stat; __case.succeeded(); } \
99  catch(exn& e) { __case.failed(); cerr << "exception = " << e.message() << elm::io::endl; } }
100 #define ELM_TEST_BEGIN(name) \
101  static class name##Test: public elm::TestCase { \
102  public: \
103  name##Test(void): elm::TestCase(#name) { } \
104  protected: \
105  virtual void execute(void) {
106 #define ELM_TEST_END \
107  } \
108  } __test;
109 
110 // shortcuts
111 #ifndef ELM_NO_SHORTCUT
112 # define CHECK_BEGIN(name) ELM_CHECK_BEGIN(name)
113 # define CHECK(tst) ELM_CHECK(tst)
114 # define CHECK_MSG(msg, res) ELM_CHECK_MSG(msg, res)
115 # define REQUIRE(tst, action) ELM_REQUIRE(tst, action)
116 # define CHECK_EQUAL(res, ref) ELM_CHECK_EQUAL(res, ref)
117 # define CHECK_END ELM_CHECK_END
118 # define CHECK_EXCEPTION(exn, stat) ELM_CHECK_EXCEPTION(exn, stat)
119 # define FAIL_ON_EXCEPTION(exn, stat) ELM_FAIL_ON_EXCEPTION(exn, stat)
120 # define TEST_BEGIN(name) ELM_TEST_BEGIN(name)
121 # define TEST_END ELM_TEST_END
122 # define CHECK_RETURN ELM_CHECK_RETURN
123 #endif
124 
125 } // elm
126 
127 #endif // ELM_UTIL_TEST_H
elm::TestCase::name
cstring name(void)
Definition: test.h:40
elm::TestCase::setFullPath
void setFullPath(bool x)
Definition: test.h:61
elm::TestSet::def
static TestSet def
Definition: test.h:70
elm::TestCase::complete
void complete(void)
Definition: util_test.cpp:239
elm::TestCase::full_path
bool full_path
Definition: test.h:65
elm::TestCase::perform
void perform(void)
Definition: util_test.cpp:287
elm::TestCase::TestCase
TestCase(CString name)
Definition: util_test.cpp:147
elm::TestCase::failed
void failed(void)
Definition: util_test.cpp:189
elm::cout
io::Output cout
elm::TestSet::Iterator::Iterator
Iterator(const TestSet &set)
Definition: test.h:76
elm::TestCase::test
void test(CString file, int line, CString text)
Definition: util_test.cpp:176
elm::CString
Definition: CString.h:17
elm::TestSet
Definition: test.h:68
elm::TestCase::execute
virtual void execute(void)
Definition: util_test.cpp:280
elm::List::Iter::Iter
Iter(void)
Definition: List.h:67
elm::TestCase::require
bool require(CString file, int line, CString text, bool result)
Definition: util_test.cpp:265
elm::TestCase::succeeded
void succeeded(void)
Definition: util_test.cpp:202
elm::TestCase::prepare
void prepare(void)
Definition: util_test.cpp:161
elm::TestSet::Iterator
Definition: test.h:74
elm::TestCase::isFullPath
bool isFullPath() const
Definition: test.h:60
elm
Definition: adapter.h:26
elm::TestCase::isSuccessful
bool isSuccessful(void) const
Definition: test.h:58
elm::Initializer
Definition: Initializer.h:14
elm::TestCase::check
void check(CString file, int line, CString text, bool result)
Definition: util_test.cpp:218
elm::TestSet::perform
void perform(void)
Definition: util_test.cpp:328
elm::TestCase::hasFailed
bool hasFailed(void) const
Definition: test.h:59
elm::TestCase::__case
TestCase & __case
Definition: test.h:64
elm::TestCase::check_equal
void check_equal(CString file, int line, CString text, const T &result, const U &reference)
Definition: test.h:47
elm::TestCase::initialize
void initialize(void)
Definition: util_test.cpp:231
elm::List
Definition: List.h:34
elm::TestCase
Definition: test.h:32
elm::TestCase::~TestCase
virtual ~TestCase(void)
Definition: util_test.cpp:298