regression.cpp 12 KB

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