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