]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/utility/test/string_ref_test2.cpp
2 Copyright (c) Marshall Clow 2012-2012.
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 For more information, see http://www.boost.org
11 #include <cstring> // for std::strchr
13 #include <boost/utility/string_ref.hpp>
15 #define BOOST_TEST_MAIN
16 #include <boost/test/unit_test.hpp>
18 typedef boost::string_ref string_ref
;
20 void ends_with ( const char *arg
) {
21 const size_t sz
= std::strlen ( arg
);
22 string_ref
sr ( arg
);
23 string_ref
sr2 ( arg
);
27 BOOST_CHECK ( sr
.ends_with ( p
));
31 while ( !sr2
.empty ()) {
32 BOOST_CHECK ( sr
.ends_with ( sr2
));
33 sr2
.remove_prefix (1);
37 while ( !sr2
.empty ()) {
38 BOOST_CHECK ( sr
.ends_with ( sr2
));
39 sr2
.remove_prefix (1);
42 char ch
= sz
== 0 ? '\0' : arg
[ sz
- 1 ];
45 BOOST_CHECK ( sr2
.ends_with ( ch
));
46 BOOST_CHECK ( !sr2
.ends_with ( ++ch
));
47 BOOST_CHECK ( sr2
.ends_with ( string_ref ()));
50 void starts_with ( const char *arg
) {
51 const size_t sz
= std::strlen ( arg
);
52 string_ref
sr ( arg
);
53 string_ref
sr2 ( arg
);
54 const char *p
= arg
+ std::strlen ( arg
) - 1;
56 std::string
foo ( arg
, p
+ 1 );
57 BOOST_CHECK ( sr
.starts_with ( foo
));
61 while ( !sr2
.empty ()) {
62 BOOST_CHECK ( sr
.starts_with ( sr2
));
63 sr2
.remove_suffix (1);
69 BOOST_CHECK ( sr2
.starts_with ( ch
));
70 BOOST_CHECK ( !sr2
.starts_with ( ++ch
));
71 BOOST_CHECK ( sr2
.starts_with ( string_ref ()));
74 void reverse ( const char *arg
) {
76 string_ref
sr1 ( arg
);
77 std::string
string1 ( sr1
.rbegin (), sr1
.rend ());
78 string_ref
sr2 ( string1
);
79 std::string
string2 ( sr2
.rbegin (), sr2
.rend ());
81 BOOST_CHECK ( std::equal ( sr2
.rbegin (), sr2
.rend (), arg
));
82 BOOST_CHECK ( string2
== arg
);
83 BOOST_CHECK ( std::equal ( sr1
.begin (), sr1
.end (), string2
.begin ()));
86 // This helper function eliminates signed vs. unsigned warnings
87 string_ref::size_type
ptr_diff ( const char *res
, const char *base
) {
88 BOOST_CHECK ( res
>= base
);
89 return static_cast<string_ref::size_type
> ( res
- base
);
92 void find ( const char *arg
) {
97 // Look for each character in the string(searching from the start)
101 string_ref::size_type pos
= sr1
.find(*p
);
102 BOOST_CHECK ( pos
!= string_ref::npos
&& ( pos
<= ptr_diff ( p
, arg
)));
106 // Look for each character in the string (searching from the end)
110 string_ref::size_type pos
= sr1
.rfind(*p
);
111 BOOST_CHECK ( pos
!= string_ref::npos
&& pos
< sr1
.size () && ( pos
>= ptr_diff ( p
, arg
)));
115 // Look for pairs on characters (searching from the start)
118 while ( *p
&& *(p
+1)) {
119 string_ref
sr3 ( p
, 2 );
120 string_ref::size_type pos
= sr1
.find ( sr3
);
121 BOOST_CHECK ( pos
!= string_ref::npos
&& pos
<= static_cast<string_ref::size_type
>( p
- arg
));
127 // for all possible chars, see if we find them in the right place.
128 // Note that strchr will/might do the _wrong_ thing if we search for NULL
129 for ( int ch
= 1; ch
< 256; ++ch
) {
130 string_ref::size_type pos
= sr1
.find(ch
);
131 const char *strp
= std::strchr ( arg
, ch
);
132 BOOST_CHECK (( strp
== NULL
) == ( pos
== string_ref::npos
));
134 BOOST_CHECK ( ptr_diff ( strp
, arg
) == pos
);
139 // for all possible chars, see if we find them in the right place.
140 // Note that strchr will/might do the _wrong_ thing if we search for NULL
141 for ( int ch
= 1; ch
< 256; ++ch
) {
142 string_ref::size_type pos
= sr1
.rfind(ch
);
143 const char *strp
= std::strrchr ( arg
, ch
);
144 BOOST_CHECK (( strp
== NULL
) == ( pos
== string_ref::npos
));
146 BOOST_CHECK ( ptr_diff ( strp
, arg
) == pos
);
150 // Find everything at the start
153 while ( !sr1
.empty ()) {
154 string_ref::size_type pos
= sr1
.find(*p
);
155 BOOST_CHECK ( pos
== 0 );
156 sr1
.remove_prefix (1);
160 // Find everything at the end
162 p
= arg
+ std::strlen ( arg
) - 1;
163 while ( !sr1
.empty ()) {
164 string_ref::size_type pos
= sr1
.rfind(*p
);
165 BOOST_CHECK ( pos
== sr1
.size () - 1 );
166 sr1
.remove_suffix (1);
170 // Find everything at the start
173 while ( !sr1
.empty ()) {
174 string_ref::size_type pos
= sr1
.find_first_of(*p
);
175 BOOST_CHECK ( pos
== 0 );
176 sr1
.remove_prefix (1);
181 // Find everything at the end
183 p
= arg
+ std::strlen ( arg
) - 1;
184 while ( !sr1
.empty ()) {
185 string_ref::size_type pos
= sr1
.find_last_of(*p
);
186 BOOST_CHECK ( pos
== sr1
.size () - 1 );
187 sr1
.remove_suffix (1);
191 // Basic sanity checking for "find_first_of / find_first_not_of"
194 while ( !sr1
.empty() ) {
195 BOOST_CHECK ( sr1
.find_first_of ( sr2
) == 0 );
196 BOOST_CHECK ( sr1
.find_first_not_of ( sr2
) == string_ref::npos
);
197 sr1
.remove_prefix ( 1 );
203 string_ref::size_type pos1
= sr1
.find_first_of(*p
);
204 string_ref::size_type pos2
= sr1
.find_first_not_of(*p
);
205 BOOST_CHECK ( pos1
!= string_ref::npos
&& pos1
< sr1
.size () && pos1
<= ptr_diff ( p
, arg
));
206 if ( pos2
!= string_ref::npos
) {
207 for ( size_t i
= 0 ; i
< pos2
; ++i
)
208 BOOST_CHECK ( sr1
[i
] == *p
);
209 BOOST_CHECK ( sr1
[ pos2
] != *p
);
212 BOOST_CHECK ( pos2
!= pos1
);
216 // Basic sanity checking for "find_last_of / find_last_not_of"
219 while ( !sr1
.empty() ) {
220 BOOST_CHECK ( sr1
.find_last_of ( sr2
) == ( sr1
.size () - 1 ));
221 BOOST_CHECK ( sr1
.find_last_not_of ( sr2
) == string_ref::npos
);
222 sr1
.remove_suffix ( 1 );
228 string_ref::size_type pos1
= sr1
.find_last_of(*p
);
229 string_ref::size_type pos2
= sr1
.find_last_not_of(*p
);
230 BOOST_CHECK ( pos1
!= string_ref::npos
&& pos1
< sr1
.size () && pos1
>= ptr_diff ( p
, arg
));
231 BOOST_CHECK ( pos2
== string_ref::npos
|| pos1
< sr1
.size ());
232 if ( pos2
!= string_ref::npos
) {
233 for ( size_t i
= sr1
.size () -1 ; i
> pos2
; --i
)
234 BOOST_CHECK ( sr1
[i
] == *p
);
235 BOOST_CHECK ( sr1
[ pos2
] != *p
);
238 BOOST_CHECK ( pos2
!= pos1
);
245 void to_string ( const char *arg
) {
252 // str2 = sr1.to_string<std::allocator<char> > ();
253 str2
= sr1
.to_string ();
254 BOOST_CHECK ( str1
== str2
);
256 #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
257 std::string str3
= static_cast<std::string
> ( sr1
);
258 BOOST_CHECK ( str1
== str3
);
262 void compare ( const char *arg
) {
265 std::string str2
= str1
;
269 BOOST_CHECK ( sr1
== sr1
); // compare string_ref and string_ref
270 BOOST_CHECK ( sr1
== str1
); // compare string and string_ref
271 BOOST_CHECK ( str1
== sr1
); // compare string_ref and string
272 BOOST_CHECK ( sr1
== arg
); // compare string_ref and pointer
273 BOOST_CHECK ( arg
== sr1
); // compare pointer and string_ref
275 if ( sr1
.size () > 0 ) {
277 BOOST_CHECK ( sr1
!= str1
);
278 BOOST_CHECK ( str1
!= sr1
);
279 BOOST_CHECK ( sr1
< str1
);
280 BOOST_CHECK ( sr1
<= str1
);
281 BOOST_CHECK ( str1
> sr1
);
282 BOOST_CHECK ( str1
>= sr1
);
284 (*str1
.rbegin()) -= 2;
285 BOOST_CHECK ( sr1
!= str1
);
286 BOOST_CHECK ( str1
!= sr1
);
287 BOOST_CHECK ( sr1
> str1
);
288 BOOST_CHECK ( sr1
>= str1
);
289 BOOST_CHECK ( str1
< sr1
);
290 BOOST_CHECK ( str1
<= sr1
);
294 const char *test_strings
[] = {
298 "AAA", // all the same
299 "adsfadadiaef;alkdg;aljt;j agl;sjrl;tjs;lga;lretj;srg[w349u5209dsfadfasdfasdfadsf",
304 BOOST_AUTO_TEST_CASE( test_main
)
306 const char **p
= &test_strings
[0];
308 while ( *p
!= NULL
) {