- Main Page
- Classes
- Files

# test.hpp

    00001 #ifndef TEST_HPP_
    00002 #define TEST_HPP_
    00003
    00004 #include <exception>
    00005 #include <iostream>
    00006 #include <ostream>
    00007
    00008 // For internal use by the test() macro.
    00009 // Turn the macro argument into a character string literal
    00010 #define test_stringify(x) #x
    00011
    00012 // For internal use by the test() macro.
    00013 // Report a test failure.
    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 // For internal use by the test() macro
    00020 // Run a test. Report a failure if the condition is false or
    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 // For internal use by the test() macro.
    00028 // Report an exception.
    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