From b5f8dfbdf0e9c97790459681f50aaa75d2f6aa67 Mon Sep 17 00:00:00 2001
From: Emmanuel Christophe <emmanuel.christophe@orfeo-toolbox.org>
Date: Wed, 30 Sep 2009 10:54:38 +0800
Subject: [PATCH] ENH: remove comparelist (now most code is common with
 compareascii), add option --epsilon-boundary to set up the limit for test
 working internally in float

---
 Testing/Code/otbTestHelper.cxx | 390 +--------------------------------
 Testing/Code/otbTestHelper.h   |   8 +-
 Testing/Code/otbTestMain.h     | 134 ++---------
 3 files changed, 21 insertions(+), 511 deletions(-)

diff --git a/Testing/Code/otbTestHelper.cxx b/Testing/Code/otbTestHelper.cxx
index 189b7c254b..9854adeee0 100644
--- a/Testing/Code/otbTestHelper.cxx
+++ b/Testing/Code/otbTestHelper.cxx
@@ -280,392 +280,6 @@ int TestHelper::RegressionTestAsciiFile(const char * testAsciiFileName, const ch
   return (nbdiff != 0) ? 1 : 0;
 }
 
-/******************************************/
-/******************************************/
-/******************************************/
-int TestHelper::RegressionTestListFile(const char * testListFileName, const char * baselineListFileName,
-    const double epsilon, std::vector<std::string> ignoredLines) const
-{
-  std::ifstream fluxfileref(baselineListFileName);
-  // stores the line number of the tested file that has already matched a line of the baseline
-  std::vector<unsigned int> usedLineInTestFile;
-  // store the number of words in each line of the tested file
-  std::vector<unsigned int> testedLineLength;
-
-  enum TypeEtat
-  {
-    ETAT_NUM, ETAT_CHAR
-  };
-
-  std::string diffListFileName(testListFileName);
-  diffListFileName += ".diff.txt";
-  std::ofstream fluxfilediff;
-
-  if (m_ReportErrors)
-  {
-    fluxfilediff.open(diffListFileName.c_str());
-  }
-
-  std::string strfileref;
-
-  int nbdiff(0);
-
-  if (!fluxfileref)
-  {
-    itkGenericExceptionMacro(<< "Impossible to open the baseline List file <"<<baselineListFileName<<">.");
-  }
-
-  TypeEtat etatPrec(ETAT_NUM), etatCour(ETAT_NUM);
-
-  std::vector<std::string> listStrDiffLineFileRef;
-  std::vector<std::string> listStrDiffLineFileTest;
-
-  // For each line of the baseline file
-  while (std::getline(fluxfileref, strfileref) != 0)
-  {
-    // test if there's ignore lines
-    bool foundexpr = false;
-    if (ignoredLines.size() > 0)
-    {
-
-      std::vector<std::string>::iterator itIgnoredLines = ignoredLines.begin();
-
-      for (; (itIgnoredLines != ignoredLines.end()); ++itIgnoredLines)
-      {
-        std::string ignoredLinesList = (*itIgnoredLines);
-        std::string::size_type loc = strfileref.find(ignoredLinesList);
-        if (loc != std::string::npos)
-        {
-          foundexpr = true;
-        }
-
-      }
-
-    }
-    // If no ignore lines
-    if (foundexpr == false)
-    {
-      // is the baseline line (ref) and the tested line (test) matches
-      bool isFound = false;
-      // tested file line number
-      unsigned int lineId = 0;
-      std::string strfiletest;
-      // Open tested file
-      std::ifstream fluxfiletest(testListFileName);
-      if (!fluxfiletest)
-      {
-        itkGenericExceptionMacro(<<"Impossible to open the test List file <"<<testListFileName<<">.");
-      }
-
-      // number of word in baseline file line
-      unsigned int refElt = 1;
-      int lastElt = strfileref[0];
-      for (unsigned int l = 1; l < strfileref.size(); l++)
-      {
-        if (!std::isspace(lastElt) && std::isspace(strfileref[l]))
-          refElt++;
-
-        lastElt = strfileref[l];
-      }
-      if (std::isspace(lastElt))
-        refElt--;
-
-      // For each line of the tested file
-      while (std::getline(fluxfiletest, strfiletest) != 0 && isFound == false)
-      {
-        lineId++;
-        otb::StringStream buffstreamTest, buffstreamRef;
-        isFound = true;
-
-        buffstreamTest << strfiletest;
-        buffstreamRef << strfileref;
-
-        //Check number of element in each line, if not equal : out
-        unsigned int testElt = 1;
-        // if alreday computed
-        if (lineId <= testedLineLength.size())
-        {
-          testElt = testedLineLength[lineId - 1];
-        }
-        else
-        {
-          lastElt = strfiletest[0];
-          for (unsigned int l = 1; l < strfiletest.size(); l++)
-          {
-            if (!std::isspace(lastElt) && std::isspace(strfiletest[l]))
-              testElt++;
-
-            lastElt = strfiletest[l];
-          }
-          if (std::isspace(lastElt))
-            testElt--;
-          testedLineLength.push_back(testElt);
-        }
-        if (refElt != testElt)
-        {
-          isFound = false;
-        }
-        // Chek word by word the match of the line
-        else
-        {
-          // Store the match result between 2 words of the same position
-          bool wordFound = true;
-          while (buffstreamRef.peek() != EOF && buffstreamTest.peek() != EOF && wordFound == true)
-          {
-            std::string strRef = "";
-            std::string strTest = "";
-
-            std::string strNumRef = "";
-            std::string strCharRef = "";
-            std::string strNumTest = "";
-            std::string strCharTest = "";
-
-            buffstreamRef >> strRef;
-            buffstreamTest >> strTest;
-
-            bool chgt = false;
-            std::string charTmpRef = "";
-            std::string charTmpTest = "";
-            unsigned int i = 0;
-
-            if (!isHexaPointerAddress(strRef))
-            {
-              //Analyse if strRef contains scientific value (ex: "-142.124e-012")
-              if (isScientificNumeric(strRef))
-              {
-                if (!isScientificNumeric(strTest))
-                {
-                  wordFound = false;
-                }
-                else if ((strRef != strTest) && (vcl_abs(atof(strRef.c_str())) > m_EpsilonBoundaryChecking)
-                    && (vcl_abs(atof(strRef.c_str()) - atof(strTest.c_str())) > epsilon * vcl_abs(atof(strRef.c_str()))))//epsilon as relative error
-                {
-                  wordFound = false;
-                }
-              }
-              else
-              {
-                while (i < strRef.size())
-                {
-                  charTmpRef = strRef[i];
-
-                  if (i < strTest.size())
-                  {
-                    charTmpTest = strTest[i];
-                  }
-
-                  if (isNumeric(charTmpRef))
-                    etatCour = ETAT_NUM;
-                  else
-                    etatCour = ETAT_CHAR;
-
-                  // "reference" state initialisation.
-                  if (i == 0)
-                    etatPrec = etatCour;
-
-                  // Case where there's a number after characteres.
-                  if ((etatCour == ETAT_NUM) && (etatPrec == ETAT_CHAR))
-                  {
-                    if (strCharRef != strCharTest)
-                    {
-                      wordFound = false;
-                    }
-
-                    strCharRef = "";
-                    strCharTest = "";
-                    strNumRef = charTmpRef;
-                    strNumTest = charTmpTest;
-                    chgt = true;
-                  }
-                  // Case where there's a character after numbers.
-                  else if ((etatCour == ETAT_CHAR) && (etatPrec == ETAT_NUM))
-                  {
-                    if ((strNumRef != strNumTest) && (vcl_abs(atof(strNumRef.c_str())) > m_EpsilonBoundaryChecking)
-                        && (vcl_abs(atof(strNumRef.c_str()) - atof(strNumTest.c_str())) > epsilon * vcl_abs(atof(
-                            strNumRef.c_str())))) //epsilon as relative error
-                    {
-                      wordFound = false;
-                    }
-
-                    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;
-                }
-                // Simpliest case : string characters or numeric value between 2 separators
-                if (!chgt)
-                {
-                  if (isNumeric(strRef))
-                  {
-                    if ((strRef != strTest) && (vcl_abs(atof(strRef.c_str())) > m_EpsilonBoundaryChecking) && (vcl_abs(
-                        atof(strRef.c_str()) - atof(strTest.c_str())) > epsilon * vcl_abs(atof(strRef.c_str())))) //epsilon as relative error
-                    {
-                      wordFound = false;
-                    }
-                  }
-                  else
-                  {
-                    if (strRef != strTest)
-                    {
-                      wordFound = false;
-                    }
-                  }
-                }
-              } // else
-            } // if(!isHexaPointerAddress(strRef))
-          } // while (buffstreamRef.peek() != EOF && buffstreamTest.peek() != EOF && wordFound == true)
-
-          if (wordFound == false)
-          {
-            isFound = false;
-          }
-
-        }
-
-        // Check that the tested line has been alreday used (2 same lines in the baseline)
-        // If yes, it won't be retained
-        if (isFound == true)
-        {
-          bool lineAlreadyUsed = false;
-          unsigned int count = 0;
-          while (count < usedLineInTestFile.size() && lineAlreadyUsed == false)
-          {
-            if (usedLineInTestFile[count] == lineId)
-            {
-              isFound = false;
-              lineAlreadyUsed = true;
-            }
-            count++;
-          }
-          if (lineAlreadyUsed == false)
-            usedLineInTestFile.push_back(lineId);
-        }
-
-      }// end while( std::getline(fluxfiletestremoved,strfiletest)!=0 && foundexpr == false )
-
-      // Stores the baseline line that hasn't found a twin in the tested file
-      if (isFound == false)
-      {
-        listStrDiffLineFileRef.push_back(strfileref);
-        nbdiff++;
-      }
-      fluxfiletest.close();
-    } // endif ( foundexpr == false )
-  }// end while( std::getline(fluxfileref,strfileref)!=0 )
-
-
-  fluxfileref.close();
-
-  // Stores the tested file lines that haven't found a twin in the baseline file
-  std::ifstream fluxfiletest(testListFileName);
-  std::string strfiletest;
-  unsigned int testNbLines = 0;
-  //  number of lines in tested file
-  while (std::getline(fluxfiletest, strfiletest))
-  {
-    testNbLines++;
-  }
-
-  if (testNbLines > usedLineInTestFile.size())
-  {
-    std::ifstream fluxfiletestTmp(testListFileName);
-    unsigned int count = 0;
-    while (std::getline(fluxfiletestTmp, strfiletest) != 0)
-    {
-      count++;
-      bool found = false;
-      unsigned int j = 0;
-      while (j < usedLineInTestFile.size() && found == false)
-      {
-        if (usedLineInTestFile[j] == count)
-        {
-          found = true;
-        }
-        j++;
-      }
-      if (found == false)
-      {
-        if (ignoredLines.size() > 0)
-        {
-          std::vector<std::string>::iterator itIgnoredLines = ignoredLines.begin();
-
-          for (; (itIgnoredLines != ignoredLines.end()); ++itIgnoredLines)
-          {
-            std::string ignoredLinesList = (*itIgnoredLines);
-            std::string::size_type loc = strfiletest.find(ignoredLinesList);
-            if (loc == std::string::npos)
-            {
-              listStrDiffLineFileTest.push_back(strfiletest);
-            }
-
-          }
-
-        }
-        else
-          listStrDiffLineFileTest.push_back(strfiletest);
-      }
-    }
-  }
-
-  if (nbdiff != 0 && m_ReportErrors)
-  {
-    std::cout << "<DartMeasurement name=\"ListFileError\" type=\"numeric/int\">";
-    std::cout << nbdiff;
-    std::cout << "</DartMeasurement>" << std::endl;
-    std::cout << "================================================================" << std::endl;
-    std::cout << "baseline List File : " << baselineListFileName << std::endl;
-    std::cout << "Test List File     : " << testListFileName << std::endl;
-    std::cout << "Diff List File     : " << diffListFileName << std::endl;
-    std::cout << "Tolerance value     : " << epsilon << std::endl;
-    std::cout << "Tolerance max check : " << m_EpsilonBoundaryChecking << std::endl;
-
-    std::cout << "Nb lines differents : " << listStrDiffLineFileRef.size() << std::endl;
-    std::cout << "Line(s) in Baseline file but not in Test file : " << listStrDiffLineFileRef.size() << std::endl;
-
-    fluxfilediff << "Nb lines differents : " << listStrDiffLineFileRef.size() << std::endl;
-    fluxfilediff << "Line(s) in Baseline file but not in Test file : " << listStrDiffLineFileRef.size() << std::endl;
-    for (unsigned int i = 0; i < listStrDiffLineFileRef.size(); ++i)
-    {
-      std::cout << listStrDiffLineFileRef[i] << std::endl;
-      fluxfilediff << ">> " << listStrDiffLineFileRef[i] << std::endl;
-    }
-    std::cout << "   -------------------------------" << std::endl;
-    std::cout << "Line(s) in Test file but not in Baseline file : " << listStrDiffLineFileTest.size() << std::endl;
-    fluxfilediff << "   -------------------------------" << std::endl;
-    fluxfilediff << "Line(s) in Test file but not in Baseline file : " << listStrDiffLineFileTest.size() << std::endl;
-
-    for (unsigned int i = 0; i < listStrDiffLineFileTest.size(); ++i)
-    {
-      std::cout << listStrDiffLineFileTest[i] << std::endl;
-      fluxfilediff << "<< " << listStrDiffLineFileTest[i] << std::endl;
-    }
-  }
-
-  if (m_ReportErrors)
-  {
-    fluxfilediff.close();
-  }
-
-  return (nbdiff != 0) ? 1 : 0;
-}
 
 /******************************************/
 /******************************************/
@@ -1489,7 +1103,9 @@ bool TestHelper::CompareLines(std::string strfileref, std::string strfiletest, i
           nbdiff++;
 
         }
-        else if ((strRef != strTest) && (vcl_abs(atof(strRef.c_str())) > m_EpsilonBoundaryChecking) && (vcl_abs(
+        else if ((strRef != strTest)
+          && (vcl_abs(atof(strRef.c_str())) > m_EpsilonBoundaryChecking)
+          && (vcl_abs(
             atof(strRef.c_str()) - atof(strTest.c_str())) > epsilon * vcl_abs(atof(strRef.c_str()))))//epsilon as relative error
         {
           if (m_ReportErrors)
diff --git a/Testing/Code/otbTestHelper.h b/Testing/Code/otbTestHelper.h
index d68673ddb0..6765ab8642 100644
--- a/Testing/Code/otbTestHelper.h
+++ b/Testing/Code/otbTestHelper.h
@@ -55,9 +55,6 @@ public:
   int RegressionTestAsciiFile(const char * testAsciiFileName, const char * baselineAsciiFileName,
       const double epsilon, std::vector<std::string> ignoredLines) const;
 
-  int RegressionTestListFile(const char * testListFileName, const char * baselineListFileName,
-      const double epsilon, std::vector<std::string> ignoredLines) const;
-
   int RegressionTestMetaData(const char *testImageFilename, const char *baselineImageFilename,
       const double toleranceDiffPixelImage) const;
 
@@ -79,6 +76,11 @@ public:
     m_IgnoreLineOrder=false;
   }
 
+  void SetEpsilonBoundaryChecking(double epsilonBoundary)
+  {
+    m_EpsilonBoundaryChecking = epsilonBoundary;
+  }
+
 private:
   bool isNumber(int i) const;
   bool isHexaNumber(int i) const;
diff --git a/Testing/Code/otbTestMain.h b/Testing/Code/otbTestMain.h
index 6fb33d4394..5cd5aa9f7a 100644
--- a/Testing/Code/otbTestMain.h
+++ b/Testing/Code/otbTestMain.h
@@ -59,6 +59,7 @@ int main(int ac, char* av[] )
   double lToleranceDiffValue(0);
   double lEpsilon(0);
   bool lIgnoreOrder(false);
+  double epsilonBoundary(0.0);
 
   std::vector<std::string> baselineFilenamesBinary;
   std::vector<std::string> testFilenamesBinary;
@@ -71,8 +72,6 @@ int main(int ac, char* av[] )
   std::vector<std::string> testFilenamesImage;
   std::vector<std::string> baselineFilenamesAscii;
   std::vector<std::string> testFilenamesAscii;
-  std::vector<std::string> baselineFilenamesList;
-  std::vector<std::string> testFilenamesList;
   std::vector<std::string> ignoredLines;
   ignoredLines.clear();
 
@@ -122,11 +121,16 @@ int main(int ac, char* av[] )
     }
     if (strcmp(av[1], "--ignore-order") == 0)
     {
-      std::cout << "******************* Order option identified" << std::endl;
       lIgnoreOrder = true;
       av += 1;
       ac -= 1;
     }
+    if (strcmp(av[1], "--epsilon-boundary") == 0)
+    {
+      epsilonBoundary = atof(av[2]);
+      av += 2;
+      ac -= 2;
+    }
     if (strcmp(av[1], "--compare-image") == 0)
     {
       lFlagRegression = true;
@@ -237,64 +241,6 @@ int main(int ac, char* av[] )
       }
 
     }
-    else if (strcmp(av[1], "--compare-list") == 0)
-    {
-      lFlagRegression = true;
-      lEpsilon = (double)(::atof(av[2]));
-      baselineFilenamesList.reserve(1);
-      testFilenamesList.reserve(1);
-      baselineFilenamesList.push_back(av[3]);
-      testFilenamesList.push_back(av[4]);
-      av += 4;
-      ac -= 4;
-
-      if ( ac > 1 )
-      {
-        if (strcmp(av[1], "--ignore-lines-with") == 0)
-        {
-          unsigned int nbIgnoredLines=(unsigned int)(::atoi(av[2]));
-          for (unsigned int  i=0; i<nbIgnoredLines; ++i )
-          {
-            ignoredLines.push_back(av[3+i]);
-          }
-          av += 2+nbIgnoredLines;
-          ac -= 2+nbIgnoredLines;
-        }
-      }
-
-    }
-
-    else if (strcmp(av[1], "--compare-n-list") == 0)
-    {
-      lFlagRegression = true;
-      lEpsilon = (double)(::atof(av[2]));
-      // Number of comparisons to do
-      unsigned int nbComparisons=(unsigned int)(::atoi(av[3]));
-      baselineFilenamesList.reserve(nbComparisons);
-      testFilenamesList.reserve(nbComparisons);
-      // Retrieve all the file names
-      for (unsigned int i = 0; i<nbComparisons;++i)
-      {
-        baselineFilenamesList.push_back(av[4+2*i]);
-        testFilenamesList.push_back(av[5+2*i]);
-      }
-      av+=3+2*nbComparisons;
-      ac-=3+2*nbComparisons;
-
-      if ( ac > 1 )
-      {
-        if (strcmp(av[1], "--ignore-lines-with") == 0)
-        {
-          unsigned int nbIgnoredLines=(unsigned int)(::atoi(av[2]));
-          for (unsigned int  i=0; i<nbIgnoredLines; ++i )
-          {
-            ignoredLines.push_back(av[3+i]);
-          }
-          av += 2+nbIgnoredLines;
-          ac -= 2+nbIgnoredLines;
-        }
-      }
-    }
     else if (strcmp(av[1], "--compare-metadata") == 0)
     {
       lFlagRegression = true;
@@ -383,6 +329,12 @@ int main(int ac, char* av[] )
           {
             testHelper.IgnoreLineOrderOff();
           }
+
+          if (epsilonBoundary != 0.0)
+          {
+            testHelper.SetEpsilonBoundaryChecking(epsilonBoundary);
+          }
+
           // Non regression test for images
           if ((baselineFilenamesImage.size()>0) && (testFilenamesImage.size()>0))
           {
@@ -530,66 +482,6 @@ int main(int ac, char* av[] )
           }
 /******************************************/
 /******************************************/
-/******************************************/
-// Non regression test for list
-          if ((baselineFilenamesList.size()>0) && (testFilenamesList.size()>0))
-          {
-            // Creates iterators on baseline filenames vector and test filenames vector
-            std::vector<std::string>::iterator itbaselineFilenames = baselineFilenamesList.begin();
-            std::vector<std::string>::iterator itTestFilenames = testFilenamesList.begin();
-            std::vector<std::string>::iterator itIgnoredLines = ignoredLines.begin();
-            // Warning message
-            if (ignoredLines.size() > 0 )
-            {
-              std::cout << "The lines containing the expressions ";
-              for (;itIgnoredLines!=ignoredLines.end();++itIgnoredLines)
-              {
-                std::cout << (*itIgnoredLines) <<" ";
-              }
-              std::cout << "are not considered"<< std::endl;
-            }
-
-            // For each couple of baseline and test file, do the comparison
-            for (;(itbaselineFilenames != baselineFilenamesList.end())
-                 &&(itTestFilenames != testFilenamesList.end());
-                 ++itbaselineFilenames,++itTestFilenames)
-            {
-              std::string baselineFilenameList= (*itbaselineFilenames);
-              std::string testFilenameList= (*itTestFilenames);
-
-              std::map<std::string,int> baselines = testHelper.RegressionTestbaselines(const_cast<char*>(baselineFilenameList.c_str()));
-              std::map<std::string,int>::reverse_iterator baseline = baselines.rbegin();
-              multiResult = 1;
-              std::cout<<"Number of baseline files: "<<baselines.size()<<std::endl;
-
-              while (baseline!=baselines.rend() && (multiResult!=0))
-              {
-                std::cout<<"Testing non-regression on file: "<<(baseline->first).c_str()<<std::endl;
-                testHelper.ReportErrorsOff();
-                baseline->second = testHelper.RegressionTestListFile(testFilenameList.c_str(),
-                                   (baseline->first).c_str(),
-                                   lEpsilon,
-                                   ignoredLines);
-
-                multiResult = baseline->second;
-                ++baseline;
-              }
-	      if (multiResult != 0)
-              {
-                baseline = baselines.rbegin();
-                testHelper.ReportErrorsOn();
-                baseline->second
-                    = testHelper.RegressionTestListFile(testFilenameList.c_str(),
-                                          (baseline->first).c_str(),
-                                          lEpsilon,
-                                          ignoredLines);
-              }
-
-              result += multiResult;
-            }
-          }
-/******************************************/
-/******************************************/
 /******************************************/
           // Non regression test for binary files
           if ((baselineFilenamesBinary.size()>0) && (testFilenamesBinary.size()>0))
-- 
GitLab