Chaste Release::3.1
NumericFileComparison.hpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, 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 
00049 class NumericFileComparison : public AbstractFileComparison
00050 {
00051 public:
00052 
00062     NumericFileComparison(std::string fileName1, std::string fileName2, bool calledCollectively=true, bool suppressOutput = false)
00063         : AbstractFileComparison(fileName1,fileName2,calledCollectively,suppressOutput)
00064     {
00065     }
00066 
00076     NumericFileComparison(const FileFinder& rFileName1, const FileFinder& rFileName2, bool calledCollectively=true, bool suppressOutput = false)
00077         : AbstractFileComparison(rFileName1,rFileName2,calledCollectively,suppressOutput)
00078     {
00079     }
00080 
00081 
00092     bool CompareFiles(double absTol=DBL_EPSILON, unsigned ignoreFirstFewLines=0,
00093                       double relTol=DBL_EPSILON, bool doTsAssert=true)
00094     {
00095 
00096         // Usually only the master process does the checking, this can be switched off in the constructor.
00097         if (mCalledCollectively && !PetscTools::AmMaster())
00098         {
00099             return true;
00100         }
00101 
00102         double data1;
00103         double data2;
00104         unsigned failures = 0;
00105         unsigned max_display_failures = 10;
00106 
00107         SkipHeaderLines(ignoreFirstFewLines);
00108 
00109         do
00110         {
00111             if (!(*mpFile1>>data1))
00112             {
00113                 // Cannot read the next token from file as a number, so try a word instead
00114                 std::string word;
00115                 mpFile1->clear(); // reset the "failbit"
00116                 if (*mpFile1 >> word)
00117                 {
00118                     data1 = A_WORD;
00119                     if (word == "#" || word == "!")
00120                     {
00121                         // Ignore comment (up to 1024 characters until newline)
00122                         mpFile1->ignore(1024, '\n');
00123                     }
00124                 }
00125                 else
00126                 {
00127                     mpFile1->clear(); // reset the "failbit"
00128                     data1 = NOTHING_TO_READ;
00129                 }
00130             }
00131             if (!(*mpFile2 >> data2))
00132             {
00133                 // Cannot read the next token from file as a number, so try a word instead
00134                 std::string word;
00135                 mpFile2->clear(); // reset the "failbit"
00136                 if (*mpFile2 >> word)
00137                 {
00138                     data2 = A_WORD;
00139                     if (word == "#" || word == "!")
00140                     {
00141                         // Ignore comment (up to 1024 characters until newline)
00142                         mpFile2->ignore(1024, '\n');
00143                     }
00144                 }
00145                 else
00146                 {
00147                     mpFile2->clear(); // reset the "failbit"
00148                     data2 = NOTHING_TO_READ;
00149                 }
00150             }
00151 
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_*/