00001 #ifndef TEST_HPP_
00002 #define TEST_HPP_
00003
00004 #include <exception>
00005 #include <iostream>
00006 #include <ostream>
00007
00008
00009
00010 #define TEST_STRINGIFY(x) #x
00011
00012
00013
00014 inline void test_failed(char const* expr, char const* file, int line)
00015 {
00016 std::cerr << file << ", line " << line << ": test failed: " << expr << '\n';
00017 }
00018
00019
00020
00021 inline void test_run(bool condition, char const* expr, char const* file, int line)
00022 {
00023 if (not condition)
00024 test_failed(expr, file, line);
00025 }
00026
00027
00028
00029 inline void test_exception(std::exception const& ex, char const* expr, char const* file, int line)
00030 {
00031 std::string msg( expr );
00032 msg += " threw an exception: ";
00033 msg += ex.what();
00034 test_failed(msg.c_str(), file, line);
00035 }
00036
00045 #define TEST(x) \
00046 try {\
00047 test_run(x, TEST_STRINGIFY(x), __FILE__, __LINE__);\
00048 }\
00049 catch(std::exception const& ex)\
00050 {\
00051 test_exception(ex, TEST_STRINGIFY(x), __FILE__, __LINE__);\
00052 }
00053
00054 #endif