TestDriver.cxx.in 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_ENTIRES@
  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, *p;
  40. size_t stringSize;
  41. stringSize = CM_CAST(size_t, strlen(string) + 1);
  42. new_string = CM_CAST(char*, malloc(sizeof(char) * stringSize));
  43. if (new_string == CM_NULL) { /* NOLINT */
  44. return CM_NULL; /* NOLINT */
  45. }
  46. strcpy(new_string, string); /* NOLINT */
  47. for (p = new_string; *p != 0; ++p) {
  48. *p = CM_CAST(char, tolower(*p));
  49. }
  50. return new_string;
  51. }
  52. int main(int ac, char* av[])
  53. {
  54. int i, testNum = 0, partial_match;
  55. char *arg;
  56. int testToRun = -1;
  57. @CMAKE_TESTDRIVER_ARGVC_FUNCTION@
  58. /* If no test name was given */
  59. /* process command line with user function. */
  60. if (ac < 2) {
  61. /* Ask for a test. */
  62. printf("Available tests:\n");
  63. for (i = 0; i < NumTests; ++i) {
  64. printf("%3d. %s\n", i, cmakeGeneratedFunctionMapEntries[i].name);
  65. }
  66. printf("To run a test, enter the test number: ");
  67. fflush(stdout);
  68. if (scanf("%d", &testNum) != 1) {
  69. printf("Couldn't parse that input as a number\n");
  70. return -1;
  71. }
  72. if (testNum >= NumTests) {
  73. printf("%3d is an invalid test number.\n", testNum);
  74. return -1;
  75. }
  76. testToRun = testNum;
  77. ac--;
  78. av++;
  79. }
  80. partial_match = 0;
  81. arg = CM_NULL; /* NOLINT */
  82. /* If partial match is requested. */
  83. if (testToRun == -1 && ac > 1) {
  84. partial_match = (strcmp(av[1], "-R") == 0) ? 1 : 0;
  85. }
  86. if (partial_match != 0 && ac < 3) {
  87. printf("-R needs an additional parameter.\n");
  88. return -1;
  89. }
  90. if (testToRun == -1) {
  91. arg = lowercase(av[1 + partial_match]);
  92. }
  93. for (i = 0; i < NumTests && testToRun == -1; ++i) {
  94. char *test_name = lowercase(cmakeGeneratedFunctionMapEntries[i].name);
  95. if (partial_match != 0 && strstr(test_name, arg) != CM_NULL) { /* NOLINT */
  96. testToRun = i;
  97. ac -= 2;
  98. av += 2;
  99. } else if (partial_match == 0 && strcmp(test_name, arg) == 0) {
  100. testToRun = i;
  101. ac--;
  102. av++;
  103. }
  104. free(test_name);
  105. }
  106. free(arg);
  107. if (testToRun != -1) {
  108. int result;
  109. @CMAKE_TESTDRIVER_BEFORE_TESTMAIN@
  110. if (testToRun < 0 || testToRun >= NumTests) {
  111. printf("testToRun was modified by TestDriver code to an invalid value: "
  112. "%3d.\n",
  113. testNum);
  114. return -1;
  115. }
  116. result = (*cmakeGeneratedFunctionMapEntries[testToRun].func)(ac, av);
  117. @CMAKE_TESTDRIVER_AFTER_TESTMAIN@
  118. return result;
  119. }
  120. /* Nothing was run, display the test names. */
  121. printf("Available tests:\n");
  122. for (i = 0; i < NumTests; ++i) {
  123. printf("%3d. %s\n", i, cmakeGeneratedFunctionMapEntries[i].name);
  124. }
  125. printf("Failed: %s is an invalid test name.\n", av[1]);
  126. return -1;
  127. }