TestDriver.cxx.in 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. 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. 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, 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. testNum = 0;
  71. if( scanf("%d", &testNum) != 1 )
  72. {
  73. printf("Couldn't parse that input as a number\n");
  74. return -1;
  75. }
  76. if (testNum >= NumTests)
  77. {
  78. printf("%3d is an invalid test number.\n", testNum);
  79. return -1;
  80. }
  81. testToRun = testNum;
  82. ac--;
  83. av++;
  84. }
  85. partial_match = 0;
  86. arg = 0;
  87. /* If partial match is requested. */
  88. if(testToRun == -1 && ac > 1)
  89. {
  90. partial_match = (strcmp(av[1], "-R") == 0) ? 1 : 0;
  91. }
  92. if (partial_match && ac < 3)
  93. {
  94. printf("-R needs an additional parameter.\n");
  95. return -1;
  96. }
  97. if(testToRun == -1)
  98. {
  99. arg = lowercase(av[1 + partial_match]);
  100. }
  101. for (i =0; i < NumTests && testToRun == -1; ++i)
  102. {
  103. test_name = lowercase(cmakeGeneratedFunctionMapEntries[i].name);
  104. if (partial_match && strstr(test_name, arg) != NULL)
  105. {
  106. testToRun = i;
  107. ac -=2;
  108. av += 2;
  109. }
  110. else if (!partial_match && strcmp(test_name, arg) == 0)
  111. {
  112. testToRun = i;
  113. ac--;
  114. av++;
  115. }
  116. free(test_name);
  117. }
  118. if(arg)
  119. {
  120. free(arg);
  121. }
  122. if(testToRun != -1)
  123. {
  124. int result;
  125. @CMAKE_TESTDRIVER_BEFORE_TESTMAIN@
  126. result = (*cmakeGeneratedFunctionMapEntries[testToRun].func)(ac, av);
  127. @CMAKE_TESTDRIVER_AFTER_TESTMAIN@
  128. return result;
  129. }
  130. /* Nothing was run, display the test names. */
  131. printf("Available tests:\n");
  132. for (i =0; i < NumTests; ++i)
  133. {
  134. printf("%3d. %s\n", i, cmakeGeneratedFunctionMapEntries[i].name);
  135. }
  136. printf("Failed: %s is an invalid test name.\n", av[1]);
  137. return -1;
  138. }