OpenMS
FuzzyStringComparator.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, The OpenMS Team -- 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>
14 
15 
16 #include <map>
17 #include <sstream>
18 
19 namespace OpenMS
20 {
21  namespace Internal
22  {
23  namespace ClassTest
24  {
25  void OPENMS_DLLAPI testStringSimilar(const char * file,
26  int line,
27  const std::string & string_1,
28  const char * string_1_stringified,
29  const std::string & string_2,
30  const char * string_2_stringified);
31 
32  bool OPENMS_DLLAPI isFileSimilar(const std::string &,
33  const std::string &);
34  }
35  }
36 
40  class OPENMS_DLLAPI FuzzyStringComparator
41  {
42 
43  friend void OPENMS_DLLAPI
45  const char * file,
46  int line,
47  const std::string & string_1,
48  const char * string_1_stringified,
49  const std::string & string_2,
50  const char * string_2_stringified);
51 
52  friend bool OPENMS_DLLAPI
54  const std::string &);
55 
58  {
59  };
60 
61 public:
62 
64 
65 
68 
70  virtual
72 
75 
78 
80 
82  const double & getAcceptableRelative() const;
83 
85  void setAcceptableRelative(const double rhs);
86 
88  const double & getAcceptableAbsolute() const;
89 
91  void setAcceptableAbsolute(const double rhs);
92 
94  const StringList & getWhitelist() const;
95 
98 
100  void setWhitelist(const StringList & rhs);
101 
103  void setMatchedWhitelist(const std::vector< std::pair<std::string, std::string> >& rhs);
104 
106  const std::vector< std::pair<std::string, std::string> >& getMatchedWhitelist() const;
107 
116  const int & getVerboseLevel() const;
117 
126  void setVerboseLevel(const int rhs);
127 
131  const int & getTabWidth() const;
132 
136  void setTabWidth(const int rhs);
137 
141  const int & getFirstColumn() const;
142 
146  void setFirstColumn(const int rhs);
147 
154  std::ostream & getLogDestination() const;
155 
166  void setLogDestination(std::ostream & rhs);
167 
175  bool compareStrings(std::string const & lhs, std::string const & rhs);
176 
185  bool compareStreams(std::istream & input_1, std::istream & input_2);
186 
201  bool compareFiles(const std::string & filename_1,
202  const std::string & filename_2);
203 
204 protected:
205 
214  bool compareLines_(std::string const & line_str_1, std::string const & line_str_2);
215 
217  void reportSuccess_() const;
218 
221  void reportFailure_(char const * const message) const;
222 
224  void writeWhitelistCases_(const std::string & prefix) const;
225 
228  void readNextLine_(std::istream & input_stream, std::string & line_string, int & line_number) const;
229 
231  bool openInputFileStream_(const std::string & filename, std::ifstream & input_stream) const;
232 
234  std::ostream * log_dest_;
235 
237  std::string input_1_name_;
239  std::string input_2_name_;
240 
242  struct InputLine
243  {
244  std::stringstream line_;
245  std::ios::pos_type line_position_;
246 
248 
250  void setToString(const std::string & s);
251 
254 
257 
265  bool ok() const;
266  };
267 
270 
273 
276 
277  std::string line_str_1_max_;
278  std::string line_str_2_max_;
279 
282 
284  double ratio_max_;
285 
288 
290  double absdiff_max_;
291 
294  {
295  double number;
296  unsigned char letter;
297  bool is_number;
298  bool is_space;
299 
301 
303  void reset();
304 
308  void fillFromInputLine(InputLine& input_line, const std::string& str_line);
309  };
310 
315 
317  struct PrefixInfo_
318  {
322 
323  PrefixInfo_(const InputLine & input_line, const int tab_width_, const int first_column_);
324  };
325 
327 
331 
337 
340 
344  std::map<String, UInt> whitelist_cases_;
345 
347  std::vector< std::pair<std::string, std::string> > matched_whitelist_;
348  }; // class FuzzyStringComparator
349 
350 } //namespace OpenMS
351 
Fuzzy comparison of strings, tolerates numeric differences.
Definition: FuzzyStringComparator.h:41
double ratio_max_allowed_
Maximum ratio of numbers allowed, see ratio_max_.
Definition: FuzzyStringComparator.h:281
InputLine input_line_2_
Definition: FuzzyStringComparator.h:269
FuzzyStringComparator(const FuzzyStringComparator &rhs)
Copy constructor intentionally not implemented.
int line_num_1_max_
Definition: FuzzyStringComparator.h:274
bool openInputFileStream_(const std::string &filename, std::ifstream &input_stream) const
opens and checks an input file stream std::ifstream
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.
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:237
int tab_width_
Definition: FuzzyStringComparator.h:329
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...
const StringList & getWhitelist() const
White list. If both lines contain the same element from this list, they are skipped over.
std::string line_str_1_max_
Definition: FuzzyStringComparator.h:277
int verbose_level_
Definition: FuzzyStringComparator.h:328
const int & getFirstColumn() const
get first column (for column numbers)
StreamElement_ element_1_
Stores information about characters, numbers, and white spaces loaded from the first input stream.
Definition: FuzzyStringComparator.h:312
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...
int line_num_2_
Definition: FuzzyStringComparator.h:272
bool compareStrings(std::string const &lhs, std::string const &rhs)
Compare two strings.
const int & getTabWidth() const
get tab width (for column numbers)
std::vector< std::pair< std::string, std::string > > matched_whitelist_
Alternative Whitelist.
Definition: FuzzyStringComparator.h:347
FuzzyStringComparator()
Constructor.
void setLogDestination(std::ostream &rhs)
Log output is written to this destination.
StringList & getWhitelist()
White list. If both lines contain the same element from this list, they are skipped over.
void reportFailure_(char const *const message) const
void setVerboseLevel(const int rhs)
verbose level
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...
bool is_status_success_
Has comparison been successful so far? Note: this flag is changed in reportFailure_();.
Definition: FuzzyStringComparator.h:336
double absdiff_max_allowed_
Maximum absolute difference of numbers allowed, see absdiff_max_.
Definition: FuzzyStringComparator.h:287
const double & getAcceptableAbsolute() const
Acceptable absolute difference (a number >= 0.0)
StreamElement_ element_2_
Stores information about characters, numbers, and white spaces loaded from the second input stream.
Definition: FuzzyStringComparator.h:314
FuzzyStringComparator & operator=(const FuzzyStringComparator &rhs)
Assignment operator intentionally not implemented.
double ratio_max_
Maximum ratio of numbers observed so far, see ratio_max_allowed_.
Definition: FuzzyStringComparator.h:284
std::string line_str_2_max_
Definition: FuzzyStringComparator.h:278
double absdiff_max_
Maximum difference of numbers observed so far, see absdiff_max_allowed_.
Definition: FuzzyStringComparator.h:290
const int & getVerboseLevel() const
verbose level
virtual ~FuzzyStringComparator()
Destructor.
StringList whitelist_
Whitelist.
Definition: FuzzyStringComparator.h:342
int first_column_
Definition: FuzzyStringComparator.h:330
int line_num_1_
Definition: FuzzyStringComparator.h:271
bool compareLines_(std::string const &line_str_1, std::string const &line_str_2)
Compare two lines of input.
std::ostream & getLogDestination() const
Log output is written to this destination.
std::ostream * log_dest_
Log and results output goes here.
Definition: FuzzyStringComparator.h:234
std::map< String, UInt > whitelist_cases_
Occurrences of whitelist entries.
Definition: FuzzyStringComparator.h:344
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:326
std::string input_2_name_
Name of second input e.g., filename.
Definition: FuzzyStringComparator.h:239
InputLine input_line_1_
Definition: FuzzyStringComparator.h:268
bool use_prefix_
use a prefix when reporting
Definition: FuzzyStringComparator.h:339
int line_num_2_max_
Definition: FuzzyStringComparator.h:275
void readNextLine_(std::istream &input_stream, std::string &line_string, int &line_number) const
Internal exception class.
Definition: FuzzyStringComparator.h:58
A more convenient string class.
Definition: String.h:34
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:44
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.
static String prefix(const String &this_s, size_t length)
Definition: StringUtilsSimple.h:122
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:243
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:244
std::ios::pos_type line_position_
Definition: FuzzyStringComparator.h:245
Wrapper for the prefix information computed for the failure report.
Definition: FuzzyStringComparator.h:318
OpenMS::String prefix_whitespaces
Definition: FuzzyStringComparator.h:320
int line_column
Definition: FuzzyStringComparator.h:321
OpenMS::String prefix
Definition: FuzzyStringComparator.h:319
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:294
double number
Definition: FuzzyStringComparator.h:295
bool is_number
Definition: FuzzyStringComparator.h:297
void fillFromInputLine(InputLine &input_line, const std::string &str_line)
bool is_space
Definition: FuzzyStringComparator.h:298
void reset()
reset all elements of the element to default value
unsigned char letter
Definition: FuzzyStringComparator.h:296