regression.cpp 16 KB

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