From 2b7567381a552e9eb17c56dfd0baa8b4636cefab Mon Sep 17 00:00:00 2001
From: Romain Garrigues <romain.garrigues@c-s.fr>
Date: Tue, 25 Jul 2006 09:43:28 +0000
Subject: [PATCH] =?UTF-8?q?Introduction=20d'une=20tol=C3=A9rance=20pour=20?=
 =?UTF-8?q?la=20comparaison=20des=20valeurs=20num=C3=A9riques=20avec=20--c?=
 =?UTF-8?q?ompare-ascii?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Code/Common/otbCommandLineArgumentParser.cxx  |  12 ++
 Code/Common/otbCommandLineArgumentParser.h    |   6 +-
 Code/Common/otbTestMain.h                     | 130 ++++++++++++++++--
 .../otbTestCommandLineArgumentParser.cxx      |   6 +-
 .../otbTestCommandLineArgumentParserList.cxx  |   8 +-
 ...TestCommandLineArgumentParserWithError.cxx |   6 +-
 Testing/Code/FeatureExtraction/CMakeLists.txt |   7 +-
 Testing/Code/IO/CMakeLists.txt                |   4 +-
 8 files changed, 151 insertions(+), 28 deletions(-)

diff --git a/Code/Common/otbCommandLineArgumentParser.cxx b/Code/Common/otbCommandLineArgumentParser.cxx
index 043b491b9e..ad3d5a034b 100755
--- a/Code/Common/otbCommandLineArgumentParser.cxx
+++ b/Code/Common/otbCommandLineArgumentParser.cxx
@@ -45,7 +45,19 @@ CommandLineArgumentParseResult
 }
 
 
+unsigned int
+CommandLineArgumentParseResult
+::GetUIntParameter(const char *option, unsigned int number) const
+{
+	return GetParameter<unsigned int>(option,number);
+}
 
+double
+CommandLineArgumentParseResult
+::GetDoubleParameter(const char *option, unsigned int number) const
+{
+	return GetParameter<double>(option,number);
+}
 
 std::string 
 CommandLineArgumentParseResult
diff --git a/Code/Common/otbCommandLineArgumentParser.h b/Code/Common/otbCommandLineArgumentParser.h
index cdcfeb8c2b..62446cb97b 100755
--- a/Code/Common/otbCommandLineArgumentParser.h
+++ b/Code/Common/otbCommandLineArgumentParser.h
@@ -62,8 +62,8 @@ public:
 
   void PrintSelf(std::ostream& os/*, itk::Indent indent*/) const;
 
-  template< typename TypeValeur >
-  TypeValeur GetParameter(const char *option, unsigned int number=0) const;
+  unsigned int GetUIntParameter(const char *option, unsigned int number=0) const;
+  double GetDoubleParameter(const char *option, unsigned int number=0) const;
 
   std::string GetStringParameter(const char *option, unsigned int number=0) const;
 
@@ -73,6 +73,8 @@ protected:
 
 private:
 
+  template< typename TypeValeur >
+  TypeValeur GetParameter(const char *option, unsigned int number=0) const;
 
   typedef std::vector< std::string > ParameterArrayType;
   typedef std::map< std::string, ParameterArrayType > OptionMapType;
diff --git a/Code/Common/otbTestMain.h b/Code/Common/otbTestMain.h
index 8f9a41b2e1..9171b09c1d 100644
--- a/Code/Common/otbTestMain.h
+++ b/Code/Common/otbTestMain.h
@@ -305,7 +305,7 @@ bool isNumeric(std::string str)
 			
 		i++;
 	}
-	if ((str.size()==0)||(nbOfPoints > 1)||(nbOfNumbers==0))
+	if ((str.size()==0)||(nbOfPoints > 1)/*||(nbOfNumbers==0)*/)
 		result = false ;
 
 	return result;
