regression.cpp 17 KB

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