CMakeServerLibTests.cxx 3.4 KB

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