regression.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 { using ::exit; using ::system; using ::strftime; using ::gmtime; using ::time; }
  27. # endif
  28. std::string get_host()
  29. {
  30. #if defined __linux__
  31. return "linux";
  32. #elif defined _WIN32
  33. return "win32";
  34. #elif defined __BEOS__
  35. return "beos";
  36. #else
  37. #error Please adapt for your platform
  38. #endif
  39. }
  40. struct entry
  41. {
  42. std::string os, identifier, name, compile_only_command, compile_link_command, html;
  43. };
  44. void replace(std::string & s,
  45. const std::string & name, const std::string & value)
  46. {
  47. std::string::size_type p = s.find(name);
  48. if(p != std::string::npos)
  49. s.replace(p, name.length(), value);
  50. }
  51. void replace_environment(std::string & s)
  52. {
  53. std::string::size_type end = 0;
  54. for(;;) {
  55. std::string::size_type pos = s.find('$', end);
  56. if(pos == std::string::npos)
  57. break;
  58. end = s.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_", pos+1);
  59. const char * env = getenv(s.substr(pos+1, end-pos-1).c_str());
  60. if(env)
  61. replace(s, s.substr(pos, end-pos), env);
  62. else
  63. break;
  64. }
  65. }
  66. // get a string line, ignoring empty lines and comment lines
  67. void getstringline( std::ifstream & is, std::string & s )
  68. {
  69. do { std::getline( is, s ); }
  70. while ( is.good()
  71. && (!s.size() || (s.size() >= 2 && s[0] == '/' && s[1] == '/')) );
  72. }
  73. template<class OutputIterator>
  74. void read_compiler_configuration(const std::string & file, OutputIterator out)
  75. {
  76. std::ifstream f(file.c_str());
  77. int lineno = 0;
  78. while(f.good()) {
  79. entry e;
  80. getstringline(f, e.os);
  81. getstringline(f, e.identifier);
  82. getstringline(f, e.name);
  83. getstringline(f, e.compile_only_command);
  84. getstringline(f, e.compile_link_command);
  85. getstringline(f, e.html);
  86. *out = e;
  87. ++out;
  88. std::string l;
  89. std::getline(f, l);
  90. lineno += 6;
  91. if(l != "") {
  92. std::cerr << file << ", line " << lineno
  93. << ": Empty line expected, got " << l << "\n";
  94. std::exit(1);
  95. }
  96. }
  97. }
  98. bool execute(const std::string & command)
  99. {
  100. std::cout << command << std::endl; // fix: endl ensures cout ordering
  101. return std::system(command.c_str()) == 0;
  102. }
  103. enum test_result {
  104. ok = 0,
  105. unknown_type,
  106. compile_failed, compile_ok, link_failed, link_ok, run_failed, run_ok
  107. };
  108. test_result compile(std::string command, const std::string & boostpath,
  109. const std::string & file)
  110. {
  111. replace(command, "%source", boostpath + "/" + file);
  112. return execute(command) ? compile_ok : compile_failed;
  113. }
  114. test_result link(std::string command, const std::string & boostpath,
  115. const std::string & file)
  116. {
  117. replace(command, "%source", boostpath + "/" + file);
  118. return execute(command) ? link_ok : link_failed;
  119. }
  120. test_result run(std::string command, const std::string & boostpath,
  121. const std::string & file)
  122. {
  123. std::string exename = "boosttmp.exe";
  124. replace(command, "%source", boostpath + "/" + file);
  125. if(execute(command)) {
  126. return execute( (get_host() == "win32" ? "" : "./") + exename ) ?
  127. run_ok : run_failed;
  128. } else {
  129. return link_failed;
  130. }
  131. }
  132. std::pair<test_result, test_result>
  133. run_test(const std::string & type, std::string compile_only_command,
  134. std::string compile_link_command,
  135. const std::string & boostpath, const std::string & source)
  136. {
  137. replace(compile_only_command, "%include", boostpath);
  138. replace(compile_link_command, "%include", boostpath);
  139. if(type == "compile")
  140. return std::make_pair(compile(compile_only_command, boostpath, source), compile_ok);
  141. else if(type == "compile-fail")
  142. return std::make_pair(compile(compile_only_command, boostpath, source), compile_failed);
  143. else if(type == "link")
  144. return std::make_pair(link(compile_link_command, boostpath, source), link_ok);
  145. else if(type == "link-fail")
  146. return std::make_pair(link(compile_link_command, boostpath, source), link_failed);
  147. else if(type == "run")
  148. return std::make_pair(run(compile_link_command, boostpath, source), run_ok);
  149. else if(type == "run-fail")
  150. return std::make_pair(run(compile_link_command, boostpath, source), run_failed);
  151. else
  152. return std::make_pair(unknown_type, ok);
  153. }
  154. template<class ForwardIterator>
  155. void do_tests(std::ostream & out,
  156. ForwardIterator firstcompiler, ForwardIterator lastcompiler,
  157. const std::string & testconfig, const std::string & boostpath)
  158. {
  159. out << "<tr>\n"
  160. << "<td>Program</td>\n"
  161. << "<td>Test<br>Type</td>\n";
  162. for(ForwardIterator it = firstcompiler; it != lastcompiler; ++it) {
  163. out << "<td>" << it->html << "</td>\n";
  164. }
  165. out << "</tr>\n";
  166. std::ifstream f(testconfig.c_str());
  167. while(f.good()) {
  168. std::string l;
  169. getstringline(f, l);
  170. if (!f.good()) break;
  171. typedef std::string::size_type sz_type;
  172. sz_type p = l.find(' ');
  173. if(p == std::string::npos) {
  174. std::cerr << "Test " << l << " is wrong\n";
  175. continue;
  176. }
  177. std::string type(l, 0, p);
  178. std::string file(l, p+1, std::string::npos); // 3rd arg to fix VC++ bug
  179. std::cout << "*** " << file << " ***\n\n";
  180. out << "<tr>\n"
  181. << "<td><a href=\"../" << file << "\">" << file << "</a></td>\n"
  182. << "<td>" << type << "</td>\n";
  183. for(ForwardIterator it = firstcompiler; it != lastcompiler; ++it) {
  184. std::cout << "** " << it->name << "\n";
  185. std::pair<test_result, test_result> result =
  186. run_test(type, it->compile_only_command, it->compile_link_command, boostpath, file);
  187. if(result.first == unknown_type) {
  188. std::cerr << "Unknown test type " << type << ", skipped\n";
  189. continue;
  190. }
  191. out << "<td>"
  192. << (result.first == result.second ? "Pass" : "<font color=\"#FF0000\">Fail</font>")
  193. << "</td>" << std::endl;
  194. std::cout << (result.first == result.second ? "Pass" : "Fail") << "\n\n";
  195. }
  196. out << "</tr>\n";
  197. }
  198. }
  199. int main(int argc, char * argv[])
  200. {
  201. // std::vector<std::string> args(argv+1, argv+argc);
  202. // hack around VC++ lack of ctor taking iterator args
  203. std::vector<std::string> args;
  204. for ( const char ** ait = (const char **)(argv+1);
  205. ait != (const char **)(argv+argc); ++ait)
  206. args.push_back(std::string( *ait ));
  207. if(args.size() < 3) {
  208. std::cerr << argv[0]
  209. << " usage: compiler-config test-config boost-path [compiler|* [[command] file]]\n";
  210. std::exit(1);
  211. }
  212. std::string compiler = (args.size() >= 4 ? args[3] : "*");
  213. std::list<entry> l;
  214. read_compiler_configuration(args[0], std::back_inserter(l));
  215. std::string host = get_host();
  216. for(std::list<entry>::iterator it = l.begin(); it != l.end(); ) {
  217. if(it->os == host && (compiler == "*" || it->identifier == compiler)) {
  218. replace_environment(it->compile_only_command);
  219. replace_environment(it->compile_link_command);
  220. ++it;
  221. } else {
  222. it = l.erase(it);
  223. }
  224. }
  225. std::string boostpath = args[2];
  226. // if file argument present, write temporary test configuration file for do_tests
  227. if(args.size() >= 5) { // file argument present
  228. std::ofstream tmp((args[1]="boosttmp.tmp").c_str());
  229. if (args.size() >= 6) tmp << args[4] << " " << args[5] << std::endl; // command present
  230. else tmp << "compile " << args[4] << std::endl; // command not present
  231. }
  232. std::ofstream out( ("cs-" + host + ".html").c_str() );
  233. char run_date[100];
  234. time_t ct;
  235. std::time(&ct);
  236. std::strftime(run_date, sizeof(run_date), "%d %b %Y %H:%M GMT", std::gmtime(&ct));
  237. out << "<html>\n<head>\n<title>\nCompiler Status: " + host + "\n</title>\n</head>\n"
  238. << "<body bgcolor=\"#ffffff\" text=\"#000000\">\n"
  239. << "<h1><img border border=\"0\" src=\"../c++boost.gif\" width=\"277\" height=\"86\"></h1>\n"
  240. << "<h1>Compiler Status: " + host + "</h1>\n"
  241. << "</p>\n"
  242. << "<p><b>Run Date:</b> " << run_date << "</p>\n"
  243. << "<p>\n"
  244. << "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n";
  245. do_tests(out, l.begin(), l.end(), args[1], boostpath);
  246. out << "</table>\n";
  247. return 0;
  248. }
粤ICP备19079148号