00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00024 #include "config.h"
00025
00026 #include <signal.h>
00027 #include <sys/types.h>
00028 #include <unistd.h>
00029
00030 #include <scalestack/kernel/module.h>
00031 #include <scalestack/kernel/test.h>
00032
00033 using namespace std;
00034 using namespace scalestack;
00035
00036 class test_suite: public scalestack::kernel::test_suite<test_suite>
00037 {
00038 public:
00039
00040 void test_constructor(void)
00041 {
00042 kernel::module& module = core->get_or_add_module("module");
00043 assert(&module.get_core() == core);
00044 assert(module.get_name() == "module");
00045 assert(module.get_pathname() == "");
00046 assert(module.get_version() == "");
00047 assert(module.get_author() == "");
00048 assert(module.get_title() == "");
00049 assert(module.get_license() == "");
00050 assert(module.get_dependencies() == "");
00051 assert(module.get_parent_count() == 0);
00052 assert(module.get_child_count() == 0);
00053 assert(module.is_loaded() == false);
00054 }
00055
00056 void test_parent_scan(void)
00057 {
00058 kernel::module& module = core->get_or_add_module("module");
00059 assert(module.begin_parent() == NULL);
00060 assert(module.next_parent() == NULL);
00061 }
00062
00063 void test_child_scan(void)
00064 {
00065 kernel::module& module = core->get_or_add_module("module");
00066 assert(module.begin_child() == NULL);
00067 assert(module.next_child() == NULL);
00068 }
00069
00070 void test_add_option(void)
00071 {
00072 kernel::module& module = core->get_or_add_module("module");
00073 module.add_option("option", "Help", "VALUE_NAME", "default_value");
00074 }
00075
00076 void test_get_option(void)
00077 {
00078 kernel::module& module = core->get_or_add_module("module");
00079 kernel::option& option = module.add_option("option", "Help", "VALUE_NAME",
00080 "default_value");
00081 assert(&module.get_option("option") == &option);
00082 assert_exception(module.get_option("Bad_option"));
00083 }
00084
00085 void test_get_option_count(void)
00086 {
00087 kernel::module& module = core->get_or_add_module("module");
00088 assert(module.get_option_count() == 0);
00089 module.add_option("option", "Help", "VALUE_NAME", "default_value");
00090 assert(module.get_option_count() == 1);
00091 module.add_option("option2", "Help", "VALUE_NAME", "default_value");
00092 assert(module.get_option_count() == 2);
00093 }
00094
00095 void test_option_scan(void)
00096 {
00097 kernel::module& module = core->get_or_add_module("module");
00098 assert(module.begin_option() == NULL);
00099 assert(module.next_option() == NULL);
00100
00101 kernel::option& option = module.add_option("option", "Help", "VALUE_NAME",
00102 "default_value");
00103 assert(module.begin_option() == &option);
00104 assert(module.next_option() == NULL);
00105
00106 kernel::option& option2 = module.add_option("option2", "Help", "VALUE_NAME",
00107 "default_value");
00108 assert(module.begin_option() == &option);
00109 assert(module.next_option() == &option2);
00110 assert(module.next_option() == NULL);
00111 }
00112
00113 void test_load_grand_parent(void)
00114 {
00115 kernel::module& module = core->get_or_add_module(
00116 "kernel::test::module_grand_parent");
00117 assert(module.get_title() == "");
00118 assert(module.is_loaded() == false);
00119 core->run();
00120 assert(module.get_title() == "module_grand_parent title");
00121 assert(module.get_parent_count() == 0);
00122 assert(module.get_child_count() == 0);
00123 }
00124
00125 void test_load_parent(void)
00126 {
00127 kernel::module& module = core->get_or_add_module(
00128 "kernel::test::module_parent");
00129 assert(module.get_title() == "");
00130 assert(module.is_loaded() == false);
00131 assert_exception(core->get_module("kernel::test::module_child"));
00132 core->run();
00133
00134 kernel::module& grand_parent = core->get_module(
00135 "kernel::test::module_grand_parent");
00136
00137 assert(grand_parent.get_title() == "module_grand_parent title");
00138 assert(grand_parent.get_parent_count() == 0);
00139 assert(grand_parent.get_child_count() == 1);
00140 assert(grand_parent.begin_child() == &module);
00141 assert(grand_parent.next_child() == NULL);
00142
00143 assert(module.get_title() == "module_parent title");
00144 assert(module.get_parent_count() == 1);
00145 assert(module.begin_parent() == &grand_parent);
00146 assert(module.next_parent() == NULL);
00147 assert(module.get_child_count() == 0);
00148 }
00149
00150 void test_load_child(void)
00151 {
00152 kernel::module& module =
00153 core->get_or_add_module("kernel::test::module_child");
00154 assert(module.get_title() == "");
00155 assert(module.is_loaded() == false);
00156 core->run();
00157
00158 kernel::module& parent = core->get_module("kernel::test::module_parent");
00159 kernel::module& grand_parent = core->get_module(
00160 "kernel::test::module_grand_parent");
00161
00162 assert(parent.get_title() == "module_parent title");
00163 assert(parent.get_parent_count() == 1);
00164 assert(parent.begin_parent() == &grand_parent);
00165 assert(parent.next_parent() == NULL);
00166 assert(parent.get_child_count() == 1);
00167 assert(parent.begin_child() == &module);
00168 assert(parent.next_child() == NULL);
00169
00170 assert(grand_parent.get_title() == "module_grand_parent title");
00171 assert(grand_parent.get_parent_count() == 0);
00172 assert(grand_parent.get_child_count() == 1);
00173 assert(grand_parent.begin_child() == &parent);
00174 assert(grand_parent.next_child() == NULL);
00175
00176 assert(module.get_title() == "module_child title");
00177 assert(module.get_parent_count() == 1);
00178 assert(module.begin_parent() == &parent);
00179 assert(module.next_parent() == NULL);
00180 assert(module.get_child_count() == 0);
00181 }
00182
00183 void test_open_error(void)
00184 {
00185 core->get_or_add_module("kernel::test::module_open_error");
00186 assert_exception(core->run());
00187 }
00188
00189 void test_sym_error(void)
00190 {
00191 core->get_or_add_module("kernel::test::module_sym_error");
00192 assert_exception(core->run());
00193 }
00194
00195 void test_load_loop(void)
00196 {
00197 core->get_or_add_module("kernel::test::module_loop");
00198 assert_exception(core->run());
00199 }
00200
00201 void test_module_scan(void)
00202 {
00203 kernel::module& module = core->get_or_add_module(
00204 "kernel::test::module_grand_parent");
00205 assert(core->begin_module() == &module);
00206 assert(core->next_module() == NULL);
00207
00208 core->run();
00209
00210 kernel::module& module2 = core->get_or_add_module(
00211 "kernel::test::module_parent");
00212
00213 assert(core->begin_module() == &module);
00214 assert(core->next_module() == &module2);
00215 assert(core->next_module() == NULL);
00216 }
00217
00218 void test_normalize_name(void)
00219 {
00220 assert(kernel::module::normalize_name("a::B::c") == "a_b_c");
00221 }
00222
00223 void test_threshold(void)
00224 {
00225 core->add_module_option_value("kernel::test::module_grand_parent", "v", "");
00226 kernel::module& module =
00227 core->get_module("kernel::test::module_grand_parent");
00228 assert(module.get_threshold() == 1);
00229 core->add_module_option_value("kernel::test::module_grand_parent", "v",
00230 "3");
00231 assert(module.get_threshold() == 3);
00232 }
00233
00234 void test_verbose(void)
00235 {
00236 cout << _("Testing verbose messages, ignore the following output:") << endl;
00237
00238 {
00239 kernel::core new_core;
00240 new_core.add_module_option_value("path", "", "scalestack/.libs");
00241 const char* argv[] =
00242 {
00243 "vvvv"
00244 };
00245 new_core.parse_arguments(1, argv);
00246 new_core.get_or_add_module("kernel::test::module_grand_parent");
00247 new_core.run();
00248 }
00249 }
00250
00251 void test_signal_shutdown(void)
00252 {
00253 core->get_or_add_module("kernel::test::module_grand_parent");
00254 core->set_signal_handlers();
00255 kill(getpid(), SIGQUIT);
00256 core->run();
00257 }
00258
00259 void test_print_help(void)
00260 {
00261 core->get_or_add_module("kernel::test::option_basic");
00262 core->get_or_add_module("kernel::test::module_grand_parent");
00263 const char* argv[] =
00264 {
00265 "help",
00266 };
00267 core->parse_arguments(1, argv);
00268 cout << _("Testing help messages, ignore the following output:") << endl;
00269 core->run();
00270 }
00271
00272 void test_print_config(void)
00273 {
00274 core->get_or_add_module("kernel::test::option_basic");
00275 core->get_or_add_module("kernel::test::module_grand_parent");
00276 const char* argv[] =
00277 {
00278 "config",
00279 };
00280 core->parse_arguments(1, argv);
00281 cout << _("Testing config messages, ignore the following output:") << endl;
00282 core->run();
00283 }
00284
00285 void test_check_invalid_options(void)
00286 {
00287 core->get_or_add_module("kernel::test::option_basic");
00288 core->add_module_option_value("kernel::test::option_basic",
00289 "Invalid", "value");
00290 assert_exception(core->run());
00291 }
00292
00293 test_suite(int argc, const char** argv):
00294 scalestack::kernel::test_suite<test_suite>(argc, argv)
00295 {
00296 test_case(test_suite::test_constructor);
00297 test_case(test_suite::test_parent_scan);
00298 test_case(test_suite::test_child_scan);
00299 test_case(test_suite::test_add_option);
00300 test_case(test_suite::test_get_option);
00301 test_case(test_suite::test_get_option_count);
00302 test_case(test_suite::test_option_scan);
00303 test_case(test_suite::test_load_grand_parent);
00304 test_case(test_suite::test_load_parent);
00305 test_case(test_suite::test_load_child);
00306 test_case(test_suite::test_open_error);
00307 test_case(test_suite::test_sym_error);
00308 test_case(test_suite::test_load_loop);
00309 test_case(test_suite::test_module_scan);
00310 test_case(test_suite::test_normalize_name);
00311 test_case(test_suite::test_threshold);
00312 test_case(test_suite::test_verbose);
00313 test_case(test_suite::test_signal_shutdown);
00314 test_case(test_suite::test_print_help);
00315 test_case(test_suite::test_print_config);
00316 test_case(test_suite::test_check_invalid_options);
00317 }
00318 };
00319
00320 int main(int argc, const char* argv[])
00321 {
00322 test_suite test_suite(argc - 1, argv + 1);
00323 test_suite.run();
00324 return 0;
00325 }