qt-build-tools/6.5.1/_tools/cmake/share/cmake-3.17/Templates/TestDriver.cxx.in

144 lines
3.7 KiB
C++
Raw Normal View History

2023-11-01 06:11:15 +08:00
#include <ctype.h> /* NOLINT */
#include <stdio.h> /* NOLINT */
#include <stdlib.h> /* NOLINT */
#include <string.h> /* NOLINT */
#if defined(_MSC_VER)
#pragma warning(disable : 4996) /* deprecation */
#endif
@CMAKE_TESTDRIVER_EXTRA_INCLUDES@
/* Forward declare test functions. */
@CMAKE_FORWARD_DECLARE_TESTS@
#ifdef __cplusplus
2023-11-01 06:20:22 +08:00
# define CM_CAST(TYPE, EXPR) static_cast<TYPE>(EXPR)
# if __cplusplus >= 201103L
# define CM_NULL nullptr
# else
# define CM_NULL NULL
# endif
2023-11-01 06:11:15 +08:00
#else
2023-11-01 06:20:22 +08:00
# define CM_CAST(TYPE, EXPR) (TYPE)(EXPR)
# define CM_NULL NULL
2023-11-01 06:11:15 +08:00
#endif
/* Create map. */
typedef int (*MainFuncPointer)(int, char* []); /* NOLINT */
typedef struct /* NOLINT */
{
const char* name;
MainFuncPointer func;
} functionMapEntry;
static functionMapEntry cmakeGeneratedFunctionMapEntries[] = {
@CMAKE_FUNCTION_TABLE_ENTIRES@
2023-11-01 06:20:22 +08:00
{ CM_NULL, CM_NULL } /* NOLINT */
2023-11-01 06:11:15 +08:00
};
static const int NumTests = CM_CAST(int,
sizeof(cmakeGeneratedFunctionMapEntries) / sizeof(functionMapEntry)) - 1;
/* Allocate and create a lowercased copy of string
(note that it has to be free'd manually) */
static char* lowercase(const char* string)
{
char *new_string, *p;
size_t stringSize;
stringSize = CM_CAST(size_t, strlen(string) + 1);
new_string = CM_CAST(char*, malloc(sizeof(char) * stringSize));
2023-11-01 06:20:22 +08:00
if (new_string == CM_NULL) { /* NOLINT */
return CM_NULL; /* NOLINT */
2023-11-01 06:11:15 +08:00
}
2023-11-01 06:20:22 +08:00
strcpy(new_string, string); /* NOLINT */
2023-11-01 06:11:15 +08:00
for (p = new_string; *p != 0; ++p) {
*p = CM_CAST(char, tolower(*p));
}
return new_string;
}
int main(int ac, char* av[])
{
int i, testNum = 0, partial_match;
char *arg;
int testToRun = -1;
@CMAKE_TESTDRIVER_ARGVC_FUNCTION@
/* If no test name was given */
/* process command line with user function. */
if (ac < 2) {
/* Ask for a test. */
printf("Available tests:\n");
for (i = 0; i < NumTests; ++i) {
printf("%3d. %s\n", i, cmakeGeneratedFunctionMapEntries[i].name);
}
printf("To run a test, enter the test number: ");
fflush(stdout);
if (scanf("%d", &testNum) != 1) {
printf("Couldn't parse that input as a number\n");
return -1;
}
if (testNum >= NumTests) {
printf("%3d is an invalid test number.\n", testNum);
return -1;
}
testToRun = testNum;
ac--;
av++;
}
partial_match = 0;
2023-11-01 06:20:22 +08:00
arg = CM_NULL; /* NOLINT */
2023-11-01 06:11:15 +08:00
/* If partial match is requested. */
if (testToRun == -1 && ac > 1) {
partial_match = (strcmp(av[1], "-R") == 0) ? 1 : 0;
}
if (partial_match != 0 && ac < 3) {
printf("-R needs an additional parameter.\n");
return -1;
}
if (testToRun == -1) {
arg = lowercase(av[1 + partial_match]);
}
for (i = 0; i < NumTests && testToRun == -1; ++i) {
char *test_name = lowercase(cmakeGeneratedFunctionMapEntries[i].name);
2023-11-01 06:20:22 +08:00
if (partial_match != 0 && strstr(test_name, arg) != CM_NULL) { /* NOLINT */
2023-11-01 06:11:15 +08:00
testToRun = i;
ac -= 2;
av += 2;
} else if (partial_match == 0 && strcmp(test_name, arg) == 0) {
testToRun = i;
ac--;
av++;
}
free(test_name);
}
free(arg);
if (testToRun != -1) {
int result;
@CMAKE_TESTDRIVER_BEFORE_TESTMAIN@
if (testToRun < 0 || testToRun >= NumTests) {
printf("testToRun was modified by TestDriver code to an invalid value: "
"%3d.\n",
testNum);
return -1;
}
result = (*cmakeGeneratedFunctionMapEntries[testToRun].func)(ac, av);
@CMAKE_TESTDRIVER_AFTER_TESTMAIN@
return result;
}
/* Nothing was run, display the test names. */
printf("Available tests:\n");
for (i = 0; i < NumTests; ++i) {
printf("%3d. %s\n", i, cmakeGeneratedFunctionMapEntries[i].name);
}
printf("Failed: %s is an invalid test name.\n", av[1]);
return -1;
}