regression.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 fee 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. * 2001-01-23 made it compile with Borland (David Abrahams)
  16. * 2001-01-22 added --diff option (Jens Maurer)
  17. */
  18. #include <iostream>
  19. #include <string>
  20. #include <list>
  21. #include <map>
  22. #include <cstdlib>
  23. #include <fstream>
  24. #include <utility>
  25. #include <ctime>
  26. // It is OK to use boost headers which contain entirely inline code.
  27. #include <boost/config.hpp>
  28. #ifdef BOOST_NO_STDC_NAMESPACE
  29. namespace std {
  30. using ::exit; using ::system; using ::strftime; using ::gmtime;
  31. using ::time; using ::time_t; using ::getenv;
  32. }
  33. #endif
  34. std::string get_host()
  35. {
  36. #if defined __linux__
  37. return "linux";
  38. #elif defined __osf__
  39. return "tru64";
  40. #elif defined __sgi
  41. return "irix";
  42. #elif defined __sun
  43. return "solaris";
  44. #elif defined _WIN32
  45. return "win32";
  46. #elif defined __BEOS__
  47. return "beos";
  48. #elif defined __hpux
  49. return "hpux";
  50. #elif defined __IBMCPP__
  51. return "aix";
  52. #elif defined __MSL__ && __dest_os == __mac_os
  53. return "macos";
  54. #elif defined __MSL__ && __dest_os == __mac_os_x || defined(__APPLE_CC__)
  55. return "macosx";
  56. #else
  57. # error Please adapt for your platform
  58. #endif
  59. }
  60. // retrieve precise system configuration as descriptive string
  61. #ifdef __unix
  62. #include <sys/utsname.h>
  63. std::string get_system_configuration()
  64. {
  65. struct utsname ut; // "struct" is required for the DEC Tru64 compiler
  66. if(uname(&ut) < 0)
  67. return "";
  68. std::string config = std::string(ut.sysname) + " " + ut.release;
  69. config = config + " (CPU: " + ut.machine + ")";
  70. return config;
  71. }
  72. #elif defined _WIN32
  73. std::string get_system_configuration()
  74. {
  75. return "Microsoft Windows 32bit";
  76. }
  77. #elif defined __BEOS__
  78. std::string get_system_configuration()
  79. {
  80. return "BeOS 5 Intel Edition";
  81. }
  82. #elif defined __MSL__ && (__dest_os == __mac_os || __dest_os == __mac_os_x) || defined(__APPLE_CC__)
  83. std::string get_system_configuration()
  84. {
  85. return "Mac OS";
  86. }
  87. #else
  88. # error Please adapt for your platform
  89. #endif
  90. struct configuration
  91. {
  92. std::string compiler_config_file, test_config_file;
  93. std::string boostpath;
  94. std::string html_output;
  95. bool highlight_differences;
  96. std::string compiler, test;
  97. // defaults
  98. configuration()
  99. : compiler_config_file("compiler.cfg"), test_config_file("regression.cfg"),
  100. boostpath(".."), html_output("cs-" + get_host() + ".html"),
  101. highlight_differences(false),
  102. compiler("*"), test("") { }
  103. };
  104. configuration parse_command_line(char **first, char **last)
  105. {
  106. configuration cfg;
  107. bool output_redirected = false;
  108. for( ; first != last; ++first) {
  109. std::string arg = *first;
  110. if(arg == "--config") {
  111. cfg.compiler_config_file = *++first;
  112. } else if(arg == "--tests") {
  113. cfg.test_config_file = *++first;
  114. } else if(arg == "--boost") {
  115. cfg.boostpath = *++first;
  116. } else if(arg == "-o" || arg == "--output") {
  117. cfg.html_output = *++first;
  118. output_redirected = true;
  119. } else if(arg == "--diff") {
  120. cfg.highlight_differences = true;
  121. } else if(arg == "--compiler") {
  122. cfg.compiler = *++first;
  123. } else if(arg.substr(0,1) == "-") {
  124. std::cerr << "usage: regression [-h | --help] [--config compiler.cfg]\n"
  125. << " [--tests regression.cfg] [--boost path] [-o output.html] [--compiler <name>]\n"
  126. << " [test]\n"
  127. << " -h or --help print this help message\n"
  128. << " --config <file> compiler configuration file (default: compiler.cfg)\n"
  129. << " --tests <file> test configuration file (default: regression.cfg)\n"
  130. << " --boost <path> filesystem path to main boost directory (default: ..)\n"
  131. << " -o <file> name of output file (default: cs-OS.html)\n"
  132. << " --diff highlight differences in output file\n"
  133. << " --compiler <name> use only compiler <name> (default: *)\n"
  134. << " test a single test, including the action (compile, run, etc.)\n";
  135. std::exit(1);
  136. } else {
  137. // add the rest of the command line to the "test" line
  138. for( ; first != last; ++first)
  139. cfg.test += std::string(*first) + " ";
  140. break;
  141. }
  142. }
  143. if(cfg.test != "" && !output_redirected) {
  144. std::cerr << "Error: Please specify the HTML output file explicitly\n"
  145. << "(using \"--output file\") when running a single test only.\n";
  146. std::exit(1);
  147. }
  148. return cfg;
  149. }
  150. struct entry
  151. {
  152. std::string os, identifier, name, compile_only_command, compile_link_command, html;
  153. };
  154. // replace the first %name in s with value
  155. void replace(std::string & s,
  156. const std::string & name, const std::string & value)
  157. {
  158. std::string::size_type p = s.find(name);
  159. if(p != std::string::npos)
  160. s.replace(p, name.length(), value);
  161. }
  162. // replace all $XXX in s with the value of getenv("XXX")
  163. void replace_environment(std::string & s)
  164. {
  165. std::string::size_type end = 0;
  166. for(;;) {
  167. std::string::size_type pos = s.find('$', end);
  168. if(pos == std::string::npos)
  169. break;
  170. end = s.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_", pos+1);
  171. const char * env = std::getenv(s.substr(pos+1, end-pos-1).c_str());
  172. if(env)
  173. replace(s, s.substr(pos, end-pos), env);
  174. else
  175. break;
  176. }
  177. }
  178. // get a string line, ignoring empty lines and comment lines
  179. void getstringline( std::ifstream & is, std::string & s )
  180. {
  181. do {
  182. std::getline( static_cast<std::istream&>(is), s ); // cast required by IRIX
  183. } while ( is.good()
  184. && (!s.size() || (s.size() >= 2 && s[0] == '/' && s[1] == '/')) );
  185. }
  186. // read the compiler configuration from file and push entry's to out
  187. template<class OutputIterator>
  188. void read_compiler_configuration(const std::string & file, OutputIterator out)
  189. {
  190. std::ifstream f(file.c_str());
  191. int lineno = 0;
  192. while(f.good()) {
  193. entry e;
  194. getstringline(f, e.os);
  195. getstringline(f, e.identifier);
  196. getstringline(f, e.name);
  197. getstringline(f, e.compile_only_command);
  198. getstringline(f, e.compile_link_command);
  199. getstringline(f, e.html);
  200. *out = e;
  201. ++out;
  202. std::string l;
  203. std::getline(f, l);
  204. lineno += 6;
  205. if(l != "") {
  206. std::cerr << file << ", line " << lineno
  207. << ": Empty line expected, got " << l << "\n";
  208. std::exit(1);
  209. }
  210. }
  211. }
  212. std::string pass_string = "Pass";
  213. std::string fail_string = "<font color=\"#FF0000\">Fail</font>";
  214. // map test name to results, one character ("P" or "F") for each compiler
  215. typedef std::map<std::string, std::string> previous_results_type;
  216. previous_results_type read_previous_results(std::istream & is)
  217. {
  218. previous_results_type result;
  219. // finite state machine
  220. enum { prefix, testname, command, testresult } status = prefix;
  221. std::string line, current_test;
  222. while(std::getline(is, line)) {
  223. if(status == prefix) {
  224. if(line.substr(0, 6) == "<table")
  225. status = testname;
  226. } else if(status == testname) {
  227. if(line.substr(0, 6) == "<td><a") {
  228. std::string::size_type pos = line.find(">", 5);
  229. if(pos == std::string::npos || pos+1 >= line.size()) {
  230. std::cerr << "Line '" << line << "' has unknown format.";
  231. continue;
  232. }
  233. std::string::size_type pos_end = line.find("<", pos);
  234. if(pos_end == std::string::npos) {
  235. std::cerr << "Line '" << line << "' has unknown format.";
  236. continue;
  237. }
  238. current_test = line.substr(pos+1, pos_end - (pos+1));
  239. status = command;
  240. } else if(line.substr(0, 8) == "</table>") {
  241. break;
  242. }
  243. } else if(status == command) {
  244. status = testresult;
  245. } else if(status == testresult) {
  246. if(line == "</tr>")
  247. status = testname;
  248. else if(line.find(pass_string) != std::string::npos)
  249. result[current_test].append("P");
  250. else if(line.find(fail_string) != std::string::npos)
  251. result[current_test].append("F");
  252. else
  253. std::cerr << "Line '" << line << "' has unknown format.";
  254. }
  255. }
  256. return result;
  257. }
  258. // run command (possibly needs portability adjustment)
  259. bool execute(const std::string & command)
  260. {
  261. std::cout << command << std::endl; // fix: endl ensures cout ordering
  262. int ret = std::system(command.c_str());
  263. if(ret != 0)
  264. std::cout << "Return code: " << ret << std::endl;
  265. return ret == 0;
  266. }
  267. enum test_result {
  268. ok = 0,
  269. unknown_type,
  270. compile_failed, compile_ok, link_failed, link_ok, run_failed, run_ok
  271. };
  272. test_result compile(std::string command, const std::string & boostpath,
  273. const std::string & file)
  274. {
  275. replace(command, "%source", boostpath + "/" + file);
  276. return execute(command) ? compile_ok : compile_failed;
  277. }
  278. test_result link(std::string command, const std::string & boostpath,
  279. const std::string & file)
  280. {
  281. replace(command, "%source", boostpath + "/" + file);
  282. return execute(command) ? link_ok : link_failed;
  283. }
  284. test_result run(std::string command, const std::string & boostpath,
  285. const std::string & file, std::string args)
  286. {
  287. std::string exename = "boosttmp.exe";
  288. replace(command, "%source", boostpath + "/" + file);
  289. if(execute(command)) {
  290. // cygwin seems to require leading ./ on some systems (JM)
  291. #if !defined(__CYGWIN__) && defined(_WIN32)
  292. if(get_host() != "win32")
  293. #endif
  294. exename = "./" + exename;
  295. replace(args, "%boost", boostpath);
  296. return execute(exename + " " + args) ? run_ok : run_failed;
  297. } else {
  298. return link_failed;
  299. }
  300. }
  301. std::pair<test_result, test_result>
  302. run_test(const std::string & type, std::string compile_only_command,
  303. std::string compile_link_command,
  304. const std::string & boostpath, const std::string & source,
  305. const std::string & args)
  306. {
  307. replace(compile_only_command, "%include", boostpath);
  308. replace(compile_link_command, "%include", boostpath);
  309. if(type == "compile")
  310. return std::make_pair(compile(compile_only_command, boostpath, source), compile_ok);
  311. else if(type == "compile-fail")
  312. return std::make_pair(compile(compile_only_command, boostpath, source), compile_failed);
  313. else if(type == "link")
  314. return std::make_pair(link(compile_link_command, boostpath, source), link_ok);
  315. else if(type == "link-fail")
  316. return std::make_pair(link(compile_link_command, boostpath, source), link_failed);
  317. else if(type == "run")
  318. return std::make_pair(run(compile_link_command, boostpath, source, args), run_ok);
  319. else if(type == "run-fail")
  320. return std::make_pair(run(compile_link_command, boostpath, source, args), run_failed);
  321. else
  322. return std::make_pair(unknown_type, ok);
  323. }
  324. template<class ForwardIterator>
  325. void do_tests(std::ostream & out,
  326. ForwardIterator firstcompiler, ForwardIterator lastcompiler,
  327. const std::string & testconfig, const std::string & boostpath,
  328. const previous_results_type& previous_results,
  329. bool highlight_diff)
  330. {
  331. out << "<tr>\n"
  332. << "<td>Program</td>\n"
  333. << "<td>Test<br>Type</td>\n";
  334. for(ForwardIterator it = firstcompiler; it != lastcompiler; ++it) {
  335. out << "<td>" << it->html << "</td>\n";
  336. }
  337. out << "</tr>\n";
  338. std::ifstream f(testconfig.c_str());
  339. while(f.good()) {
  340. std::string l;
  341. getstringline(f, l);
  342. if (!f.good()) break;
  343. typedef std::string::size_type sz_type;
  344. sz_type p = l.find(' ');
  345. if(p == std::string::npos) {
  346. std::cerr << "Test " << l << " is wrong\n";
  347. continue;
  348. }
  349. std::string type(l, 0, p);
  350. sz_type end_filename = l.find(' ', p+1);
  351. std::string file, args;
  352. if(end_filename == std::string::npos) {
  353. file = l.substr(p+1, std::string::npos);
  354. } else {
  355. file = l.substr(p+1, end_filename-(p+1));
  356. args = l.substr(end_filename+1, std::string::npos);
  357. }
  358. std::cout << "*** " << file << " ***\n\n";
  359. out << "<tr>\n"
  360. << "<td><a href=\"../" << file << "\">" << file << "</a></td>\n"
  361. << "<td>" << type << "</td>\n";
  362. previous_results_type::const_iterator prev_iter =
  363. previous_results.find(file);
  364. std::string previous = (prev_iter == previous_results.end() ?
  365. std::string("") : prev_iter->second);
  366. std::string::size_type i = 0;
  367. for(ForwardIterator it = firstcompiler; it != lastcompiler; ++it, ++i) {
  368. std::cout << "** " << it->name << "\n";
  369. std::pair<test_result, test_result> result =
  370. run_test(type, it->compile_only_command, it->compile_link_command, boostpath, file, args);
  371. if(result.first == unknown_type) {
  372. std::cerr << "Unknown test type " << type << ", skipped\n";
  373. continue;
  374. }
  375. bool pass = result.first == result.second;
  376. char prev = (i < previous.size() ? previous[i] : ' ');
  377. bool changed = highlight_diff &&
  378. ((prev == 'F' && pass) || (prev == 'P' && !pass) || prev == ' ');
  379. out << "<td>"
  380. << (changed ? "<font size=\"+3\"><em>" : "")
  381. << (pass ? pass_string : fail_string)
  382. << (changed ? "</em></font>" : "")
  383. << "</td>" << std::endl;
  384. std::cout << (result.first == result.second ? "Pass" : "Fail") << "\n\n";
  385. }
  386. out << "</tr>\n";
  387. }
  388. }
  389. int main(int argc, char * argv[])
  390. {
  391. configuration config = parse_command_line(argv+1, argv+argc);
  392. std::list<entry> compilers;
  393. read_compiler_configuration(config.compiler_config_file,
  394. std::back_inserter(compilers));
  395. std::string host = get_host();
  396. for(std::list<entry>::iterator it = compilers.begin(); it != compilers.end(); ) {
  397. if(it->os == host && (config.compiler == "*" ||
  398. it->identifier == config.compiler)) {
  399. replace_environment(it->compile_only_command);
  400. replace_environment(it->compile_link_command);
  401. ++it;
  402. } else {
  403. it = compilers.erase(it);
  404. }
  405. }
  406. if(compilers.empty())
  407. std::cerr << "You do not have any compatible compilers defined." << std::endl;
  408. // if explicit test requested, write temporary file for do_tests
  409. if(config.test != "") {
  410. std::ofstream tmp((config.test_config_file="boosttmp.tmp").c_str());
  411. tmp << config.test << std::endl;
  412. }
  413. previous_results_type previous_results;
  414. if(config.highlight_differences) {
  415. std::ifstream in(config.html_output.c_str());
  416. previous_results = read_previous_results(in);
  417. }
  418. std::ofstream out( config.html_output.c_str() );
  419. char run_date[100];
  420. std::time_t ct;
  421. std::time(&ct);
  422. std::strftime(run_date, sizeof(run_date), "%d %b %Y %H:%M GMT", std::gmtime(&ct));
  423. out << "<html>\n<head>\n<title>\nCompiler Status: " + host + "\n</title>\n</head>\n"
  424. << "<body bgcolor=\"#ffffff\" text=\"#000000\">\n"
  425. << "<h1><img border border=\"0\" src=\"../c++boost.gif\" width=\"277\" height=\"86\"></h1>\n"
  426. << "<h1>Compiler Status: " + host + "</h1>\n"
  427. << "\n"
  428. << "<p><b>Run Date:</b> " << run_date << "</p>\n"
  429. << "<p><b>System Configuration:</b> " << get_system_configuration()
  430. << "</p>\n"
  431. << "<p>\n"
  432. << "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n";
  433. do_tests(out, compilers.begin(), compilers.end(), config.test_config_file, config.boostpath,
  434. previous_results, config.highlight_differences);
  435. out << "</table></p>\n<p>\n";
  436. if(host == "linux")
  437. out << "Notes: A hand-crafted &lt;limits&gt; Standard header has been\n"
  438. << "applied to all configurations.\n"
  439. << "The tests were run on a GNU libc 2.2.2 system which has improved\n"
  440. << "wide character support compared to 2.1.x and earlier versions.";
  441. else if(host == "irix" || host == "tru64")
  442. out << "Note: For the 'clib' configuration, the missing new-style C\n"
  443. << "library headers &lt;cXXX&gt; have been supplied.\n";
  444. out << "</p>\n</body>\n</html>" << std::endl;
  445. return 0;
  446. }
粤ICP备19079148号