FileComparison.hpp

00001 /*
00002 
00003 Copyright (c) 2005-2014, 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 FILECOMPARISON_HPP_
00036 #define FILECOMPARISON_HPP_
00037 
00038 #include "AbstractFileComparison.hpp"
00039 #include <vector>
00040 #include <boost/foreach.hpp>
00041 
00047 class FileComparison : public AbstractFileComparison
00048 {
00049 private:
00051     bool mIgnoreCommentLines;
00052 
00057     std::vector<std::string> mCommentLineStarts;
00058 
00060     std::vector<std::string> mIgnorableContent;
00061 
00062 public:
00072     FileComparison(std::string fileName1, std::string fileName2, bool calledCollectively=true, bool suppressOutput = false)
00073         : AbstractFileComparison(fileName1, fileName2, calledCollectively, suppressOutput),
00074           mIgnoreCommentLines(true)
00075     {
00076         SetupCommentLines();
00077     }
00078 
00088     FileComparison(const FileFinder& rFileName1, const FileFinder& rFileName2, bool calledCollectively=true, bool suppressOutput = false)
00089         : AbstractFileComparison(rFileName1, rFileName2, calledCollectively, suppressOutput),
00090           mIgnoreCommentLines(true)
00091     {
00092         SetupCommentLines();
00093     }
00094 
00100     void SetupCommentLines()
00101     {
00102         mCommentLineStarts.push_back("#");
00103         mCommentLineStarts.push_back("!");
00104         mCommentLineStarts.push_back("Created by Chaste");
00105         mCommentLineStarts.push_back("<!-- Created by Chaste");
00106     }
00107 
00115     void SetIgnoreCommentLines(bool ignore=true)
00116     {
00117         mIgnoreCommentLines = ignore;
00118         if (!ignore)
00119         {
00120             mCommentLineStarts.clear();
00121         }
00122     }
00123 
00130     void SetIgnoreLinesBeginningWith(std::string lineStart)
00131     {
00132         mIgnoreCommentLines = true;
00133         mCommentLineStarts.push_back(lineStart);
00134     }
00135 
00142     void IgnoreLinesContaining(const std::string& rIgnorableText)
00143     {
00144         mIgnorableContent.push_back(rIgnorableText);
00145     }
00146 
00153     bool CompareFiles(unsigned ignoreFirstFewLines=0, bool doTsAssert=true)
00154     {
00155         // Usually only the master process does the checking, this can be switched off in the constructor.
00156         if (mCalledCollectively && !PetscTools::AmMaster())
00157         {
00158             return true;
00159         }
00160 
00161         std::string data1;
00162         std::string data2;
00163         unsigned failures = 0;
00164         unsigned max_display_failures = 10;
00165 
00166         SkipHeaderLines(ignoreFirstFewLines);
00167 
00168         bool files_empty = false;
00169         do
00170         {
00171             std::string buffer1;
00172             std::string buffer2;
00173             getline(*mpFile1,buffer1);
00174             getline(*mpFile2,buffer2);
00175 
00176             if (mIgnoreCommentLines)
00177             {
00178                 bool skip_this_line = false;
00179                 for (unsigned i=0; i<mCommentLineStarts.size(); i++)
00180                 {
00181                     // Check for lines starting with a comment symbol
00182                     size_t found1 = buffer1.find(mCommentLineStarts[i]);
00183                     size_t found2 = buffer2.find(mCommentLineStarts[i]);
00184                     if (found1 == 0 && found2 == 0)
00185                     {
00186                         skip_this_line = true;
00187                         break;
00188                     }
00189                 }
00190                 if (skip_this_line)
00191                 {
00192                     continue;
00193                 }
00194             }
00195 
00196             // Check for lines containing ignorable text
00197             if (!mIgnorableContent.empty())
00198             {
00199                 bool skip_this_line = false;
00200                 BOOST_FOREACH(const std::string& rText, mIgnorableContent)
00201                 {
00202                     size_t found1 = buffer1.find(rText);
00203                     size_t found2 = buffer2.find(rText);
00204                     if (found1 != std::string::npos && found2 != std::string::npos)
00205                     {
00206                         skip_this_line = true;
00207                         break;
00208                     }
00209                 }
00210                 if (skip_this_line)
00211                 {
00212                     continue;
00213                 }
00214             }
00215 
00216             if (!(buffer1==buffer2) && !files_empty)
00217             {
00218                 if (failures++ < max_display_failures && !mSuppressOutput)
00219                 {
00220                     // Display error
00221                     std::stringstream message;
00222                     message << "Line " << mLineNum << " differs in files " << mFilename1 << " and " << mFilename2;
00223 
00224                     TS_TRACE(message.str());
00225                     TS_TRACE( buffer1 );
00226                     TS_TRACE( buffer2 );
00227                 }
00228             }
00229             mLineNum++;
00230         }
00231         while (mpFile1->good() && mpFile2->good());
00232         // If either is not good(), then it means that there's nothing to read from the file, or a file input error.
00233 
00234         if (doTsAssert)
00235         {
00236             // Force CxxTest error if there were any major differences
00237             TS_ASSERT_EQUALS(failures, 0u);
00238             // If that assertion tripped...
00239             if (failures > 0u && !mSuppressOutput)
00240             {
00241 #define COVERAGE_IGNORE
00242                 TS_TRACE("Files " + mFilename1 + " and " + mFilename2 + " differ.");
00243 #undef COVERAGE_IGNORE
00244             }
00245         }
00246 
00247         ResetFiles();
00248 
00249         return (failures==0);
00250     }
00251 };
00252 
00253 #endif /*FILECOMPARISON_HPP_*/

Generated by  doxygen 1.6.2