cmake/Templates/TestDriver.cxx.in

200 lines
5.1 KiB
C++
Raw Normal View History

2017-04-14 19:02:05 +02:00
#include <ctype.h> /* NOLINT */
#include <stdio.h> /* NOLINT */
#include <stdlib.h> /* NOLINT */
#include <string.h> /* NOLINT */
2021-09-14 00:13:48 +02:00
#include <time.h> /* NOLINT */
2015-04-27 22:25:09 +02:00
#if defined(_MSC_VER)
2017-04-14 19:02:05 +02:00
#pragma warning(disable : 4996) /* deprecation */
2015-04-27 22:25:09 +02:00
#endif
@CMAKE_TESTDRIVER_EXTRA_INCLUDES@
/* Forward declare test functions. */
@CMAKE_FORWARD_DECLARE_TESTS@
2017-04-14 19:02:05 +02:00
#ifdef __cplusplus
2020-02-01 23:06:01 +01:00
# define CM_CAST(TYPE, EXPR) static_cast<TYPE>(EXPR)
# if __cplusplus >= 201103L
# define CM_NULL nullptr
# else
# define CM_NULL NULL
# endif
2023-07-02 19:51:09 +02:00
# define CM_NAMESPACE_BEGIN namespace {
# define CM_NAMESPACE_END }
# define CM_LOCAL
2017-04-14 19:02:05 +02:00
#else
2020-02-01 23:06:01 +01:00
# define CM_CAST(TYPE, EXPR) (TYPE)(EXPR)
# define CM_NULL NULL
2023-07-02 19:51:09 +02:00
# define CM_NAMESPACE_BEGIN
# define CM_NAMESPACE_END
# define CM_LOCAL static
2017-04-14 19:02:05 +02:00
#endif
2023-07-02 19:51:09 +02:00
CM_NAMESPACE_BEGIN
/* Create map. */
2018-08-09 18:06:22 +02:00
typedef int (*MainFuncPointer)(int, char* []); /* NOLINT */
typedef struct /* NOLINT */
{
const char* name;
MainFuncPointer func;
} functionMapEntry;
2023-07-02 19:51:09 +02:00
CM_LOCAL const functionMapEntry cmakeGeneratedFunctionMapEntries[] = {
2021-09-14 00:13:48 +02:00
@CMAKE_FUNCTION_TABLE_ENTRIES@
2020-02-01 23:06:01 +01:00
{ CM_NULL, CM_NULL } /* NOLINT */
};
2023-07-02 19:51:09 +02:00
CM_LOCAL const int NumTests = CM_CAST(int,
2017-07-20 19:35:53 +02:00
sizeof(cmakeGeneratedFunctionMapEntries) / sizeof(functionMapEntry)) - 1;
2017-04-14 19:02:05 +02:00
/* Allocate and create a lowercased copy of string
(note that it has to be free'd manually) */
2023-07-02 19:51:09 +02:00
CM_LOCAL char* lowercase(const char* string)
{
2020-08-30 11:54:41 +02:00
char *new_string;
char *p;
2017-07-20 19:35:53 +02:00
size_t stringSize;
2017-04-14 19:02:05 +02:00
stringSize = CM_CAST(size_t, strlen(string) + 1);
new_string = CM_CAST(char*, malloc(sizeof(char) * stringSize));
2020-02-01 23:06:01 +01:00
if (new_string == CM_NULL) { /* NOLINT */
return CM_NULL; /* NOLINT */
2017-04-14 19:02:05 +02:00
}
2020-02-01 23:06:01 +01:00
strcpy(new_string, string); /* NOLINT */
2017-04-14 19:02:05 +02:00
for (p = new_string; *p != 0; ++p) {
*p = CM_CAST(char, tolower(*p));
}
return new_string;
}
2023-07-02 19:51:09 +02:00
CM_LOCAL int isTestSkipped(const char *name, int n_skipped_tests, char *skipped_tests[]) {
2021-09-14 00:13:48 +02:00
int i;
for (i = 0; i < n_skipped_tests; i++) {
if (strcmp(name, skipped_tests[i]) == 0) {
return 1;
}
}
return 0;
}
2023-07-02 19:51:09 +02:00
CM_NAMESPACE_END
2017-04-14 19:02:05 +02:00
int main(int ac, char* av[])
{
2020-08-30 11:54:41 +02:00
int i;
int testNum = 0;
int partial_match;
2021-09-14 00:13:48 +02:00
int run_all;
2018-04-23 21:13:27 +02:00
char *arg;
int testToRun = -1;
@CMAKE_TESTDRIVER_ARGVC_FUNCTION@
2013-03-16 19:13:01 +02:00
/* If no test name was given */
/* process command line with user function. */
2017-04-14 19:02:05 +02:00
if (ac < 2) {
/* Ask for a test. */
printf("Available tests:\n");
2017-04-14 19:02:05 +02:00
for (i = 0; i < NumTests; ++i) {
printf("%3d. %s\n", i, cmakeGeneratedFunctionMapEntries[i].name);
2017-04-14 19:02:05 +02:00
}
printf("To run a test, enter the test number: ");
fflush(stdout);
2017-04-14 19:02:05 +02:00
if (scanf("%d", &testNum) != 1) {
printf("Couldn't parse that input as a number\n");
return -1;
2017-04-14 19:02:05 +02:00
}
if (testNum >= NumTests) {
printf("%3d is an invalid test number.\n", testNum);
return -1;
2017-04-14 19:02:05 +02:00
}
testToRun = testNum;
ac--;
av++;
2017-04-14 19:02:05 +02:00
}
partial_match = 0;
2021-09-14 00:13:48 +02:00
run_all = 0;
2020-02-01 23:06:01 +01:00
arg = CM_NULL; /* NOLINT */
2021-09-14 00:13:48 +02:00
/* If partial match or running all tests are requested. */
2017-04-14 19:02:05 +02:00
if (testToRun == -1 && ac > 1) {
partial_match = (strcmp(av[1], "-R") == 0) ? 1 : 0;
2021-09-14 00:13:48 +02:00
run_all = (strcmp(av[1], "-A") == 0) ? 1 : 0;
2017-04-14 19:02:05 +02:00
}
if (partial_match != 0 && ac < 3) {
printf("-R needs an additional parameter.\n");
return -1;
2017-04-14 19:02:05 +02:00
}
2021-09-14 00:13:48 +02:00
if (run_all == 1) {
clock_t t;
int status = 0;
const char* status_message = CM_NULL;
printf("TAP version 13\n");
printf("1..%d\n", NumTests);
for (i = 0; i < NumTests; ++i) {
const char *name = cmakeGeneratedFunctionMapEntries[i].name;
if (ac > 2) {
if (isTestSkipped(name, ac - 2, av + 2) == 1) {
printf("ok %d %s # SKIP\n", i + 1, name);
continue;
}
}
t = clock();
status = (*cmakeGeneratedFunctionMapEntries[i].func)(ac, av);
t = clock() - t;
status_message = (status == -1) ? "not ok" : "ok";
{
2021-11-02 14:08:44 +01:00
double time_taken = CM_CAST(double, t) / CLOCKS_PER_SEC;
2021-09-14 00:13:48 +02:00
printf("%s %d %s # %f\n", status_message, i + 1, name, time_taken);
}
}
printf("All tests finished.\n");
return 0;
}
2017-04-14 19:02:05 +02:00
if (testToRun == -1) {
arg = lowercase(av[1 + partial_match]);
2017-04-14 19:02:05 +02:00
}
for (i = 0; i < NumTests && testToRun == -1; ++i) {
2018-04-23 21:13:27 +02:00
char *test_name = lowercase(cmakeGeneratedFunctionMapEntries[i].name);
2020-02-01 23:06:01 +01:00
if (partial_match != 0 && strstr(test_name, arg) != CM_NULL) { /* NOLINT */
testToRun = i;
2017-04-14 19:02:05 +02:00
ac -= 2;
av += 2;
2017-04-14 19:02:05 +02:00
} else if (partial_match == 0 && strcmp(test_name, arg) == 0) {
testToRun = i;
ac--;
av++;
}
2017-04-14 19:02:05 +02:00
free(test_name);
}
free(arg);
if (testToRun != -1) {
int result;
@CMAKE_TESTDRIVER_BEFORE_TESTMAIN@
2017-04-14 19:02:05 +02:00
if (testToRun < 0 || testToRun >= NumTests) {
printf("testToRun was modified by TestDriver code to an invalid value: "
"%3d.\n",
testNum);
2014-08-03 19:52:23 +02:00
return -1;
2017-04-14 19:02:05 +02:00
}
result = (*cmakeGeneratedFunctionMapEntries[testToRun].func)(ac, av);
@CMAKE_TESTDRIVER_AFTER_TESTMAIN@
return result;
2017-04-14 19:02:05 +02:00
}
2013-03-16 19:13:01 +02:00
/* Nothing was run, display the test names. */
printf("Available tests:\n");
2017-04-14 19:02:05 +02:00
for (i = 0; i < NumTests; ++i) {
printf("%3d. %s\n", i, cmakeGeneratedFunctionMapEntries[i].name);
2017-04-14 19:02:05 +02:00
}
printf("Failed: %s is an invalid test name.\n", av[1]);
2013-03-16 19:13:01 +02:00
return -1;
}