regression.cpp 15 KB

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