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#include <OpenMS/CONCEPT/Types.h>
13
14
15#include <map>
16#include <sstream>
17
18namespace OpenMS
19{
20 namespace Internal
21 {
22 namespace ClassTest
23 {
24 void OPENMS_DLLAPI testStringSimilar(const char * file,
25 int line,
26 const std::string & string_1,
27 const char * string_1_stringified,
28 const std::string & string_2,
29 const char * string_2_stringified);
30
31 bool OPENMS_DLLAPI isFileSimilar(const std::string &,
32 const std::string &);
33 }
34 }
35
39 class OPENMS_DLLAPI FuzzyStringComparator
40 {
41
42 friend void OPENMS_DLLAPI
43 Internal::ClassTest::testStringSimilar(
44 const char * file,
45 int line,
46 const std::string & string_1,
47 const char * string_1_stringified,
48 const std::string & string_2,
49 const char * string_2_stringified);
50
51 friend bool OPENMS_DLLAPI
52 Internal::ClassTest::isFileSimilar(const std::string &,
53 const std::string &);
54
57 {
58 };
59
60public:
61
63
64
67
69 virtual
71
74
77
79
81 const double & getAcceptableRelative() const;
82
84 void setAcceptableRelative(const double rhs);
85
87 const double & getAcceptableAbsolute() const;
88
90 void setAcceptableAbsolute(const double rhs);
91
93 const StringList & getWhitelist() const;
94
97
99 void setWhitelist(const StringList & rhs);
100
102 void setMatchedWhitelist(const std::vector< std::pair<std::string, std::string> >& rhs);
103
105 const std::vector< std::pair<std::string, std::string> >& getMatchedWhitelist() const;
106
115 const int & getVerboseLevel() const;
116
125 void setVerboseLevel(const int rhs);
126
130 const int & getTabWidth() const;
131
135 void setTabWidth(const int rhs);
136
140 const int & getFirstColumn() const;
141
145 void setFirstColumn(const int rhs);
146
153 std::ostream & getLogDestination() const;
154
165 void setLogDestination(std::ostream & rhs);
166
174 bool compareStrings(std::string const & lhs, std::string const & rhs);
175
184 bool compareStreams(std::istream & input_1, std::istream & input_2);
185
200 bool compareFiles(const std::string & filename_1,
201 const std::string & filename_2);
202
203protected:
204
213 bool compareLines_(std::string const & line_str_1, std::string const & line_str_2);
214
216 void reportSuccess_() const;
217
220 void reportFailure_(char const * const message) const;
221
223 void writeWhitelistCases_(const std::string & prefix) const;
224
227 void readNextLine_(std::istream & input_stream, std::string & line_string, int & line_number) const;
228
230 bool openInputFileStream_(const std::string & filename, std::ifstream & input_stream) const;
231
233 std::ostream * log_dest_;
234
236 std::string input_1_name_;
238 std::string input_2_name_;
239
242 {
243 std::stringstream line_;
244 std::ios::pos_type line_position_;
245
247
249 void setToString(const std::string & s);
250
253
256
264 bool ok() const;
265 };
266
269
272
275
276 std::string line_str_1_max_;
277 std::string line_str_2_max_;
278
281
284
287
290
293 {
294 double number;
295 unsigned char letter;
298
300
302 void reset();
303
307 void fillFromInputLine(InputLine& input_line, const std::string& str_line);
308 };
309
314
317 {
318 std::string prefix;
321
322 PrefixInfo_(const InputLine & input_line, const int tab_width_, const int first_column_);
323 };
324
326
330
336
339
343 std::map<std::string, UInt> whitelist_cases_;
344
346 std::vector< std::pair<std::string, std::string> > matched_whitelist_;
347 }; // class FuzzyStringComparator
348
349} //namespace OpenMS
350
Fuzzy comparison of strings, tolerates numeric differences.
Definition FuzzyStringComparator.h:40
double ratio_max_allowed_
Maximum ratio of numbers allowed, see ratio_max_.
Definition FuzzyStringComparator.h:280
InputLine input_line_2_
Definition FuzzyStringComparator.h:268
FuzzyStringComparator(const FuzzyStringComparator &rhs)
Copy constructor intentionally not implemented.
const int & getFirstColumn() const
get first column (for column numbers)
int line_num_1_max_
Definition FuzzyStringComparator.h:273
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 setWhitelist(const StringList &rhs)
White list. If both lines contain the same element from this list, they are skipped over.
std::map< std::string, UInt > whitelist_cases_
Occurrences of whitelist entries.
Definition FuzzyStringComparator.h:343
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:236
int tab_width_
Definition FuzzyStringComparator.h:328
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:276
std::ostream & getLogDestination() const
Log output is written to this destination.
int verbose_level_
Definition FuzzyStringComparator.h:327
StreamElement_ element_1_
Stores information about characters, numbers, and white spaces loaded from the first input stream.
Definition FuzzyStringComparator.h:311
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)
StringList & getWhitelist()
White list. If both lines contain the same element from this list, they are skipped over.
int line_num_2_
Definition FuzzyStringComparator.h:271
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:346
const StringList & getWhitelist() const
White list. If both lines contain the same element from this list, they are skipped over.
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:335
double absdiff_max_allowed_
Maximum absolute difference of numbers allowed, see absdiff_max_.
Definition FuzzyStringComparator.h:286
StreamElement_ element_2_
Stores information about characters, numbers, and white spaces loaded from the second input stream.
Definition FuzzyStringComparator.h:313
double ratio_max_
Maximum ratio of numbers observed so far, see ratio_max_allowed_.
Definition FuzzyStringComparator.h:283
std::string line_str_2_max_
Definition FuzzyStringComparator.h:277
double absdiff_max_
Maximum difference of numbers observed so far, see absdiff_max_allowed_.
Definition FuzzyStringComparator.h:289
virtual ~FuzzyStringComparator()
Destructor.
StringList whitelist_
Whitelist.
Definition FuzzyStringComparator.h:341
const int & getVerboseLevel() const
verbose level
int first_column_
Definition FuzzyStringComparator.h:329
int line_num_1_
Definition FuzzyStringComparator.h:270
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:233
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:325
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:238
InputLine input_line_1_
Definition FuzzyStringComparator.h:267
bool use_prefix_
use a prefix when reporting
Definition FuzzyStringComparator.h:338
int line_num_2_max_
Definition FuzzyStringComparator.h:274
void readNextLine_(std::istream &input_stream, std::string &line_string, int &line_number) const
Internal exception class.
Definition FuzzyStringComparator.h:57
std::vector< std::string > StringList
Vector of String.
Definition TypeAliases.h:39
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:242
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:243
std::ios::pos_type line_position_
Definition FuzzyStringComparator.h:244
Wrapper for the prefix information computed for the failure report.
Definition FuzzyStringComparator.h:317
std::string prefix_whitespaces
Definition FuzzyStringComparator.h:319
std::string prefix
Definition FuzzyStringComparator.h:318
int line_column
Definition FuzzyStringComparator.h:320
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:293
double number
Definition FuzzyStringComparator.h:294
bool is_number
Definition FuzzyStringComparator.h:296
void fillFromInputLine(InputLine &input_line, const std::string &str_line)
bool is_space
Definition FuzzyStringComparator.h:297
void reset()
reset all elements of the element to default value
unsigned char letter
Definition FuzzyStringComparator.h:295