OpenMS
Loading...
Searching...
No Matches
FuzzyStringComparator.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Timo Sachsenberg $
6// $Authors: Clemens Groepl, Stephan Aiche $
7// --------------------------------------------------------------------------
8
9#pragma once
10
11
12#include <fstream>
13#include <istream>
14#include <map>
15#include <ostream>
16#include <sstream>
17#include <string>
18#include <utility>
19#include <vector>
20
21namespace OpenMS
22{
23 namespace Internal
24 {
25 namespace ClassTest
26 {
27 void testStringSimilar(const char * file,
28 int line,
29 const std::string & string_1,
30 const char * string_1_stringified,
31 const std::string & string_2,
32 const char * string_2_stringified);
33
34 bool isFileSimilar(const std::string &,
35 const std::string &);
36 }
37 }
38
43 {
44
45 friend void
47 const char * file,
48 int line,
49 const std::string & string_1,
50 const char * string_1_stringified,
51 const std::string & string_2,
52 const char * string_2_stringified);
53
54 friend bool
56 const std::string &);
57
60 {
61 };
62
63public:
64
66
67
70
72 virtual
74
77
80
82
84 const double & getAcceptableRelative() const;
85
87 void setAcceptableRelative(const double rhs);
88
90 const double & getAcceptableAbsolute() const;
91
93 void setAcceptableAbsolute(const double rhs);
94
96 const std::vector<std::string> & getWhitelist() const;
97
99 std::vector<std::string> & getWhitelist();
100
102 void setWhitelist(const std::vector<std::string> & rhs);
103
105 void setMatchedWhitelist(const std::vector< std::pair<std::string, std::string> >& rhs);
106
108 const std::vector< std::pair<std::string, std::string> >& getMatchedWhitelist() const;
109
118 const int & getVerboseLevel() const;
119
128 void setVerboseLevel(const int rhs);
129
133 const int & getTabWidth() const;
134
138 void setTabWidth(const int rhs);
139
143 const int & getFirstColumn() const;
144
148 void setFirstColumn(const int rhs);
149
156 std::ostream & getLogDestination() const;
157
168 void setLogDestination(std::ostream & rhs);
169
177 bool compareStrings(std::string const & lhs, std::string const & rhs);
178
187 bool compareStreams(std::istream & input_1, std::istream & input_2);
188
203 bool compareFiles(const std::string & filename_1,
204 const std::string & filename_2);
205
206protected:
207
216 bool compareLines_(std::string const & line_str_1, std::string const & line_str_2);
217
219 void reportSuccess_() const;
220
223 void reportFailure_(char const * const message) const;
224
226 void writeWhitelistCases_(const std::string & prefix) const;
227
230 void readNextLine_(std::istream & input_stream, std::string & line_string, int & line_number) const;
231
233 bool openInputFileStream_(const std::string & filename, std::ifstream & input_stream) const;
234
236 std::ostream * log_dest_;
237
239 std::string input_1_name_;
241 std::string input_2_name_;
242
245 {
246 std::stringstream line_;
247 std::ios::pos_type line_position_;
248
250
252 void setToString(const std::string & s);
253
256
259
267 bool ok() const;
268 };
269
272
275
278
279 std::string line_str_1_max_;
280 std::string line_str_2_max_;
281
284
287
290
293
296 {
297 double number;
298 unsigned char letter;
301
303
305 void reset();
306
310 void fillFromInputLine(InputLine& input_line, const std::string& str_line);
311 };
312
317
320 {
321 std::string prefix;
324
325 PrefixInfo_(const InputLine & input_line, const int tab_width_, const int first_column_);
326 };
327
329
333
339
342
344 std::vector<std::string> whitelist_;
346 std::map<std::string, unsigned int> whitelist_cases_;
347
349 std::vector< std::pair<std::string, std::string> > matched_whitelist_;
350 }; // class FuzzyStringComparator
351
352} //namespace OpenMS
353
Fuzzy comparison of strings, tolerates numeric differences.
Definition FuzzyStringComparator.h:43
double ratio_max_allowed_
Maximum ratio of numbers allowed, see ratio_max_.
Definition FuzzyStringComparator.h:283
InputLine input_line_2_
Definition FuzzyStringComparator.h:271
FuzzyStringComparator(const FuzzyStringComparator &rhs)
Copy constructor intentionally not implemented.
const int & getFirstColumn() const
get first column (for column numbers)
const std::vector< std::string > & getWhitelist() const
White list. If both lines contain the same element from this list, they are skipped over.
int line_num_1_max_
Definition FuzzyStringComparator.h:276
bool openInputFileStream_(const std::string &filename, std::ifstream &input_stream) const
opens and checks an input file stream std::ifstream
const int & getTabWidth() const
get tab width (for column numbers)
void reportSuccess_() const
Report good news.
void setFirstColumn(const int rhs)
set first column (for column numbers)
void setAcceptableRelative(const double rhs)
Acceptable relative error (a number >= 1.0)
std::string input_1_name_
Name of first input e.g., filename.
Definition FuzzyStringComparator.h:239
int tab_width_
Definition FuzzyStringComparator.h:331
void setWhitelist(const std::vector< std::string > &rhs)
White list. If both lines contain the same element from this list, they are skipped over.
void setTabWidth(const int rhs)
set tab width (for column numbers)
void writeWhitelistCases_(const std::string &prefix) const
Write info about hits in the whitelist.
void setMatchedWhitelist(const std::vector< std::pair< std::string, std::string > > &rhs)
Matched white list. If file 1 contains element 1 and file 2 contains element 2, they are skipped over...
std::string line_str_1_max_
Definition FuzzyStringComparator.h:279
std::ostream & getLogDestination() const
Log output is written to this destination.
int verbose_level_
Definition FuzzyStringComparator.h:330
StreamElement_ element_1_
Stores information about characters, numbers, and white spaces loaded from the first input stream.
Definition FuzzyStringComparator.h:314
bool compareFiles(const std::string &filename_1, const std::string &filename_2)
Simple diff-like application to compare two input files. Numeric differences are tolerated up to a ce...
const double & getAcceptableAbsolute() const
Acceptable absolute difference (a number >= 0.0)
int line_num_2_
Definition FuzzyStringComparator.h:274
bool compareStrings(std::string const &lhs, std::string const &rhs)
Compare two strings.
std::vector< std::pair< std::string, std::string > > matched_whitelist_
Alternative Whitelist.
Definition FuzzyStringComparator.h:349
FuzzyStringComparator()
Constructor.
void setLogDestination(std::ostream &rhs)
Log output is written to this destination.
FuzzyStringComparator & operator=(const FuzzyStringComparator &rhs)
Assignment operator intentionally not implemented.
void reportFailure_(char const *const message) const
void setVerboseLevel(const int rhs)
verbose level
bool is_status_success_
Has comparison been successful so far? Note: this flag is changed in reportFailure_();.
Definition FuzzyStringComparator.h:338
double absdiff_max_allowed_
Maximum absolute difference of numbers allowed, see absdiff_max_.
Definition FuzzyStringComparator.h:289
StreamElement_ element_2_
Stores information about characters, numbers, and white spaces loaded from the second input stream.
Definition FuzzyStringComparator.h:316
double ratio_max_
Maximum ratio of numbers observed so far, see ratio_max_allowed_.
Definition FuzzyStringComparator.h:286
std::string line_str_2_max_
Definition FuzzyStringComparator.h:280
double absdiff_max_
Maximum difference of numbers observed so far, see absdiff_max_allowed_.
Definition FuzzyStringComparator.h:292
virtual ~FuzzyStringComparator()
Destructor.
std::vector< std::string > whitelist_
Whitelist.
Definition FuzzyStringComparator.h:344
std::vector< std::string > & getWhitelist()
White list. If both lines contain the same element from this list, they are skipped over.
const int & getVerboseLevel() const
verbose level
int first_column_
Definition FuzzyStringComparator.h:332
int line_num_1_
Definition FuzzyStringComparator.h:273
bool compareLines_(std::string const &line_str_1, std::string const &line_str_2)
Compare two lines of input.
std::ostream * log_dest_
Log and results output goes here.
Definition FuzzyStringComparator.h:236
void setAcceptableAbsolute(const double rhs)
Acceptable absolute difference (a number >= 0.0)
const double & getAcceptableRelative() const
Acceptable relative error (a number >= 1.0)
bool compareStreams(std::istream &input_1, std::istream &input_2)
Compare two streams of input.
bool is_absdiff_small_
Definition FuzzyStringComparator.h:328
const std::vector< std::pair< std::string, std::string > > & getMatchedWhitelist() const
Matched white list. If file 1 contains element 1 and file 2 contains element 2, they are skipped over...
std::string input_2_name_
Name of second input e.g., filename.
Definition FuzzyStringComparator.h:241
InputLine input_line_1_
Definition FuzzyStringComparator.h:270
bool use_prefix_
use a prefix when reporting
Definition FuzzyStringComparator.h:341
int line_num_2_max_
Definition FuzzyStringComparator.h:277
void readNextLine_(std::istream &input_stream, std::string &line_string, int &line_number) const
std::map< std::string, unsigned int > whitelist_cases_
Occurrences of whitelist entries.
Definition FuzzyStringComparator.h:346
Internal exception class.
Definition FuzzyStringComparator.h:60
void testStringSimilar(const char *file, int line, const std::string &string_1, const char *string_1_stringified, const std::string &string_2, const char *string_2_stringified)
Compare strings using absdiff_max_allowed and ratio_max_allowed.
bool isFileSimilar(const std::string &filename_1, const std::string &filename_2)
Compare files using absdiff_max_allowed and ratio_max_allowed.
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Stores information about the current input line (i.e., stream for the line and the current position i...
Definition FuzzyStringComparator.h:245
bool ok() const
Convert to bool.
void updatePosition()
Save current position of the stream.
void seekGToSavedPosition()
Resets the stream to the last saved position.
void setToString(const std::string &s)
Initialize the input line to the passed string.
std::stringstream line_
Definition FuzzyStringComparator.h:246
std::ios::pos_type line_position_
Definition FuzzyStringComparator.h:247
Wrapper for the prefix information computed for the failure report.
Definition FuzzyStringComparator.h:320
std::string prefix_whitespaces
Definition FuzzyStringComparator.h:322
std::string prefix
Definition FuzzyStringComparator.h:321
int line_column
Definition FuzzyStringComparator.h:323
PrefixInfo_(const InputLine &input_line, const int tab_width_, const int first_column_)
Stores information about characters, numbers, and white spaces loaded from the InputStream.
Definition FuzzyStringComparator.h:296
double number
Definition FuzzyStringComparator.h:297
bool is_number
Definition FuzzyStringComparator.h:299
void fillFromInputLine(InputLine &input_line, const std::string &str_line)
bool is_space
Definition FuzzyStringComparator.h:300
void reset()
reset all elements of the element to default value
unsigned char letter
Definition FuzzyStringComparator.h:298