Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 1 | // (C) Copyright Gennadiy Rozental 2005-2008. |
| 2 | // Use, modification, and distribution are subject to the |
| 3 | // Boost Software License, ELOG_VER 1.0. (See accompanying file |
| 4 | // http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | // See http://www.boost.org/libs/test for the library home page. |
| 7 | // |
| 8 | // File : $RCSfile$ |
| 9 | // |
| 10 | // Version : $Revision: 54633 $ |
| 11 | // |
| 12 | // Description : Facilities to perform interaction based testng of logged expectations |
| 13 | // *************************************************************************** |
| 14 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 15 | #ifndef NDNBOOST_TEST_LOGGED_EXPECTATIONS_IPP_120905GER |
| 16 | #define NDNBOOST_TEST_LOGGED_EXPECTATIONS_IPP_120905GER |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 17 | |
| 18 | // Boost.Test |
| 19 | #include <ndnboost/test/detail/config.hpp> |
| 20 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 21 | #if NDNBOOST_TEST_SUPPORT_INTERACTION_TESTING |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 22 | |
| 23 | #include <ndnboost/test/detail/global_typedef.hpp> |
| 24 | |
| 25 | #include <ndnboost/test/utils/callback.hpp> |
| 26 | #include <ndnboost/test/utils/iterator/token_iterator.hpp> |
| 27 | |
| 28 | #include <ndnboost/test/interaction_based.hpp> |
| 29 | #include <ndnboost/test/test_tools.hpp> |
| 30 | |
| 31 | #include <ndnboost/test/detail/suppress_warnings.hpp> |
| 32 | |
| 33 | // Boost |
| 34 | #include <ndnboost/lexical_cast.hpp> |
| 35 | |
| 36 | // STL |
| 37 | #include <fstream> |
| 38 | |
| 39 | //____________________________________________________________________________// |
| 40 | |
| 41 | namespace ndnboost { |
| 42 | |
| 43 | using namespace ::ndnboost::unit_test; |
| 44 | |
| 45 | namespace itest { |
| 46 | |
| 47 | // ************************************************************************** // |
| 48 | // ************** logged expectation test implementation ************** // |
| 49 | // ************************************************************************** // |
| 50 | |
| 51 | struct expectations_logger : itest::manager { |
| 52 | // Constructor |
| 53 | expectations_logger( const_string log_file_name, bool test_or_log ); |
| 54 | |
| 55 | virtual bool decision_point( const_string, std::size_t ); |
| 56 | virtual unsigned enter_scope( const_string, std::size_t, const_string scope_name ); |
| 57 | virtual void allocated( const_string, std::size_t, void*, std::size_t s ); |
| 58 | virtual void data_flow( const_string d ); |
| 59 | virtual std::string return_value( const_string default_value ); |
| 60 | |
| 61 | private: |
| 62 | // Data members |
| 63 | bool m_test_or_log; |
| 64 | std::fstream m_log_file; |
| 65 | }; |
| 66 | |
| 67 | literal_string ELOG_VER = "1.0"; |
| 68 | literal_string CLMN_SEP = "|"; |
| 69 | static const char LINE_SEP = '\n'; |
| 70 | |
| 71 | literal_string FILE_SIG = "ELOG"; |
| 72 | literal_string SCOPE_SIG = "SCOPE"; |
| 73 | literal_string ALLOC_SIG = "ALLOC"; |
| 74 | literal_string DP_SIG = "SWITCH"; |
| 75 | literal_string DATA_SIG = "DATA"; |
| 76 | literal_string RETURN_SIG = "RETURN"; |
| 77 | |
| 78 | //____________________________________________________________________________// |
| 79 | |
| 80 | expectations_logger::expectations_logger( const_string log_file_name, bool test_or_log ) |
| 81 | : m_test_or_log( test_or_log ) |
| 82 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 83 | NDNBOOST_REQUIRE_MESSAGE( !log_file_name.is_empty(), "Empty expectations log file name" ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 84 | |
| 85 | m_log_file.open( log_file_name.begin(), test_or_log ? std::ios::in : std::ios::out ); |
| 86 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 87 | NDNBOOST_REQUIRE_MESSAGE( m_log_file.is_open(), |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 88 | "Can't open expectations log file " << log_file_name |
| 89 | << " for " << ( m_test_or_log ? "reading" : "writing") ); |
| 90 | |
| 91 | if( m_test_or_log ) { |
| 92 | std::string line; |
| 93 | |
| 94 | std::getline( m_log_file, line, LINE_SEP ); |
| 95 | |
| 96 | const_string cline( line ); |
| 97 | string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none)); |
| 98 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 99 | NDNBOOST_CHECK_EQUAL( *tit, FILE_SIG ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 100 | ++tit; |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 101 | NDNBOOST_CHECK_EQUAL( *tit, ELOG_VER ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 102 | } |
| 103 | else { |
| 104 | m_log_file << FILE_SIG << CLMN_SEP << ELOG_VER << LINE_SEP; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | //____________________________________________________________________________// |
| 109 | |
| 110 | bool |
| 111 | expectations_logger::decision_point( const_string, std::size_t ) |
| 112 | { |
| 113 | if( m_test_or_log ) { |
| 114 | std::string line; |
| 115 | |
| 116 | std::getline( m_log_file, line, LINE_SEP ); |
| 117 | |
| 118 | const_string cline( line ); |
| 119 | string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none)); |
| 120 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 121 | NDNBOOST_CHECK_EQUAL( *tit, DP_SIG ); ++tit; |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 122 | return lexical_cast<bool>( *tit ); |
| 123 | } |
| 124 | else { |
| 125 | m_log_file << DP_SIG << CLMN_SEP << std::boolalpha << true << LINE_SEP; |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | //____________________________________________________________________________// |
| 132 | |
| 133 | unsigned |
| 134 | expectations_logger::enter_scope( const_string, std::size_t, const_string scope_name ) |
| 135 | { |
| 136 | if( m_test_or_log ) { |
| 137 | std::string line; |
| 138 | |
| 139 | std::getline( m_log_file, line, LINE_SEP ); |
| 140 | |
| 141 | const_string cline( line ); |
| 142 | string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none)); |
| 143 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 144 | NDNBOOST_CHECK_EQUAL( *tit, SCOPE_SIG ); ++tit; |
| 145 | NDNBOOST_CHECK_EQUAL( *tit, scope_name ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 146 | } |
| 147 | else { |
| 148 | m_log_file << SCOPE_SIG << CLMN_SEP << scope_name << LINE_SEP; |
| 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | //____________________________________________________________________________// |
| 155 | |
| 156 | void |
| 157 | expectations_logger::allocated( const_string, std::size_t, void*, std::size_t s ) |
| 158 | { |
| 159 | if( m_test_or_log ) { |
| 160 | std::string line; |
| 161 | |
| 162 | std::getline( m_log_file, line, LINE_SEP ); |
| 163 | |
| 164 | const_string cline( line ); |
| 165 | string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none)); |
| 166 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 167 | NDNBOOST_CHECK_EQUAL( *tit, ALLOC_SIG ); ++tit; |
| 168 | NDNBOOST_CHECK_EQUAL( lexical_cast<std::size_t>( *tit ), s ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 169 | } |
| 170 | else { |
| 171 | m_log_file << ALLOC_SIG << CLMN_SEP << s << LINE_SEP; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | //____________________________________________________________________________// |
| 176 | |
| 177 | void |
| 178 | expectations_logger::data_flow( const_string d ) |
| 179 | { |
| 180 | if( m_test_or_log ) { |
| 181 | std::string line; |
| 182 | |
| 183 | std::getline( m_log_file, line, LINE_SEP ); |
| 184 | |
| 185 | const_string cline( line ); |
| 186 | string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none)); |
| 187 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 188 | NDNBOOST_CHECK_EQUAL( *tit, DATA_SIG ); ++tit; |
| 189 | NDNBOOST_CHECK_EQUAL( *tit, d ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 190 | } |
| 191 | else { |
| 192 | m_log_file << DATA_SIG << CLMN_SEP << d << LINE_SEP; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | //____________________________________________________________________________// |
| 197 | |
| 198 | std::string |
| 199 | expectations_logger::return_value( const_string default_value ) |
| 200 | { |
| 201 | if( m_test_or_log ) { |
| 202 | std::string line; |
| 203 | |
| 204 | std::getline( m_log_file, line, LINE_SEP ); |
| 205 | |
| 206 | const_string cline( line ); |
| 207 | string_token_iterator tit( cline, (dropped_delimeters = CLMN_SEP, kept_delimeters = dt_none)); |
| 208 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 209 | NDNBOOST_CHECK_EQUAL( *tit, RETURN_SIG ); ++tit; |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 210 | |
| 211 | return std::string( tit->begin(), tit->size() ); |
| 212 | } |
| 213 | else { |
| 214 | m_log_file << RETURN_SIG << CLMN_SEP << default_value << LINE_SEP; |
| 215 | |
| 216 | return std::string(); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | //____________________________________________________________________________// |
| 221 | |
| 222 | // ************************************************************************** // |
| 223 | // ************** logged expectations test ************** // |
| 224 | // ************************************************************************** // |
| 225 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 226 | void NDNBOOST_TEST_DECL |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 227 | logged_expectations( callback0<> const& F, const_string log_file_name, bool test_or_log ) |
| 228 | { |
| 229 | expectations_logger el( log_file_name, test_or_log ); |
| 230 | |
| 231 | F(); |
| 232 | } |
| 233 | |
| 234 | //____________________________________________________________________________// |
| 235 | |
| 236 | } // namespace itest |
| 237 | |
| 238 | } // namespace ndnboost |
| 239 | |
| 240 | //____________________________________________________________________________// |
| 241 | |
| 242 | #include <ndnboost/test/detail/enable_warnings.hpp> |
| 243 | |
| 244 | #endif // not ancient compiler |
| 245 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 246 | #endif // NDNBOOST_TEST_LOGGED_EXPECTATIONS_IPP_120905GER |