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