replace_all_test.cc
Go to the documentation of this file.
1 #define BOOST_TEST_MODULE (replace_all_test)
2 #include "boost/test/unit_test.hpp"
3 
4 #include "cetlib/replace_all.h"
5 
6 #include <string>
7 
8 using cet::replace_all;
9 using std::string;
10 
11 namespace {
12  void
13  testit(std::string start,
14  std::string const find,
15  std::string const replace,
16  std::string const ref)
17  {
18  replace_all(start, find, replace);
19  BOOST_TEST(start == ref);
20  }
21 }
22 
23 BOOST_AUTO_TEST_SUITE(replace_all_test)
24 
25 BOOST_AUTO_TEST_CASE(replace_simple)
26 {
27  testit("Once upon a time", "a", "some", "Once upon some time");
28  testit("Once upon a time", " ", "_", "Once_upon_a_time");
29  testit("Once upon a time", "time", "song", "Once upon a song");
30 }
31 
33 {
34  testit("Once upon a time", "z", "y", "Once upon a time");
35 }
36 
37 BOOST_AUTO_TEST_CASE(edge_replace)
38 {
39  testit("Once upon a time", "O", "o", "once upon a time");
40  testit("Once upon a timE", "E", "e", "Once upon a time");
41  testit("Once upuponon a time", "upon", "", "Once upon a time");
42 }
43 
44 BOOST_AUTO_TEST_CASE(repeat_replace)
45 {
46  testit("Once uponuponupon a time", "upon", "-", "Once --- a time");
47  testit("Once --- a time", "-", "z", "Once zzz a time");
48  testit("Once --- a time", "-", "upon", "Once uponuponupon a time");
49  testit("Once -------- a time", "---", "upon", "Once uponupon-- a time");
50 }
51 
52 BOOST_AUTO_TEST_CASE(substring_replace)
53 {
54  testit("Once upon a time", "upon", "uponst", "Once uponst a time");
55  testit("Once upon a time", "upon", "stupon", "Once stupon a time");
56 }
57 
58 BOOST_AUTO_TEST_SUITE_END()
static void replace_all(std::string &str, const std::string &old, const std::string &repl)
std::string string
Definition: nybbler.cc:12
bool replace_all(std::string &in, std::string const &from, std::string const &to)
Replace all occurrences of from in string with to.
Definition: replace_all.cc:4
BOOST_AUTO_TEST_CASE(replace_simple)