]> Creatis software - clitk.git/blob - tests/tools/toolTestRunner.cxx
89deab5a20614c20219e4a48db8184a0eefa5e4e
[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
37 std::string getTmpFileName(){
38   char fileName[] = "/tmp/vvTempXXXXXX";
39   
40   #ifdef _WIN32
41     errno_t err = tmpfile_s(&fileName, strlen(fileName));
42   #else
43     int err=0;
44     int fd = mkstemp(fileName);
45     if(fd==-1) err=1;
46   #endif
47   if(err){
48    std::cout<<"couldnot create file. Exiting"<<std::endl;
49    exit(TEST_EXITED);
50   }
51   return std::string(fileName);
52 }
53
54
55 /**
56  * argv
57  * [1] executable
58  * [2] random options
59  * [2.x] -o
60  * [2.x+1] outFileName
61  * [3] reference file
62  * 
63  * [2.x] and [2.x+1] are optional
64  */
65 int main(int argc, char** argv){
66   //reference file must exist or we fail
67   char* refFile = argv[argc-1];
68   if(!(itksys::SystemTools::FileExists(refFile, true))){
69     std::cout<<"refFile "<<refFile<<" doesn't exist"<<std::endl; 
70     return 1;
71   } 
72   
73   std::ostringstream cmd_line;
74   cmd_line<<CLITK_TEST_TOOLS_PATH;
75   for(int i=1; i<argc-1; i++){
76       //we should ensure the file exists, find an -i index or a long file name maybe?
77       cmd_line<<argv[i]<<" ";
78   }
79
80   //look for the need of generating an output file
81   int outputOptionIndex = getOutputOptionIndex(argc, argv);
82   std::string outFile;
83   if(NO_OUTPUT_OPTION==outputOptionIndex){
84     outFile = getTmpFileName();
85     std::cout<<outFile<<std::endl;
86     cmd_line<<">"<<outFile;
87   }else{
88     //todo test this else branch
89     outFile =  std::string(CLITK_TEST_DATA_PATH);
90     outFile += argv[outputOptionIndex];
91   }
92   //run the command line
93   system(cmd_line.str().c_str());
94   
95   //files should be equal, so if this is the case return success=0
96   int fail = (itksys::SystemTools::FilesDiffer(outFile.c_str(), refFile))?1:0;
97   remove(outFile.c_str());
98   return fail;
99 }