OpenMS  2.4.0
FuzzyStringComparator.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2018.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg $
32 // $Authors: Clemens Groepl, Stephan Aiche $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/CONCEPT/Types.h>
40 
41 #include <iosfwd>
42 #include <map>
43 
44 #ifdef OPENMS_HAS_STREAM_EXTRACTION_BUG
45 #include <boost/lexical_cast.hpp>
46 #endif // OPENMS_HAS_STREAM_EXTRACTION_BUG
47 
48 namespace OpenMS
49 {
50  namespace Internal
51  {
52  namespace ClassTest
53  {
54  void OPENMS_DLLAPI testStringSimilar(const char * file,
55  int line,
56  const std::string & string_1,
57  const char * string_1_stringified,
58  const std::string & string_2,
59  const char * string_2_stringified);
60 
61  bool OPENMS_DLLAPI isFileSimilar(const std::string &,
62  const std::string &);
63  }
64  }
65 
69  class OPENMS_DLLAPI FuzzyStringComparator
70  {
71 
72  friend void OPENMS_DLLAPI
74  const char * file,
75  int line,
76  const std::string & string_1,
77  const char * string_1_stringified,
78  const std::string & string_2,
79  const char * string_2_stringified);
80 
81  friend bool OPENMS_DLLAPI
82  Internal::ClassTest::isFileSimilar(const std::string &,
83  const std::string &);
84 
87  {
88  };
89 
90 public:
91 
93 
94 
97 
99  virtual
101 
104 
106  FuzzyStringComparator & operator=(const FuzzyStringComparator & rhs);
107 
109 
111  const double & getAcceptableRelative() const;
112 
114  void setAcceptableRelative(const double rhs);
115 
117  const double & getAcceptableAbsolute() const;
118 
120  void setAcceptableAbsolute(const double rhs);
121 
123  const StringList & getWhitelist() const;
124 
126  StringList & getWhitelist();
127 
129  void setWhitelist(const StringList & rhs);
130 
132  void setMatchedWhitelist(const std::vector< std::pair<std::string, std::string> >& rhs);
133 
135  const std::vector< std::pair<std::string, std::string> >& getMatchedWhitelist() const;
136 
145  const int & getVerboseLevel() const;
146 
155  void setVerboseLevel(const int rhs);
156 
160  const int & getTabWidth() const;
161 
165  void setTabWidth(const int rhs);
166 
170  const int & getFirstColumn() const;
171 
175  void setFirstColumn(const int rhs);
176 
183  std::ostream & getLogDestination() const;
184 
195  void setLogDestination(std::ostream & rhs);
196 
204  bool compareStrings(std::string const & lhs, std::string const & rhs);
205 
214  bool compareStreams(std::istream & input_1, std::istream & input_2);
215 
230  bool compareFiles(const std::string & filename_1,
231  const std::string & filename_2);
232 
233 protected:
234 
243  bool compareLines_(std::string const & line_str_1, std::string const & line_str_2);
244 
246  void reportSuccess_() const;
247 
250  void reportFailure_(char const * const message) const;
251 
253  void writeWhitelistCases_(const std::string & prefix) const;
254 
257  void readNextLine_(std::istream & input_stream, std::string & line_string, int & line_number) const;
258 
260  bool openInputFileStream_(const std::string & filename, std::ifstream & input_stream) const;
261 
263  std::ostream * log_dest_;
264 
266  std::string input_1_name_;
268  std::string input_2_name_;
269 
271  struct InputLine
272  {
273  std::stringstream line_;
274  std::ios::pos_type line_position_;
275 
276  InputLine();
277 
279  void setToString(const std::string & s);
280 
282  void updatePosition();
283 
285  void seekGToSavedPosition();
286 
294  bool ok() const;
295  };
296 
299 
302 
305 
306  std::string line_str_1_max_;
307  std::string line_str_2_max_;
308 
311 
313  double ratio_max_;
314 
317 
319  double absdiff_max_;
320 
323  {
324  double number;
325  unsigned char letter;
326  bool is_number;
327  bool is_space;
328 
329  StreamElement_();
330 
332  void reset();
333 #ifdef OPENMS_HAS_STREAM_EXTRACTION_BUG
334 
343  bool readdigits(InputLine & input_line, std::string & target_buffer, char & c_buffer);
344 
352  bool tryExtractDouble(InputLine & input_line, double & target);
353 #endif
354  void fillFromInputLine(InputLine & input_line);
356  };
357 
362 
364  struct PrefixInfo_
365  {
369 
370  PrefixInfo_(const InputLine & input_line, const int tab_width_, const int first_column_);
371  };
372 
374 
378 
384 
387 
391  std::map<String, UInt> whitelist_cases_;
392 
394  std::vector< std::pair<std::string, std::string> > matched_whitelist_;
395  }; // class FuzzyStringComparator
396 
397 } //namespace OpenMS
398 
OpenMS::FuzzyStringComparator::setAcceptableRelative
void setAcceptableRelative(const double rhs)
Acceptable relative error (a number >= 1.0)
OpenMS::FuzzyStringComparator::StreamElement_
Stores information about characters, numbers, and white spaces loaded from the InputStream.
Definition: FuzzyStringComparator.h:322
OpenMS::TOPPBase
Base class for TOPP applications.
Definition: TOPPBase.h:150
OpenMS::FuzzyStringComparator::use_prefix_
bool use_prefix_
use a prefix when reporting
Definition: FuzzyStringComparator.h:386
OpenMS::FuzzyStringComparator::setWhitelist
void setWhitelist(const StringList &rhs)
White list. If both lines contain the same element from this list, they are skipped over.
OpenMS::Exception::IllegalArgument
A method or algorithm argument contains illegal values.
Definition: Exception.h:648
OpenMS::FuzzyStringComparator::StreamElement_::number
double number
Definition: FuzzyStringComparator.h:324
OpenMS::Internal::ClassTest::isFileSimilar
bool isFileSimilar(const std::string &filename_1, const std::string &filename_2)
Compare files using absdiff_max_allowed and ratio_max_allowed.
Types.h
OpenMS::FuzzyStringComparator::whitelist_
StringList whitelist_
Whitelist.
Definition: FuzzyStringComparator.h:389
OpenMS::FuzzyStringComparator::tab_width_
int tab_width_
Definition: FuzzyStringComparator.h:376
OpenMS::FuzzyStringComparator::first_column_
int first_column_
Definition: FuzzyStringComparator.h:377
OpenMS::FuzzyStringComparator::verbose_level_
int verbose_level_
Definition: FuzzyStringComparator.h:375
OpenMS::FuzzyStringComparator::input_line_2_
InputLine input_line_2_
Definition: FuzzyStringComparator.h:298
OpenMS::FuzzyStringComparator::line_num_2_
int line_num_2_
Definition: FuzzyStringComparator.h:301
OpenMS::String
A more convenient string class.
Definition: String.h:57
OpenMS::FuzzyStringComparator::ratio_max_
double ratio_max_
Maximum ratio of numbers observed so far, see ratio_max_allowed_.
Definition: FuzzyStringComparator.h:313
OpenMS::FuzzyStringComparator::line_num_1_
int line_num_1_
Definition: FuzzyStringComparator.h:300
OpenMS::FuzzyStringComparator::StreamElement_::is_number
bool is_number
Definition: FuzzyStringComparator.h:326
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
OpenMS::FuzzyStringComparator::whitelist_cases_
std::map< String, UInt > whitelist_cases_
Occurrences of whitelist entries.
Definition: FuzzyStringComparator.h:391
OpenMS::FuzzyStringComparator::input_1_name_
std::string input_1_name_
Name of first input e.g., filename.
Definition: FuzzyStringComparator.h:266
OpenMS::Internal::ClassTest::testStringSimilar
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.
OpenMS::FuzzyStringComparator::is_status_success_
bool is_status_success_
Has comparison been successful so far? Note: this flag is changed in reportFailure_();.
Definition: FuzzyStringComparator.h:383
ListUtils.h
OpenMS::FuzzyStringComparator
Fuzzy comparison of strings, tolerates numeric differences.
Definition: FuzzyStringComparator.h:69
OpenMS::FuzzyStringComparator::line_num_2_max_
int line_num_2_max_
Definition: FuzzyStringComparator.h:304
OpenMS::FuzzyStringComparator::input_2_name_
std::string input_2_name_
Name of second input e.g., filename.
Definition: FuzzyStringComparator.h:268
OpenMS::FuzzyStringComparator::PrefixInfo_
Wrapper for the prefix information computed for the failure report.
Definition: FuzzyStringComparator.h:364
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::FuzzyStringComparator::setFirstColumn
void setFirstColumn(const int rhs)
set first column (for column numbers)
OpenMS::FuzzyStringComparator::line_num_1_max_
int line_num_1_max_
Definition: FuzzyStringComparator.h:303
OpenMS::FuzzyStringComparator::InputLine::line_position_
std::ios::pos_type line_position_
Definition: FuzzyStringComparator.h:274
OpenMS::FuzzyStringComparator::line_str_1_max_
std::string line_str_1_max_
Definition: FuzzyStringComparator.h:306
OpenMS::Internal::ClassTest::setWhitelist
void setWhitelist(const char *const, const int line, const std::string &whitelist)
set the whitelist_
OpenMS::FuzzyStringComparator::absdiff_max_allowed_
double absdiff_max_allowed_
Maximum absolute difference of numbers allowed, see absdiff_max_.
Definition: FuzzyStringComparator.h:316
OpenMS::FuzzyStringComparator::PrefixInfo_::prefix
OpenMS::String prefix
Definition: FuzzyStringComparator.h:366
OpenMS::FuzzyStringComparator::element_2_
StreamElement_ element_2_
Stores information about characters, numbers, and white spaces loaded from the second input stream.
Definition: FuzzyStringComparator.h:361
OpenMS::FuzzyStringComparator::InputLine::line_
std::stringstream line_
Definition: FuzzyStringComparator.h:273
OpenMS::FuzzyStringComparator::setTabWidth
void setTabWidth(const int rhs)
set tab width (for column numbers)
OpenMS::FuzzyStringComparator::StreamElement_::letter
unsigned char letter
Definition: FuzzyStringComparator.h:325
OpenMS::FuzzyStringComparator::ratio_max_allowed_
double ratio_max_allowed_
Maximum ratio of numbers allowed, see ratio_max_.
Definition: FuzzyStringComparator.h:310
OpenMS::FuzzyStringComparator::absdiff_max_
double absdiff_max_
Maximum difference of numbers observed so far, see absdiff_max_allowed_.
Definition: FuzzyStringComparator.h:319
OpenMS::StringList
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:73
OpenMS::FuzzyStringComparator::setMatchedWhitelist
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...
main
int main(int argc, const char **argv)
Definition: INIFileEditor.cpp:73
OpenMS::FuzzyStringComparator::matched_whitelist_
std::vector< std::pair< std::string, std::string > > matched_whitelist_
Alternative Whitelist.
Definition: FuzzyStringComparator.h:394
OpenMS::FuzzyStringComparator::log_dest_
std::ostream * log_dest_
Log and results output goes here.
Definition: FuzzyStringComparator.h:263
FuzzyStringComparator.h
String.h
OpenMS::FuzzyStringComparator::PrefixInfo_::line_column
int line_column
Definition: FuzzyStringComparator.h:368
OpenMS::FuzzyStringComparator::input_line_1_
InputLine input_line_1_
Definition: FuzzyStringComparator.h:297
OpenMS::FuzzyStringComparator::StreamElement_::is_space
bool is_space
Definition: FuzzyStringComparator.h:327
OpenMS::FuzzyStringComparator::PrefixInfo_::prefix_whitespaces
OpenMS::String prefix_whitespaces
Definition: FuzzyStringComparator.h:367
OpenMS::FuzzyStringComparator::element_1_
StreamElement_ element_1_
Stores information about characters, numbers, and white spaces loaded from the first input stream.
Definition: FuzzyStringComparator.h:359
OpenMS::FuzzyStringComparator::setAcceptableAbsolute
void setAcceptableAbsolute(const double rhs)
Acceptable absolute difference (a number >= 0.0)
OpenMS::FuzzyStringComparator::InputLine
Stores information about the current input line (i.e., stream for the line and the current position i...
Definition: FuzzyStringComparator.h:271
OpenMS::FuzzyStringComparator::setVerboseLevel
void setVerboseLevel(const int rhs)
verbose level
OpenMS::FuzzyStringComparator::compareFiles
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...
TOPPBase.h
OpenMS::FuzzyStringComparator::AbortComparison
Internal exception class.
Definition: FuzzyStringComparator.h:86
OpenMS::FuzzyStringComparator::line_str_2_max_
std::string line_str_2_max_
Definition: FuzzyStringComparator.h:307
OpenMS::FuzzyStringComparator::is_absdiff_small_
bool is_absdiff_small_
Definition: FuzzyStringComparator.h:373
StringListUtils.h