test_non_char.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // test_non_char.cpp
  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. #include <algorithm>
  8. #include <boost/xpressive/xpressive.hpp>
  9. #include <boost/xpressive/traits/null_regex_traits.hpp>
  10. #include "./test.hpp"
  11. ///////////////////////////////////////////////////////////////////////////////
  12. // test_static
  13. void test_static()
  14. {
  15. static int const data[] = {0, 1, 2, 3, 4, 5, 6};
  16. null_regex_traits<int> nul;
  17. basic_regex<int const *> rex = imbue(nul)(1 >> +((set= 2,3) | 4) >> 5);
  18. match_results<int const *> what;
  19. if(!regex_search(data, data + (sizeof(data)/sizeof(*data)), what, rex))
  20. {
  21. BOOST_ERROR("regex_search on integral data failed");
  22. }
  23. else
  24. {
  25. BOOST_CHECK(*what[0].first == 1);
  26. BOOST_CHECK(*what[0].second == 6);
  27. }
  28. }
  29. ///////////////////////////////////////////////////////////////////////////////
  30. // UChar
  31. struct UChar
  32. {
  33. UChar(unsigned int code = 0)
  34. : code_(code)
  35. {}
  36. operator unsigned int () const
  37. {
  38. return this->code_;
  39. }
  40. private:
  41. unsigned int code_;
  42. };
  43. ///////////////////////////////////////////////////////////////////////////////
  44. // UChar_traits
  45. struct UChar_traits
  46. : null_regex_traits<UChar>
  47. {};
  48. ///////////////////////////////////////////////////////////////////////////////
  49. // test_dynamic
  50. void test_dynamic()
  51. {
  52. typedef std::vector<UChar>::const_iterator uchar_iterator;
  53. typedef basic_regex<uchar_iterator> uregex;
  54. typedef match_results<uchar_iterator> umatch;
  55. typedef regex_compiler<uchar_iterator, UChar_traits> uregex_compiler;
  56. std::string pattern_("b.*r"), str_("foobarboo");
  57. std::vector<UChar> pattern(pattern_.begin(), pattern_.end());
  58. std::vector<UChar> str(str_.begin(), str_.end());
  59. UChar_traits tr;
  60. uregex_compiler compiler(tr);
  61. uregex urx = compiler.compile(pattern);
  62. umatch what;
  63. if(!regex_search(str, what, urx))
  64. {
  65. BOOST_ERROR("regex_search on UChar failed");
  66. }
  67. else
  68. {
  69. BOOST_CHECK_EQUAL(3, what.position());
  70. BOOST_CHECK_EQUAL(3, what.length());
  71. }
  72. // test for range-based regex_replace
  73. std::vector<UChar> output = regex_replace(str, urx, pattern_);
  74. std::string output_(output.begin(), output.end());
  75. std::string expected("foob.*rboo");
  76. BOOST_CHECK_EQUAL(output_, expected);
  77. }
  78. ///////////////////////////////////////////////////////////////////////////////
  79. // init_unit_test_suite
  80. //
  81. test_suite* init_unit_test_suite( int argc, char* argv[] )
  82. {
  83. test_suite *test = BOOST_TEST_SUITE("test_non_char");
  84. test->add(BOOST_TEST_CASE(&test_static));
  85. test->add(BOOST_TEST_CASE(&test_dynamic));
  86. return test;
  87. }