cmCreateTestSourceList.cxx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCreateTestSourceList.h"
  14. // cmCreateTestSourceList
  15. bool cmCreateTestSourceList::InitialPass(std::vector<std::string> const& argsIn)
  16. {
  17. if (argsIn.size() < 3)
  18. {
  19. this->SetError("called with wrong number of arguments.");
  20. return false;
  21. }
  22. std::vector<std::string> args;
  23. cmSystemTools::ExpandListArguments(argsIn, args, true);
  24. std::vector<std::string>::iterator i = args.begin();
  25. // Name of the source list
  26. const char* sourceList = i->c_str();
  27. ++i;
  28. // Name of the test driver
  29. std::string driver = m_Makefile->GetCurrentOutputDirectory();
  30. driver += "/";
  31. driver += *i;
  32. driver += ".cxx";
  33. ++i;
  34. std::ofstream fout(driver.c_str());
  35. if (!fout)
  36. {
  37. std::string err = "Could not create file ";
  38. err += driver;
  39. err += " for cmCreateTestSourceList command.";
  40. this->SetError(err.c_str());
  41. return false;
  42. }
  43. // Create the test driver file
  44. fout <<
  45. "#include <stdio.h>\n"
  46. "#include <stdlib.h>\n"
  47. "#include <string.h>\n"
  48. "\n"
  49. "// Forward declare test functions\n"
  50. "\n";
  51. std::vector<std::string>::iterator testsBegin = i;
  52. std::vector<std::string> tests_func_name;
  53. // The rest of the arguments consist of a list of test source files.
  54. // Sadly, they can be in directories. Let's find a unique function
  55. // name for the corresponding test, and push it to the tests_func_name
  56. // list.
  57. // For the moment:
  58. // - replace spaces ' ', ':' and '/' with underscores '_'
  59. for(i = testsBegin; i != args.end(); ++i)
  60. {
  61. std::string func_name = *i;
  62. cmSystemTools::ConvertToUnixSlashes(func_name);
  63. cmSystemTools::ReplaceString(func_name, " ", "_");
  64. cmSystemTools::ReplaceString(func_name, "/", "_");
  65. cmSystemTools::ReplaceString(func_name, ":", "_");
  66. tests_func_name.push_back(func_name);
  67. fout << "int " << func_name << "(int, char**);\n";
  68. }
  69. fout <<
  70. "\n"
  71. "// Create map\n"
  72. "\n"
  73. "typedef int (*MainFuncPointer)(int , char**);\n"
  74. "struct functionMapEntry\n"
  75. "{\n"
  76. " const char* name;\n"
  77. " MainFuncPointer func;\n"
  78. "};\n"
  79. "\n"
  80. "functionMapEntry cmakeGeneratedFunctionMapEntries[] = {\n";
  81. int numTests = 0;
  82. std::vector<std::string>::iterator j;
  83. for(i = testsBegin, j = tests_func_name.begin(); i != args.end(); ++i, ++j)
  84. {
  85. fout <<
  86. " {\n"
  87. " \"" << *i << "\",\n"
  88. " " << *j << "\n"
  89. " },\n";
  90. numTests++;
  91. }
  92. fout <<
  93. "};\n"
  94. "\n"
  95. "// Allocate and create a lowercased copy of string\n"
  96. "\n"
  97. "char* lowercase(const char *string)\n"
  98. "{\n"
  99. " char *new_string = new char[strlen(string) + 1];\n"
  100. " if (!new_string)\n"
  101. " {\n"
  102. " return NULL;\n"
  103. " }\n"
  104. " strcpy(new_string, string);\n"
  105. " char *p = new_string;\n"
  106. " while (*p != NULL)\n"
  107. " {\n"
  108. " *p = tolower(*p);\n"
  109. " ++p;\n"
  110. " }\n"
  111. " return new_string;\n"
  112. "}\n"
  113. "\n"
  114. "int main(int ac, char** av)\n"
  115. "{\n"
  116. " int NumTests = " << numTests << ";\n"
  117. " int i;\n"
  118. " \n"
  119. " // If no test name was given\n"
  120. " if (ac < 2)\n"
  121. " {\n"
  122. " // If there is only one test, then run it with the arguments\n"
  123. " if (NumTests == 1)\n"
  124. " {\n"
  125. " return (*cmakeGeneratedFunctionMapEntries[0].func)(ac, av);\n"
  126. " }\n"
  127. " \n"
  128. " // Ask for a test\n"
  129. " printf(\"Available tests:\\n\");\n"
  130. " for (i =0; i < NumTests; ++i)\n"
  131. " {\n"
  132. " printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
  133. " }\n"
  134. " printf(\"To run a test, enter the test number: \");\n"
  135. " int testNum = 0;\n"
  136. " scanf(\"%d\", &testNum);\n"
  137. " if (testNum >= NumTests)\n"
  138. " {\n"
  139. " printf(\"%3d is an invalid test number.\\n\", testNum);\n"
  140. " return -1;\n"
  141. " }\n"
  142. " return (*cmakeGeneratedFunctionMapEntries[testNum].func)(ac-1, av+1);\n"
  143. " }\n"
  144. " \n"
  145. " // If partial match is requested\n"
  146. " int partial_match = (strcmp(av[1], \"-R\") == 0) ? 1 : 0;\n"
  147. " if (partial_match && ac < 3)\n"
  148. " {\n"
  149. " printf(\"-R needs an additional parameter.\\n\");\n"
  150. " return -1;\n"
  151. " }\n"
  152. " \n"
  153. " char *arg = lowercase(av[1 + partial_match]);\n"
  154. " for (i =0; i < NumTests; ++i)\n"
  155. " {\n"
  156. " char *test_name = lowercase(cmakeGeneratedFunctionMapEntries[i].name);\n"
  157. " if (partial_match && strstr(test_name, arg) != NULL)\n"
  158. " {\n"
  159. " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 2, av + 2);\n"
  160. " }\n"
  161. " else if (!partial_match && strcmp(test_name, arg) == 0)\n"
  162. " {\n"
  163. " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 1, av + 1);\n"
  164. " }\n"
  165. " delete [] test_name;\n"
  166. " }\n"
  167. " delete [] arg;\n"
  168. " \n"
  169. " // If the test was not found but there is only one test, then\n"
  170. " // run it with the arguments\n"
  171. " if (NumTests == 1)\n"
  172. " {\n"
  173. " return (*cmakeGeneratedFunctionMapEntries[0].func)(ac, av);\n"
  174. " }\n"
  175. " \n"
  176. " // Nothing was run, display the test names\n"
  177. " printf(\"Available tests:\\n\");\n"
  178. " for (i =0; i < NumTests; ++i)\n"
  179. " {\n"
  180. " printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
  181. " }\n"
  182. " printf(\"Failed: %s is an invalid test name.\\n\", av[1]);\n"
  183. " \n"
  184. " return -1;\n"
  185. "}\n";
  186. fout.close();
  187. // Create the source list
  188. cmSourceFile cfile;
  189. cfile.SetIsAnAbstractClass(false);
  190. cfile.SetName(args[1].c_str(),
  191. m_Makefile->GetCurrentOutputDirectory(),
  192. "cxx",
  193. false);
  194. m_Makefile->AddSource(cfile, sourceList);
  195. for(i = testsBegin; i != args.end(); ++i)
  196. {
  197. cmSourceFile cfile;
  198. cfile.SetIsAnAbstractClass(false);
  199. cfile.SetName(i->c_str(),
  200. m_Makefile->GetCurrentDirectory(),
  201. m_Makefile->GetSourceExtensions(),
  202. m_Makefile->GetHeaderExtensions());
  203. m_Makefile->AddSource(cfile, sourceList);
  204. }
  205. return true;
  206. }