@@ -315,10 +315,17 @@ int RegressionTestAsciiFile(const char * testAsciiFileName, const char * baselin
 {
 	std::ifstream fluxfiletest(testAsciiFileName);
 	std::ifstream fluxfileref(baselineAsciiFileName);
+	enum TypeEtat { ETAT_NUM, ETAT_CHAR } ;
+
+	std::string diffAsciiFileName(testAsciiFileName);
+	diffAsciiFileName += ".diff.txt" ;
+	std::ofstream fluxfilediff(diffAsciiFileName.c_str());
+	
 	std::string strfiletest;
 	std::string strfileref;
 
 	int nbdiff(0);
+	int numLine(1);
     	if (!fluxfiletest)
     	{
     		std::cerr << "Impossible to open the test ASCII file <"<<testAsciiFileName<<">.\n";
@@ -330,6 +337,8 @@ int RegressionTestAsciiFile(const char * testAsciiFileName, const char * baselin
     		return 1000;
     	}
 
+	TypeEtat etatPrec, etatCour ;
+
 	while ( std::getline(fluxfileref,strfileref)!=0  )
 	{
 		otb::StringStream buffstreamRef, buffstreamTest ;
@@ -342,26 +351,121 @@ int RegressionTestAsciiFile(const char * testAsciiFileName, const char * baselin
 		while (buffstreamRef.peek() != EOF)
 		{
 			std::string strRef;
-			std::string strTest;			
+			std::string strTest;
+			
+			std::string strNumRef;
+			std::string strCharRef;
+			std::string strNumTest;
+			std::string strCharTest;
 			
 			buffstreamRef >> strRef ;
-			buffstreamTest >> strTest ;
+			buffstreamTest >> strTest ;
+			
+			bool chgt= false;
+			std::string charTmpRef;
+			std::string charTmpTest;
+	
+			unsigned int i=0;
+
+			while (i < strRef.size())
+			{
+				charTmpRef=strRef[i];
+				charTmpTest=strTest[i];
+				
+				if (isNumeric(charTmpRef))
+					etatCour = ETAT_NUM;
+				else 
+					etatCour = ETAT_CHAR;
+				
+				// initialisation de l'état de "référence"
+				if (i==0)
+					etatPrec=etatCour;
+
+				// Cas où l'on a un chiffre après des caractères
+				if ((etatCour==ETAT_NUM)&&(etatPrec==ETAT_CHAR))
+				{
+					if ( strCharRef != strCharTest )
+					{
+						fluxfilediff << "Diff at line " << numLine 
+						     << " : " << strCharRef
+						     << " != " << strCharTest << std::endl ;
+						nbdiff++;
+					}
+										
+					strCharRef="";
+					strCharTest="";
+					strNumRef=charTmpRef;
+					strNumTest=charTmpTest;
+					chgt=true;
+				}
+				// Cas où l'on a un caractère après des chiffres
+				else if ((etatCour==ETAT_CHAR)&&(etatPrec==ETAT_NUM))
+				{
+					
+					if (fabs(atof(strNumRef.c_str())-atof(strNumTest.c_str())) > epsilon)
+					{
+						fluxfilediff << "Diff at line " << numLine << " : fabs ( (" 
+						     << strNumRef << ") - (" << strNumTest
+						     << ") ) > " << epsilon << std::endl ;
+						nbdiff++;
+					}	
+					
+					strNumRef="";
+					strNumTest="";
+					strCharRef=charTmpRef;
+					strCharTest=charTmpTest;
+					chgt=true;
+				}
+				else if (etatCour==etatPrec)
+				{
+					if (etatCour==ETAT_CHAR)
+					{
+						strCharRef+=charTmpRef;
+						strCharTest+=charTmpTest;
+					}						
+					else
+					{
+						strNumRef+=charTmpRef;
+						strNumTest+=charTmpTest;
+					}
+				}
+
+				etatPrec = etatCour;
+				i++;
+			}	
 			
-			if (isNumeric(strRef))
-			{
-				if ((atof(strRef.c_str())-atof(strTest.c_str())) > epsilon)
-					nbdiff++;
-			}
-			else 
-			{
-				if ( strRef != strTest )
-					nbdiff++;
+			// Cas le plus simple : chaine de caractere ou valeur numérique entre 2 separateurs
+			if (!chgt)
+			{
+				if (isNumeric(strRef))
+				{
+					
+					if (fabs(atof(strRef.c_str())-atof(strTest.c_str())) > epsilon)
+					{
+						fluxfilediff << "Diff at line " << numLine << " : fabs( (" 
+							     << strRef << ") - (" << strTest
+							     << ") ) > " << epsilon << std::endl ;
+						nbdiff++;
+					}
+				}
+				else 
+				{
+					if ( strRef != strTest )
+					{
+						fluxfilediff << "Diff at line " << numLine 
+							     << " : " << strRef
+							     << " != " << strTest << std::endl ;
+						nbdiff++;
+					}
+				}
 			}
-		}
+		}
+		numLine++;
 	}
 	
 	fluxfiletest.close();
 	fluxfileref.close();
+	fluxfilediff.close();
 	
 	if ( nbdiff!=0 && reportErrors)
 	{
diff --git a/Testing/Code/Common/otbTestCommandLineArgumentParser.cxx b/Testing/Code/Common/otbTestCommandLineArgumentParser.cxx
index dd47f05798..5b78f6e4bd 100755
--- a/Testing/Code/Common/otbTestCommandLineArgumentParser.cxx
+++ b/Testing/Code/Common/otbTestCommandLineArgumentParser.cxx
@@ -46,16 +46,16 @@ int otbTestCommandLineArgumentParser( int argc, char * argv[] )
 		/*char* ch = new char[8];
 		ch = "-entier" ;*/
 		//unsigned int lEntier = otb::GetParameter<unsigned int>(parseResult,"-entier");
-        unsigned int lEntier = parseResult->GetParameter<unsigned int>("-entier");
+        unsigned int lEntier = parseResult->GetUIntParameter("-entier");
 		std::cout << "Entier : "<<lEntier<<std::endl;
 	if( parseResult->IsOptionPresent("-deuxentiers"))
 	  {
 		//unsigned int lEntierDeux = otb::GetParameter<unsigned int>(parseResult,"-deuxentiers",1);
-          unsigned int lEntierDeux = parseResult->GetParameter<unsigned int>("-deuxentiers",1);
+          unsigned int lEntierDeux = parseResult->GetUIntParameter("-deuxentiers",1);
 		  std::cout << "Entier : "<<lEntier<<std::endl;
 	  }
 	//double lDouble = otb::GetParameter<double>(parseResult,"-double");
-	double lDouble = parseResult->GetParameter<double>("-double");
+	double lDouble = parseResult->GetDoubleParameter("-double");
         std::cout << "Double : "<<lDouble<<std::endl;
     } 
 
diff --git a/Testing/Code/Common/otbTestCommandLineArgumentParserList.cxx b/Testing/Code/Common/otbTestCommandLineArgumentParserList.cxx
index d32537ecaf..9e8908ee05 100644
--- a/Testing/Code/Common/otbTestCommandLineArgumentParserList.cxx
+++ b/Testing/Code/Common/otbTestCommandLineArgumentParserList.cxx
@@ -44,23 +44,23 @@ int otbTestCommandLineArgumentParserList( int argc, char * argv[] )
 
         std::cout << "Image : "<<parseResult->GetStringParameter("-image")<<std::endl;
         //unsigned int lEntier = otb::GetParameter<unsigned int>(parseResult,"-entier");
-		unsigned int lEntier = parseResult->GetParameter<unsigned int>("-entier");
+		unsigned int lEntier = parseResult->GetUIntParameter("-entier");
         std::cout << "Entier : "<<lEntier<<std::endl;
 	if( parseResult->IsOptionPresent("-deuxentiers"))
 	  {
 		//unsigned int lEntierDeux = otb::GetParameter<unsigned int>(parseResult,"-deuxentiers",1);
-		unsigned int lEntierDeux = parseResult->GetParameter<unsigned int>("-deuxentiers",1);
+		unsigned int lEntierDeux = parseResult->GetUIntParameter("-deuxentiers",1);
           std::cout << "Entier : "<<lEntier<<std::endl;
 	  }
 	//double lDouble = otb::GetParameter<double>(parseResult,"-double");
-	double lDouble = parseResult->GetParameter<double>("-double");
+	double lDouble = parseResult->GetDoubleParameter("-double");
         std::cout << "Double : "<<lDouble<<std::endl;
 		
         std::cout << "List de Double : "<<parseResult->GetNumberOfParameters("-doubles")<<std::endl;
         for (int i =0 ; i<parseResult->GetNumberOfParameters("-doubles") ; i++)
         {
 			//double value = otb::GetParameter<double>(parseResult,"-doubles",i);
-			double value = parseResult->GetParameter<double>("-doubles",i);
+			double value = parseResult->GetDoubleParameter("-doubles",i);
                 std::cout << "  "<<value;
         }
         std::cout << std::endl;
diff --git a/Testing/Code/Common/otbTestCommandLineArgumentParserWithError.cxx b/Testing/Code/Common/otbTestCommandLineArgumentParserWithError.cxx
index 3c613e0f2e..81049f7a04 100644
--- a/Testing/Code/Common/otbTestCommandLineArgumentParserWithError.cxx
+++ b/Testing/Code/Common/otbTestCommandLineArgumentParserWithError.cxx
@@ -43,16 +43,16 @@ int otbTestCommandLineArgumentParserWithError( int argc, char * argv[] )
 
         std::cout << "Image : "<<parseResult->GetStringParameter("-image")<<std::endl;
         //unsigned int lEntier = otb::GetParameter<unsigned int>(parseResult,"-entier");
-		unsigned int lEntier = parseResult->GetParameter<unsigned int>("-entier");
+		unsigned int lEntier = parseResult->GetUIntParameter("-entier");
         std::cout << "Entier : "<<lEntier<<std::endl;
 	if( parseResult->IsOptionPresent("-deuxentiers"))
 	  {
 		//unsigned int lEntierDeux = otb::GetParameter<unsigned int>(parseResult,"-deuxentiers",1);
-		unsigned int lEntierDeux = parseResult->GetParameter<unsigned int>("-deuxentiers",1);
+		unsigned int lEntierDeux = parseResult->GetUIntParameter("-deuxentiers",1);
           std::cout << "Entier : "<<lEntier<<std::endl;
 	  }
 	//double lDouble = otb::GetParameter<double>(parseResult,"-double");
-	double lDouble = parseResult->GetParameter<double>("-double");
+	double lDouble = parseResult->GetDoubleParameter("-double");
         std::cout << "Double : "<<lDouble<<std::endl;
     } 
 
diff --git a/Testing/Code/FeatureExtraction/CMakeLists.txt b/Testing/Code/FeatureExtraction/CMakeLists.txt
index 616007b598..da05225075 100755
--- a/Testing/Code/FeatureExtraction/CMakeLists.txt
+++ b/Testing/Code/FeatureExtraction/CMakeLists.txt
@@ -12,7 +12,12 @@ SET(TEMP ${OTBTesting_BINARY_DIR}/Temporary)
 #Tolerance sur diff pixel image
 #Utiliser EPSILON lorsuqe par exemple sous Windows, il y a qques differences
 SET(TOL 0.0)
-SET(EPSILON 0.0000001)
+SET(EPSILON 0.0001)
+
+#Tolerance sur la difference des valeurs numeriques dans le --compare-ascii
+SET(EPS 0.001)
+
+
 
 SET(FEATUREEXTRACTION_TESTS ${CXX_TEST_PATH}/otbFeatureExtractionTests)
 
diff --git a/Testing/Code/IO/CMakeLists.txt b/Testing/Code/IO/CMakeLists.txt
index a0846e50ce..42c8aa3c59 100755
--- a/Testing/Code/IO/CMakeLists.txt
+++ b/Testing/Code/IO/CMakeLists.txt
@@ -12,8 +12,8 @@ SET(TEMP ${OTBTesting_BINARY_DIR}/Temporary)
 #Tolerance sur diff pixel image
 SET(TOL 0.0)
 
-#Epsilon pour le compare ascii
-SET(EPS 0.0001)
+#Tolerance sur la difference des valeurs numeriques dans le --compare-ascii
+SET(EPS 0.001)
 
 # !!!!!  Pointe sur un exe généré dans le repertoire Common
 SET(COMMON_TESTS ${CXX_TEST_PATH}/otbCommonTests)
-- 
GitLab