NumericFileComparison.hpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef NUMERICFILECOMPARISON_HPP_
00036 #define NUMERICFILECOMPARISON_HPP_
00037
00038 #include <cfloat>
00039
00040 #include "AbstractFileComparison.hpp"
00041 #include "MathsCustomFunctions.hpp"
00042
00043 #define A_WORD DBL_MAX
00044 #define NOTHING_TO_READ DBL_MIN
00045
00052 class NumericFileComparison : public AbstractFileComparison
00053 {
00054 private:
00065 void ReadNextToken(std::ifstream* pFile, double& rData)
00066 {
00067 if (!(*pFile>>rData))
00068 {
00069
00070 std::string word;
00071 pFile->clear();
00072 if (*pFile >> word)
00073 {
00074 rData = A_WORD;
00075 if (word == "#" || word == "!")
00076 {
00077
00078 pFile->ignore(1024, '\n');
00079 }
00080 }
00081 else
00082 {
00083 pFile->clear();
00084 rData = NOTHING_TO_READ;
00085 }
00086 }
00087 }
00088
00089 public:
00090
00100 NumericFileComparison(std::string fileName1, std::string fileName2, bool calledCollectively=true, bool suppressOutput = false)
00101 : AbstractFileComparison(fileName1,fileName2,calledCollectively,suppressOutput)
00102 {
00103 }
00104
00114 NumericFileComparison(const FileFinder& rFileName1, const FileFinder& rFileName2, bool calledCollectively=true, bool suppressOutput = false)
00115 : AbstractFileComparison(rFileName1,rFileName2,calledCollectively,suppressOutput)
00116 {
00117 }
00118
00119
00131 bool CompareFiles(double absTol=DBL_EPSILON, unsigned ignoreFirstFewLines=0,
00132 double relTol=DBL_EPSILON, bool doTsAssert=true)
00133 {
00134
00135
00136 if (mCalledCollectively && !PetscTools::AmMaster())
00137 {
00138 return true;
00139 }
00140
00141 double data1;
00142 double data2;
00143 unsigned failures = 0;
00144 unsigned max_display_failures = 10;
00145
00146 SkipHeaderLines(ignoreFirstFewLines);
00147
00148 do
00149 {
00150 ReadNextToken(mpFile1, data1);
00151 ReadNextToken(mpFile2, data2);
00152 bool ok = CompareDoubles::WithinAnyTolerance(data1, data2, relTol, absTol);
00153 if (!ok)
00154 {
00155 if (failures++ < max_display_failures && !mSuppressOutput)
00156 {
00157
00158 CompareDoubles::WithinAnyTolerance(data1, data2, relTol, absTol, true);
00159 }
00160 }
00161 }
00162 while (data1 != NOTHING_TO_READ && data2 != NOTHING_TO_READ);
00163
00164 if (doTsAssert)
00165 {
00166
00167 TS_ASSERT_EQUALS(failures, 0u);
00168
00169 if (failures > 0u && !mSuppressOutput)
00170 {
00171 #define COVERAGE_IGNORE
00172
00173 TS_TRACE("Files " + mFilename1 + " and " + mFilename2 + " numerically differ.");
00174 #undef COVERAGE_IGNORE
00175 }
00176 }
00177
00178 ResetFiles();
00179
00180 return (failures==0);
00181 }
00182 };
00183
00184 #endif