TestDriver.cxx.in 3.5 KB

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