Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Templates / TestDriver.cxx.in
blob9e5303004a7f0e98090d6d289a12977bc80b14a4
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
6 @CMAKE_TESTDRIVER_EXTRA_INCLUDES@
9 /* Forward declare test functions. */
10 @CMAKE_FORWARD_DECLARE_TESTS@
12 /* Create map. */
14 typedef int (*MainFuncPointer)(int , char*[]);
15 typedef struct
17 const char* name;
18 MainFuncPointer func;
19 } functionMapEntry;
21 functionMapEntry cmakeGeneratedFunctionMapEntries[] = {
22 @CMAKE_FUNCTION_TABLE_ENTIRES@
23 {0,0}
26 /* Allocate and create a lowercased copy of string
27 (note that it has to be free'd manually) */
29 char* lowercase(const char *string)
31 char *new_string, *p;
33 #ifdef __cplusplus
34 new_string = static_cast<char *>(malloc(sizeof(char) *
35 static_cast<size_t>(strlen(string) + 1)));
36 #else
37 new_string = (char *)(malloc(sizeof(char) * (size_t)(strlen(string) + 1)));
38 #endif
40 if (!new_string)
42 return 0;
44 strcpy(new_string, string);
45 p = new_string;
46 while (*p != 0)
48 #ifdef __cplusplus
49 *p = static_cast<char>(tolower(*p));
50 #else
51 *p = (char)(tolower(*p));
52 #endif
54 ++p;
56 return new_string;
59 int main(int ac, char *av[])
61 int i, NumTests, testNum, partial_match;
62 char *arg, *test_name;
63 int count;
64 int testToRun = -1;
66 @CMAKE_TESTDRIVER_ARGVC_FUNCTION@
68 for(count =0; cmakeGeneratedFunctionMapEntries[count].name != 0; count++)
71 NumTests = count;
72 /* If no test name was given */
73 /* process command line with user function. */
74 if (ac < 2)
76 /* Ask for a test. */
77 printf("Available tests:\n");
78 for (i =0; i < NumTests; ++i)
80 printf("%3d. %s\n", i, cmakeGeneratedFunctionMapEntries[i].name);
82 printf("To run a test, enter the test number: ");
83 fflush(stdout);
84 testNum = 0;
85 if( scanf("%d", &testNum) != 1 )
87 printf("Couldn't parse that input as a number\n");
88 return -1;
90 if (testNum >= NumTests)
92 printf("%3d is an invalid test number.\n", testNum);
93 return -1;
95 testToRun = testNum;
96 ac--;
97 av++;
99 partial_match = 0;
100 arg = 0;
101 /* If partial match is requested. */
102 if(testToRun == -1 && ac > 1)
104 partial_match = (strcmp(av[1], "-R") == 0) ? 1 : 0;
106 if (partial_match && ac < 3)
108 printf("-R needs an additional parameter.\n");
109 return -1;
111 if(testToRun == -1)
113 arg = lowercase(av[1 + partial_match]);
115 for (i =0; i < NumTests && testToRun == -1; ++i)
117 test_name = lowercase(cmakeGeneratedFunctionMapEntries[i].name);
118 if (partial_match && strstr(test_name, arg) != NULL)
120 testToRun = i;
121 ac -=2;
122 av += 2;
124 else if (!partial_match && strcmp(test_name, arg) == 0)
126 testToRun = i;
127 ac--;
128 av++;
130 free(test_name);
132 if(arg)
134 free(arg);
136 if(testToRun != -1)
138 int result;
139 @CMAKE_TESTDRIVER_BEFORE_TESTMAIN@
140 result = (*cmakeGeneratedFunctionMapEntries[testToRun].func)(ac, av);
141 @CMAKE_TESTDRIVER_AFTER_TESTMAIN@
142 return result;
146 /* Nothing was run, display the test names. */
147 printf("Available tests:\n");
148 for (i =0; i < NumTests; ++i)
150 printf("%3d. %s\n", i, cmakeGeneratedFunctionMapEntries[i].name);
152 printf("Failed: %s is an invalid test name.\n", av[1]);
154 return -1;