regression.cpp 15 KB

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