/////////////////////////////////////////////////////////////////////////////// // test.hpp // // Copyright 2004 Eric Niebler. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_XPRESSIVE_TEST_TEST_HPP_EAN_10_04_2005 #define BOOST_XPRESSIVE_TEST_TEST_HPP_EAN_10_04_2005 // MS compatible compilers support #pragma once #if defined(_MSC_VER) && (_MSC_VER >= 1020) # pragma once #endif #include #include #include #include #include #include #include #include "./test_minimal.hpp" using namespace boost::xpressive; #define L(x) BOOST_XPR_CSTR_(char_type, x) #define BOOST_XPR_CHECK(pred) \ if( pred ) {} else { BOOST_ERROR( this->format_msg(#pred).c_str() ); } using namespace boost::xpressive; /////////////////////////////////////////////////////////////////////////////// // backrefs // template inline std::vector > backrefs(Char const *br0, ...) { using namespace std; std::vector > backrefs; if(0 != br0) { backrefs.push_back(br0); va_list va; va_start(va, br0); Char const *brN; while(0 != (brN = va_arg(va, Char const *))) { backrefs.push_back(brN); } va_end(va); } return backrefs; } /////////////////////////////////////////////////////////////////////////////// // struct no_match_t {}; no_match_t const no_match = {}; template struct test_case; template std::string format_msg(test_case const &test, char const *msg); /////////////////////////////////////////////////////////////////////////////// // test_case // template struct test_case { typedef BidiIter iterator_type; typedef typename boost::iterator_value::type char_type; typedef basic_regex regex_type; typedef std::basic_string string_type; typedef std::vector backrefs_type; test_case(std::string section, string_type str, regex_type rex, backrefs_type brs) : section_(section) , str_(str) , rex_(rex) , brs_(brs) { } test_case(std::string section, string_type str, regex_type rex, no_match_t) : section_(section) , str_(str) , rex_(rex) , brs_() { } void run() const { char_type const empty[] = {0}; match_results what; if(regex_search(this->str_, what, this->rex_)) { // match succeeded: was it expected to succeed? BOOST_XPR_CHECK(what.size() == this->brs_.size()); for(std::size_t i = 0; i < what.size() && i < this->brs_.size(); ++i) { BOOST_XPR_CHECK(!what[i].matched && this->brs_[i] == empty || this->brs_[i] == what[i].str()); } } else { // match failed: was it expected to fail? BOOST_XPR_CHECK(0 == this->brs_.size()); } } private: std::string format_msg(char const *msg) const { return this->section_ + " : " + msg; } std::string section_; string_type str_; regex_type rex_; std::vector brs_; }; /////////////////////////////////////////////////////////////////////////////// // test_runner template struct test_runner : std::unary_function, void> { void operator ()(test_case const &test) const { test.run(); } }; /////////////////////////////////////////////////////////////////////////////// // helpful debug routines /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// // remove all occurances of sz from str inline void string_remove(std::string &str, char const *sz) { std::string::size_type i = 0, n = std::strlen(sz); while(std::string::npos != (i=str.find(sz,i))) { str.erase(i,n); } } /////////////////////////////////////////////////////////////////////////////// // display_type2 // for type T, write typeid::name after performing some substitutions template inline void display_type2() { std::string str = typeid(T).name(); string_remove(str, "struct "); string_remove(str, "boost::"); string_remove(str, "xpressive::"); string_remove(str, "detail::"); string_remove(str, "fusion::"); //std::printf("%s\n\n", str.c_str()); std::printf("%s\nwdith=%d\nis_pure=%s\n\n", str.c_str() , detail::width_of::value , detail::is_pure::value ? "true" : "false"); } /////////////////////////////////////////////////////////////////////////////// // display_type // display the type of the deduced template argument template inline void display_type(T const &) { display_type2(); } /////////////////////////////////////////////////////////////////////////////// // test_compile // try to compile a given static regular expression template inline void test_compile(Xpr const &xpr) { typedef typename boost::iterator_value::type char_type; typedef boost::xpressive::regex_traits traits_type; boost::xpressive::detail::xpression_visitor visitor; display_type(boost::proto::compile( xpr , boost::xpressive::detail::end_xpression() , visitor , boost::xpressive::detail::seq_tag() )); } #endif