| 1 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | // test_minimal.hpp |
|---|
| 3 | // |
|---|
| 4 | // Copyright 2004 Eric Niebler. Distributed under the Boost |
|---|
| 5 | // Software License, Version 1.0. (See accompanying file |
|---|
| 6 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 7 | |
|---|
| 8 | #ifndef BOOST_XPRESSIVE_TEST_REGRESS_HPP_EAN_10_04_2005 |
|---|
| 9 | #define BOOST_XPRESSIVE_TEST_REGRESS_HPP_EAN_10_04_2005 |
|---|
| 10 | |
|---|
| 11 | #include <string> |
|---|
| 12 | #include <cstring> |
|---|
| 13 | #include <iostream> |
|---|
| 14 | #include <stdexcept> |
|---|
| 15 | #include <boost/current_function.hpp> |
|---|
| 16 | |
|---|
| 17 | template<typename T = void> |
|---|
| 18 | struct counter |
|---|
| 19 | { |
|---|
| 20 | static int value; |
|---|
| 21 | }; |
|---|
| 22 | |
|---|
| 23 | template<typename T> |
|---|
| 24 | int counter<T>::value = 0; |
|---|
| 25 | |
|---|
| 26 | struct execution_aborted |
|---|
| 27 | { |
|---|
| 28 | }; |
|---|
| 29 | |
|---|
| 30 | inline void |
|---|
| 31 | report_error(const char *msg, const char *file, int line, const char *func_name, bool is_msg = false) |
|---|
| 32 | { |
|---|
| 33 | ++counter<>::value; |
|---|
| 34 | std::cerr << file << "(" << line << "): "; |
|---|
| 35 | |
|---|
| 36 | if(is_msg) |
|---|
| 37 | { |
|---|
| 38 | std::cerr << msg; |
|---|
| 39 | } |
|---|
| 40 | else |
|---|
| 41 | { |
|---|
| 42 | std::cerr << "test " << msg << " failed"; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | if(0 == std::strcmp(func_name, "(unknown)")) |
|---|
| 46 | { |
|---|
| 47 | std::cerr << " in function: '" << func_name << "'"; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | std::cerr << std::endl; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | inline void |
|---|
| 54 | report_critical_error(const char *msg, const char *file, int line, const char *func_name, bool is_msg = false) |
|---|
| 55 | { |
|---|
| 56 | report_error(msg, file, line, func_name, is_msg); |
|---|
| 57 | throw execution_aborted(); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | #define BOOST_CHECK(exp) \ |
|---|
| 61 | ((exp) \ |
|---|
| 62 | ? static_cast<void>(0) \ |
|---|
| 63 | : report_error(#exp, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION)) |
|---|
| 64 | |
|---|
| 65 | #define BOOST_ERROR(msg_) \ |
|---|
| 66 | report_error((msg_), __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, true) |
|---|
| 67 | |
|---|
| 68 | #define BOOST_REQUIRE(exp) \ |
|---|
| 69 | ((exp) \ |
|---|
| 70 | ? static_cast<void>(0) \ |
|---|
| 71 | : report_critical_error(#exp, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION)) |
|---|
| 72 | |
|---|
| 73 | #define BOOST_FAIL(msg_) \ |
|---|
| 74 | report_critical_error((msg_) ,__FILE__, __LINE__, BOOST_CURRENT_FUNCTION, true) |
|---|
| 75 | |
|---|
| 76 | int test_main(int argc, char *argv[]); |
|---|
| 77 | |
|---|
| 78 | int main(int argc, char *argv[]) |
|---|
| 79 | { |
|---|
| 80 | try |
|---|
| 81 | { |
|---|
| 82 | int run_result = test_main(argc, argv); |
|---|
| 83 | BOOST_CHECK(run_result == 0); |
|---|
| 84 | } |
|---|
| 85 | catch(std::exception const &ex) |
|---|
| 86 | { |
|---|
| 87 | std::string msg = std::string("exception \"") + ex.what() + "\" caught"; |
|---|
| 88 | BOOST_ERROR(msg.c_str()); |
|---|
| 89 | std::cerr << "\n**** Testing aborted."; |
|---|
| 90 | ++counter<>::value; |
|---|
| 91 | } |
|---|
| 92 | catch(execution_aborted) |
|---|
| 93 | { |
|---|
| 94 | std::cerr << "\n**** Testing aborted."; |
|---|
| 95 | ++counter<>::value; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | if(counter<>::value != 0) |
|---|
| 99 | { |
|---|
| 100 | std::cerr << "\n**** " << counter<>::value |
|---|
| 101 | << " error" << (counter<>::value > 1 ? "s" : "") << " detected\n"; |
|---|
| 102 | return -1; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | std::cout << "\n**** no errors detected\n"; |
|---|
| 106 | return 0; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | #endif |
|---|