Chaste  Release::2018.1
NumericFileComparison.hpp
1 /*
2 
3 Copyright (c) 2005-2018, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 #ifndef NUMERICFILECOMPARISON_HPP_
36 #define NUMERICFILECOMPARISON_HPP_
37 
38 #include <cfloat>
39 
40 #include "AbstractFileComparison.hpp"
41 #include "MathsCustomFunctions.hpp"
42 
43 #define A_WORD DBL_MAX
44 #define NOTHING_TO_READ DBL_MIN
45 
53 {
54 private:
65  void ReadNextToken(std::ifstream* pFile, double& rData)
66  {
67  if (!(*pFile>>rData))
68  {
69  // Cannot read the next token from file as a number, so try a word instead
70  std::string word;
71  pFile->clear(); // reset the "failbit"
72  if (*pFile >> word)
73  {
74  rData = A_WORD;
75  if (word == "#" || word == "!")
76  {
77  // Ignore comment (up to 1024 characters until newline)
78  pFile->ignore(1024, '\n');
79  }
80  }
81  else
82  {
83  pFile->clear(); // reset the "failbit"
84  rData = NOTHING_TO_READ;
85  }
86  }
87  }
88 
89 public:
90 
100  NumericFileComparison(std::string fileName1, std::string fileName2, bool calledCollectively=true, bool suppressOutput = false)
101  : AbstractFileComparison(fileName1, fileName2, calledCollectively, suppressOutput)
102  {
103  }
104 
114  NumericFileComparison(const FileFinder& rFileName1, const FileFinder& rFileName2, bool calledCollectively=true, bool suppressOutput = false)
115  : AbstractFileComparison(rFileName1, rFileName2, calledCollectively, suppressOutput)
116  {
117  }
118 
119 
131  bool CompareFiles(double absTol=DBL_EPSILON, unsigned ignoreFirstFewLines=0,
132  double relTol=DBL_EPSILON, bool doTsAssert=true)
133  {
134 
135  // Usually only the master process does the checking, this can be switched off in the constructor.
137  {
138  return true;
139  }
140 
141  double data1;
142  double data2;
143  unsigned failures = 0;
144  unsigned max_display_failures = 10;
145 
146  SkipHeaderLines(ignoreFirstFewLines);
147 
148  do
149  {
150  ReadNextToken(mpFile1, data1);
151  ReadNextToken(mpFile2, data2);
152  bool ok = CompareDoubles::WithinAnyTolerance(data1, data2, relTol, absTol);
153  if (!ok)
154  {
155  if (failures++ < max_display_failures && !mSuppressOutput)
156  {
157  // Display error
158  CompareDoubles::WithinAnyTolerance(data1, data2, relTol, absTol, true);
159  }
160  }
161  }
162  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
163 
164  if (doTsAssert)
165  {
166  // Force CxxTest error if there were any major differences
167  TS_ASSERT_EQUALS(failures, 0u);
168  // If that assertion tripped...
169  if (failures > 0u && !mSuppressOutput)
170  {
171  // Report the paths to the files
172  TS_TRACE("Files " + mFilename1 + " and " + mFilename2 + " numerically differ.");
173  }
174  }
175 
176  ResetFiles();
177 
178  return (failures==0);
179  }
180 };
181 
182 #endif /*NUMERICFILECOMPARISON_HPP_*/
static bool WithinAnyTolerance(double number1, double number2, double relTol=DBL_EPSILON, double absTol=DBL_EPSILON, bool printError=false)
void SkipHeaderLines(unsigned numLinesToSkip)
static bool AmMaster()
Definition: PetscTools.cpp:120
NumericFileComparison(const FileFinder &rFileName1, const FileFinder &rFileName2, bool calledCollectively=true, bool suppressOutput=false)
NumericFileComparison(std::string fileName1, std::string fileName2, bool calledCollectively=true, bool suppressOutput=false)
void ReadNextToken(std::ifstream *pFile, double &rData)
bool CompareFiles(double absTol=DBL_EPSILON, unsigned ignoreFirstFewLines=0, double relTol=DBL_EPSILON, bool doTsAssert=true)