TestDriver.cxx.in 3.7 KB

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