cmsysTestsCxx.cxx 4.2 KB

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