TestDriver.cxx.in 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. @CMAKE_TESTDRIVER_EXTRA_INCLUDES@
  6. /* Forward declare test functions. */
  7. @CMAKE_FORWARD_DECLARE_TESTS@
  8. /* Create map. */
  9. typedef int (*MainFuncPointer)(int , char*[]);
  10. typedef struct
  11. {
  12. const char* name;
  13. MainFuncPointer func;
  14. } functionMapEntry;
  15. static functionMapEntry cmakeGeneratedFunctionMapEntries[] = {
  16. @CMAKE_FUNCTION_TABLE_ENTIRES@
  17. {0,0}
  18. };
  19. /* Allocate and create a lowercased copy of string
  20. (note that it has to be free'd manually) */
  21. static char* lowercase(const char *string)
  22. {
  23. char *new_string, *p;
  24. #ifdef __cplusplus
  25. new_string = static_cast<char *>(malloc(sizeof(char) *
  26. static_cast<size_t>(strlen(string) + 1)));
  27. #else
  28. new_string = (char *)(malloc(sizeof(char) * (size_t)(strlen(string) + 1)));
  29. #endif
  30. if (!new_string)
  31. {
  32. return 0;
  33. }
  34. strcpy(new_string, string);
  35. p = new_string;
  36. while (*p != 0)
  37. {
  38. #ifdef __cplusplus
  39. *p = static_cast<char>(tolower(*p));
  40. #else
  41. *p = (char)(tolower(*p));
  42. #endif
  43. ++p;
  44. }
  45. return new_string;
  46. }
  47. int main(int ac, char *av[])
  48. {
  49. int i, NumTests, testNum = 0, partial_match;
  50. char *arg, *test_name;
  51. int count;
  52. int testToRun = -1;
  53. @CMAKE_TESTDRIVER_ARGVC_FUNCTION@
  54. for(count =0; cmakeGeneratedFunctionMapEntries[count].name != 0; count++)
  55. {
  56. }
  57. NumTests = count;
  58. /* If no test name was given */
  59. /* process command line with user function. */
  60. if (ac < 2)
  61. {
  62. /* Ask for a test. */
  63. printf("Available tests:\n");
  64. for (i =0; i < NumTests; ++i)
  65. {
  66. printf("%3d. %s\n", i, cmakeGeneratedFunctionMapEntries[i].name);
  67. }
  68. printf("To run a test, enter the test number: ");
  69. fflush(stdout);
  70. if( scanf("%d", &testNum) != 1 )
  71. {
  72. printf("Couldn't parse that input as a number\n");
  73. return -1;
  74. }
  75. if (testNum >= NumTests)
  76. {
  77. printf("%3d is an invalid test number.\n", testNum);
  78. return -1;
  79. }
  80. testToRun = testNum;
  81. ac--;
  82. av++;
  83. }
  84. partial_match = 0;
  85. arg = 0;
  86. /* If partial match is requested. */
  87. if(testToRun == -1 && ac > 1)
  88. {
  89. partial_match = (strcmp(av[1], "-R") == 0) ? 1 : 0;
  90. }
  91. if (partial_match && ac < 3)
  92. {
  93. printf("-R needs an additional parameter.\n");
  94. return -1;
  95. }
  96. if(testToRun == -1)
  97. {
  98. arg = lowercase(av[1 + partial_match]);
  99. }
  100. for (i =0; i < NumTests && testToRun == -1; ++i)
  101. {
  102. test_name = lowercase(cmakeGeneratedFunctionMapEntries[i].name);
  103. if (partial_match && strstr(test_name, arg) != NULL)
  104. {
  105. testToRun = i;
  106. ac -=2;
  107. av += 2;
  108. }
  109. else if (!partial_match && strcmp(test_name, arg) == 0)
  110. {
  111. testToRun = i;
  112. ac--;
  113. av++;
  114. }
  115. free(test_name);
  116. }
  117. if(arg)
  118. {
  119. free(arg);
  120. }
  121. if(testToRun != -1)
  122. {
  123. int result;
  124. @CMAKE_TESTDRIVER_BEFORE_TESTMAIN@
  125. if (testToRun < 0 || testToRun >= NumTests)
  126. {
  127. printf(
  128. "testToRun was modified by TestDriver code to an invalid value: %3d.\n",
  129. testNum);
  130. return -1;
  131. }
  132. result = (*cmakeGeneratedFunctionMapEntries[testToRun].func)(ac, av);
  133. @CMAKE_TESTDRIVER_AFTER_TESTMAIN@
  134. return result;
  135. }
  136. /* Nothing was run, display the test names. */
  137. printf("Available tests:\n");
  138. for (i =0; i < NumTests; ++i)
  139. {
  140. printf("%3d. %s\n", i, cmakeGeneratedFunctionMapEntries[i].name);
  141. }
  142. printf("Failed: %s is an invalid test name.\n", av[1]);
  143. return -1;
  144. }