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.
21template<class Formatter>
22struct formatter_result {
23 typedef boost::iterator_range<const char*> type;
24};
25
26template<class Formatter>
27struct checked_formatter {
28public:
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 }
35private:
36 Formatter formatter_;
37};
38
39template<class Formatter>
40checked_formatter<Formatter>
41make_checked_formatter(const Formatter& formatter) {
42 return checked_formatter<Formatter>(formatter);
43}
44
45void 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.begin(),
54 source,
55 boost::first_finder("$replace"),
56 make_checked_formatter(boost::const_formatter("ok")));
57 BOOST_CHECK(pos == output.begin() + expected.size());
58 output.erase(std::remove(output.begin(), output.end(), '\0'), output.end());
59 BOOST_CHECK_EQUAL(output, expected);
60
61 output =
62 boost::find_format_copy(
63 source,
64 boost::first_finder("$replace"),
65 make_checked_formatter(boost::const_formatter("ok")));
66 BOOST_CHECK_EQUAL(output, expected);
67
68 // now try finding a string that doesn't exist
69 output.resize(80);
70 pos =
71 boost::find_format_copy(
72 output.begin(),
73 source,
74 boost::first_finder("$noreplace"),
75 make_checked_formatter(boost::const_formatter("bad")));
76 BOOST_CHECK(pos == output.begin() + source.size());
77 output.erase(std::remove(output.begin(), output.end(), '\0'), output.end());
78 BOOST_CHECK_EQUAL(output, source);
79
80 output =
81 boost::find_format_copy(
82 source,
83 boost::first_finder("$noreplace"),
84 make_checked_formatter(boost::const_formatter("bad")));
85 BOOST_CHECK_EQUAL(output, source);
86
87 // in place version
88 output = source;
89 boost::find_format(
90 output,
91 boost::first_finder("$replace"),
92 make_checked_formatter(boost::const_formatter("ok")));
93 BOOST_CHECK_EQUAL(output, expected);
94 output = source;
95 boost::find_format(
96 output,
97 boost::first_finder("$noreplace"),
98 make_checked_formatter(boost::const_formatter("bad")));
99 BOOST_CHECK_EQUAL(output, source);
100}
101
102void 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.begin(),
110 source,
111 boost::first_finder("$replace"),
112 boost::const_formatter("ok"));
113 BOOST_CHECK(pos == output.begin() + expected.size());
114 output.erase(std::remove(output.begin(), output.end(), '\0'), output.end());
115 BOOST_CHECK_EQUAL(output, expected);
116
117 output =
118 boost::find_format_all_copy(
119 source,
120 boost::first_finder("$replace"),
121 make_checked_formatter(boost::const_formatter("ok")));
122 BOOST_CHECK_EQUAL(output, expected);
123
124 // now try finding a string that doesn't exist
125 output.resize(80);
126 pos =
127 boost::find_format_all_copy(
128 output.begin(),
129 source,
130 boost::first_finder("$noreplace"),
131 make_checked_formatter(boost::const_formatter("bad")));
132 BOOST_CHECK(pos == output.begin() + source.size());
133 output.erase(std::remove(output.begin(), output.end(), '\0'), output.end());
134 BOOST_CHECK_EQUAL(output, source);
135
136 output =
137 boost::find_format_all_copy(
138 source,
139 boost::first_finder("$noreplace"),
140 make_checked_formatter(boost::const_formatter("bad")));
141 BOOST_CHECK_EQUAL(output, source);
142
143 // in place version
144 output = source;
145 boost::find_format_all(
146 output,
147 boost::first_finder("$replace"),
148 make_checked_formatter(boost::const_formatter("ok")));
149 BOOST_CHECK_EQUAL(output, expected);
150 output = source;
151 boost::find_format_all(
152 output,
153 boost::first_finder("$noreplace"),
154 make_checked_formatter(boost::const_formatter("bad")));
155 BOOST_CHECK_EQUAL(output, source);
156}
157
158BOOST_AUTO_TEST_CASE( test_main )
159{
160 find_format_test();
161 find_format_all_test();
162}
163