00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00024 #ifndef SCALESTACK_KERNEL_TEST_H
00025 #define SCALESTACK_KERNEL_TEST_H
00026
00027 #include <cassert>
00028 #include <exception>
00029 #include <functional>
00030 #include <iostream>
00031 #include <string>
00032 #include <utility>
00033 #include <vector>
00034
00035 #include <scalestack/kernel/core.h>
00036 #include <scalestack/kernel/macros.h>
00037
00038 namespace scalestack
00039 {
00040 namespace kernel
00041 {
00042
00043 template <class T>
00044 class test_suite
00045 {
00046 public:
00047
00048 test_suite(int argc, const char** argv);
00049
00050 virtual ~test_suite();
00051
00055 virtual void setup(void);
00056
00060 virtual void teardown(void);
00061
00065 void run(void);
00066
00070 void add_default_threading_service(void);
00071
00075 void add_default_event_service(void);
00076
00077 typedef std::vector<std::pair<std::string, std::mem_fun_t<void, T> > >
00078 test_cases;
00079
00080 protected:
00081
00082 kernel::core* core;
00083 test_cases _test_cases;
00084
00085 private:
00086
00090 test_suite(const test_suite&);
00091
00095 test_suite& operator=(const test_suite&);
00096
00097 int _argc;
00098 const char** _argv;
00099 };
00100
00101 template <class T>
00102 inline test_suite<T>::test_suite(int argc, const char** argv):
00103 core(),
00104 _test_cases(),
00105 _argc(argc),
00106 _argv(argv)
00107 {
00108 }
00109
00110 template <class T>
00111 inline test_suite<T>::~test_suite()
00112 {
00113 }
00114
00115 template <class T>
00116 inline void test_suite<T>::setup(void)
00117 {
00118 }
00119
00120 template <class T>
00121 inline void test_suite<T>::teardown(void)
00122 {
00123 }
00124
00125 template <class T>
00126 inline void test_suite<T>::run(void)
00127 {
00128 for (typename test_cases::iterator test = _test_cases.begin();
00129 test != _test_cases.end(); ++test)
00130 {
00131 std::cout << "+++ " << test->first << std::endl;
00132 core = new kernel::core;
00133 core->parse_arguments(_argc, _argv);
00134 core->add_module_option_value("path", "", "scalestack/.libs");
00135 setup();
00136 test->second.operator()(static_cast<T*>(this));
00137 teardown();
00138 size_t verbose = core->get_threshold();
00139 delete core;
00140 if (verbose > 0)
00141 std::cout << "--- " << test->first << std::endl << std::endl;
00142 }
00143 }
00144
00145 template <class T>
00146 inline void test_suite<T>::add_default_threading_service(void)
00147 {
00148 core->add_module_option_value("threading::service", "",
00149 "threading::pthreads");
00150 }
00151
00152 template <class T>
00153 inline void test_suite<T>::add_default_event_service(void)
00154 {
00155 core->add_module_option_value("event::service", "", "event::libevent");
00156 }
00157
00158 #define test_case(_name) \
00159 _test_cases.push_back(std::make_pair(SCALESTACK_QUOTE(_name), \
00160 std::mem_fun(&_name)))
00161
00162 #define assert_exception(_method) \
00163 do \
00164 { \
00165 bool _test_exception = false; \
00166 try \
00167 { \
00168 _method; \
00169 } \
00170 catch (std::exception&) \
00171 { \
00172 _test_exception = true; \
00173 } \
00174 assert(_test_exception); \
00175 } \
00176 while (false)
00177
00178 }
00179 }
00180
00181 #endif