Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
ClassTest.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-2015.
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: Stephan Aiche $
32 // $Authors: Marc Sturm, Clemens Groepl $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_CONCEPT_CLASSTEST_H
36 #define OPENMS_CONCEPT_CLASSTEST_H
37 
38 // Avoid OpenMS includes here at all costs
39 // When the included headers are changed, *all* tests have to be recompiled!
40 // Use the ClassTest class if you need add high-level functionality.
41 // Includes in the C-file are ok...
42 #include <OpenMS/CONCEPT/Types.h>
46 #include <OpenMS/SYSTEM/File.h>
47 #include <OpenMS/OpenMSConfig.h>
48 #include <OpenMS/config.h>
49 
50 #include <cmath> // fabs
51 #include <cstdio> // tmpnam()
52 #include <cstdlib> // getenv()
53 #include <cstring>
54 #include <fstream>
55 #include <iostream>
56 #include <list>
57 #include <string>
58 #include <vector>
59 
60 // Empty declaration to avoid problems in case the namespace is not
61 // yet defined (e.g. TEST/ClassTest_test.cpp)
62 
64 #ifndef std__cout
65 #define std__cout std::cout
66 #endif
67 
68 namespace OpenMS
69 {
70  namespace Internal
71  {
73  namespace ClassTest
74  {
75 
80  bool OPENMS_DLLAPI
81  validate(const std::vector<std::string>& file_names);
82 
84  std::string OPENMS_DLLAPI
85  tmpFileName(const std::string& file, int line);
86 
88  inline bool OPENMS_DLLAPI
89  isRealType(float)
90  {
91  return true;
92  }
93 
95  inline bool OPENMS_DLLAPI
96  isRealType(double)
97  {
98  return true;
99  }
100 
102  inline bool OPENMS_DLLAPI
103  isRealType(long double)
104  {
105  return true;
106  }
107 
109  inline bool OPENMS_DLLAPI
111  {
112  return true;
113  }
114 
116  template <typename T>
117  inline bool
118  isRealType(const T&)
119  {
120  return false;
121  }
122 
128  void OPENMS_DLLAPI
129  testRealSimilar(const char* file, int line, long double number_1,
130  const char* number_1_stringified,
131  bool number_1_is_realtype, Int number_1_written_digits,
132  long double number_2, const char* number_2_stringified,
133  bool /* number_2_is_realtype */, Int number_2_written_digits);
134 
136  bool OPENMS_DLLAPI
137  isRealSimilar(long double number_1, long double number_2);
138 
146  void OPENMS_DLLAPI
147  testStringSimilar(const char* file, int line,
148  const std::string& string_1,
149  const char* string_1_stringified,
150  const std::string& string_2,
151  const char* string_2_stringified);
152 
154  void OPENMS_DLLAPI
155  testStringEqual(const char* file, int line,
156  const std::string& string_1,
157  const char* string_1_stringified,
158  const std::string& string_2,
159  const char* string_2_stringified);
160 
166  bool OPENMS_DLLAPI
167  isFileSimilar(const std::string& filename_1,
168  const std::string& filename_2);
169 
171  void OPENMS_DLLAPI
172  initialNewline();
173 
175  void OPENMS_DLLAPI
176  printWithPrefix(const std::string& text, const int marked = -1);
177 
179  void OPENMS_DLLAPI
180  setWhitelist(const char* const /* file */, const int line,
181  const std::string& whitelist);
182 
184  extern OPENMS_DLLAPI double ratio_max_allowed;
185 
187  extern OPENMS_DLLAPI double ratio_max;
188 
190  extern OPENMS_DLLAPI double ratio;
191 
193  extern OPENMS_DLLAPI double absdiff_max_allowed;
194 
196  extern OPENMS_DLLAPI double absdiff_max;
197 
199  extern OPENMS_DLLAPI double absdiff;
200 
201  extern OPENMS_DLLAPI int line_num_1_max;
202  extern OPENMS_DLLAPI int line_num_2_max;
203 
205  extern OPENMS_DLLAPI int verbose;
206 
208  extern OPENMS_DLLAPI bool all_tests;
209 
211  extern OPENMS_DLLAPI bool test;
212 
214  extern OPENMS_DLLAPI bool this_test;
215 
217  extern OPENMS_DLLAPI int exception;
218 
220  extern OPENMS_DLLAPI std::string exception_name;
221 
223  extern OPENMS_DLLAPI std::string exception_message;
224 
226  extern OPENMS_DLLAPI std::string test_name;
227 
229  extern OPENMS_DLLAPI int start_section_line;
230 
232  extern OPENMS_DLLAPI int test_line;
233 
235  extern OPENMS_DLLAPI const char* version_string;
236 
238  extern OPENMS_DLLAPI std::vector<std::string> tmp_file_list;
239 
241  extern OPENMS_DLLAPI std::vector<UInt> failed_lines_list;
242 
244  extern OPENMS_DLLAPI std::ifstream infile;
245 
247  extern OPENMS_DLLAPI std::ifstream templatefile;
248 
250  extern OPENMS_DLLAPI bool equal_files;
251 
253  extern OPENMS_DLLAPI char line_buffer[65536];
254 
256  extern OPENMS_DLLAPI int test_count;
257 
259  extern OPENMS_DLLAPI std::string add_message;
260 
265  extern OPENMS_DLLAPI std::string fuzzy_message;
266 
268  extern OPENMS_DLLAPI bool newline;
269 
270  template <typename T1, typename T2>
271  void
272  testEqual(const char* /*file*/, int line, const T1& expression_1,
273  const char* expression_1_stringified,
274  const T2& expression_2,
275  const char* expression_2_stringified)
276  {
277  ++test_count;
278  test_line = line;
279  this_test = bool(expression_1 == T1(expression_2));
280  test = test && this_test;
281  {
282  initialNewline();
283  if (this_test)
284  {
285  std__cout << " + line " << line << ": TEST_EQUAL("
286  << expression_1_stringified << ','
287  << expression_2_stringified << "): got " << expression_1
288  << ", expected " << expression_2 << std::endl;
289  }
290  else
291  {
292  std__cout << " - line " << line << ": TEST_EQUAL("
293  << expression_1_stringified << ','
294  << expression_2_stringified << "): got " << expression_1
295  << ", expected " << expression_2 << std::endl;
296  failed_lines_list.push_back(line);
297  }
298  }
299  }
300 
301  template <typename T1, typename T2>
302  void
303  testNotEqual(const char* /*file*/, int line, const T1& expression_1,
304  const char* expression_1_stringified,
305  const T2& expression_2,
306  const char* expression_2_stringified)
307  {
308  ++test_count;
309  test_line = line;
310  this_test = !(expression_1 == T1(expression_2));
311  test = test && this_test;
312  {
313  initialNewline();
314  if (this_test)
315  {
316  std__cout << " + line " << line << ": TEST_NOT_EQUAL("
317  << expression_1_stringified << ','
318  << expression_2_stringified << "): got " << expression_1
319  << ", forbidden is " << expression_2 << std::endl;
320  }
321  else
322  {
323  std__cout << " - line " << line << ": TEST_NOT_EQUAL("
324  << expression_1_stringified << ','
325  << expression_2_stringified << "): got " << expression_1
326  << ", forbidden is " << expression_2 << std::endl;
327  failed_lines_list.push_back(line);
328  }
329  }
330  }
331 
332  }
333  }
334 }
335 
336 // A namespace alias - apparently these cannot be documented using doxygen (?)
337 namespace TEST = OpenMS::Internal::ClassTest;
338 
363 
364 //@name test and subtest
366 
392 #define START_TEST(class_name, version) \
393  int main(int argc, char** argv) \
394  { \
395  OpenMS::UInt64 seed = 2453440375; \
396  OpenMS::UniqueIdGenerator::setSeed(seed); \
397  TEST::version_string = version; \
398  \
399  if (argc > 1) \
400  { \
401  std::cerr \
402  << "This is " << argv[0] << ", the test program for the\n" \
403  << # class_name " class.\n" \
404  "\n" \
405  "On successful operation it returns PASSED,\n" \
406  "otherwise FAILED is printed.\n"; \
407  return 1; \
408  } \
409  \
410  try {
411 
423 #define END_TEST \
424  /* global try block */ \
425  } \
426  catch (::OpenMS::Exception::BaseException& e) \
427  { \
428  TEST::this_test = false; \
429  TEST::test = false; \
430  TEST::all_tests = false; \
431  { \
432  TEST::initialNewline(); \
433  std__cout << "Error: Caught unexpected OpenMS exception of type '" \
434  << e.getName() \
435  << "'"; \
436  if ((e.getLine() > 0) && (std::strcmp(e.getFile(), "") != 0)) \
437  { \
438  std__cout << " thrown in line " << e.getLine() << " of file '" << e.getFile() \
439  << "' in function '" << e.getFunction() << "'"; \
440  } \
441  std__cout << " - Message: " << e.what() << std::endl; \
442  } \
443  } \
444  /* catch std:: exceptions */ \
445  catch (std::exception& e) \
446  { \
447  TEST::this_test = false; \
448  TEST::test = false; \
449  TEST::all_tests = false; \
450  { \
451  TEST::initialNewline(); \
452  std__cout << "Error: Caught unexpected std::exception" << std::endl; \
453  std__cout << " - Message: " << e.what() << std::endl; \
454  } \
455  } \
456  /* catch all other exceptions */ \
457  catch (...) \
458  { \
459  TEST::this_test = false; \
460  TEST::test = false; \
461  TEST::all_tests = false; \
462  { \
463  TEST::initialNewline(); \
464  std__cout << "Error: Caught unidentified and unexpected exception - No message." \
465  << std::endl; \
466  } \
467  } \
468  /* check validity of temporary files if known */ \
469  if (!TEST::validate(TEST::tmp_file_list)) \
470  { \
471  TEST::all_tests = false; \
472  } \
473  /* check for exit code */ \
474  if (!TEST::all_tests) \
475  { \
476  std__cout << "FAILED" << std::endl; \
477  if (TEST::add_message != "") std__cout << "Message: " \
478  << TEST::add_message \
479  << std::endl; \
480  std__cout << "Failed lines: "; \
481  for (OpenMS::Size i = 0; i < TEST::failed_lines_list.size(); ++i) \
482  { \
483  std__cout << TEST::failed_lines_list[i] << " "; \
484  } \
485  std__cout << std::endl; \
486  return 1; \
487  } \
488  else \
489  { \
490  /* remove temporary files*/ \
491  for (OpenMS::Size i = 0; i < TEST::tmp_file_list.size(); ++i) \
492  { \
493  if (!OpenMS::File::remove(TEST::tmp_file_list[i])) \
494  { \
495  std__cout << "Warning: unable to remove temporary file '" \
496  << TEST::tmp_file_list[i] \
497  << "'" \
498  << std::endl; \
499  } \
500  } \
501  std__cout << "PASSED"; \
502  if (TEST::add_message != "") std__cout << " (" << TEST::add_message << ")"; \
503  std__cout << std::endl; \
504  return 0; \
505  } \
506  }
507 
530 #define START_SECTION(name_of_test) \
531  TEST::test = true; \
532  TEST::newline = false; \
533  TEST::test_name = # name_of_test; \
534  TEST::test_count = 0; \
535  TEST::start_section_line = __LINE__; \
536  std__cout << "checking " << TEST::test_name << " ... " << std::flush; \
537  try \
538  { \
539  while (true) \
540  {
541 
567 #define END_SECTION \
568  break; \
569  } \
570  } \
571  catch (::OpenMS::Exception::BaseException& e) \
572  { \
573  TEST::this_test = false; \
574  TEST::test = false; \
575  TEST::all_tests = false; \
576  { \
577  TEST::initialNewline(); \
578  std__cout << "Error: Caught unexpected exception of type '" << e.getName() << "'"; \
579  if ((e.getLine() > 0) && (std::strcmp(e.getFile(), "") != 0)) \
580  { \
581  std__cout << " thrown in line " << e.getLine() << " of file '" << e.getFile() \
582  << "' in function '" << e.getFunction() << "'"; \
583  } \
584  std__cout << " - Message: " << e.what() << std::endl; \
585  } \
586  } \
587  /* catch std:: exceptions */ \
588  catch (std::exception& e) \
589  { \
590  TEST::this_test = false; \
591  TEST::test = false; \
592  TEST::all_tests = false; \
593  { \
594  TEST::initialNewline(); \
595  std__cout << "Error: Caught std::exception" << std::endl; \
596  std__cout << " - Message: " << e.what() << std::endl; \
597  } \
598  } \
599  /* catch all other exceptions */ \
600  catch (...) \
601  { \
602  TEST::this_test = false; \
603  TEST::test = false; \
604  TEST::all_tests = false; \
605  { \
606  TEST::initialNewline(); \
607  std__cout << "Error: Caught unidentified and unexpected exception - No message." \
608  << std::endl; \
609  } \
610  } \
611  TEST::all_tests = TEST::all_tests && TEST::test; \
612  { \
613  if (TEST::test) \
614  { \
615  std__cout << ": passed" << std::endl; \
616  } \
617  else \
618  { \
619  std__cout << ": failed" << std::endl; \
620  } \
621  } \
622  /* issue a warning if no tests were performed (unless in destructor)*/ \
623  if (TEST::test_count == 0) \
624  { \
625  bool destructor = false; \
626  for (OpenMS::Size i = 0; i != TEST::test_name.size(); ++i) \
627  { \
628  if (TEST::test_name[i] == '~') \
629  { \
630  destructor = true; \
631  break; \
632  } \
633  } \
634  if (!destructor) std__cout << "Warning: no subtests performed in '" \
635  << TEST::test_name \
636  << "' (line " \
637  << __LINE__ \
638  << ")!" \
639  << std::endl \
640  << std::flush; \
641  } \
642  std__cout << std::endl;
643 
645 
665 #define TEST_EQUAL(a, b) TEST::testEqual(__FILE__, __LINE__, (a), (# a), (b), (# b));
666 
678 #define TEST_NOT_EQUAL(a, b) TEST::testNotEqual(__FILE__, __LINE__, (a), (# a), (b), (# b));
679 
692 #define TEST_STRING_EQUAL(a, b) TEST::testStringEqual(__FILE__, __LINE__, (a), (# a), (b), (# b));
693 
708 #define TEST_FILE_EQUAL(filename, templatename) \
709  { \
710  ++TEST::test_count; \
711  TEST::equal_files = true; \
712  TEST::infile.open(filename, std::ios::in); \
713  TEST::templatefile.open(templatename, std::ios::in); \
714  \
715  if (TEST::infile.good() && TEST::templatefile.good()) \
716  { \
717  std::string TEST_FILE__template_line; \
718  std::string TEST_FILE__line; \
719  \
720  while (TEST::infile.good() && TEST::templatefile.good()) \
721  { \
722  TEST::templatefile.getline(TEST::line_buffer, 65535); \
723  TEST_FILE__template_line = TEST::line_buffer; \
724  TEST::infile.getline(TEST::line_buffer, 65535); \
725  TEST_FILE__line = TEST::line_buffer; \
726  \
727  TEST::equal_files &= (TEST_FILE__template_line == TEST_FILE__line); \
728  if (TEST_FILE__template_line != TEST_FILE__line) \
729  { \
730  { \
731  TEST::initialNewline(); \
732  std__cout << " TEST_FILE_EQUAL: line mismatch:\n got: '" \
733  << TEST_FILE__line << "'\n expected: '" \
734  << TEST_FILE__template_line << "'" << std::endl; \
735  } \
736  } \
737  } \
738  } \
739  else \
740  { \
741  TEST::equal_files = false; \
742  { \
743  TEST::initialNewline(); \
744  std__cout << " + line " \
745  << __LINE__ \
746  << ": TEST_FILE_EQUAL(" \
747  << # filename \
748  << ", " \
749  << # templatename; \
750  std__cout << ") : " << " cannot open file: \""; \
751  if (!TEST::infile.good()) \
752  { \
753  std__cout << filename << "\" (input file) "; \
754  } \
755  if (!TEST::templatefile.good()) \
756  { \
757  std__cout << templatename << "\" (template file) "; \
758  } \
759  std__cout << std::endl; \
760  \
761  } \
762  } \
763  TEST::infile.close(); \
764  TEST::templatefile.close(); \
765  TEST::infile.clear(); \
766  TEST::templatefile.clear(); \
767  \
768  TEST::this_test = TEST::equal_files; \
769  TEST::test = TEST::test && TEST::this_test; \
770  { \
771  TEST::initialNewline(); \
772  if (TEST::this_test) \
773  { \
774  std__cout << " + line " \
775  << __LINE__ \
776  << ": TEST_FILE_EQUAL(" \
777  << # filename \
778  << ", " \
779  << # templatename \
780  << "): true"; \
781  } \
782  else \
783  { \
784  std__cout << " - line " \
785  << __LINE__ \
786  << ": TEST_FILE_EQUAL(" \
787  << # filename \
788  << ", " \
789  << # templatename \
790  << "): false (different files: " \
791  << filename \
792  << " " \
793  << templatename \
794  << " )\n"; \
795  TEST::failed_lines_list.push_back(TEST::test_line); \
796  } \
797  } \
798  }
799 
815 #define TEST_REAL_SIMILAR(a, b) TEST::testRealSimilar(__FILE__, __LINE__, (a), (# a), TEST::isRealType(a), writtenDigits(a), (b), (# b), TEST::isRealType(b), writtenDigits(b));
816 
832 #define TEST_STRING_SIMILAR(a, b) TEST::testStringSimilar(__FILE__, __LINE__, (a), (# a), (b), (# b));
833 
848 #define TEST_FILE_SIMILAR(a, b) \
849  { \
850  ++TEST::test_count; \
851  TEST::test_line = __LINE__; \
852  TEST::this_test = TEST::isFileSimilar((a), (b)); \
853  TEST::test = TEST::test && TEST::this_test; \
854  { \
855  TEST::initialNewline(); \
856  if (TEST::this_test) \
857  { \
858  std__cout << " + line " << __LINE__ \
859  << ": TEST_FILE_SIMILAR(" # a "," # b "): absolute: " \
860  << precisionWrapper(TEST::absdiff) \
861  << " (" \
862  << precisionWrapper(TEST::absdiff_max_allowed) \
863  << "), relative: " \
864  << precisionWrapper(TEST::ratio) \
865  << " (" \
866  << precisionWrapper(TEST::ratio_max_allowed) \
867  << ")" \
868  << std::endl; \
869  std__cout << "message: \n"; \
870  std__cout << TEST::fuzzy_message; \
871  } \
872  else \
873  { \
874  std__cout << " - line " << TEST::test_line << \
875  ": TEST_FILE_SIMILAR(" # a "," # b ") ... -\n"; \
876  std__cout << "message: \n"; \
877  std__cout << TEST::fuzzy_message; \
878  TEST::failed_lines_list.push_back(TEST::test_line); \
879  } \
880  } \
881  }
882 
895 #define TOLERANCE_RELATIVE(a) \
896  TEST::ratio_max_allowed = (a); \
897  { \
898  TEST::initialNewline(); \
899  std__cout << " + line " << __LINE__ << \
900  ": TOLERANCE_RELATIVE(" << TEST::ratio_max_allowed << \
901  ") (\"" # a "\")" << std::endl; \
902  }
903 
915 #define TOLERANCE_ABSOLUTE(a) \
916  TEST::absdiff_max_allowed = (a); \
917  { \
918  TEST::initialNewline(); \
919  std__cout << " + line " << __LINE__ << \
920  ": TOLERANCE_ABSOLUTE(" << TEST::absdiff_max_allowed << \
921  ") (\"" # a "\")" << std::endl; \
922  }
923 
929 #define WHITELIST(a) TEST::setWhitelist(__FILE__, __LINE__, (a));
930 
943 #define TEST_EXCEPTION(exception_type, command) \
944  { \
945  ++TEST::test_count; \
946  TEST::test_line = __LINE__; \
947  TEST::exception = 0; \
948  try \
949  { \
950  command; \
951  } \
952  catch (exception_type) \
953  { \
954  TEST::exception = 1; \
955  } \
956  catch (::OpenMS::Exception::BaseException e) \
957  { \
958  TEST::exception = 2; \
959  TEST::exception_name = e.getName(); \
960  } \
961  catch (...) \
962  { \
963  TEST::exception = 3; \
964  } \
965  TEST::this_test = (TEST::exception == 1); \
966  TEST::test = TEST::test && TEST::this_test; \
967  \
968  { \
969  TEST::initialNewline(); \
970  switch (TEST::exception) \
971  { \
972  case 0: \
973  std__cout << " - line " << TEST::test_line << \
974  ": TEST_EXCEPTION(" # exception_type "," # command \
975  "): no exception thrown!" << std::endl; \
976  TEST::failed_lines_list.push_back(TEST::test_line); \
977  break; \
978  case 1: \
979  std__cout << " + line " << TEST::test_line << \
980  ": TEST_EXCEPTION(" # exception_type "," # command \
981  "): OK" << std::endl; \
982  break; \
983  case 2: \
984  std__cout << " - line " << TEST::test_line << \
985  ": TEST_EXCEPTION(" # exception_type "," # command \
986  "): wrong exception thrown! \"" \
987  << TEST::exception_name << "\"" << std::endl; \
988  TEST::failed_lines_list.push_back(TEST::test_line); \
989  break; \
990  case 3: \
991  std__cout << " - line " << TEST::test_line << \
992  ": TEST_EXCEPTION(" # exception_type "," # command \
993  "): wrong exception thrown!" << std::endl; \
994  TEST::failed_lines_list.push_back(TEST::test_line); \
995  break; \
996  } \
997  } \
998  }
999 
1011 #ifdef OPENMS_ASSERTIONS
1012 #define TEST_PRECONDITION_VIOLATED(command) TEST_EXCEPTION(Exception::Precondition, command);
1013 #else
1014 #define TEST_PRECONDITION_VIOLATED(command) STATUS("TEST_PRECONDITION_VIOLATED(" # command ") - skipped");
1015 #endif
1016 
1028 #ifdef OPENMS_ASSERTIONS
1029 #define TEST_POSTCONDITION_VIOLATED(command) TEST_EXCEPTION(Exception::Postcondition, command);
1030 #else
1031 #define TEST_POSTCONDITION_VIOLATED(command) STATUS("TEST_POSTCONDITION_VIOLATED(" # command ") - skipped");
1032 #endif
1033 
1034 
1051 #define TEST_EXCEPTION_WITH_MESSAGE(exception_type, command, message) \
1052  { \
1053  ++TEST::test_count; \
1054  TEST::test_line = __LINE__; \
1055  TEST::exception = 0; \
1056  try \
1057  { \
1058  command; \
1059  } \
1060  catch (exception_type et) \
1061  { \
1062  if (std::string(et.getMessage()) != std::string(message)) \
1063  { \
1064  TEST::exception = 4; \
1065  TEST::exception_message = et.getMessage(); \
1066  } \
1067  else TEST::exception = 1; \
1068  } \
1069  catch (::OpenMS::Exception::BaseException e) \
1070  { \
1071  TEST::exception = 2; \
1072  TEST::exception_name = e.getName(); \
1073  } \
1074  catch (...) \
1075  { \
1076  TEST::exception = 3; \
1077  } \
1078  TEST::this_test = (TEST::exception == 1); \
1079  TEST::test = TEST::test && TEST::this_test; \
1080  \
1081  { \
1082  TEST::initialNewline(); \
1083  switch (TEST::exception) \
1084  { \
1085  case 0: \
1086  std__cout << " - line " << TEST::test_line << \
1087  ": TEST_EXCEPTION_WITH_MESSAGE(" # exception_type "," # command ", " # message \
1088  "): no exception thrown!" << std::endl; \
1089  TEST::failed_lines_list.push_back(TEST::test_line); \
1090  break; \
1091  case 1: \
1092  /* this is actually what we want to get: */ \
1093  std__cout << " + line " << TEST::test_line << \
1094  ": TEST_EXCEPTION_WITH_MESSAGE(" # exception_type "," # command ", " # message \
1095  "): OK" << std::endl; \
1096  break; \
1097  case 2: \
1098  std__cout << " - line " << TEST::test_line << \
1099  ": TEST_EXCEPTION_WITH_MESSAGE(" # exception_type "," # command ", " # message \
1100  "): wrong exception thrown! \"" << \
1101  TEST::exception_name << "\"" << std::endl; \
1102  TEST::failed_lines_list.push_back(TEST::test_line); \
1103  break; \
1104  case 3: \
1105  std__cout << " - line " << TEST::test_line << \
1106  ": TEST_EXCEPTION_WITH_MESSAGE(" # exception_type "," # command ", " # message \
1107  "): wrong exception thrown!" << std::endl; \
1108  TEST::failed_lines_list.push_back(TEST::test_line); \
1109  break; \
1110  case 4: \
1111  std__cout << " - line " << TEST::test_line << \
1112  ": TEST_EXCEPTION_WITH_MESSAGE(" # exception_type "," # command ", " # message \
1113  "): exception has wrong message: got '" << \
1114  TEST::exception_message << \
1115  "', expected '" << \
1116  (message) << \
1117  "'" << std::endl; \
1118  TEST::failed_lines_list.push_back(TEST::test_line); \
1119  break; \
1120  } \
1121  } \
1122  }
1123 
1139 #define NEW_TMP_FILE(filename) \
1140  { \
1141  filename = TEST::tmpFileName(__FILE__, __LINE__); \
1142  TEST::tmp_file_list.push_back(filename); \
1143  { \
1144  TEST::initialNewline(); \
1145  std__cout << " creating new temporary filename '" \
1146  << filename \
1147  << "' (line " \
1148  << __LINE__ \
1149  << ")" \
1150  << std::endl; \
1151  } \
1152  }
1153 
1161 #define ABORT_IF(condition) \
1162  if (condition) \
1163  { \
1164  { \
1165  TEST::initialNewline(); \
1166  std__cout << " - line " << __LINE__ << \
1167  ": ABORT_IF(" # condition "): TEST ABORTED" << \
1168  std::endl; \
1169  TEST::failed_lines_list.push_back(TEST::test_line); \
1170  } \
1171  break; \
1172  }
1173 
1191 #define STATUS(message) \
1192  { \
1193  TEST::initialNewline(); \
1194  std__cout << " line " \
1195  << __LINE__ \
1196  << ": status: " \
1197  << message \
1198  << std::endl; \
1199  }
1200 
1210 #define ADD_MESSAGE(message) \
1211  TEST::add_message = message;
1212 
1222 #define NOT_TESTABLE \
1223  TEST::test_count = 1;
1224 
1226 
1227 #endif //OPENMS_CONCEPT_CLASSTEST_H
double absdiff_max_allowed
Maximum absolute difference of numbers allowed, see TOLERANCE_ABSOLUTE.
int test_count
Counter for the number of elementary tests within the current subsection.
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.
std::ifstream templatefile
Template (correct) file used by TEST_FILE_EQUAL.
std::string exception_name
(Used by various macros. Stores the &quot;name&quot; of the exception, if applicable.)
void setWhitelist(const char *const , const int line, const std::string &whitelist)
set the whitelist_
bool newline
(Flags whether a new line is in place, depending on context and verbosity setting. Used by initialNewline() and some macros.)
bool this_test
Status of last elementary test.
std::ifstream infile
Questionable file tested by TEST_FILE_EQUAL.
int start_section_line
Line where current subsection started.
std::vector< std::string > tmp_file_list
List of tmp file names (these will be cleaned up, see NEW_TMP_FILE)
std::string add_message
See ADD_MESSAGE.
double ratio_max
Maximum ratio of numbers observed so far, see TOLERANCE_RELATIVE.
void testEqual(const char *, int line, const T1 &expression_1, const char *expression_1_stringified, const T2 &expression_2, const char *expression_2_stringified)
Definition: ClassTest.h:272
std::string tmpFileName(const std::string &file, int line)
Creates a temporary file name from the test name and the line.
char line_buffer[65536]
(A buffer for one line from a file. Used by TEST_FILE_EQUAL.)
bool isRealType(float)
This overload returns true; float is a floating point type.
Definition: ClassTest.h:89
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition: DataValue.h:57
std::string exception_message
(Used by various macros. Stores the &quot;message&quot; of the exception, if applicable.)
#define std__cout
Provide a point of redirection for testing the test macros, see ClassTest_test.cpp.
Definition: ClassTest.h:65
bool equal_files
(A variable used by TEST_FILE_EQUAL)
void testNotEqual(const char *, int line, const T1 &expression_1, const char *expression_1_stringified, const T2 &expression_2, const char *expression_2_stringified)
Definition: ClassTest.h:303
int exception
(Used by various macros. Indicates a rough category of the exception being caught.)
double ratio
Recent ratio of numbers, see TOLERANCE_RELATIVE.
const char * version_string
Version string supplied with START_TEST.
bool all_tests
Status of the whole test.
int test_line
Line of current elementary test.
void testRealSimilar(const char *file, int line, long double number_1, const char *number_1_stringified, bool number_1_is_realtype, Int number_1_written_digits, long double number_2, const char *number_2_stringified, bool, Int number_2_written_digits)
Compare floating point numbers using absdiff_max_allowed and ratio_max_allowed.
void initialNewline()
make sure we have a newline before results from first subtest
std::string test_name
Name of current subsection.
std::vector< UInt > failed_lines_list
List of all failed lines for summary at the end of the test.
bool isRealSimilar(long double number_1, long double number_2)
used by testRealSimilar()
double absdiff
Recent absolute difference of numbers, see TOLERANCE_ABSOLUTE.
bool isFileSimilar(const std::string &filename_1, const std::string &filename_2)
Compare files using absdiff_max_allowed and ratio_max_allowed.
bool test
Status of the current subsection.
int Int
Signed integer type.
Definition: Types.h:96
double absdiff_max
Maximum difference of numbers observed so far, see TOLERANCE_ABSOLUTE.
bool validate(const std::vector< std::string > &file_names)
Validates the given files against the XML schema (if available)
int verbose
Verbosity level ( &quot;-v&quot; is 1 and &quot;-V&quot; is 2 )
void testStringEqual(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)
used by TEST_STRING_EQUAL
void printWithPrefix(const std::string &text, const int marked=-1)
print the text, each line gets a prefix, the marked line number gets a special prefix ...
std::string fuzzy_message
Last message from a fuzzy comparison. Written by isRealSimilar(), testStringSimilar(), isFileSimilar(). Read by TEST_REAL_SIMILAR, TEST_STRING_SIMILAR, TEST_FILE_SIMILAR;.
double ratio_max_allowed
Maximum ratio of numbers allowed, see TOLERANCE_RELATIVE.

OpenMS / TOPP release 2.0.0 Documentation generated on Wed Mar 30 2016 16:18:38 using doxygen 1.8.5