regression.cpp 15 KB

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