Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkTestMain.h

Go to the documentation of this file.
00001 
00002 /*=========================================================================
00003 
00004   Program:   Insight Segmentation & Registration Toolkit
00005   Module:    $RCSfile: itkTestMain.h,v $
00006   Language:  C++
00007   Date:      $Date: 2002/10/11 22:05:24 $
00008   Version:   $Revision: 1.5 $
00009 
00010   Copyright (c) 2002 Insight Consortium. All rights reserved.
00011   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00012 
00013   Portions of this code are covered under the VTK copyright.
00014   See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.
00015 
00016      This software is distributed WITHOUT ANY WARRANTY; without even 
00017      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00018      PURPOSE.  See the above copyright notices for more information.
00019 
00020 =========================================================================*/
00021 
00022 
00023 // This file is used to create TestDriver executables
00024 // These executables are able to register a function pointer to a string name
00025 // in a lookup table.   By including this file, it creates a main function
00026 // that calls RegisterTests() then looks up the function pointer for the test
00027 // specified on the command line.
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       // Invoke the test's "main" function.
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 

Generated at Wed Mar 12 01:13:11 2003 for ITK by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2000