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