regression.cpp 7.1 KB

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