]> Creatis software - clitk.git/blob - tests/tools/toolTestRunner.cxx
0eb1f902a418171f53774694c3f7f979f99bc906
[clitk.git] / tests / tools / toolTestRunner.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================*/
18 #include <iostream>
19 #include <sstream>
20 #include <string>
21 #include <fstream>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <itksys/SystemTools.hxx>
25 const int NO_OUTPUT_OPTION=-1;
26 int getOutputOptionIndex(int argc, char** argv){
27   for(int i=1; i<argc; i++){
28       std::string s = argv[i];
29       if(s=="-o"){
30          return i+1;
31       }
32   }
33   return NO_OUTPUT_OPTION;
34 }
35 inline char* getTmpFileName(){
36   char buffer [L_tmpnam];
37   char* back = tmpnam (buffer);
38   if(back==NULL){
39     exit(1);
40   }
41   return back;
42 }
43 /**
44  * argv
45  * [1] executable
46  * [2] random options
47  * [2.x] -o
48  * [2.x+1] outFileName
49  * [3] reference file
50  * 
51  * [2.x] and [2.x+1] are optional
52  */
53 int main(int argc, char** argv){
54   //reference file must exist or we fail
55   char* refFile = argv[argc-1];
56   if(!(itksys::SystemTools::FileExists(refFile, true))){
57     std::cout<<"refFile "<<refFile<<" doesn't exist"<<std::endl; 
58     return 1;
59   } 
60   
61   std::ostringstream cmd_line;
62   cmd_line<<CLITK_TEST_TOOLS_PATH;
63   for(int i=1; i<argc-1; i++){
64   //we should ensure the file exists, find an -i index or a long file name maybe?
65       cmd_line<<argv[i]<<" ";
66   }
67
68   //look for the need of generating an output file
69   int outputOptionIndex = getOutputOptionIndex(argc, argv);
70   char* outFile;
71   if(NO_OUTPUT_OPTION==outputOptionIndex){
72     outFile = getTmpFileName();
73     cmd_line<<" > "<<outFile;
74   }else{
75     //todo test this else branch
76     std::string s = std::string(CLITK_TEST_DATA_PATH);
77     s+=argv[outputOptionIndex];
78     //DO NOT MODIFY outFile
79     outFile = (char*)s.c_str();
80   }
81   
82   //run the command line
83   system(cmd_line.str().c_str());
84   
85   //files should be equal, so if this is the case return success=0
86   int fail = (itksys::SystemTools::FilesDiffer(outFile, refFile))?1:0;
87   remove(outFile);
88   return fail;
89 }