TestDriver.cxx.in 2.9 KB

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