Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 1 | // (C) Copyright Gennadiy Rozental 2001-2008. |
| 2 | // (C) Copyright Beman Dawes 1995-2001. |
| 3 | // Distributed under the Boost Software License, Version 1.0. |
| 4 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 5 | // http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | // See http://www.boost.org/libs/test for the library home page. |
| 8 | // |
| 9 | // File : $RCSfile$ |
| 10 | // |
| 11 | // Version : $Revision: 49312 $ |
| 12 | // |
| 13 | // Description : main function implementation for Program Executon Monitor |
| 14 | // *************************************************************************** |
| 15 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 16 | #ifndef NDNBOOST_TEST_CPP_MAIN_IPP_012205GER |
| 17 | #define NDNBOOST_TEST_CPP_MAIN_IPP_012205GER |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 18 | |
| 19 | // Boost.Test |
| 20 | #include <ndnboost/test/execution_monitor.hpp> |
| 21 | #include <ndnboost/test/detail/config.hpp> |
| 22 | #include <ndnboost/test/utils/basic_cstring/io.hpp> |
| 23 | |
| 24 | // Boost |
| 25 | #include <ndnboost/cstdlib.hpp> // for exit codes |
| 26 | #include <ndnboost/config.hpp> // for workarounds |
| 27 | |
| 28 | // STL |
| 29 | #include <iostream> |
| 30 | #include <cstdlib> // std::getenv |
| 31 | #include <cstring> // std::strerror |
| 32 | |
| 33 | #include <ndnboost/test/detail/suppress_warnings.hpp> |
| 34 | |
| 35 | //____________________________________________________________________________// |
| 36 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 37 | #ifdef NDNBOOST_NO_STDC_NAMESPACE |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 38 | namespace std { using ::getenv; using ::strerror; } |
| 39 | #endif |
| 40 | |
| 41 | namespace { |
| 42 | |
| 43 | struct cpp_main_caller { |
| 44 | cpp_main_caller( int (*cpp_main_func)( int argc, char* argv[] ), int argc, char** argv ) |
| 45 | : m_cpp_main_func( cpp_main_func ) |
| 46 | , m_argc( argc ) |
| 47 | , m_argv( argv ) {} |
| 48 | |
| 49 | int operator()() { return (*m_cpp_main_func)( m_argc, m_argv ); } |
| 50 | |
| 51 | private: |
| 52 | // Data members |
| 53 | int (*m_cpp_main_func)( int argc, char* argv[] ); |
| 54 | int m_argc; |
| 55 | char** m_argv; |
| 56 | }; |
| 57 | |
| 58 | } // local namespace |
| 59 | |
| 60 | // ************************************************************************** // |
| 61 | // ************** prg_exec_monitor_main ************** // |
| 62 | // ************************************************************************** // |
| 63 | |
| 64 | namespace ndnboost { |
| 65 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 66 | int NDNBOOST_TEST_DECL |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 67 | prg_exec_monitor_main( int (*cpp_main)( int argc, char* argv[] ), int argc, char* argv[] ) |
| 68 | { |
| 69 | int result = 0; |
| 70 | |
| 71 | try { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 72 | ndnboost::unit_test::const_string p( std::getenv( "NDNBOOST_TEST_CATCH_SYSTEM_ERRORS" ) ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 73 | ::ndnboost::execution_monitor ex_mon; |
| 74 | |
| 75 | ex_mon.p_catch_system_errors.value = p != "no"; |
| 76 | |
| 77 | result = ex_mon.execute( |
| 78 | ::ndnboost::unit_test::callback0<int>( cpp_main_caller( cpp_main, argc, argv ) ) ); |
| 79 | |
| 80 | if( result == 0 ) |
| 81 | result = ::ndnboost::exit_success; |
| 82 | else if( result != ::ndnboost::exit_success ) { |
| 83 | std::cout << "\n**** error return code: " << result << std::endl; |
| 84 | result = ::ndnboost::exit_failure; |
| 85 | } |
| 86 | } |
| 87 | catch( ::ndnboost::execution_exception const& exex ) { |
| 88 | std::cout << "\n**** exception(" << exex.code() << "): " << exex.what() << std::endl; |
| 89 | result = ::ndnboost::exit_exception_failure; |
| 90 | } |
| 91 | catch( ::ndnboost::system_error const& ex ) { |
| 92 | std::cout << "\n**** failed to initialize execution monitor." |
| 93 | << "\n**** expression at fault: " << ex.p_failed_exp |
| 94 | << "\n**** error(" << ex.p_errno << "): " << std::strerror( ex.p_errno ) << std::endl; |
| 95 | result = ::ndnboost::exit_exception_failure; |
| 96 | } |
| 97 | |
| 98 | if( result != ::ndnboost::exit_success ) { |
| 99 | std::cerr << "******** errors detected; see standard output for details ********" << std::endl; |
| 100 | } |
| 101 | else { |
| 102 | // Some prefer a confirming message when all is well, while others don't |
| 103 | // like the clutter. Use an environment variable to avoid command |
| 104 | // line argument modifications; for use in production programs |
| 105 | // that's a no-no in some organizations. |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 106 | ::ndnboost::unit_test::const_string p( std::getenv( "NDNBOOST_PRG_MON_CONFIRM" ) ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 107 | if( p != "no" ) { |
| 108 | std::cerr << std::flush << "no errors detected" << std::endl; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return result; |
| 113 | } |
| 114 | |
| 115 | } // namespace ndnboost |
| 116 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 117 | #if !defined(NDNBOOST_TEST_DYN_LINK) && !defined(NDNBOOST_TEST_NO_MAIN) |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 118 | |
| 119 | // ************************************************************************** // |
| 120 | // ************** main function for tests using lib ************** // |
| 121 | // ************************************************************************** // |
| 122 | |
| 123 | int cpp_main( int argc, char* argv[] ); // prototype for user's cpp_main() |
| 124 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 125 | int NDNBOOST_TEST_CALL_DECL |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 126 | main( int argc, char* argv[] ) |
| 127 | { |
| 128 | return ::ndnboost::prg_exec_monitor_main( &cpp_main, argc, argv ); |
| 129 | } |
| 130 | |
| 131 | //____________________________________________________________________________// |
| 132 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 133 | #endif // !NDNBOOST_TEST_DYN_LINK && !NDNBOOST_TEST_NO_MAIN |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 134 | |
| 135 | //____________________________________________________________________________// |
| 136 | |
| 137 | #include <ndnboost/test/detail/enable_warnings.hpp> |
| 138 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 139 | #endif // NDNBOOST_TEST_CPP_MAIN_IPP_012205GER |