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 
StreamElement_ element_2_
Stores information about characters, numbers, and white spaces loaded from the second input stream...
Definition: FuzzyStringComparator.h:361
int verbose_level_
Definition: FuzzyStringComparator.h:375
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.
A more convenient string class.
Definition: String.h:57
Internal exception class.
Definition: FuzzyStringComparator.h:86
Stores information about the current input line (i.e., stream for the line and the current position i...
Definition: FuzzyStringComparator.h:271
double ratio_max_allowed_
Maximum ratio of numbers allowed, see ratio_max_.
Definition: FuzzyStringComparator.h:310
Stores information about characters, numbers, and white spaces loaded from the InputStream.
Definition: FuzzyStringComparator.h:322
double absdiff_max_
Maximum difference of numbers observed so far, see absdiff_max_allowed_.
Definition: FuzzyStringComparator.h:319
int line_column
Definition: FuzzyStringComparator.h:368
double absdiff_max_allowed_
Maximum absolute difference of numbers allowed, see absdiff_max_.
Definition: FuzzyStringComparator.h:316
OpenMS::String prefix
Definition: FuzzyStringComparator.h:366
bool use_prefix_
use a prefix when reporting
Definition: FuzzyStringComparator.h:386
std::ostream * log_dest_
Log and results output goes here.
Definition: FuzzyStringComparator.h:263
void setWhitelist(const char *const, const int line, const std::string &whitelist)
set the whitelist_
std::vector< std::pair< std::string, std::string > > matched_whitelist_
Alternative Whitelist.
Definition: FuzzyStringComparator.h:394
unsigned char letter
Definition: FuzzyStringComparator.h:325
std::ios::pos_type line_position_
Definition: FuzzyStringComparator.h:274
bool is_space
Definition: FuzzyStringComparator.h:327
Fuzzy comparison of strings, tolerates numeric differences.
Definition: FuzzyStringComparator.h:69
std::string line_str_2_max_
Definition: FuzzyStringComparator.h:307
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
int line_num_2_
Definition: FuzzyStringComparator.h:301
std::map< String, UInt > whitelist_cases_
Occurrences of whitelist entries.
Definition: FuzzyStringComparator.h:391
bool is_number
Definition: FuzzyStringComparator.h:326
OpenMS::String prefix_whitespaces
Definition: FuzzyStringComparator.h:367
double number
Definition: FuzzyStringComparator.h:324
int first_column_
Definition: FuzzyStringComparator.h:377
int tab_width_
Definition: FuzzyStringComparator.h:376
double ratio_max_
Maximum ratio of numbers observed so far, see ratio_max_allowed_.
Definition: FuzzyStringComparator.h:313
int line_num_1_
Definition: FuzzyStringComparator.h:300
StreamElement_ element_1_
Stores information about characters, numbers, and white spaces loaded from the first input stream...
Definition: FuzzyStringComparator.h:359
StringList whitelist_
Whitelist.
Definition: FuzzyStringComparator.h:389
InputLine input_line_2_
Definition: FuzzyStringComparator.h:298
bool is_status_success_
Has comparison been successful so far? Note: this flag is changed in reportFailure_();.
Definition: FuzzyStringComparator.h:383
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:73
std::string input_2_name_
Name of second input e.g., filename.
Definition: FuzzyStringComparator.h:268
int line_num_1_max_
Definition: FuzzyStringComparator.h:303
std::string line_str_1_max_
Definition: FuzzyStringComparator.h:306
std::stringstream line_
Definition: FuzzyStringComparator.h:273
bool isFileSimilar(const std::string &filename_1, const std::string &filename_2)
Compare files using absdiff_max_allowed and ratio_max_allowed.
bool is_absdiff_small_
Definition: FuzzyStringComparator.h:373
Wrapper for the prefix information computed for the failure report.
Definition: FuzzyStringComparator.h:364
int line_num_2_max_
Definition: FuzzyStringComparator.h:304
InputLine input_line_1_
Definition: FuzzyStringComparator.h:297
std::string input_1_name_
Name of first input e.g., filename.
Definition: FuzzyStringComparator.h:266