regression.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* boost regression test program
  2. *
  3. * Copyright Jens Maurer 2000
  4. * Permission to use, copy, modify, sell, and distribute this software
  5. * is hereby granted without free provided that the above copyright notice
  6. * appears in all copies and that both that copyright notice and this
  7. * permission notice appear in supporting documentation,
  8. *
  9. * Jens Maurer makes no representations about the suitability of this
  10. * software for any purpose. It is provided "as is" without express or
  11. * implied warranty.
  12. *
  13. * See http://www.boost.org for most recent version including documentation.
  14. */
  15. #include <iostream>
  16. #include <string>
  17. #include <list>
  18. #include <vector>
  19. #include <cstdlib>
  20. #include <fstream>
  21. #include <utility>
  22. #include <ctime>
  23. // It is OK to use boost headers which contain entirely inline code.
  24. #include <boost/config.hpp>
  25. #ifdef BOOST_NO_STDC_NAMESPACE
  26. namespace std {
  27. using ::exit; using ::system; using ::strftime; using ::gmtime;
  28. using ::time; using ::time_t;
  29. }
  30. #endif
  31. std::string get_host()
  32. {
  33. #if defined __linux__
  34. return "linux";
  35. #elif defined __osf__
  36. return "tru64";
  37. #elif defined __sgi
  38. return "irix";
  39. #elif defined _WIN32
  40. return "win32";
  41. #elif defined __BEOS__
  42. return "beos";
  43. #else
  44. # error Please adapt for your platform
  45. #endif
  46. }
  47. // retrieve precise system configuration as descriptive string
  48. #ifdef __unix
  49. #include <sys/utsname.h>
  50. std::string get_system_configuration()
  51. {
  52. struct utsname ut; // "struct" is required for the DEC Tru64 compiler
  53. if(uname(&ut) < 0)
  54. return "";
  55. std::string config = std::string(ut.sysname) + " " + ut.release;
  56. config = config + " (CPU: " + ut.machine + ")";
  57. return config;
  58. }
  59. #elif defined _WIN32
  60. std::string get_system_configuration()
  61. {
  62. return "Microsoft Windows 32bit";
  63. }
  64. #elif defined __BEOS__
  65. std::string get_system_configuration()
  66. {
  67. return "BeOS";
  68. }
  69. #else
  70. # error Please adapt for your platform
  71. #endif
  72. struct configuration
  73. {
  74. std::string compiler_config_file, test_config_file;
  75. std::string boostpath;
  76. std::string html_output;
  77. std::string compiler, test;
  78. // defaults
  79. configuration()
  80. : compiler_config_file("compiler.cfg"), test_config_file("regression.cfg"),
  81. boostpath(".."), html_output("cs-" + get_host() + ".html"),
  82. compiler("*"), test("") { }
  83. };
  84. configuration parse_command_line(char **first, char **last)
  85. {
  86. configuration cfg;
  87. for( ; first != last; ++first) {
  88. std::string arg = *first;
  89. if(arg == "--config") {
  90. cfg.compiler_config_file = *++first;
  91. } else if(arg == "--tests") {
  92. cfg.test_config_file = *++first;
  93. } else if(arg == "--boost") {
  94. cfg.boostpath = *++first;
  95. } else if(arg == "-o" || arg == "--output") {
  96. cfg.html_output = *++first;
  97. } else if(arg == "--compiler") {
  98. cfg.compiler = *++first;
  99. } else if(arg.substr(0,1) == "-") {
  100. std::cerr << "usage: regression [-h | --help] [--config compiler.cfg]\n"
  101. << " [--tests regression.cfg] [--boost path] [-o output.html] [--compiler <name>]\n"
  102. << " [test]\n"
  103. << " -h or --help print this help message\n"
  104. << " --config <file> compiler configuration file (default: compiler.cfg)\n"
  105. << " --tests <file> test configuration file (default: regression.cfg)\n"
  106. << " --boost <path> filesystem path to main boost directory (default: ..)\n"
  107. << " -o <file> name of output file (default: cs-OS.html)\n"
  108. << " --compiler <name> use only compiler <name> (default: *)\n"
  109. << " test a single test, including the action (compile, run, etc.)\n";
  110. std::exit(1);
  111. } else {
  112. // add the rest of the command line to the "test" line
  113. for( ; first != last; ++first)
  114. cfg.test += std::string(*first) + " ";
  115. break;
  116. }
  117. }
  118. return cfg;
  119. }
  120. struct entry
  121. {
  122. std::string os, identifier, name, compile_only_command, compile_link_command, html;
  123. };
  124. // replace the first %name in s with value
  125. void replace(std::string & s,
  126. const std::string & name, const std::string & value)
  127. {
  128. std::string::size_type p = s.find(name);
  129. if(p != std::string::npos)
  130. s.replace(p, name.length(), value);
  131. }
  132. // replace all $XXX in s with the value of getenv("XXX")
  133. void replace_environment(std::string & s)
  134. {
  135. std::string::size_type end = 0;
  136. for(;;) {
  137. std::string::size_type pos = s.find('$', end);
  138. if(pos == std::string::npos)
  139. break;
  140. end = s.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_", pos+1);
  141. const char * env = getenv(s.substr(pos+1, end-pos-1).c_str());
  142. if(env)
  143. replace(s, s.substr(pos, end-pos), env);
  144. else
  145. break;
  146. }
  147. }
  148. // get a string line, ignoring empty lines and comment lines
  149. void getstringline( std::ifstream & is, std::string & s )
  150. {
  151. do {
  152. std::getline( static_cast<std::istream&>(is), s ); // cast required by IRIX
  153. } while ( is.good()
  154. && (!s.size() || (s.size() >= 2 && s[0] == '/' && s[1] == '/')) );
  155. }
  156. // read the compiler configuration from file and push entry's to out
  157. template<class OutputIterator>
  158. void read_compiler_configuration(const std::string & file, OutputIterator out)
  159. {
  160. std::ifstream f(file.c_str());
  161. int lineno = 0;
  162. while(f.good()) {
  163. entry e;
  164. getstringline(f, e.os);
  165. getstringline(f, e.identifier);
  166. getstringline(f, e.name);
  167. getstringline(f, e.compile_only_command);
  168. getstringline(f, e.compile_link_command);
  169. getstringline(f, e.html);
  170. *out = e;
  171. ++out;
  172. std::string l;
  173. std::getline(f, l);
  174. lineno += 6;
  175. if(l != "") {
  176. std::cerr << file << ", line " << lineno
  177. << ": Empty line expected, got " << l << "\n";
  178. std::exit(1);
  179. }
  180. }
  181. }
  182. // run command (possibly needs portability adjustment)
  183. bool execute(const std::string & command)
  184. {
  185. std::cout << command << std::endl; // fix: endl ensures cout ordering
  186. return std::system(command.c_str()) == 0;
  187. }
  188. enum test_result {
  189. ok = 0,
  190. unknown_type,
  191. compile_failed, compile_ok, link_failed, link_ok, run_failed, run_ok
  192. };
  193. test_result compile(std::string command, const std::string & boostpath,
  194. const std::string & file)
  195. {
  196. replace(command, "%source", boostpath + "/" + file);
  197. return execute(command) ? compile_ok : compile_failed;
  198. }
  199. test_result link(std::string command, const std::string & boostpath,
  200. const std::string & file)
  201. {
  202. replace(command, "%source", boostpath + "/" + file);
  203. return execute(command) ? link_ok : link_failed;
  204. }
  205. test_result run(std::string command, const std::string & boostpath,
  206. const std::string & file, const std::string & args)
  207. {
  208. std::string exename = "boosttmp.exe";
  209. replace(command, "%source", boostpath + "/" + file);
  210. if(execute(command)) {
  211. if(get_host() != "win32")
  212. exename = "./" + exename;
  213. return execute(exename + " " + args) ? run_ok : run_failed;
  214. } else {
  215. return link_failed;
  216. }
  217. }
  218. std::pair<test_result, test_result>
  219. run_test(const std::string & type, std::string compile_only_command,
  220. std::string compile_link_command,
  221. const std::string & boostpath, const std::string & source,
  222. const std::string & args)
  223. {
  224. replace(compile_only_command, "%include", boostpath);
  225. replace(compile_link_command, "%include", boostpath);
  226. if(type == "compile")
  227. return std::make_pair(compile(compile_only_command, boostpath, source), compile_ok);
  228. else if(type == "compile-fail")
  229. return std::make_pair(compile(compile_only_command, boostpath, source), compile_failed);
  230. else if(type == "link")
  231. return std::make_pair(link(compile_link_command, boostpath, source), link_ok);
  232. else if(type == "link-fail")
  233. return std::make_pair(link(compile_link_command, boostpath, source), link_failed);
  234. else if(type == "run")
  235. return std::make_pair(run(compile_link_command, boostpath, source, args), run_ok);
  236. else if(type == "run-fail")
  237. return std::make_pair(run(compile_link_command, boostpath, source, args), run_failed);
  238. else
  239. return std::make_pair(unknown_type, ok);
  240. }
  241. template<class ForwardIterator>
  242. void do_tests(std::ostream & out,
  243. ForwardIterator firstcompiler, ForwardIterator lastcompiler,
  244. const std::string & testconfig, const std::string & boostpath)
  245. {
  246. out << "<tr>\n"
  247. << "<td>Program</td>\n"
  248. << "<td>Test<br>Type</td>\n";
  249. for(ForwardIterator it = firstcompiler; it != lastcompiler; ++it) {
  250. out << "<td>" << it->html << "</td>\n";
  251. }
  252. out << "</tr>\n";
  253. std::ifstream f(testconfig.c_str());
  254. while(f.good()) {
  255. std::string l;
  256. getstringline(f, l);
  257. if (!f.good()) break;
  258. typedef std::string::size_type sz_type;
  259. sz_type p = l.find(' ');
  260. if(p == std::string::npos) {
  261. std::cerr << "Test " << l << " is wrong\n";
  262. continue;
  263. }
  264. std::string type(l, 0, p);
  265. sz_type end_filename = l.find(' ', p+1);
  266. std::string file(l, p+1, end_filename-(p+1));
  267. std::string args(l, end_filename+1, std::string::npos);
  268. std::cout << "*** " << file << " ***\n\n";
  269. out << "<tr>\n"
  270. << "<td><a href=\"../" << file << "\">" << file << "</a></td>\n"
  271. << "<td>" << type << "</td>\n";
  272. for(ForwardIterator it = firstcompiler; it != lastcompiler; ++it) {
  273. std::cout << "** " << it->name << "\n";
  274. std::pair<test_result, test_result> result =
  275. run_test(type, it->compile_only_command, it->compile_link_command, boostpath, file, args);
  276. if(result.first == unknown_type) {
  277. std::cerr << "Unknown test type " << type << ", skipped\n";
  278. continue;
  279. }
  280. out << "<td>"
  281. << (result.first == result.second ? "Pass" : "<font color=\"#FF0000\">Fail</font>")
  282. << "</td>" << std::endl;
  283. std::cout << (result.first == result.second ? "Pass" : "Fail") << "\n\n";
  284. }
  285. out << "</tr>\n";
  286. }
  287. }
  288. int main(int argc, char * argv[])
  289. {
  290. configuration config = parse_command_line(argv+1, argv+argc);
  291. std::list<entry> l;
  292. read_compiler_configuration(config.compiler_config_file,
  293. std::back_inserter(l));
  294. std::string host = get_host();
  295. for(std::list<entry>::iterator it = l.begin(); it != l.end(); ) {
  296. if(it->os == host && (config.compiler == "*" ||
  297. it->identifier == config.compiler)) {
  298. replace_environment(it->compile_only_command);
  299. replace_environment(it->compile_link_command);
  300. ++it;
  301. } else {
  302. it = l.erase(it);
  303. }
  304. }
  305. // if explicit test requested, write temporary file for do_tests
  306. if(config.test != "") {
  307. std::ofstream tmp((config.test_config_file="boosttmp.tmp").c_str());
  308. tmp << config.test << std::endl;
  309. }
  310. std::ofstream out( config.html_output.c_str() );
  311. char run_date[100];
  312. std::time_t ct;
  313. std::time(&ct);
  314. std::strftime(run_date, sizeof(run_date), "%d %b %Y %H:%M GMT", std::gmtime(&ct));
  315. out << "<html>\n<head>\n<title>\nCompiler Status: " + host + "\n</title>\n</head>\n"
  316. << "<body bgcolor=\"#ffffff\" text=\"#000000\">\n"
  317. << "<h1><img border border=\"0\" src=\"../c++boost.gif\" width=\"277\" height=\"86\"></h1>\n"
  318. << "<h1>Compiler Status: " + host + "</h1>\n"
  319. << "\n"
  320. << "<p><b>Run Date:</b> " << run_date << "</p>\n"
  321. << "<p><b>System Configuration:</b> " << get_system_configuration()
  322. << "</p>\n"
  323. << "<p>\n"
  324. << "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n";
  325. do_tests(out, l.begin(), l.end(), config.test_config_file, config.boostpath);
  326. out << "</table></p>\n<p>\n";
  327. if(host == "linux")
  328. out << "Notes: A hand-crafted &lt;limits&gt; Standard header has been\n"
  329. << "applied to all configurations.\n"
  330. << "The tests were run on a GNU libc 2.2 system which has improved\n"
  331. << "wide character support compared to previous versions.";
  332. else if(host == "irix" || host == "tru64")
  333. out << "Note: For the 'clib' configuration, the missing new-style C\n"
  334. << "library headers &lt;cXXX&gt; have been supplied.\n";
  335. out << "</p>\n</body>\n</html>" << std::endl;
  336. return 0;
  337. }
粤ICP备19079148号