]> Creatis software - clitk.git/blob - tests/tools/toolTestRunner.cxx
768afecc200b7241d63b92ab6d0ab3419a504cf6
[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 const int TEST_EXITED=1;
27 int getOutputOptionIndex(int argc, char** argv){
28   for(int i=1; i<argc; i++){
29       std::string s = argv[i];
30       if(s=="-o"){
31          return i+1;
32       }
33   }
34   return NO_OUTPUT_OPTION;
35 }
36 std::string getTmpFileName(){
37   char fileName[] = "/tmp/vvTempXXXXXX";
38   int fd = mkstemp(fileName);
39   if(fd==-1){
40    std::cout<<"couldnot create file. Exiting"<<std::endl;
41    exit(TEST_EXITED);
42   }
43   return std::string(fileName);
44 }
45 /**
46  * argv
47  * [1] executable
48  * [2] random options
49  * [2.x] -o
50  * [2.x+1] outFileName
51  * [3] reference file
52  * 
53  * [2.x] and [2.x+1] are optional
54  */
55 int main(int argc, char** argv){
56   //reference file must exist or we fail
57   char* refFile = argv[argc-1];
58   if(!(itksys::SystemTools::FileExists(refFile, true))){
59     std::cout<<"refFile "<<refFile<<" doesn't exist"<<std::endl; 
60     return 1;
61   } 
62   
63   std::ostringstream cmd_line;
64   cmd_line<<CLITK_TEST_TOOLS_PATH;
65   for(int i=1; i<argc-1; i++){
66       //we should ensure the file exists, find an -i index or a long file name maybe?
67       cmd_line<<argv[i]<<" ";
68   }
69
70   //look for the need of generating an output file
71   int outputOptionIndex = getOutputOptionIndex(argc, argv);
72   std::string outFile;
73   if(NO_OUTPUT_OPTION==outputOptionIndex){
74     outFile = getTmpFileName();
75     std::cout<<outFile<<std::endl;
76     cmd_line<<">"<<outFile;
77   }else{
78     //todo test this else branch
79     outFile =  std::string(CLITK_TEST_DATA_PATH);
80     outFile += argv[outputOptionIndex];
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.c_str(), refFile))?1:0;
87   remove(outFile.c_str());
88   return fail;
89 }