find_format_test.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Boost string_algo library find_format_test.cpp file ------------------//
  2. // Copyright (c) 2009 Steven Watanabe
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org for updates, documentation, and revision history.
  7. #include <boost/algorithm/string/find_format.hpp>
  8. #include <boost/algorithm/string/finder.hpp>
  9. #include <boost/algorithm/string/formatter.hpp>
  10. // Include unit test framework
  11. #define BOOST_TEST_MAIN
  12. #include <boost/test/unit_test.hpp>
  13. #include <boost/test/test_tools.hpp>
  14. // We're only using const_formatter.
  15. template<class Formatter>
  16. struct formatter_result {
  17. typedef boost::iterator_range<const char*> type;
  18. };
  19. template<class Formatter>
  20. struct checked_formatter {
  21. public:
  22. checked_formatter(const Formatter& formatter) : formatter_(formatter) {}
  23. template< typename T >
  24. typename formatter_result<Formatter>::type operator()( const T & s ) const {
  25. BOOST_CHECK( !s.empty() );
  26. return formatter_(s);
  27. }
  28. private:
  29. Formatter formatter_;
  30. };
  31. template<class Formatter>
  32. checked_formatter<Formatter>
  33. make_checked_formatter(const Formatter& formatter) {
  34. return checked_formatter<Formatter>(formatter);
  35. }
  36. void find_format_test()
  37. {
  38. const std::string source = "$replace $replace";
  39. std::string expected = "ok $replace";
  40. std::string output(80, '\0');
  41. std::string::iterator pos =
  42. boost::find_format_copy(
  43. output.begin(),
  44. source,
  45. boost::first_finder("$replace"),
  46. make_checked_formatter(boost::const_formatter("ok")));
  47. BOOST_CHECK(pos == output.begin() + expected.size());
  48. output.erase(std::remove(output.begin(), output.end(), '\0'), output.end());
  49. BOOST_CHECK_EQUAL(output, expected);
  50. output =
  51. boost::find_format_copy(
  52. source,
  53. boost::first_finder("$replace"),
  54. make_checked_formatter(boost::const_formatter("ok")));
  55. BOOST_CHECK_EQUAL(output, expected);
  56. // now try finding a string that doesn't exist
  57. output.resize(80);
  58. pos =
  59. boost::find_format_copy(
  60. output.begin(),
  61. source,
  62. boost::first_finder("$noreplace"),
  63. make_checked_formatter(boost::const_formatter("bad")));
  64. BOOST_CHECK(pos == output.begin() + source.size());
  65. output.erase(std::remove(output.begin(), output.end(), '\0'), output.end());
  66. BOOST_CHECK_EQUAL(output, source);
  67. output =
  68. boost::find_format_copy(
  69. source,
  70. boost::first_finder("$noreplace"),
  71. make_checked_formatter(boost::const_formatter("bad")));
  72. BOOST_CHECK_EQUAL(output, source);
  73. // in place version
  74. output = source;
  75. boost::find_format(
  76. output,
  77. boost::first_finder("$replace"),
  78. make_checked_formatter(boost::const_formatter("ok")));
  79. BOOST_CHECK_EQUAL(output, expected);
  80. output = source;
  81. boost::find_format(
  82. output,
  83. boost::first_finder("$noreplace"),
  84. make_checked_formatter(boost::const_formatter("bad")));
  85. BOOST_CHECK_EQUAL(output, source);
  86. }
  87. void find_format_all_test()
  88. {
  89. const std::string source = "$replace $replace";
  90. std::string expected = "ok ok";
  91. std::string output(80, '\0');
  92. std::string::iterator pos =
  93. boost::find_format_all_copy(output.begin(),
  94. source,
  95. boost::first_finder("$replace"),
  96. boost::const_formatter("ok"));
  97. BOOST_CHECK(pos == output.begin() + expected.size());
  98. output.erase(std::remove(output.begin(), output.end(), '\0'), output.end());
  99. BOOST_CHECK_EQUAL(output, expected);
  100. output =
  101. boost::find_format_all_copy(
  102. source,
  103. boost::first_finder("$replace"),
  104. make_checked_formatter(boost::const_formatter("ok")));
  105. BOOST_CHECK_EQUAL(output, expected);
  106. // now try finding a string that doesn't exist
  107. output.resize(80);
  108. pos =
  109. boost::find_format_all_copy(
  110. output.begin(),
  111. source,
  112. boost::first_finder("$noreplace"),
  113. make_checked_formatter(boost::const_formatter("bad")));
  114. BOOST_CHECK(pos == output.begin() + source.size());
  115. output.erase(std::remove(output.begin(), output.end(), '\0'), output.end());
  116. BOOST_CHECK_EQUAL(output, source);
  117. output =
  118. boost::find_format_all_copy(
  119. source,
  120. boost::first_finder("$noreplace"),
  121. make_checked_formatter(boost::const_formatter("bad")));
  122. BOOST_CHECK_EQUAL(output, source);
  123. // in place version
  124. output = source;
  125. boost::find_format_all(
  126. output,
  127. boost::first_finder("$replace"),
  128. make_checked_formatter(boost::const_formatter("ok")));
  129. BOOST_CHECK_EQUAL(output, expected);
  130. output = source;
  131. boost::find_format_all(
  132. output,
  133. boost::first_finder("$noreplace"),
  134. make_checked_formatter(boost::const_formatter("bad")));
  135. BOOST_CHECK_EQUAL(output, source);
  136. }
  137. BOOST_AUTO_TEST_CASE( test_main )
  138. {
  139. find_format_test();
  140. find_format_all_test();
  141. }