1// Boost string_algo library substr_test.cpp file ------------------//
2
3// Copyright Pavol Droba 2002-2003. Use, modification and
4// distribution is subject to the Boost Software License, Version
5// 1.0. (See 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/regex.hpp>
11#include <boost/algorithm/string/join.hpp>
12#include <boost/algorithm/string/sequence_traits.hpp>
13// equals predicate is used for result comparison
14#include <boost/algorithm/string/predicate.hpp>
15
16
17// Include unit test framework
18#define BOOST_TEST_MAIN
19#include <boost/test/unit_test.hpp>
20
21#include <string>
22#include <vector>
23#include <iostream>
24#include <boost/regex.hpp>
25#include <boost/test/test_tools.hpp>
26
27using namespace std;
28using namespace boost;
29
30static void find_test()
31{
32 string str1("123a1cxxxa23cXXXa456c321");
33 const char* pch1="123a1cxxxa23cXXXa456c321";
34 regex rx("a[0-9]+c");
35 vector<int> vec1( str1.begin(), str1.end() );
36 vector<string> tokens;
37
38 // find results
39 iterator_range<string::iterator> nc_result;
40 iterator_range<string::const_iterator> cv_result;
41
42 iterator_range<vector<int>::iterator> nc_vresult;
43 iterator_range<vector<int>::const_iterator> cv_vresult;
44
45 iterator_range<const char*> ch_result;
46
47 // basic tests
48 nc_result=find_regex( str1, rx );
49 BOOST_CHECK(
50 ( (nc_result.begin()-str1.begin()) == 3) &&
51 ( (nc_result.end()-str1.begin()) == 6) );
52
53 cv_result=find_regex( str1, rx );
54 BOOST_CHECK(
55 ( (cv_result.begin()-str1.begin()) == 3) &&
56 ( (cv_result.end()-str1.begin()) == 6) );
57
58 ch_result=find_regex( pch1, rx );
59 BOOST_CHECK(( (ch_result.begin() - pch1 ) == 3) && ( (ch_result.end() - pch1 ) == 6 ) );
60
61 // multi-type comparison test
62 nc_vresult=find_regex( vec1, rx );
63 BOOST_CHECK(
64 ( (nc_result.begin()-str1.begin()) == 3) &&
65 ( (nc_result.end()-str1.begin()) == 6) );
66
67 cv_vresult=find_regex( vec1, rx );
68 BOOST_CHECK(
69 ( (cv_result.begin()-str1.begin()) == 3) &&
70 ( (cv_result.end()-str1.begin()) == 6) );
71
72 // find_all_regex test
73 find_all_regex( tokens, str1, rx );
74
75 BOOST_REQUIRE( tokens.size()==3 );
76 BOOST_CHECK( tokens[0]==string("a1c") );
77 BOOST_CHECK( tokens[1]==string("a23c") );
78 BOOST_CHECK( tokens[2]==string("a456c") );
79
80 // split_regex test
81 split_regex( tokens, str1, rx );
82
83 BOOST_REQUIRE( tokens.size()==4 );
84 BOOST_CHECK( tokens[0]==string("123") );
85 BOOST_CHECK( tokens[1]==string("xxx") );
86 BOOST_CHECK( tokens[2]==string("XXX") );
87 BOOST_CHECK( tokens[3]==string("321") );
88
89}
90
91static void join_test()
92{
93 // Prepare inputs
94 vector<string> tokens1;
95 tokens1.push_back("xx");
96 tokens1.push_back("abc");
97 tokens1.push_back("xx");
98
99#ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
100 BOOST_CHECK( equals(join_if(tokens1, "-", regex("x+")), "xx-xx") );
101 BOOST_CHECK( equals(join_if(tokens1, "-", regex("[abc]+")), "abc") );
102#else
103 BOOST_CHECK( equals(join_if_regex(tokens1, "-", regex("x+")), "xx-xx") );
104 BOOST_CHECK( equals(join_if_regex(tokens1, "-", regex("[abc]+")), "abc") );
105#endif
106}
107
108static void replace_test()
109{
110 string str1("123a1cxxxa23cXXXa456c321");
111 regex rx1("a([0-9]+)c");
112 regex rx2("([xX]+)");
113 regex rx3("_[^_]*_");
114 string fmt1("_A$1C_");
115 string fmt2("_xXx_");
116 vector<int> vec1( str1.begin(), str1.end() );
117
118 // immutable tests
119
120 // basic tests
121 BOOST_CHECK( replace_regex_copy( str1, rx1, fmt1 )==string("123_A1C_xxxa23cXXXa456c321") );
122 BOOST_CHECK( replace_all_regex_copy( str1, rx1, fmt1 )==string("123_A1C_xxx_A23C_XXX_A456C_321") );
123 BOOST_CHECK( erase_regex_copy( str1, rx1 )==string("123xxxa23cXXXa456c321") );
124 BOOST_CHECK( erase_all_regex_copy( str1, rx1 )==string(string("123xxxXXX321")) );
125
126 // output iterator variants test
127 string strout;
128 replace_regex_copy( back_inserter(strout), str1, rx1, fmt1 );
129 BOOST_CHECK( strout==string("123_A1C_xxxa23cXXXa456c321") );
130 strout.clear();
131 replace_all_regex_copy( back_inserter(strout), str1, rx1, fmt1 );
132 BOOST_CHECK( strout==string("123_A1C_xxx_A23C_XXX_A456C_321") );
133 strout.clear();
134 erase_regex_copy( back_inserter(strout), str1, rx1 );
135 BOOST_CHECK( strout==string("123xxxa23cXXXa456c321") );
136 strout.clear();
137 erase_all_regex_copy( back_inserter(strout), str1, rx1 );
138 BOOST_CHECK( strout==string("123xxxXXX321") );
139 strout.clear();
140
141 // in-place test
142 replace_regex( str1, rx1, fmt2 );
143 BOOST_CHECK( str1==string("123_xXx_xxxa23cXXXa456c321") );
144
145 replace_all_regex( str1, rx2, fmt1 );
146 BOOST_CHECK( str1==string("123__AxXxC___AxxxC_a23c_AXXXC_a456c321") );
147 erase_regex( str1, rx3 );
148 BOOST_CHECK( str1==string("123AxXxC___AxxxC_a23c_AXXXC_a456c321") );
149 erase_all_regex( str1, rx3 );
150 BOOST_CHECK( str1==string("123AxXxCa23ca456c321") );
151}
152
153BOOST_AUTO_TEST_CASE( test_main )
154{
155 find_test();
156 join_test();
157 replace_test();
158}
159