 |
OpenMS
2.4.0
|
Go to the documentation of this file.
47 #include <OpenMS/OpenMSConfig.h>
48 #include <OpenMS/config.h>
65 #define std__cout std::cout
81 validate(
const std::vector<std::string>& file_names);
84 std::string 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);
181 const std::string& whitelist);
190 extern OPENMS_DLLAPI
double ratio;
199 extern OPENMS_DLLAPI
double absdiff;
205 extern OPENMS_DLLAPI
int verbose;
211 extern OPENMS_DLLAPI
bool test;
226 extern OPENMS_DLLAPI std::string
test_name;
244 extern OPENMS_DLLAPI std::ifstream
infile;
268 extern OPENMS_DLLAPI
bool newline;
270 template <
typename T1,
typename T2>
272 testEqual(
const char* ,
int line,
const T1& expression_1,
273 const char* expression_1_stringified,
274 const T2& expression_2,
275 const char* expression_2_stringified)
279 this_test = bool(expression_1 == T1(expression_2));
285 std__cout <<
" + line " << line <<
": TEST_EQUAL("
286 << expression_1_stringified <<
','
287 << expression_2_stringified <<
"): got " << expression_1
288 <<
", expected " << expression_2 << std::endl;
292 std__cout <<
" - line " << line <<
": TEST_EQUAL("
293 << expression_1_stringified <<
','
294 << expression_2_stringified <<
"): got " << expression_1
295 <<
", expected " << expression_2 << std::endl;
301 template <
typename T1,
typename T2>
304 const char* expression_1_stringified,
305 const T2& expression_2,
306 const char* expression_2_stringified)
310 this_test = !(expression_1 == T1(expression_2));
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;
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;
391 #define START_TEST(class_name, version) \
392 int main(int argc, char** argv) \
394 OpenMS::UInt64 seed = 2453440375; \
395 OpenMS::UniqueIdGenerator::setSeed(seed); \
396 TEST::version_string = version; \
401 << "This is " << argv[0] << ", the test program for the\n" \
402 << # class_name " class.\n" \
404 "On successful operation it returns PASSED,\n" \
405 "otherwise FAILED is printed.\n"; \
425 catch (::OpenMS::Exception::BaseException& e) \
427 TEST::this_test = false; \
428 TEST::test = false; \
429 TEST::all_tests = false; \
431 TEST::initialNewline(); \
432 std__cout << "Error: Caught unexpected OpenMS exception of type '" \
435 if ((e.getLine() > 0) && (std::strcmp(e.getFile(), "") != 0)) \
437 std__cout << " thrown in line " << e.getLine() << " of file '" << e.getFile() \
438 << "' in function '" << e.getFunction() << "'"; \
440 std__cout << " - Message: " << e.what() << std::endl; \
444 catch (std::exception& e) \
446 TEST::this_test = false; \
447 TEST::test = false; \
448 TEST::all_tests = false; \
450 TEST::initialNewline(); \
451 std__cout << "Error: Caught unexpected std::exception" << std::endl; \
452 std__cout << " - Message: " << e.what() << std::endl; \
458 TEST::this_test = false; \
459 TEST::test = false; \
460 TEST::all_tests = false; \
462 TEST::initialNewline(); \
463 std__cout << "Error: Caught unidentified and unexpected exception - No message." \
468 if (!TEST::validate(TEST::tmp_file_list)) \
470 TEST::all_tests = false; \
473 if (!TEST::all_tests) \
475 std__cout << "FAILED" << std::endl; \
476 if (TEST::add_message != "") std__cout << "Message: " \
477 << TEST::add_message \
479 std__cout << "Failed lines: "; \
480 for (OpenMS::Size i = 0; i < TEST::failed_lines_list.size(); ++i) \
482 std__cout << TEST::failed_lines_list[i] << " "; \
484 std__cout << std::endl; \
490 for (OpenMS::Size i = 0; i < TEST::tmp_file_list.size(); ++i) \
492 if (!OpenMS::File::remove(TEST::tmp_file_list[i])) \
494 std__cout << "Warning: unable to remove temporary file '" \
495 << TEST::tmp_file_list[i] \
500 std__cout << "PASSED"; \
501 if (TEST::add_message != "") std__cout << " (" << TEST::add_message << ")"; \
502 std__cout << std::endl; \
529 #define START_SECTION(name_of_test) \
531 TEST::newline = false; \
532 TEST::test_name = # name_of_test; \
533 TEST::test_count = 0; \
534 TEST::start_section_line = __LINE__; \
535 std__cout << "checking " << TEST::test_name << " ... " << std::flush; \
566 #define END_SECTION \
570 catch (::OpenMS::Exception::BaseException& e) \
572 TEST::this_test = false; \
573 TEST::test = false; \
574 TEST::all_tests = false; \
576 TEST::initialNewline(); \
577 std__cout << "Error: Caught unexpected exception of type '" << e.getName() << "'"; \
578 if ((e.getLine() > 0) && (std::strcmp(e.getFile(), "") != 0)) \
580 std__cout << " thrown in line " << e.getLine() << " of file '" << e.getFile() \
581 << "' in function '" << e.getFunction() << "'"; \
583 std__cout << " - Message: " << e.what() << std::endl; \
587 catch (std::exception& e) \
589 TEST::this_test = false; \
590 TEST::test = false; \
591 TEST::all_tests = false; \
593 TEST::initialNewline(); \
594 std__cout << "Error: Caught std::exception" << std::endl; \
595 std__cout << " - Message: " << e.what() << std::endl; \
601 TEST::this_test = false; \
602 TEST::test = false; \
603 TEST::all_tests = false; \
605 TEST::initialNewline(); \
606 std__cout << "Error: Caught unidentified and unexpected exception - No message." \
610 TEST::all_tests = TEST::all_tests && TEST::test; \
614 std__cout << ": passed" << std::endl; \
618 std__cout << ": failed" << std::endl; \
622 if (TEST::test_count == 0) \
624 bool destructor = false; \
625 for (OpenMS::Size i = 0; i != TEST::test_name.size(); ++i) \
627 if (TEST::test_name[i] == '~') \
633 if (!destructor) std__cout << "Warning: no subtests performed in '" \
641 std__cout << std::endl;
664 #define TEST_EQUAL(a, b) TEST::testEqual(__FILE__, __LINE__, (a), (# a), (b), (# b));
677 #define TEST_NOT_EQUAL(a, b) TEST::testNotEqual(__FILE__, __LINE__, (a), (# a), (b), (# b));
691 #define TEST_STRING_EQUAL(a, b) TEST::testStringEqual(__FILE__, __LINE__, (a), (# a), (b), (# b));
707 #define TEST_FILE_EQUAL(filename, templatename) \
709 ++TEST::test_count; \
710 TEST::equal_files = true; \
711 TEST::infile.open(filename, std::ios::in); \
712 TEST::templatefile.open(templatename, std::ios::in); \
714 if (TEST::infile.good() && TEST::templatefile.good()) \
716 std::string TEST_FILE__template_line; \
717 std::string TEST_FILE__line; \
719 while (TEST::infile.good() && TEST::templatefile.good()) \
721 TEST::templatefile.getline(TEST::line_buffer, 65535); \
722 TEST_FILE__template_line = TEST::line_buffer; \
723 TEST::infile.getline(TEST::line_buffer, 65535); \
724 TEST_FILE__line = TEST::line_buffer; \
726 TEST::equal_files &= (TEST_FILE__template_line == TEST_FILE__line); \
727 if (TEST_FILE__template_line != TEST_FILE__line) \
730 TEST::initialNewline(); \
731 std__cout << " TEST_FILE_EQUAL: line mismatch:\n got: '" \
732 << TEST_FILE__line << "'\n expected: '" \
733 << TEST_FILE__template_line << "'" << std::endl; \
740 TEST::equal_files = false; \
742 TEST::initialNewline(); \
743 std__cout << " + line " \
745 << ": TEST_FILE_EQUAL(" \
749 std__cout << ") : " << " cannot open file: \""; \
750 if (!TEST::infile.good()) \
752 std__cout << filename << "\" (input file) "; \
754 if (!TEST::templatefile.good()) \
756 std__cout << templatename << "\" (template file) "; \
758 std__cout << std::endl; \
762 TEST::infile.close(); \
763 TEST::templatefile.close(); \
764 TEST::infile.clear(); \
765 TEST::templatefile.clear(); \
767 TEST::this_test = TEST::equal_files; \
768 TEST::test = TEST::test && TEST::this_test; \
770 TEST::initialNewline(); \
771 if (TEST::this_test) \
773 std__cout << " + line " \
775 << ": TEST_FILE_EQUAL(" \
783 std__cout << " - line " \
785 << ": TEST_FILE_EQUAL(" \
789 << "): false (different files: " \
794 TEST::failed_lines_list.push_back(TEST::test_line); \
814 #define TEST_REAL_SIMILAR(a, b) TEST::testRealSimilar(__FILE__, __LINE__, (a), (# a), TEST::isRealType(a), writtenDigits(a), (b), (# b), TEST::isRealType(b), writtenDigits(b));
831 #define TEST_STRING_SIMILAR(a, b) TEST::testStringSimilar(__FILE__, __LINE__, (a), (# a), (b), (# b));
847 #define TEST_FILE_SIMILAR(a, b) \
849 ++TEST::test_count; \
850 TEST::test_line = __LINE__; \
851 TEST::this_test = TEST::isFileSimilar((a), (b)); \
852 TEST::test = TEST::test && TEST::this_test; \
854 TEST::initialNewline(); \
855 if (TEST::this_test) \
857 std__cout << " + line " << __LINE__ \
858 << ": TEST_FILE_SIMILAR(" # a "," # b "): absolute: " \
859 << precisionWrapper(TEST::absdiff) \
861 << precisionWrapper(TEST::absdiff_max_allowed) \
863 << precisionWrapper(TEST::ratio) \
865 << precisionWrapper(TEST::ratio_max_allowed) \
868 std__cout << "message: \n"; \
869 std__cout << TEST::fuzzy_message; \
873 std__cout << " - line " << TEST::test_line << \
874 ": TEST_FILE_SIMILAR(" # a "," # b ") ... -\n"; \
875 std__cout << "message: \n"; \
876 std__cout << TEST::fuzzy_message; \
877 TEST::failed_lines_list.push_back(TEST::test_line); \
894 #define TOLERANCE_RELATIVE(a) \
895 TEST::ratio_max_allowed = (a); \
897 TEST::initialNewline(); \
898 std__cout << " + line " << __LINE__ << \
899 ": TOLERANCE_RELATIVE(" << TEST::ratio_max_allowed << \
900 ") (\"" # a "\")" << std::endl; \
914 #define TOLERANCE_ABSOLUTE(a) \
915 TEST::absdiff_max_allowed = (a); \
917 TEST::initialNewline(); \
918 std__cout << " + line " << __LINE__ << \
919 ": TOLERANCE_ABSOLUTE(" << TEST::absdiff_max_allowed << \
920 ") (\"" # a "\")" << std::endl; \
928 #define WHITELIST(a) TEST::setWhitelist(__FILE__, __LINE__, (a));
942 #define TEST_EXCEPTION(exception_type, command) \
944 ++TEST::test_count; \
945 TEST::test_line = __LINE__; \
946 TEST::exception = 0; \
951 catch (exception_type) \
953 TEST::exception = 1; \
955 catch (::OpenMS::Exception::BaseException e) \
957 TEST::exception = 2; \
958 TEST::exception_name = e.getName(); \
962 TEST::exception = 3; \
964 TEST::this_test = (TEST::exception == 1); \
965 TEST::test = TEST::test && TEST::this_test; \
968 TEST::initialNewline(); \
969 switch (TEST::exception) \
972 std__cout << " - line " << TEST::test_line << \
973 ": TEST_EXCEPTION(" # exception_type "," # command \
974 "): no exception thrown!" << std::endl; \
975 TEST::failed_lines_list.push_back(TEST::test_line); \
978 std__cout << " + line " << TEST::test_line << \
979 ": TEST_EXCEPTION(" # exception_type "," # command \
980 "): OK" << std::endl; \
983 std__cout << " - line " << TEST::test_line << \
984 ": TEST_EXCEPTION(" # exception_type "," # command \
985 "): wrong exception thrown! \"" \
986 << TEST::exception_name << "\"" << std::endl; \
987 TEST::failed_lines_list.push_back(TEST::test_line); \
990 std__cout << " - line " << TEST::test_line << \
991 ": TEST_EXCEPTION(" # exception_type "," # command \
992 "): wrong exception thrown!" << std::endl; \
993 TEST::failed_lines_list.push_back(TEST::test_line); \
1010 #ifdef OPENMS_ASSERTIONS
1011 #define TEST_PRECONDITION_VIOLATED(command) TEST_EXCEPTION(Exception::Precondition, command);
1013 #define TEST_PRECONDITION_VIOLATED(command) STATUS("TEST_PRECONDITION_VIOLATED(" # command ") - skipped");
1027 #ifdef OPENMS_ASSERTIONS
1028 #define TEST_POSTCONDITION_VIOLATED(command) TEST_EXCEPTION(Exception::Postcondition, command);
1030 #define TEST_POSTCONDITION_VIOLATED(command) STATUS("TEST_POSTCONDITION_VIOLATED(" # command ") - skipped");
1050 #define TEST_EXCEPTION_WITH_MESSAGE(exception_type, command, message) \
1052 ++TEST::test_count; \
1053 TEST::test_line = __LINE__; \
1054 TEST::exception = 0; \
1059 catch (exception_type et) \
1061 if (std::string(et.getMessage()) != std::string(message)) \
1063 TEST::exception = 4; \
1064 TEST::exception_message = et.getMessage(); \
1066 else TEST::exception = 1; \
1068 catch (::OpenMS::Exception::BaseException e) \
1070 TEST::exception = 2; \
1071 TEST::exception_name = e.getName(); \
1075 TEST::exception = 3; \
1077 TEST::this_test = (TEST::exception == 1); \
1078 TEST::test = TEST::test && TEST::this_test; \
1081 TEST::initialNewline(); \
1082 switch (TEST::exception) \
1085 std__cout << " - line " << TEST::test_line << \
1086 ": TEST_EXCEPTION_WITH_MESSAGE(" # exception_type "," # command ", " # message \
1087 "): no exception thrown!" << std::endl; \
1088 TEST::failed_lines_list.push_back(TEST::test_line); \
1092 std__cout << " + line " << TEST::test_line << \
1093 ": TEST_EXCEPTION_WITH_MESSAGE(" # exception_type "," # command ", " # message \
1094 "): OK" << std::endl; \
1097 std__cout << " - line " << TEST::test_line << \
1098 ": TEST_EXCEPTION_WITH_MESSAGE(" # exception_type "," # command ", " # message \
1099 "): wrong exception thrown! \"" << \
1100 TEST::exception_name << "\"" << std::endl; \
1101 TEST::failed_lines_list.push_back(TEST::test_line); \
1104 std__cout << " - line " << TEST::test_line << \
1105 ": TEST_EXCEPTION_WITH_MESSAGE(" # exception_type "," # command ", " # message \
1106 "): wrong exception thrown!" << std::endl; \
1107 TEST::failed_lines_list.push_back(TEST::test_line); \
1110 std__cout << " - line " << TEST::test_line << \
1111 ": TEST_EXCEPTION_WITH_MESSAGE(" # exception_type "," # command ", " # message \
1112 "): exception has wrong message: got '" << \
1113 TEST::exception_message << \
1114 "', expected '" << \
1117 TEST::failed_lines_list.push_back(TEST::test_line); \
1138 #define NEW_TMP_FILE(filename) \
1140 filename = TEST::tmpFileName(__FILE__, __LINE__); \
1141 TEST::tmp_file_list.push_back(filename); \
1143 TEST::initialNewline(); \
1144 std__cout << " creating new temporary filename '" \
1160 #define ABORT_IF(condition) \
1164 TEST::initialNewline(); \
1165 std__cout << " - line " << __LINE__ << \
1166 ": ABORT_IF(" # condition "): TEST ABORTED" << \
1168 TEST::failed_lines_list.push_back(TEST::test_line); \
1190 #define STATUS(message) \
1192 TEST::initialNewline(); \
1193 std__cout << " line " \
1209 #define ADD_MESSAGE(message) \
1210 TEST::add_message = message;
1221 #define NOT_TESTABLE \
1222 TEST::test_count = 1;
#define std__cout
Provide a point of redirection for testing the test macros, see ClassTest_test.cpp.
Definition: ClassTest.h:65
bool this_test
Status of last elementary test.
bool isRealType(float)
This overload returns true; float is a floating point type.
Definition: ClassTest.h:89
bool validate(const std::vector< std::string > &file_names)
Validates the given files against the XML schema (if available)
bool isFileSimilar(const std::string &filename_1, const std::string &filename_2)
Compare files using absdiff_max_allowed and ratio_max_allowed.
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
char line_buffer[65536]
(A buffer for one line from a file. Used by TEST_FILE_EQUAL.)
double absdiff_max
Maximum difference of numbers observed so far, see TOLERANCE_ABSOLUTE.
double ratio_max_allowed
Maximum ratio of numbers allowed, see TOLERANCE_RELATIVE.
std::string fuzzy_message
Last message from a fuzzy comparison. Written by isRealSimilar(), testStringSimilar(),...
double ratio
Recent ratio of numbers, see TOLERANCE_RELATIVE.
std::ifstream templatefile
Template (correct) file used by TEST_FILE_EQUAL.
int exception
(Used by various macros. Indicates a rough category of the exception being caught....
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 newline
(Flags whether a new line is in place, depending on context and verbosity setting....
std::string exception_name
(Used by various macros. Stores the "name" of the exception, if applicable.)
int start_section_line
Line where current subsection started.
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 Int
Signed integer type.
Definition: Types.h:102
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
std::string add_message
See ADD_MESSAGE.
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.
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition: DataValue.h:56
std::vector< std::string > tmp_file_list
List of tmp file names (these will be cleaned up, see NEW_TMP_FILE)
void setWhitelist(const char *const, const int line, const std::string &whitelist)
set the whitelist_
void initialNewline()
make sure we have a newline before results from first subtest
std::vector< UInt > failed_lines_list
List of all failed lines for summary at the end of the test.
double ratio_max
Maximum ratio of numbers observed so far, see TOLERANCE_RELATIVE.
double absdiff
Recent absolute difference of numbers, see TOLERANCE_ABSOLUTE.
bool isRealSimilar(long double number_1, long double number_2)
used by testRealSimilar()
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.
std::string exception_message
(Used by various macros. Stores the "message" of the exception, if applicable.)
bool equal_files
(A variable used by TEST_FILE_EQUAL)
bool test
Status of the current subsection.
int verbose
Verbosity level ( "-v" is 1 and "-V" is 2 )
const char * version_string
Version string supplied with START_TEST.
bool all_tests
Status of the whole test.
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 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
Namespace for class tests.
Definition: ClassTest.h:73
std::string test_name
Name of current subsection.
int test_line
Line of current elementary test.
std::ifstream infile
Questionable file tested by TEST_FILE_EQUAL.