test.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // test.hpp
  3. //
  4. // Copyright 2008 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. #ifndef BOOST_XPRESSIVE_TEST_TEST_HPP_EAN_10_04_2005
  8. #define BOOST_XPRESSIVE_TEST_TEST_HPP_EAN_10_04_2005
  9. // MS compatible compilers support #pragma once
  10. #if defined(_MSC_VER)
  11. # pragma once
  12. #endif
  13. #include <string>
  14. #include <vector>
  15. #include <cstdio>
  16. #include <cstdarg>
  17. #include <functional>
  18. #include <boost/range/iterator_range.hpp>
  19. #include <boost/xpressive/xpressive_static.hpp>
  20. #include <boost/test/unit_test.hpp>
  21. using namespace boost::unit_test;
  22. using namespace boost::xpressive;
  23. #define L(x) BOOST_XPR_CSTR_(char_type, x)
  24. #define BOOST_XPR_CHECK(pred) \
  25. if( pred ) {} else { BOOST_ERROR( this->section_ << " : " << #pred ); }
  26. using namespace boost::xpressive;
  27. ///////////////////////////////////////////////////////////////////////////////
  28. // backrefs
  29. //
  30. #if defined(__cplusplus_cli)
  31. #pragma managed(push, off)
  32. #endif
  33. template<typename Char>
  34. inline std::vector<std::basic_string<Char> > backrefs(Char const *br0, ...)
  35. {
  36. using namespace std;
  37. std::vector<std::basic_string<Char> > backrefs;
  38. if(0 != br0)
  39. {
  40. backrefs.push_back(br0);
  41. va_list va;
  42. va_start(va, br0);
  43. Char const *brN;
  44. while(0 != (brN = va_arg(va, Char const *)))
  45. {
  46. backrefs.push_back(brN);
  47. }
  48. va_end(va);
  49. }
  50. return backrefs;
  51. }
  52. #if defined(__cplusplus_cli)
  53. #pragma managed(pop)
  54. #endif
  55. ///////////////////////////////////////////////////////////////////////////////
  56. //
  57. struct no_match_t {};
  58. no_match_t const no_match = {};
  59. ///////////////////////////////////////////////////////////////////////////////
  60. // xpr_test_case
  61. //
  62. template<typename BidiIter>
  63. struct xpr_test_case
  64. {
  65. typedef BidiIter iterator_type;
  66. typedef typename boost::iterator_value<iterator_type>::type char_type;
  67. typedef basic_regex<iterator_type> regex_type;
  68. typedef std::basic_string<char_type> string_type;
  69. typedef std::vector<string_type> backrefs_type;
  70. xpr_test_case(std::string section, string_type str, regex_type rex, backrefs_type brs)
  71. : section_(section)
  72. , str_(str)
  73. , rex_(rex)
  74. , brs_(brs)
  75. {
  76. }
  77. xpr_test_case(std::string section, string_type str, regex_type rex, no_match_t)
  78. : section_(section)
  79. , str_(str)
  80. , rex_(rex)
  81. , brs_()
  82. {
  83. }
  84. void run() const
  85. {
  86. char_type const empty[] = {0};
  87. match_results<BidiIter> what;
  88. if(regex_search(this->str_, what, this->rex_))
  89. {
  90. // match succeeded: was it expected to succeed?
  91. BOOST_XPR_CHECK(what.size() == this->brs_.size());
  92. for(std::size_t i = 0; i < what.size() && i < this->brs_.size(); ++i)
  93. {
  94. BOOST_XPR_CHECK((!what[i].matched && this->brs_[i] == empty) || this->brs_[i] == what[i].str());
  95. }
  96. }
  97. else
  98. {
  99. // match failed: was it expected to fail?
  100. BOOST_XPR_CHECK(0 == this->brs_.size());
  101. }
  102. }
  103. private:
  104. std::string section_;
  105. string_type str_;
  106. regex_type rex_;
  107. std::vector<string_type> brs_;
  108. };
  109. ///////////////////////////////////////////////////////////////////////////////
  110. // test_runner
  111. template<typename BidiIter>
  112. struct test_runner
  113. {
  114. typedef xpr_test_case<BidiIter> argument_type;
  115. typedef void result_type;
  116. void operator ()(xpr_test_case<BidiIter> const &test) const
  117. {
  118. test.run();
  119. }
  120. };
  121. ///////////////////////////////////////////////////////////////////////////////
  122. // helpful debug routines
  123. ///////////////////////////////////////////////////////////////////////////////
  124. ///////////////////////////////////////////////////////////////////////////////
  125. // remove all occurances of sz from str
  126. inline void string_remove(std::string &str, char const *sz)
  127. {
  128. std::string::size_type i = 0, n = std::strlen(sz);
  129. while(std::string::npos != (i=str.find(sz,i)))
  130. {
  131. str.erase(i,n);
  132. }
  133. }
  134. ///////////////////////////////////////////////////////////////////////////////
  135. // display_type2
  136. // for type T, write typeid::name after performing some substitutions
  137. template<typename T>
  138. inline void display_type2()
  139. {
  140. std::string str = typeid(T).name();
  141. string_remove(str, "struct ");
  142. string_remove(str, "boost::");
  143. string_remove(str, "xpressive::");
  144. string_remove(str, "detail::");
  145. string_remove(str, "fusion::");
  146. //std::printf("%s\n\n", str.c_str());
  147. std::printf("%s\nwdith=%d\nuse_simple_repeat=%s\n\n", str.c_str()
  148. , detail::width_of<T, char>::value
  149. , detail::use_simple_repeat<T, char>::value ? "true" : "false");
  150. }
  151. ///////////////////////////////////////////////////////////////////////////////
  152. // display_type
  153. // display the type of the deduced template argument
  154. template<typename T>
  155. inline void display_type(T const &)
  156. {
  157. display_type2<T>();
  158. }
  159. #endif