NumericFileComparison.hpp

00001 /*
00002 
00003 Copyright (c) 2005-2015, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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             // Cannot read the next token from file as a number, so try a word instead
00070             std::string word;
00071             pFile->clear(); // reset the "failbit"
00072             if (*pFile >> word)
00073             {
00074                 rData = A_WORD;
00075                 if (word == "#" || word == "!")
00076                 {
00077                     // Ignore comment (up to 1024 characters until newline)
00078                     pFile->ignore(1024, '\n');
00079                 }
00080             }
00081             else
00082             {
00083                 pFile->clear(); // reset the "failbit"
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         // Usually only the master process does the checking, this can be switched off in the constructor.
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                     // Display error
00158                     CompareDoubles::WithinAnyTolerance(data1, data2, relTol, absTol, true);
00159                 }
00160             }
00161         }
00162         while (data1 != NOTHING_TO_READ && data2 != NOTHING_TO_READ); // If either is a NOTHING_TO_READ, then it means that there's nothing to read from the file
00163 
00164         if (doTsAssert)
00165         {
00166             // Force CxxTest error if there were any major differences
00167             TS_ASSERT_EQUALS(failures, 0u);
00168             // If that assertion tripped...
00169             if (failures > 0u && !mSuppressOutput)
00170             {
00171 #define COVERAGE_IGNORE
00172                 // Report the paths to the files
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 /*NUMERICFILECOMPARISON_HPP_*/

Generated by  doxygen 1.6.2