Chaste Commit::ca8ccdedf819b6e02855bc0e8e6f50bdecbc5208
NumericFileComparison.hpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, 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
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF 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"
42
43#include <cxxtest/TestSuite.h>
44
45#define A_WORD DBL_MAX
46#define NOTHING_TO_READ DBL_MIN
47
55{
56private:
67 void ReadNextToken(std::ifstream* pFile, double& rData)
68 {
69 if (!(*pFile>>rData))
70 {
71 // Cannot read the next token from file as a number, so try a word instead
72 std::string word;
73 pFile->clear(); // reset the "failbit"
74 if (*pFile >> word)
75 {
76 rData = A_WORD;
77 if (word == "#" || word == "!")
78 {
79 // Ignore comment (up to 1024 characters until newline)
80 pFile->ignore(1024, '\n');
81 }
82 }
83 else
84 {
85 pFile->clear(); // reset the "failbit"
86 rData = NOTHING_TO_READ;
87 }
88 }
89 }
90
91public:
92
102 NumericFileComparison(std::string fileName1, std::string fileName2, bool calledCollectively=true, bool suppressOutput = false)
103 : AbstractFileComparison(fileName1, fileName2, calledCollectively, suppressOutput)
104 {
105 }
106
116 NumericFileComparison(const FileFinder& rFileName1, const FileFinder& rFileName2, bool calledCollectively=true, bool suppressOutput = false)
117 : AbstractFileComparison(rFileName1, rFileName2, calledCollectively, suppressOutput)
118 {
119 }
120
121
133 bool CompareFiles(double absTol=DBL_EPSILON, unsigned ignoreFirstFewLines=0,
134 double relTol=DBL_EPSILON, bool doTsAssert=true)
135 {
136
137 // Usually only the master process does the checking, this can be switched off in the constructor.
139 {
140 return true;
141 }
142
143 double data1;
144 double data2;
145 unsigned failures = 0;
146 unsigned max_display_failures = 10;
147
148 SkipHeaderLines(ignoreFirstFewLines);
149
150 do
151 {
152 ReadNextToken(mpFile1, data1);
153 ReadNextToken(mpFile2, data2);
154 bool ok = CompareDoubles::WithinAnyTolerance(data1, data2, relTol, absTol);
155 if (!ok)
156 {
157 if (failures++ < max_display_failures && !mSuppressOutput)
158 {
159 // Display error
160 CompareDoubles::WithinAnyTolerance(data1, data2, relTol, absTol, true);
161 }
162 }
163 }
164 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
165
166 if (doTsAssert)
167 {
168 // Force CxxTest error if there were any major differences
169 TS_ASSERT_EQUALS(failures, 0u);
170 // If that assertion tripped...
171 if (failures > 0u && !mSuppressOutput)
172 {
173 // Report the paths to the files
174 TS_TRACE("Files " + mFilename1 + " and " + mFilename2 + " numerically differ.");
175 }
176 }
177
178 ResetFiles();
179
180 return (failures==0);
181 }
182};
183
184#endif /*NUMERICFILECOMPARISON_HPP_*/
void SkipHeaderLines(unsigned numLinesToSkip)
static bool WithinAnyTolerance(double number1, double number2, double relTol=DBL_EPSILON, double absTol=DBL_EPSILON, bool printError=false)
void ReadNextToken(std::ifstream *pFile, double &rData)
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)
bool CompareFiles(double absTol=DBL_EPSILON, unsigned ignoreFirstFewLines=0, double relTol=DBL_EPSILON, bool doTsAssert=true)
static bool AmMaster()