Medical Imaging Interaction Toolkit  2018.4.99-4c24e3cb
Medical Imaging Interaction Toolkit
MitkNewModuleTestDriver_main.cpp
Go to the documentation of this file.
1 #include <ctype.h> /* NOLINT */
2 #include <stdio.h> /* NOLINT */
3 #include <stdlib.h> /* NOLINT */
4 #include <string.h> /* NOLINT */
5 
6 #if defined(_MSC_VER)
7 #pragma warning(disable : 4996) /* deprecation */
8 #endif
9 
11 
12 
13 /* Forward declare test functions. */
15 int mitkExampleDataStructureTest(int, char*[]);
16 
17 
18 #ifdef __cplusplus
19 #define CM_CAST(TYPE, EXPR) static_cast<TYPE>(EXPR)
20 #else
21 #define CM_CAST(TYPE, EXPR) (TYPE)(EXPR)
22 #endif
23 
24 /* Create map. */
25 
26 typedef int (*MainFuncPointer)(int, char* []); /* NOLINT */
27 typedef struct /* NOLINT */
28 {
29  const char* name;
30  MainFuncPointer func;
31 } functionMapEntry;
32 
33 static functionMapEntry cmakeGeneratedFunctionMapEntries[] = {
34  {
35  "mitkExampleDataStructureReaderWriterTest",
37  },
38  {
39  "mitkExampleDataStructureTest",
41  },
42 
43  { NULL, NULL } /* NOLINT */
44 };
45 
46 static const int NumTests = CM_CAST(int,
47  sizeof(cmakeGeneratedFunctionMapEntries) / sizeof(functionMapEntry)) - 1;
48 
49 /* Allocate and create a lowercased copy of string
50  (note that it has to be free'd manually) */
51 static char* lowercase(const char* string)
52 {
53  char *new_string, *p;
54  size_t stringSize;
55 
56  stringSize = CM_CAST(size_t, strlen(string) + 1);
57  new_string = CM_CAST(char*, malloc(sizeof(char) * stringSize));
58 
59  if (new_string == NULL) { /* NOLINT */
60  return NULL; /* NOLINT */
61  }
62  strcpy(new_string, string);
63  for (p = new_string; *p != 0; ++p) {
64  *p = CM_CAST(char, tolower(*p));
65  }
66  return new_string;
67 }
68 
69 int main(int ac, char* av[])
70 {
71  int i, testNum = 0, partial_match;
72  char *arg;
73  int testToRun = -1;
74 
75 
76 
77  /* If no test name was given */
78  /* process command line with user function. */
79  if (ac < 2) {
80  /* Ask for a test. */
81  printf("Available tests:\n");
82  for (i = 0; i < NumTests; ++i) {
83  printf("%3d. %s\n", i, cmakeGeneratedFunctionMapEntries[i].name);
84  }
85  printf("To run a test, enter the test number: ");
86  fflush(stdout);
87  if (scanf("%d", &testNum) != 1) {
88  printf("Couldn't parse that input as a number\n");
89  return -1;
90  }
91  if (testNum >= NumTests) {
92  printf("%3d is an invalid test number.\n", testNum);
93  return -1;
94  }
95  testToRun = testNum;
96  ac--;
97  av++;
98  }
99  partial_match = 0;
100  arg = NULL; /* NOLINT */
101  /* If partial match is requested. */
102  if (testToRun == -1 && ac > 1) {
103  partial_match = (strcmp(av[1], "-R") == 0) ? 1 : 0;
104  }
105  if (partial_match != 0 && ac < 3) {
106  printf("-R needs an additional parameter.\n");
107  return -1;
108  }
109  if (testToRun == -1) {
110  arg = lowercase(av[1 + partial_match]);
111  }
112  for (i = 0; i < NumTests && testToRun == -1; ++i) {
113  char *test_name = lowercase(cmakeGeneratedFunctionMapEntries[i].name);
114  if (partial_match != 0 && strstr(test_name, arg) != NULL) { /* NOLINT */
115  testToRun = i;
116  ac -= 2;
117  av += 2;
118  } else if (partial_match == 0 && strcmp(test_name, arg) == 0) {
119  testToRun = i;
120  ac--;
121  av++;
122  }
123  free(test_name);
124  }
125  free(arg);
126  if (testToRun != -1) {
127  int result;
128 
129 for (int avIndex = 1; avIndex < ac; ++avIndex) globalCmdLineArgs.push_back(av[avIndex]);
131 ;
132  if (testToRun < 0 || testToRun >= NumTests) {
133  printf("testToRun was modified by TestDriver code to an invalid value: "
134  "%3d.\n",
135  testNum);
136  return -1;
137  }
138  result = (*cmakeGeneratedFunctionMapEntries[testToRun].func)(ac, av);
140  return result;
141  }
142 
143  /* Nothing was run, display the test names. */
144  printf("Available tests:\n");
145  for (i = 0; i < NumTests; ++i) {
146  printf("%3d. %s\n", i, cmakeGeneratedFunctionMapEntries[i].name);
147  }
148  printf("Failed: %s is an invalid test name.\n", av[1]);
149 
150  return -1;
151 }
static void Register()
registers MITK logging backend at mbilog
Definition: mitkLog.cpp:71
int mitkExampleDataStructureTest(int, char *[])
int(* MainFuncPointer)(int, char *[])
#define CM_CAST(TYPE, EXPR)
static char * lowercase(const char *string)
static const int NumTests
int main(int ac, char *av[])
int mitkExampleDataStructureReaderWriterTest(int, char *[])
static functionMapEntry cmakeGeneratedFunctionMapEntries[]
static void Unregister()
Unregisters MITK logging backend at mbilog.
Definition: mitkLog.cpp:79
std::vector< std::string > globalCmdLineArgs