TestDriver.cxx.in 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include <ctype.h> /* NOLINT */
  2. #include <stdio.h> /* NOLINT */
  3. #include <stdlib.h> /* NOLINT */
  4. #include <string.h> /* NOLINT */
  5. #include <time.h> /* NOLINT */
  6. #if defined(_MSC_VER)
  7. #pragma warning(disable : 4996) /* deprecation */
  8. #endif
  9. @CMAKE_TESTDRIVER_EXTRA_INCLUDES@
  10. /* Forward declare test functions. */
  11. @CMAKE_FORWARD_DECLARE_TESTS@
  12. #ifdef __cplusplus
  13. # define CM_CAST(TYPE, EXPR) static_cast<TYPE>(EXPR)
  14. # if __cplusplus >= 201103L
  15. # define CM_NULL nullptr
  16. # else
  17. # define CM_NULL NULL
  18. # endif
  19. # define CM_NAMESPACE_BEGIN namespace {
  20. # define CM_NAMESPACE_END }
  21. # define CM_LOCAL
  22. #else
  23. # define CM_CAST(TYPE, EXPR) (TYPE)(EXPR)
  24. # define CM_NULL NULL
  25. # define CM_NAMESPACE_BEGIN
  26. # define CM_NAMESPACE_END
  27. # define CM_LOCAL static
  28. #endif
  29. CM_NAMESPACE_BEGIN
  30. /* Create map. */
  31. typedef int (*MainFuncPointer)(int, char* []); /* NOLINT */
  32. typedef struct /* NOLINT */
  33. {
  34. const char* name;
  35. MainFuncPointer func;
  36. } functionMapEntry;
  37. CM_LOCAL const functionMapEntry cmakeGeneratedFunctionMapEntries[] = {
  38. @CMAKE_FUNCTION_TABLE_ENTRIES@
  39. { CM_NULL, CM_NULL } /* NOLINT */
  40. };
  41. CM_LOCAL const int NumTests = CM_CAST(int,
  42. sizeof(cmakeGeneratedFunctionMapEntries) / sizeof(functionMapEntry)) - 1;
  43. /* Allocate and create a lowercased copy of string
  44. (note that it has to be free'd manually) */
  45. CM_LOCAL char* lowercase(const char* string)
  46. {
  47. char *new_string;
  48. char *p;
  49. size_t stringSize;
  50. stringSize = CM_CAST(size_t, strlen(string) + 1);
  51. new_string = CM_CAST(char*, malloc(sizeof(char) * stringSize));
  52. if (new_string == CM_NULL) { /* NOLINT */
  53. return CM_NULL; /* NOLINT */
  54. }
  55. strcpy(new_string, string); /* NOLINT */
  56. for (p = new_string; *p != 0; ++p) {
  57. *p = CM_CAST(char, tolower(*p));
  58. }
  59. return new_string;
  60. }
  61. CM_LOCAL int isTestSkipped(const char *name, int n_skipped_tests, char *skipped_tests[]) {
  62. int i;
  63. for (i = 0; i < n_skipped_tests; i++) {
  64. if (strcmp(name, skipped_tests[i]) == 0) {
  65. return 1;
  66. }
  67. }
  68. return 0;
  69. }
  70. CM_NAMESPACE_END
  71. int main(int ac, char* av[])
  72. {
  73. int i;
  74. int testNum = 0;
  75. int partial_match;
  76. int run_all;
  77. char *arg;
  78. int testToRun = -1;
  79. @CMAKE_TESTDRIVER_ARGVC_FUNCTION@
  80. /* If no test name was given */
  81. /* process command line with user function. */
  82. if (ac < 2) {
  83. /* Ask for a test. */
  84. printf("Available tests:\n");
  85. for (i = 0; i < NumTests; ++i) {
  86. printf("%3d. %s\n", i, cmakeGeneratedFunctionMapEntries[i].name);
  87. }
  88. printf("To run a test, enter the test number: ");
  89. fflush(stdout);
  90. if (scanf("%d", &testNum) != 1) {
  91. printf("Couldn't parse that input as a number\n");
  92. return -1;
  93. }
  94. if (testNum >= NumTests) {
  95. printf("%3d is an invalid test number.\n", testNum);
  96. return -1;
  97. }
  98. testToRun = testNum;
  99. ac--;
  100. av++;
  101. }
  102. partial_match = 0;
  103. run_all = 0;
  104. arg = CM_NULL; /* NOLINT */
  105. /* If partial match or running all tests are requested. */
  106. if (testToRun == -1 && ac > 1) {
  107. partial_match = (strcmp(av[1], "-R") == 0) ? 1 : 0;
  108. run_all = (strcmp(av[1], "-A") == 0) ? 1 : 0;
  109. }
  110. if (partial_match != 0 && ac < 3) {
  111. printf("-R needs an additional parameter.\n");
  112. return -1;
  113. }
  114. if (run_all == 1) {
  115. clock_t t;
  116. int status = 0;
  117. const char* status_message = CM_NULL;
  118. printf("TAP version 13\n");
  119. printf("1..%d\n", NumTests);
  120. for (i = 0; i < NumTests; ++i) {
  121. const char *name = cmakeGeneratedFunctionMapEntries[i].name;
  122. if (ac > 2) {
  123. if (isTestSkipped(name, ac - 2, av + 2) == 1) {
  124. printf("ok %d %s # SKIP\n", i + 1, name);
  125. continue;
  126. }
  127. }
  128. t = clock();
  129. status = (*cmakeGeneratedFunctionMapEntries[i].func)(ac, av);
  130. t = clock() - t;
  131. status_message = (status == -1) ? "not ok" : "ok";
  132. {
  133. double time_taken = CM_CAST(double, t) / CLOCKS_PER_SEC;
  134. printf("%s %d %s # %f\n", status_message, i + 1, name, time_taken);
  135. }
  136. }
  137. printf("All tests finished.\n");
  138. return 0;
  139. }
  140. if (testToRun == -1) {
  141. arg = lowercase(av[1 + partial_match]);
  142. }
  143. for (i = 0; i < NumTests && testToRun == -1; ++i) {
  144. char *test_name = lowercase(cmakeGeneratedFunctionMapEntries[i].name);
  145. if (partial_match != 0 && strstr(test_name, arg) != CM_NULL) { /* NOLINT */
  146. testToRun = i;
  147. ac -= 2;
  148. av += 2;
  149. } else if (partial_match == 0 && strcmp(test_name, arg) == 0) {
  150. testToRun = i;
  151. ac--;
  152. av++;
  153. }
  154. free(test_name);
  155. }
  156. free(arg);
  157. if (testToRun != -1) {
  158. int result;
  159. @CMAKE_TESTDRIVER_BEFORE_TESTMAIN@
  160. if (testToRun < 0 || testToRun >= NumTests) {
  161. printf("testToRun was modified by TestDriver code to an invalid value: "
  162. "%3d.\n",
  163. testNum);
  164. return -1;
  165. }
  166. result = (*cmakeGeneratedFunctionMapEntries[testToRun].func)(ac, av);
  167. @CMAKE_TESTDRIVER_AFTER_TESTMAIN@
  168. return result;
  169. }
  170. /* Nothing was run, display the test names. */
  171. printf("Available tests:\n");
  172. for (i = 0; i < NumTests; ++i) {
  173. printf("%3d. %s\n", i, cmakeGeneratedFunctionMapEntries[i].name);
  174. }
  175. printf("Failed: %s is an invalid test name.\n", av[1]);
  176. return -1;
  177. }