regression.cpp 15 KB

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