Chaste Commit::ca8ccdedf819b6e02855bc0e8e6f50bdecbc5208
AbstractFileComparison.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
36#ifndef ABSTRACTFILECOMPARISON_HPP_
37#define ABSTRACTFILECOMPARISON_HPP_
38
39#include <string>
40#include "Exception.hpp"
41#include "FileFinder.hpp"
42#include "OutputFileHandler.hpp"
43#include "PetscTools.hpp"
44
45#include <cxxtest/TestSuite.h>
46
51{
52public:
53
64 const FileFinder& rFileFinder2,
65 bool calledCollectively,
66 bool suppressOutput):
67 mFilename1(rFileFinder1.GetAbsolutePath()),
68 mFilename2(rFileFinder2.GetAbsolutePath()),
69 mCalledCollectively(calledCollectively),
70 mSuppressOutput(suppressOutput)
71 {
72 Setup();
73 }
74
84 AbstractFileComparison(std::string fileName1,
85 std::string fileName2,
86 bool calledCollectively,
87 bool suppressOutput):
88 mFilename1(fileName1),
89 mFilename2(fileName2),
90 mCalledCollectively(calledCollectively),
91 mSuppressOutput(suppressOutput)
92 {
93 Setup();
94 }
95
100 {
101 if (mpFile1)
102 {
103 mpFile1->close();
104 delete mpFile1;
105 }
106 if (mpFile2)
107 {
108 mpFile2->close();
109 delete mpFile2;
110 }
111 }
112
113protected:
114
115 std::string mFilename1;
116 std::string mFilename2;
118 std::ifstream* mpFile1;
119 std::ifstream* mpFile2;
121 unsigned mLineNum;
127
132 {
134 {
135 // We want to reset the files to allow this method to be called again, with different tolerances for instance.
136 mpFile1->close();
137 mpFile2->close();
138 mpFile1->open(mFilename1.c_str());
139 mpFile2->open(mFilename2.c_str());
140 mLineNum = 1u;
141 }
142 }
143
148 void SkipHeaderLines(unsigned numLinesToSkip)
149 {
151 {
152 for (unsigned line_number=0; line_number<numLinesToSkip; line_number++)
153 {
154 char buffer[1024];
155 mpFile1->getline(buffer, 1024);
156 mpFile2->getline(buffer, 1024);
157 TS_ASSERT(!mpFile1->fail()); // Here we assume there are at least "ignoreFirstFewLines" lines...
158 TS_ASSERT(!mpFile2->fail()); // ...and that they are lines of no more than 1024 characters
159 mLineNum++;
160 }
161 }
162 }
163
164private:
168 void Setup()
169 {
171 {
172 PetscTools::Barrier("AbstractFileComparison::Setup");
173 }
175 {
176 mpFile1 = new std::ifstream(mFilename1.c_str());
177
178 // If it doesn't exist - throw exception
179 if (!mpFile1->is_open())
180 {
181 delete mpFile1;
182 mpFile1 = NULL;
183 EXCEPTION("Couldn't open file: " + mFilename1);
184 }
185
186 mpFile2 = new std::ifstream(mFilename2.c_str());
187
188 // If it doesn't exist - throw exception
189 if (!mpFile2->is_open())
190 {
191 mpFile1->close();
192 delete mpFile1;
193 mpFile1 = NULL;
194 delete mpFile2;
195 mpFile2 = NULL;
196 EXCEPTION("Couldn't open file: " + mFilename2);
197 }
198
199 mLineNum = 1u;
200 }
201 else
202 {
203 mpFile1 = NULL;
204 mpFile2 = NULL;
205 }
206 }
207};
208
209#endif // ABSTRACTFILECOMPARISON_HPP_
#define EXCEPTION(message)
AbstractFileComparison(const FileFinder &rFileFinder1, const FileFinder &rFileFinder2, bool calledCollectively, bool suppressOutput)
void SkipHeaderLines(unsigned numLinesToSkip)
AbstractFileComparison(std::string fileName1, std::string fileName2, bool calledCollectively, bool suppressOutput)
static bool AmMaster()
static void Barrier(const std::string callerId="")