]> Creatis software - clitk.git/blobdiff - tests/tools/toolTestRunner.cxx
tmpnam_s for windows
[clitk.git] / tests / tools / toolTestRunner.cxx
index 4e13ec09ee31fd67304cd869ea326b3dfdb82ec1..7237c686323869d285771c2bee2977ff31b2b99c 100644 (file)
@@ -23,6 +23,7 @@
 #include <stdlib.h>
 #include <itksys/SystemTools.hxx>
 const int NO_OUTPUT_OPTION=-1;
+const int TEST_EXITED=1;
 int getOutputOptionIndex(int argc, char** argv){
   for(int i=1; i<argc; i++){
       std::string s = argv[i];
@@ -32,18 +33,27 @@ int getOutputOptionIndex(int argc, char** argv){
   }
   return NO_OUTPUT_OPTION;
 }
-char* getTmpFileName(){
-  char buffer [L_tmpnam];
-  char * pointer;
 
-  char* back = tmpnam (buffer);
-  if(back==NULL){
-    std::cout<<"fail to get temporary file name"<<std::endl;
-    exit(1);
+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) err=1;
+  #endif
+  if(err){
+   std::cout<<"couldnot create file. Exiting"<<std::endl;
+   exit(TEST_EXITED);
   }
-  return pointer;
+  return std::string(fileName);
 }
-//todo, check if files exist
+
+
 /**
  * argv
  * [1] executable
@@ -55,25 +65,37 @@ char* getTmpFileName(){
  * [2.x] and [2.x+1] are optional
  */
 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::ostringstream cmd_line;
-  for(int i=1; i<argc; i++){
+  cmd_line<<CLITK_TEST_TOOLS_PATH;
+  for(int i=1; i<argc-1; i++){
+      //we should ensure the file exists, find an -i index or a long file name maybe?
       cmd_line<<argv[i]<<" ";
   }
+
+  //look for the need of generating an output file
   int outputOptionIndex = getOutputOptionIndex(argc, argv);
-  char* outFile;
+  std::string outFile;
   if(NO_OUTPUT_OPTION==outputOptionIndex){
     outFile = getTmpFileName();
-    cmd_line<<" "<<outFile;
+    std::cout<<outFile<<std::endl;
+    cmd_line<<">"<<outFile;
   }else{
-    outFile = argv[outputOptionIndex];
+    //todo test this else branch
+    outFile =  std::string(CLITK_TEST_DATA_PATH);
+    outFile += argv[outputOptionIndex];
   }
-  
-  std::cout<<"running "<<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
-  char* refFile = argv[argc-1];
-  std::cout<<"comapring "<<outFile<<" to "<<refFile<<std::endl;
-  bool fail = (itksys::SystemTools::FilesDiffer(outFile, refFile))?0:1;
-  remove(outFile);
+  int fail = (itksys::SystemTools::FilesDiffer(outFile.c_str(), refFile))?1:0;
+  remove(outFile.c_str());
   return fail;
 }