45 #include <OpenMS/OpenMSConfig.h>
46 #include <OpenMS/config.h>
58 #define stdcout std::cout
74 validate(
const std::vector<std::string>& file_names);
77 std::string OPENMS_DLLAPI
81 inline bool OPENMS_DLLAPI
88 inline bool OPENMS_DLLAPI
95 inline bool OPENMS_DLLAPI
102 inline bool OPENMS_DLLAPI
109 inline bool OPENMS_DLLAPI
116 template <
typename T>
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 ,
Int number_2_written_digits);
148 const std::string& string_1,
149 const char* string_1_stringified,
150 const std::string& string_2,
151 const char* string_2_stringified);
156 const std::string& string_1,
157 const char* string_1_stringified,
158 const std::string& string_2,
159 const char* string_2_stringified);
168 const std::string& filename_2);
187 void OPENMS_DLLAPI
mainInit(
const char* version,
const char* class_name,
int argc,
const char* argv0);
198 void OPENMS_DLLAPI
filesEqual(
int line,
const char* filename,
const char* templatename,
const char* filename_stringified,
const char* templatename_stringified);
206 const std::string& whitelist);
215 extern OPENMS_DLLAPI
double ratio;
224 extern OPENMS_DLLAPI
double absdiff;
230 extern OPENMS_DLLAPI
int verbose;
236 extern OPENMS_DLLAPI
bool test;
251 extern OPENMS_DLLAPI std::string
test_name;
269 extern OPENMS_DLLAPI std::ifstream
infile;
293 extern OPENMS_DLLAPI
bool newline;
295 template <
typename T1,
typename T2>
297 testEqual(
const char* ,
int line,
const T1& expression_1,
298 const char* expression_1_stringified,
299 const T2& expression_2,
300 const char* expression_2_stringified)
304 this_test = bool(expression_1 == T1(expression_2));
312 stdcout <<
" + line " << line <<
": TEST_EQUAL("
313 << expression_1_stringified <<
','
314 << expression_2_stringified <<
"): got '" << expression_1
315 <<
"', expected '" << expression_2 <<
"'\n";
320 stdcout <<
" - line " << line <<
": TEST_EQUAL("
321 << expression_1_stringified <<
','
322 << expression_2_stringified <<
"): got '" << expression_1
323 <<
"', expected '" << expression_2 <<
"'\n";
329 template <
typename T1,
typename T2>
332 const char* expression_1_stringified,
333 const T2& expression_2,
334 const char* expression_2_stringified)
338 this_test = !(expression_1 == T1(expression_2));
346 stdcout <<
" + line " << line <<
": TEST_NOT_EQUAL("
347 << expression_1_stringified <<
','
348 << expression_2_stringified <<
"): got '" << expression_1
349 <<
"', forbidden is '" << expression_2 <<
"'\n";
354 stdcout <<
" - line " << line <<
": TEST_NOT_EQUAL("
355 << expression_1_stringified <<
','
356 << expression_2_stringified <<
"): got '" << expression_1
357 <<
"', forbidden is '" << expression_2 <<
"'\n";
422 #define START_TEST(class_name, version) \
423 int main(int argc, char** argv) \
425 TEST::mainInit(version, #class_name, argc, argv[0]); \
442 catch (::OpenMS::Exception::BaseException& e) \
444 TEST::this_test = false; \
445 TEST::test = false; \
446 TEST::all_tests = false; \
448 TEST::initialNewline(); \
449 stdcout << "Error: Caught unexpected OpenMS exception of type '" \
452 if ((e.getLine() > 0) && std::strcmp(e.getFile(), "")) \
454 stdcout << " thrown in line " << e.getLine() << " of file '" << e.getFile() \
455 << "' in function '" << e.getFunction() << "'"; \
457 stdcout << " - Message: " << e.what() << std::endl; \
461 catch (std::exception& e) \
463 TEST::this_test = false; \
464 TEST::test = false; \
465 TEST::all_tests = false; \
467 TEST::initialNewline(); \
468 stdcout << "Error: Caught unexpected std::exception\n"; \
469 stdcout << " - Message: " << e.what() << std::endl; \
475 TEST::this_test = false; \
476 TEST::test = false; \
477 TEST::all_tests = false; \
479 TEST::initialNewline(); \
480 stdcout << "Error: Caught unidentified and unexpected exception - No message." \
485 if (!TEST::validate(TEST::tmp_file_list)) \
487 TEST::all_tests = false; \
489 if (TEST::verbose == 0) \
491 stdcout << "Output of successful tests were suppressed. Set the environment variable 'OPENMS_TEST_VERBOSE=True' to enable them." << std::endl; \
494 if (!TEST::all_tests) \
496 stdcout << "FAILED" << std::endl; \
497 if (TEST::add_message != "") stdcout << "Message: " \
498 << TEST::add_message \
500 stdcout << "Failed lines: "; \
501 for (OpenMS::Size i = 0; i < TEST::failed_lines_list.size(); ++i) \
503 stdcout << TEST::failed_lines_list[i] << " "; \
505 stdcout << std::endl; \
511 TEST::removeTempFiles(); \
512 stdcout << "PASSED"; \
513 if (TEST::add_message != "") stdcout << " (" << TEST::add_message << ")"; \
514 stdcout << std::endl; \
541 #define START_SECTION(name_of_test) \
543 TEST::newline = false; \
544 TEST::test_name = # name_of_test; \
545 TEST::test_count = 0; \
546 TEST::start_section_line = __LINE__; \
547 stdcout << "checking " << TEST::test_name << " ... " << std::flush; \
578 #define END_SECTION \
582 catch (::OpenMS::Exception::BaseException& e) \
584 TEST::this_test = false; \
585 TEST::test = false; \
586 TEST::all_tests = false; \
588 TEST::initialNewline(); \
589 stdcout << "Error: Caught unexpected exception of type '" << e.getName() << "'"; \
590 if ((e.getLine() > 0) && (std::strcmp(e.getFile(), "") != 0)) \
592 stdcout << " thrown in line " << e.getLine() << " of file '" << e.getFile() \
593 << "' in function '" << e.getFunction() << "'"; \
595 stdcout << " - Message: " << e.what() << std::endl; \
599 catch (std::exception& e) \
601 TEST::this_test = false; \
602 TEST::test = false; \
603 TEST::all_tests = false; \
605 TEST::initialNewline(); \
606 stdcout << "Error: Caught std::exception" << std::endl; \
607 stdcout << " - Message: " << e.what() << std::endl; \
613 TEST::this_test = false; \
614 TEST::test = false; \
615 TEST::all_tests = false; \
617 TEST::initialNewline(); \
618 stdcout << "Error: Caught unidentified and unexpected exception - No message." \
622 TEST::all_tests = TEST::all_tests && TEST::test; \
626 stdcout << ": passed\n"; \
630 stdcout << ": failed\n"; \
634 if (TEST::test_count == 0) \
636 if (OpenMS::String(TEST::test_name).has('~')) \
637 stdcout << "Warning: no subtests performed in '" \
643 stdcout << std::endl;
666 #define TEST_EQUAL(a, b) TEST::testEqual(__FILE__, __LINE__, (a), (# a), (b), (# b));
679 #define TEST_NOT_EQUAL(a, b) TEST::testNotEqual(__FILE__, __LINE__, (a), (# a), (b), (# b));
693 #define TEST_STRING_EQUAL(a, b) TEST::testStringEqual(__FILE__, __LINE__, (a), (# a), (b), (# b));
709 #define TEST_FILE_EQUAL(filename, templatename) \
711 TEST::filesEqual(__LINE__, filename, templatename, #filename, #templatename); \
729 #define TEST_REAL_SIMILAR(a, b) TEST::testRealSimilar(__FILE__, __LINE__, (a), (# a), TEST::isRealType(a), writtenDigits(a), (b), (# b), TEST::isRealType(b), writtenDigits(b));
746 #define TEST_STRING_SIMILAR(a, b) TEST::testStringSimilar(__FILE__, __LINE__, (a), (# a), (b), (# b));
762 #define TEST_FILE_SIMILAR(a, b) \
764 ++TEST::test_count; \
765 TEST::test_line = __LINE__; \
766 TEST::this_test = TEST::isFileSimilar((a), (b)); \
767 TEST::test = TEST::test && TEST::this_test; \
769 TEST::initialNewline(); \
770 if (TEST::this_test) \
772 if (TEST::verbose > 1) \
774 stdcout << " + line " << __LINE__ \
775 << ": TEST_FILE_SIMILAR(" # a "," # b "): absolute: " \
776 << precisionWrapper(TEST::absdiff) \
778 << precisionWrapper(TEST::absdiff_max_allowed) \
780 << precisionWrapper(TEST::ratio) \
782 << precisionWrapper(TEST::ratio_max_allowed) \
784 stdcout << "message: \n"; \
785 stdcout << TEST::fuzzy_message; \
790 stdcout << " - line " << TEST::test_line << \
791 ": TEST_FILE_SIMILAR(" # a "," # b ") ... -\n"; \
792 stdcout << "message: \n"; \
793 stdcout << TEST::fuzzy_message; \
794 TEST::failed_lines_list.push_back(TEST::test_line); \
811 #define TOLERANCE_RELATIVE(a) \
812 TEST::ratio_max_allowed = (a); \
814 TEST::initialNewline(); \
815 if (TEST::verbose > 1) \
817 stdcout << " + line " << __LINE__ << \
818 ": TOLERANCE_RELATIVE(" << TEST::ratio_max_allowed << \
819 ") (\"" # a "\")\n"; \
834 #define TOLERANCE_ABSOLUTE(a) \
835 TEST::absdiff_max_allowed = (a); \
837 TEST::initialNewline(); \
838 if (TEST::verbose > 1) \
840 stdcout << " + line " << __LINE__ << \
841 ": TOLERANCE_ABSOLUTE(" << TEST::absdiff_max_allowed << \
842 ") (\"" # a "\")\n"; \
851 #define WHITELIST(a) TEST::setWhitelist(__FILE__, __LINE__, (a));
865 #define TEST_EXCEPTION(exception_type, command) \
867 ++TEST::test_count; \
868 TEST::test_line = __LINE__; \
869 TEST::exception = 0; \
874 catch (exception_type&) \
876 TEST::exception = 1; \
878 catch (::OpenMS::Exception::BaseException& e) \
880 TEST::exception = 2; \
881 TEST::exception_name = e.getName(); \
885 TEST::exception = 3; \
887 TEST::this_test = (TEST::exception == 1); \
888 TEST::test = TEST::test && TEST::this_test; \
891 TEST::initialNewline(); \
892 switch (TEST::exception) \
895 stdcout << " - line " << TEST::test_line << \
896 ": TEST_EXCEPTION(" # exception_type "," # command \
897 "): no exception thrown!\n"; \
898 TEST::failed_lines_list.push_back(TEST::test_line); \
901 if (TEST::verbose > 1) \
903 stdcout << " + line " << TEST::test_line << \
904 ": TEST_EXCEPTION(" # exception_type "," # command \
909 stdcout << " - line " << TEST::test_line << \
910 ": TEST_EXCEPTION(" # exception_type "," # command \
911 "): wrong exception thrown! \"" \
912 << TEST::exception_name << "\"\n"; \
913 TEST::failed_lines_list.push_back(TEST::test_line); \
916 stdcout << " - line " << TEST::test_line << \
917 ": TEST_EXCEPTION(" # exception_type "," # command \
918 "): wrong exception thrown!\n"; \
919 TEST::failed_lines_list.push_back(TEST::test_line); \
936 #ifdef OPENMS_ASSERTIONS
937 #define TEST_PRECONDITION_VIOLATED(command) TEST_EXCEPTION(Exception::Precondition, command);
939 #define TEST_PRECONDITION_VIOLATED(command) STATUS("TEST_PRECONDITION_VIOLATED(" # command ") - skipped");
953 #ifdef OPENMS_ASSERTIONS
954 #define TEST_POSTCONDITION_VIOLATED(command) TEST_EXCEPTION(Exception::Postcondition, command);
956 #define TEST_POSTCONDITION_VIOLATED(command) STATUS("TEST_POSTCONDITION_VIOLATED(" # command ") - skipped");
976 #define TEST_EXCEPTION_WITH_MESSAGE(exception_type, command, message) \
978 ++TEST::test_count; \
979 TEST::test_line = __LINE__; \
980 TEST::exception = 0; \
985 catch (exception_type& et) \
987 if (std::string(et.what()) != std::string(message)) \
989 TEST::exception = 4; \
990 TEST::exception_message = et.what(); \
992 else TEST::exception = 1; \
994 catch (::OpenMS::Exception::BaseException& e) \
996 TEST::exception = 2; \
997 TEST::exception_name = e.getName(); \
1001 TEST::exception = 3; \
1003 TEST::this_test = (TEST::exception == 1); \
1004 TEST::test = TEST::test && TEST::this_test; \
1007 TEST::initialNewline(); \
1008 switch (TEST::exception) \
1011 stdcout << " - line " << TEST::test_line << \
1012 ": TEST_EXCEPTION_WITH_MESSAGE(" # exception_type "," # command ", " # message \
1013 "): no exception thrown!\n"; \
1014 TEST::failed_lines_list.push_back(TEST::test_line); \
1017 if (TEST::verbose > 1) \
1020 stdcout << " + line " << TEST::test_line << \
1021 ": TEST_EXCEPTION_WITH_MESSAGE(" # exception_type "," # command ", " # message \
1026 stdcout << " - line " << TEST::test_line << \
1027 ": TEST_EXCEPTION_WITH_MESSAGE(" # exception_type "," # command ", " # message \
1028 "): wrong exception thrown! \"" << \
1029 TEST::exception_name << "\"\n"; \
1030 TEST::failed_lines_list.push_back(TEST::test_line); \
1033 stdcout << " - line " << TEST::test_line << \
1034 ": TEST_EXCEPTION_WITH_MESSAGE(" # exception_type "," # command ", " # message \
1035 "): wrong exception thrown!\n"; \
1036 TEST::failed_lines_list.push_back(TEST::test_line); \
1039 stdcout << " - line " << TEST::test_line << \
1040 ": TEST_EXCEPTION_WITH_MESSAGE(" # exception_type "," # command ", " # message \
1041 "): exception has wrong message: got '" << \
1042 TEST::exception_message << \
1043 "', expected '" << \
1044 (message) << "'\n"; \
1045 TEST::failed_lines_list.push_back(TEST::test_line); \
1066 #define NEW_TMP_FILE(filename) \
1068 filename = TEST::tmpFileName(__FILE__, __LINE__); \
1069 TEST::tmp_file_list.push_back(filename); \
1071 TEST::initialNewline(); \
1072 stdcout << " creating new temporary filename '" \
1087 #define ABORT_IF(condition) \
1091 TEST::test_line = __LINE__; \
1092 TEST::this_test = false; \
1093 TEST::test = TEST::test && TEST::this_test; \
1094 TEST::failed_lines_list.push_back(TEST::test_line); \
1095 TEST::initialNewline(); \
1096 stdcout << " - line " << TEST::test_line << \
1097 ": ABORT_IF(" # condition "): TEST ABORTED\n"; \
1119 #define STATUS(message) \
1121 TEST::initialNewline(); \
1122 stdcout << " line " \
1138 #define ADD_MESSAGE(message) \
1139 TEST::add_message = message;
1150 #define NOT_TESTABLE \
1151 TEST::test_count = 1;
#define stdcout
Provide a point of redirection for testing the test macros, see ClassTest_test.cpp.
Definition: ClassTest.h:58
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition: DataValue.h:59
Class to hold strings, numeric values, vectors of strings and vectors of numeric values using the stl...
Definition: ParamValue.h:55
int Int
Signed integer type.
Definition: Types.h:102
Namespace for class tests.
Definition: ClassTest.h:67
double ratio_max
Maximum ratio of numbers observed so far, see TOLERANCE_RELATIVE.
int verbose
Verbosity level ( "-v" is 1 and "-V" is 2 )
int test_count
Counter for the number of elementary tests within the current subsection.
void initialNewline()
make sure we have a newline before results from first subtest
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.
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:297
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.
double absdiff
Recent absolute difference of numbers, see TOLERANCE_ABSOLUTE.
char line_buffer[65536]
(A buffer for one line from a file. Used by TEST_FILE_EQUAL.)
std::string add_message
See ADD_MESSAGE.
bool validate(const std::vector< std::string > &file_names)
Validates the given files against the XML schema (if available)
std::string fuzzy_message
Last message from a fuzzy comparison. Written by isRealSimilar(), testStringSimilar(),...
void mainInit(const char *version, const char *class_name, int argc, const char *argv0)
Set up some classtest variables as obtained from the 'START_TEST' macro and check that no additional ...
bool all_tests
Status of the whole test.
std::string exception_message
(Used by various macros. Stores the "message" of the exception, if applicable.)
std::string test_name
Name of current subsection.
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
double absdiff_max
Maximum difference of numbers observed so far, see TOLERANCE_ABSOLUTE.
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
bool newline
(Flags whether a new line is in place, depending on context and verbosity setting....
std::string tmpFileName(const std::string &file, int line)
Creates a temporary file name from the test name and the line.
bool test
Status of the current subsection.
double absdiff_max_allowed
Maximum absolute difference of numbers allowed, see TOLERANCE_ABSOLUTE.
const char * version_string
Version string supplied with START_TEST.
std::ifstream infile
Questionable file tested by TEST_FILE_EQUAL.
std::string exception_name
(Used by various macros. Stores the "name" of the exception, if applicable.)
std::vector< UInt > failed_lines_list
List of all failed lines for summary at the end of the test.
void removeTempFiles()
removed all temporary files created with the NEW_TMP_FILE macro
double ratio
Recent ratio of numbers, see TOLERANCE_RELATIVE.
std::vector< std::string > tmp_file_list
List of tmp file names (these will be cleaned up, see NEW_TMP_FILE)
bool isFileSimilar(const std::string &filename_1, const std::string &filename_2)
Compare files using absdiff_max_allowed and ratio_max_allowed.
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:331
bool this_test
Status of last elementary test.
std::ifstream templatefile
Template (correct) file used by TEST_FILE_EQUAL.
double ratio_max_allowed
Maximum ratio of numbers allowed, see TOLERANCE_RELATIVE.
void setWhitelist(const char *const, const int line, const std::string &whitelist)
set the whitelist_
bool isRealType(float)
This overload returns true; float is a floating point type.
Definition: ClassTest.h:82
bool equal_files
(A variable used by TEST_FILE_EQUAL)
int start_section_line
Line where current subsection started.
void filesEqual(int line, const char *filename, const char *templatename, const char *filename_stringified, const char *templatename_stringified)
Test if two files are exactly equal (used in TEST_FILE_EQUAL macro)
int exception
(Used by various macros. Indicates a rough category of the exception being caught....
bool isRealSimilar(long double number_1, long double number_2)
used by testRealSimilar()
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47