regression.cpp 17 KB

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