]> Creatis software - clitk.git/blob - tests/tools/toolTestRunner.cxx
753aa37bcef992c0bccf53ff922ae6c3724aab86
[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 #ifdef _WIN32
39   char fileName[L_tmpnam_s];
40   errno_t err = tmpnam_s(fileName);
41 #else
42   char fileName[] = "/tmp/vvTempXXXXXX";
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 void assertFalse(int fail, const std::string &message=""){
55   if(fail){
56     std::cout<<message<<std::endl; 
57     exit(1);
58   }
59 }
60 bool isLineToIgnore(const std::string line){
61         if(std::string::npos == line.find_first_of("ITK_InputFilterName")){
62                 return true;
63         }
64         return false;
65 }
66 bool mhdCmp(const std::string &file, const std::string &refFile){
67         bool sameFiles = true;
68         std::ifstream in(file.c_str());
69         std::ifstream ref(file.c_str());
70         std::string line;
71         std::string refLine;
72         while ( in.good() && ref.good()){
73                 getline (in,line);
74                 //does the line begins by an attribute to ignore
75                 if(isLineToIgnore(line)){
76                         continue;
77                 }
78                 
79                 getline(ref,refLine);
80                 while(isLineToIgnore(refLine)){
81                         getline(ref,refLine);
82                 }
83                 if(line!=refLine){
84                         sameFiles = false;
85                         break;
86                 }
87         }
88         in.close();
89         ref.close();
90         //check files same length
91         return sameFiles;
92 }
93
94 #ifdef _WIN32
95 void dosToUnixFile(std::string dosFile, std::string unixedFile){
96                 
97         std::ifstream ifile(dosFile.c_str(),std::ios::binary);
98         ifile.seekg(0,std::ios_base::end);
99         long s=ifile.tellg();
100         char *buffer=new char[s];
101         ifile.seekg(0);
102         ifile.read(buffer,s);
103         ifile.close();
104         std::string txt(buffer,s);
105         delete[] buffer;
106         size_t off=0;
107         while ((off=txt.find("\r\n",off))!=std::string::npos)
108                 txt.replace(off,sizeof("\r\n")-1,"\n");
109         std::ofstream ofile(unixedFile.c_str());
110         ofile.write(txt.c_str(),txt.size());
111         
112 }
113 #endif
114
115 std::string mhdToRawName(const std::string &mhdName){
116         int found = mhdName.find_last_of(".");
117   return mhdName.substr(0, found)+".raw";
118 }
119 /**
120  * argv
121  * [1] executable
122  * [2] random options
123  * [2.x] -o
124  * [3] reference file
125  * 
126  * [2.x] is optional. If set a temporary file will be generated. So NO need to pass a random outputFileName
127  */
128 int main(int argc, char** argv){
129   //reference file must exist or we fail
130   char* refFile = argv[argc-1];
131         std::string strRefFile = std::string(refFile);
132   assertFalse(!(itksys::SystemTools::FileExists(refFile, true)), "refFile "+strRefFile+" doesn't exist");
133   
134   std::ostringstream cmd_line;
135   cmd_line<<CLITK_TEST_TOOLS_PATH;
136   for(int i=1; i<argc-1; i++){
137       //we should ensure the file exists, find an -i index or a long file name maybe?
138       cmd_line<<argv[i]<<" ";
139   }
140
141   //look for the need of generating an output file
142   int outputOptionIndex = getOutputOptionIndex(argc, argv);
143   std::string outFile;
144   if(NO_OUTPUT_OPTION==outputOptionIndex){
145      outFile = getTmpFileName();
146      cmd_line<<">"<<outFile;
147   }else{
148      outFile = argv[argc-2];
149   }
150   std::cout<<cmd_line.str()<<std::endl;;
151   //run the command line
152   system(cmd_line.str().c_str());
153   
154   
155         //compare source files
156 #ifdef _WIN32
157         std::string unixedOutFile= getTmpFileName();
158         //replace \r\n
159         dosToUnixFile(outFile, unixedOutFile);
160         assertFalse(!mhdCmp(unixedOutFile, refFile), "Generated mhd file != ref File");
161   remove(unixedOutFile.c_str());
162 #else
163   assertFalse(!mhdCmp(outFile.c_str(), refFile), "Generated mhd file != ref File");
164 #endif
165   
166   std::string refRawFile = mhdToRawName(strRefFile);
167   std::string rawFile = mhdToRawName(outFile);
168   
169   if((itksys::SystemTools::FileExists(refRawFile.c_str(), true))){
170     //compare the raw stuff
171     if((itksys::SystemTools::FileExists(rawFile.c_str(), true))){
172        std::cout<<"Checking raws"<<std::endl;
173        assertFalse(itksys::SystemTools::FilesDiffer(refRawFile.c_str(), rawFile.c_str()), "Raws are different");
174     }
175     //file is not removed if there is a fail
176     remove(rawFile.c_str());
177   }
178   //neither the mhd is
179   remove(outFile.c_str());
180   
181   //success
182   return 0;
183 }
184
185