]> Creatis software - clitk.git/blobdiff - tests/tools/toolTestRunner.cxx
With ITKv5, change VectorResample and VectorCast Image Filter to Resample and Cast...
[clitk.git] / tests / tools / toolTestRunner.cxx
index 768afecc200b7241d63b92ab6d0ab3419a504cf6..212321106ec1a32458977c592be1e19a4274a8a6 100644 (file)
@@ -33,32 +33,103 @@ int getOutputOptionIndex(int argc, char** argv){
   }
   return NO_OUTPUT_OPTION;
 }
+
 std::string getTmpFileName(){
+#ifdef _WIN32
+  char fileName[L_tmpnam_s];
+  errno_t err = tmpnam_s(fileName);
+#else
   char fileName[] = "/tmp/vvTempXXXXXX";
+  int err=0;
   int fd = mkstemp(fileName);
-  if(fd==-1){
+  if(fd==-1) err=1;
+#endif
+  if(err){
    std::cout<<"couldnot create file. Exiting"<<std::endl;
    exit(TEST_EXITED);
   }
   return std::string(fileName);
 }
+
+void assertFalse(int fail, const std::string &message=""){
+  if(fail){
+    std::cout<<message<<std::endl; 
+    exit(1);
+  }
+}
+bool isLineToIgnore(const std::string line){
+       if(std::string::npos == line.find_first_of("ITK_InputFilterName")){
+               return true;
+       }
+       return false;
+}
+bool mhdCmp(const std::string &file, const std::string &refFile){
+       bool sameFiles = true;
+       std::ifstream in(file.c_str());
+       std::ifstream ref(file.c_str());
+       std::string line;
+       std::string refLine;
+       while ( in.good() && ref.good()){
+               getline (in,line);
+               //does the line begins by an attribute to ignore
+               if(isLineToIgnore(line)){
+                       continue;
+               }
+               
+               getline(ref,refLine);
+               while(isLineToIgnore(refLine)){
+                       getline(ref,refLine);
+               }
+               if(line!=refLine){
+                       sameFiles = false;
+                       break;
+               }
+       }
+       in.close();
+       ref.close();
+       //check files same length
+       return sameFiles;
+}
+
+#ifdef _WIN32
+void dosToUnixFile(std::string dosFile, std::string unixedFile){
+               
+       std::ifstream ifile(dosFile.c_str(),std::ios::binary);
+       ifile.seekg(0,std::ios_base::end);
+       long s=ifile.tellg();
+       char *buffer=new char[s];
+       ifile.seekg(0);
+       ifile.read(buffer,s);
+       ifile.close();
+       std::string txt(buffer,s);
+       delete[] buffer;
+       size_t off=0;
+       while ((off=txt.find("\r\n",off))!=std::string::npos)
+               txt.replace(off,sizeof("\r\n")-1,"\n");
+       std::ofstream ofile(unixedFile.c_str());
+       ofile.write(txt.c_str(),txt.size());
+       
+}
+#endif
+
+std::string mhdToRawName(const std::string &mhdName){
+       int found = mhdName.find_last_of(".");
+  return mhdName.substr(0, found)+".raw";
+}
 /**
  * argv
  * [1] executable
  * [2] random options
  * [2.x] -o
- * [2.x+1] outFileName
  * [3] reference file
  * 
- * [2.x] and [2.x+1] are optional
+ * [2.x] is optional. If set a temporary file will be generated. So NO need to pass a random outputFileName
  */
 int main(int argc, char** argv){
   //reference file must exist or we fail
   char* refFile = argv[argc-1];
-  if(!(itksys::SystemTools::FileExists(refFile, true))){
-    std::cout<<"refFile "<<refFile<<" doesn't exist"<<std::endl; 
-    return 1;
-  } 
+       std::string strRefFile = std::string(refFile);
+  assertFalse(!(itksys::SystemTools::FileExists(refFile, true)), "refFile "+strRefFile+" doesn't exist");
   
   std::ostringstream cmd_line;
   cmd_line<<CLITK_TEST_TOOLS_PATH;
@@ -71,19 +142,44 @@ int main(int argc, char** argv){
   int outputOptionIndex = getOutputOptionIndex(argc, argv);
   std::string outFile;
   if(NO_OUTPUT_OPTION==outputOptionIndex){
-    outFile = getTmpFileName();
-    std::cout<<outFile<<std::endl;
-    cmd_line<<">"<<outFile;
+     outFile = getTmpFileName();
+     cmd_line<<">"<<outFile;
   }else{
-    //todo test this else branch
-    outFile =  std::string(CLITK_TEST_DATA_PATH);
-    outFile += argv[outputOptionIndex];
+     outFile = argv[argc-2];
   }
+  std::cout<<cmd_line.str()<<std::endl;;
   //run the command line
   system(cmd_line.str().c_str());
   
-  //files should be equal, so if this is the case return success=0
-  int fail = (itksys::SystemTools::FilesDiffer(outFile.c_str(), refFile))?1:0;
+       assertFalse(!itksys::SystemTools::FileExists(outFile.c_str(), true), "no mhd have been generated");
+  
+       //compare source files
+#ifdef _WIN32
+       std::string unixedOutFile= getTmpFileName();
+       //replace \r\n
+       dosToUnixFile(outFile, unixedOutFile);
+       assertFalse(!mhdCmp(unixedOutFile, refFile), "Generated mhd file != ref File");
+  remove(unixedOutFile.c_str());
+#else
+  assertFalse(!mhdCmp(outFile.c_str(), refFile), "Generated mhd file != ref File");
+#endif
+  std::string refRawFile = mhdToRawName(strRefFile);
+  std::string rawFile = mhdToRawName(outFile);
+  
+  if((itksys::SystemTools::FileExists(refRawFile.c_str(), true))){
+    //compare the raw stuff
+    if((itksys::SystemTools::FileExists(rawFile.c_str(), true))){
+       std::cout<<"Checking raws"<<std::endl;
+       assertFalse(itksys::SystemTools::FilesDiffer(refRawFile.c_str(), rawFile.c_str()), "Raws are different");
+    }
+    //file is not removed if there is a fail
+    remove(rawFile.c_str());
+  }
+  //neither the mhd is
   remove(outFile.c_str());
-  return fail;
+  
+  //success
+  return 0;
 }
+
+