ENH: update for CMake 2.6
[cmake.git] / Templates / TestDriver.cxx.in
blob41fccaeb1691c9d3ec7080a8a63022ffe936fc2e
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;
32 new_string = (char *)malloc(sizeof(char) * (size_t)(strlen(string) + 1));
33 if (!new_string)
35 return 0;
37 strcpy(new_string, string);
38 p = new_string;
39 while (*p != 0)
41 *p = (char)tolower(*p);
42 ++p;
44 return new_string;
47 int main(int ac, char *av[])
49 int i, NumTests, testNum, partial_match;
50 char *arg, *test_name;
51 int count;
52 int testToRun = -1;
54 @CMAKE_TESTDRIVER_ARGVC_FUNCTION@
56 for(count =0; cmakeGeneratedFunctionMapEntries[count].name != 0; count++)
59 NumTests = count;
60 /* If no test name was given */
61 /* process command line with user function. */
62 if (ac < 2)
64 /* Ask for a test. */
65 printf("Available tests:\n");
66 for (i =0; i < NumTests; ++i)
68 printf("%3d. %s\n", i, cmakeGeneratedFunctionMapEntries[i].name);
70 printf("To run a test, enter the test number: ");
71 fflush(stdout);
72 testNum = 0;
73 scanf("%d", &testNum);
74 if (testNum >= NumTests)
76 printf("%3d is an invalid test number.\n", testNum);
77 return -1;
79 testToRun = testNum;
80 ac--;
81 av++;
83 partial_match = 0;
84 arg = 0;
85 /* If partial match is requested. */
86 if(testToRun == -1 && ac > 1)
88 partial_match = (strcmp(av[1], "-R") == 0) ? 1 : 0;
90 if (partial_match && ac < 3)
92 printf("-R needs an additional parameter.\n");
93 return -1;
95 if(testToRun == -1)
97 arg = lowercase(av[1 + partial_match]);
99 for (i =0; i < NumTests && testToRun == -1; ++i)
101 test_name = lowercase(cmakeGeneratedFunctionMapEntries[i].name);
102 if (partial_match && strstr(test_name, arg) != NULL)
104 testToRun = i;
105 ac -=2;
106 av += 2;
108 else if (!partial_match && strcmp(test_name, arg) == 0)
110 testToRun = i;
111 ac--;
112 av++;
114 free(test_name);
116 if(arg)
118 free(arg);
120 if(testToRun != -1)
122 int result;
123 @CMAKE_TESTDRIVER_BEFORE_TESTMAIN@
124 result = (*cmakeGeneratedFunctionMapEntries[testToRun].func)(ac, av);
125 @CMAKE_TESTDRIVER_AFTER_TESTMAIN@
126 return result;
130 /* Nothing was run, display the test names. */
131 printf("Available tests:\n");
132 for (i =0; i < NumTests; ++i)
134 printf("%3d. %s\n", i, cmakeGeneratedFunctionMapEntries[i].name);
136 printf("Failed: %s is an invalid test name.\n", av[1]);
138 return -1;