Chaste Release::3.1
FileComparison.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 FILECOMPARISON_HPP_
00036 #define FILECOMPARISON_HPP_
00037 
00038 #include "AbstractFileComparison.hpp"
00039 
00045 class FileComparison : public AbstractFileComparison
00046 {
00047 private:
00049     bool mIgnoreCommentLines;
00050 
00056     std::vector<std::string> mCommentLineStarts;
00057 public:
00058 
00068     FileComparison(std::string fileName1, std::string fileName2, bool calledCollectively=true, bool suppressOutput = false)
00069         : AbstractFileComparison(fileName1, fileName2, calledCollectively,suppressOutput),
00070           mIgnoreCommentLines(true)
00071     {
00072         SetupCommentLines();
00073     }
00074 
00084     FileComparison(const FileFinder& rFileName1, const FileFinder& rFileName2, bool calledCollectively=true, bool suppressOutput = false)
00085         : AbstractFileComparison(rFileName1, rFileName2, calledCollectively,suppressOutput),
00086           mIgnoreCommentLines(true)
00087     {
00088         SetupCommentLines();
00089     }
00090 
00096     void SetupCommentLines()
00097     {
00098         mCommentLineStarts.push_back("#");
00099         mCommentLineStarts.push_back("!");
00100         mCommentLineStarts.push_back("Created by Chaste");
00101     }
00102 
00107     void SetIgnoreCommentLines(bool ignore=true)
00108     {
00109         mIgnoreCommentLines = ignore;
00110     }
00111 
00118     void SetIgnoreLinesBeginningWith(std::string lineStart)
00119     {
00120         mIgnoreCommentLines = true;
00121         mCommentLineStarts.push_back(lineStart);
00122     }
00123 
00132     bool CompareFiles(unsigned ignoreFirstFewLines=0, bool doTsAssert=true)
00133     {
00134         // Usually only the master process does the checking, this can be switched off in the constructor.
00135         if (mCalledCollectively && !PetscTools::AmMaster())
00136         {
00137             return true;
00138         }
00139 
00140         std::string data1;
00141         std::string data2;
00142         unsigned failures = 0;
00143         unsigned max_display_failures = 10;
00144 
00145         SkipHeaderLines(ignoreFirstFewLines);
00146 
00147         bool files_empty = false;
00148         do
00149         {
00150             std::string buffer1;
00151             std::string buffer2;
00152             getline(*mpFile1,buffer1);
00153             getline(*mpFile2,buffer2);
00154 
00155             if (mIgnoreCommentLines)
00156             {
00157                 bool skip_this_line = false;
00158                 for (unsigned i=0; i<mCommentLineStarts.size(); i++)
00159                 {
00160                     // Check for lines starting with "#"
00161                     size_t found1 = buffer1.find(mCommentLineStarts[i]);
00162                     size_t found2 = buffer2.find(mCommentLineStarts[i]);
00163                     if (found1 == 0 && found2 == 0)
00164                     {
00165                         skip_this_line = true;
00166                     }
00167                 }
00168                 if (skip_this_line)
00169                 {
00170                     continue;
00171                 }
00172             }
00173 
00174             if (!(buffer1==buffer2) && !files_empty)
00175             {
00176                 if (failures++ < max_display_failures && !mSuppressOutput)
00177                 {
00178                     // Display error
00179                     std::stringstream message;
00180                     message << "Line " << mLineNum << " differs in files " << mFilename1 << " and " << mFilename2;
00181 
00182                     TS_TRACE(message.str());
00183                     TS_TRACE( buffer1 );
00184                     TS_TRACE( buffer2 );
00185                 }
00186             }
00187             mLineNum++;
00188         }
00189         while (mpFile1->good() && mpFile2->good());
00190         // If either is not good(), then it means that there's nothing to read from the file, or a file input error.
00191 
00192         if (doTsAssert)
00193         {
00194             // Force CxxTest error if there were any major differences
00195             TS_ASSERT_EQUALS(failures, 0u);
00196             // If that assertion tripped...
00197             if (failures > 0u && !mSuppressOutput)
00198             {
00199 #define COVERAGE_IGNORE
00200                 TS_TRACE("Files " + mFilename1 + " and " + mFilename2 + " differ.");
00201 #undef COVERAGE_IGNORE
00202             }
00203         }
00204 
00205         ResetFiles();
00206 
00207         return (failures==0);
00208     }
00209 };
00210 
00211 #endif /*FILECOMPARISON_HPP_*/