1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================*/
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];
34 return NO_OUTPUT_OPTION;
37 std::string getTmpFileName(){
39 char fileName[L_tmpnam_s];
40 errno_t err = tmpnam_s(fileName);
42 char fileName[] = "/tmp/vvTempXXXXXX";
44 int fd = mkstemp(fileName);
48 std::cout<<"couldnot create file. Exiting"<<std::endl;
51 return std::string(fileName);
54 void assertFalse(int fail, const std::string &message=""){
56 std::cout<<message<<std::endl;
62 void dosToUnixFile(std::string dosFile, std::string unixedFile){
64 std::ifstream ifile(dosFile.c_str(),std::ios::binary);
65 ifile.seekg(0,std::ios_base::end);
67 char *buffer=new char[s];
71 std::string txt(buffer,s);
74 while ((off=txt.find("\r\n",off))!=std::string::npos)
75 txt.replace(off,sizeof("\r\n")-1,"\n");
76 std::ofstream ofile(unixedFile.c_str());
77 ofile.write(txt.c_str(),txt.size());
82 std::string mhdToRawName(const std::string &mhdName){
83 int found = mhdName.find_last_of(".");
84 return mhdName.substr(0, found)+".raw";
93 * [2.x] is optional. If set a temporary file will be generated. So NO need to pass a random outputFileName
95 int main(int argc, char** argv){
96 //reference file must exist or we fail
97 char* refFile = argv[argc-1];
98 std::string strRefFile = std::string(refFile);
99 assertFalse(!(itksys::SystemTools::FileExists(refFile, true)), "refFile "+strRefFile+" doesn't exist");
101 std::ostringstream cmd_line;
102 cmd_line<<CLITK_TEST_TOOLS_PATH;
103 for(int i=1; i<argc-1; i++){
104 //we should ensure the file exists, find an -i index or a long file name maybe?
105 cmd_line<<argv[i]<<" ";
108 //look for the need of generating an output file
109 int outputOptionIndex = getOutputOptionIndex(argc, argv);
111 if(NO_OUTPUT_OPTION==outputOptionIndex){
112 outFile = getTmpFileName();
113 cmd_line<<">"<<outFile;
115 outFile = argv[argc-2];
117 std::cout<<cmd_line.str()<<std::endl;;
118 //run the command line
119 system(cmd_line.str().c_str());
122 //compare source files
124 std::string unixedOutFile= getTmpFileName();
126 dosToUnixFile(outFile, unixedOutFile);
127 assertFalse((itksys::SystemTools::FilesDiffer(unixedOutFile.c_str(), refFile)), "Generated mhd file != ref File");
128 remove(unixedOutFile.c_str());
130 assertFalse((itksys::SystemTools::FilesDiffer(outFile.c_str(), refFile)), "Generated mhd file != ref File");
133 std::string refRawFile = mhdToRawName(strRefFile);
134 std::string rawFile = mhdToRawName(outFile);
136 if((itksys::SystemTools::FileExists(refRawFile.c_str(), true))){
137 //compare the raw stuff
138 if((itksys::SystemTools::FileExists(rawFile.c_str(), true))){
139 std::cout<<"Checking raws"<<std::endl;
140 assertFalse(itksys::SystemTools::FilesDiffer(refRawFile.c_str(), rawFile.c_str()), "Raws are different");
142 //file is not removed if there is a fail
143 remove(rawFile.c_str());
146 remove(outFile.c_str());