00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "itkWin32Header.h"
00029 #include <map>
00030 #include <string>
00031 #include <iostream>
00032 #include "itkMultiThreader.h"
00033
00034 typedef int (*MainFuncPointer)(int , char**);
00035 std::map<std::string, MainFuncPointer> StringToTestFunctionMap;
00036
00037 #define REGISTER_TEST(test) \
00038 extern int test(int, char**); \
00039 StringToTestFunctionMap[#test] = test
00040
00041 void RegisterTests();
00042 void PrintAvailableTests()
00043 {
00044 std::cout << "Available tests:\n";
00045 std::map<std::string, MainFuncPointer>::iterator j = StringToTestFunctionMap.begin();
00046 int i = 0;
00047 while(j != StringToTestFunctionMap.end())
00048 {
00049 std::cout << i << ". " << j->first << "\n";
00050 ++i;
00051 ++j;
00052 }
00053 }
00054
00055 int main(int ac, char** av)
00056 {
00057 RegisterTests();
00058 std::string testToRun;
00059 if(ac < 2)
00060 {
00061 PrintAvailableTests();
00062 std::cout << "To run a test, enter the test number: ";
00063 int testNum = 0;
00064 std::cin >> testNum;
00065 std::map<std::string, MainFuncPointer>::iterator j = StringToTestFunctionMap.begin();
00066 int i = 0;
00067 while(j != StringToTestFunctionMap.end() && i < testNum)
00068 {
00069 ++i;
00070 ++j;
00071 }
00072 if(j == StringToTestFunctionMap.end())
00073 {
00074 std::cerr << testNum << " is an invalid test number\n";
00075 return -1;
00076 }
00077 testToRun = j->first;
00078 }
00079 else
00080 {
00081 if (strcmp(av[1], "--with-threads") == 0)
00082 {
00083 int numThreads = atoi(av[2]);
00084 itk::MultiThreader::SetGlobalDefaultNumberOfThreads(numThreads);
00085 av += 2;
00086 ac -= 2;
00087 }
00088 else if (strcmp(av[1], "--without-threads") == 0)
00089 {
00090 itk::MultiThreader::SetGlobalDefaultNumberOfThreads(1);
00091 av += 1;
00092 ac -= 1;
00093 }
00094 testToRun = av[1];
00095 }
00096 std::map<std::string, MainFuncPointer>::iterator j = StringToTestFunctionMap.find(testToRun);
00097 if(j != StringToTestFunctionMap.end())
00098 {
00099 MainFuncPointer f = j->second;
00100 int result = -1;
00101 try
00102 {
00103
00104 result = (*f)(ac-1, av+1);
00105 }
00106 catch(const itk::ExceptionObject& e)
00107 {
00108 std::cerr << "ITK test driver caught an ITK exception:\n";
00109 std::cerr << e.GetFile() << ":" << e.GetLine() << ":\n"
00110 << e.GetDescription() << "\n";
00111 result = -1;
00112 }
00113 catch(const std::exception& e)
00114 {
00115 std::cerr << "ITK test driver caught an exception:\n";
00116 std::cerr << e.what() << "\n";
00117 result = -1;
00118 }
00119 catch(...)
00120 {
00121 std::cerr << "ITK test driver caught an unknown exception!!!\n";
00122 result = -1;
00123 }
00124 return result;
00125 }
00126 PrintAvailableTests();
00127 std::cerr << "Failed: " << testToRun << ": No test registered with name " << testToRun << "\n";
00128 return -1;
00129 }
00130