CMakeLibTests.cxx 4.0 KB

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