cmsysTestsC.c 3.5 KB

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