]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/utility/test/string_view_test2.cpp
2549e648ac1bbe9e14b9a5d59b147b4ba1ca9e54
[ceph.git] / ceph / src / boost / libs / utility / test / string_view_test2.cpp
1 /*
2 Copyright (c) Marshall Clow 2012-2012.
3
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)
6
7 For more information, see http://www.boost.org
8 */
9
10 #include <new> // for placement new
11 #include <iostream>
12 #include <cstddef> // for NULL, std::size_t, std::ptrdiff_t
13 #include <cstring> // for std::strchr and std::strcmp
14 #include <cstdlib> // for std::malloc and std::free
15
16 #include <boost/utility/string_view.hpp>
17 #include <boost/config.hpp>
18
19 #include <boost/core/lightweight_test.hpp>
20
21 typedef boost::string_view string_view;
22
23 void ends_with ( const char *arg ) {
24 const size_t sz = std::strlen ( arg );
25 string_view sr ( arg );
26 string_view sr2 ( arg );
27 const char *p = arg;
28
29 while ( *p ) {
30 BOOST_TEST ( sr.ends_with ( p ));
31 ++p;
32 }
33
34 while ( !sr2.empty ()) {
35 BOOST_TEST ( sr.ends_with ( sr2 ));
36 sr2.remove_prefix (1);
37 }
38
39 sr2 = arg;
40 while ( !sr2.empty ()) {
41 BOOST_TEST ( sr.ends_with ( sr2 ));
42 sr2.remove_prefix (1);
43 }
44
45 char ch = sz == 0 ? '\0' : arg [ sz - 1 ];
46 sr2 = arg;
47 if ( sz > 0 )
48 BOOST_TEST ( sr2.ends_with ( ch ));
49 BOOST_TEST ( !sr2.ends_with ( ++ch ));
50 BOOST_TEST ( sr2.ends_with ( string_view()));
51 }
52
53 void starts_with ( const char *arg ) {
54 const size_t sz = std::strlen ( arg );
55 string_view sr ( arg );
56 string_view sr2 ( arg );
57 const char *p = arg + std::strlen ( arg ) - 1;
58 while ( p >= arg ) {
59 std::string foo ( arg, p + 1 );
60 BOOST_TEST ( sr.starts_with ( foo ));
61 --p;
62 }
63
64 while ( !sr2.empty ()) {
65 BOOST_TEST ( sr.starts_with ( sr2 ));
66 sr2.remove_suffix (1);
67 }
68
69 char ch = *arg;
70 sr2 = arg;
71 if ( sz > 0 )
72 BOOST_TEST ( sr2.starts_with ( ch ));
73 BOOST_TEST ( !sr2.starts_with ( ++ch ));
74 BOOST_TEST ( sr2.starts_with ( string_view ()));
75 }
76
77 void reverse ( const char *arg ) {
78 // Round trip
79 string_view sr1 ( arg );
80 std::string string1 ( sr1.rbegin (), sr1.rend ());
81 string_view sr2 ( string1 );
82 std::string string2 ( sr2.rbegin (), sr2.rend ());
83
84 BOOST_TEST ( std::equal ( sr2.rbegin (), sr2.rend (), arg ));
85 BOOST_TEST ( string2 == arg );
86 BOOST_TEST ( std::equal ( sr1.begin (), sr1.end (), string2.begin ()));
87 }
88
89 // This helper function eliminates signed vs. unsigned warnings
90 string_view::size_type ptr_diff ( const char *res, const char *base ) {
91 BOOST_TEST ( res >= base );
92 return static_cast<string_view::size_type> ( res - base );
93 }
94
95 void find ( const char *arg ) {
96 string_view sr1;
97 string_view sr2;
98 const char *p;
99
100 // Look for each character in the string(searching from the start)
101 p = arg;
102 sr1 = arg;
103 while ( *p ) {
104 string_view::size_type pos = sr1.find(*p);
105 BOOST_TEST ( pos != string_view::npos && ( pos <= ptr_diff ( p, arg )));
106 ++p;
107 }
108
109 // Look for each character in the string (searching from the end)
110 p = arg;
111 sr1 = arg;
112 while ( *p ) {
113 string_view::size_type pos = sr1.rfind(*p);
114 BOOST_TEST ( pos != string_view::npos && pos < sr1.size () && ( pos >= ptr_diff ( p, arg )));
115 ++p;
116 }
117
118 // Look for pairs on characters (searching from the start)
119 sr1 = arg;
120 p = arg;
121 while ( *p && *(p+1)) {
122 string_view sr3 ( p, 2 );
123 string_view::size_type pos = sr1.find ( sr3 );
124 BOOST_TEST ( pos != string_view::npos && pos <= static_cast<string_view::size_type>( p - arg ));
125 p++;
126 }
127
128 sr1 = arg;
129 p = arg;
130 // for all possible chars, see if we find them in the right place.
131 // Note that strchr will/might do the _wrong_ thing if we search for NULL
132 for ( int ch = 1; ch < 256; ++ch ) {
133 string_view::size_type pos = sr1.find(ch);
134 const char *strp = std::strchr ( arg, ch );
135 BOOST_TEST (( strp == NULL ) == ( pos == string_view::npos ));
136 if ( strp != NULL )
137 BOOST_TEST ( ptr_diff ( strp, arg ) == pos );
138 }
139
140 sr1 = arg;
141 p = arg;
142 // for all possible chars, see if we find them in the right place.
143 // Note that strchr will/might do the _wrong_ thing if we search for NULL
144 for ( int ch = 1; ch < 256; ++ch ) {
145 string_view::size_type pos = sr1.rfind(ch);
146 const char *strp = std::strrchr ( arg, ch );
147 BOOST_TEST (( strp == NULL ) == ( pos == string_view::npos ));
148 if ( strp != NULL )
149 BOOST_TEST ( ptr_diff ( strp, arg ) == pos );
150 }
151
152
153 // Find everything at the start
154 p = arg;
155 sr1 = arg;
156 while ( !sr1.empty ()) {
157 string_view::size_type pos = sr1.find(*p);
158 BOOST_TEST ( pos == 0 );
159 sr1.remove_prefix (1);
160 ++p;
161 }
162
163 // Find everything at the end
164 sr1 = arg;
165 p = arg + std::strlen ( arg ) - 1;
166 while ( !sr1.empty ()) {
167 string_view::size_type pos = sr1.rfind(*p);
168 BOOST_TEST ( pos == sr1.size () - 1 );
169 sr1.remove_suffix (1);
170 --p;
171 }
172
173 // Find everything at the start
174 sr1 = arg;
175 p = arg;
176 while ( !sr1.empty ()) {
177 string_view::size_type pos = sr1.find_first_of(*p);
178 BOOST_TEST ( pos == 0 );
179 sr1.remove_prefix (1);
180 ++p;
181 }
182
183
184 // Find everything at the end
185 sr1 = arg;
186 p = arg + std::strlen ( arg ) - 1;
187 while ( !sr1.empty ()) {
188 string_view::size_type pos = sr1.find_last_of(*p);
189 BOOST_TEST ( pos == sr1.size () - 1 );
190 sr1.remove_suffix (1);
191 --p;
192 }
193
194 // Basic sanity checking for "find_first_of / find_first_not_of"
195 sr1 = arg;
196 sr2 = arg;
197 while ( !sr1.empty() ) {
198 BOOST_TEST ( sr1.find_first_of ( sr2 ) == 0 );
199 BOOST_TEST ( sr1.find_first_not_of ( sr2 ) == string_view::npos );
200 sr1.remove_prefix ( 1 );
201 }
202
203 p = arg;
204 sr1 = arg;
205 while ( *p ) {
206 string_view::size_type pos1 = sr1.find_first_of(*p);
207 string_view::size_type pos2 = sr1.find_first_not_of(*p);
208 BOOST_TEST ( pos1 != string_view::npos && pos1 < sr1.size () && pos1 <= ptr_diff ( p, arg ));
209 if ( pos2 != string_view::npos ) {
210 for ( size_t i = 0 ; i < pos2; ++i )
211 BOOST_TEST ( sr1[i] == *p );
212 BOOST_TEST ( sr1 [ pos2 ] != *p );
213 }
214
215 BOOST_TEST ( pos2 != pos1 );
216 ++p;
217 }
218
219 // Basic sanity checking for "find_last_of / find_last_not_of"
220 sr1 = arg;
221 sr2 = arg;
222 while ( !sr1.empty() ) {
223 BOOST_TEST ( sr1.find_last_of ( sr2 ) == ( sr1.size () - 1 ));
224 BOOST_TEST ( sr1.find_last_not_of ( sr2 ) == string_view::npos );
225 sr1.remove_suffix ( 1 );
226 }
227
228 p = arg;
229 sr1 = arg;
230 while ( *p ) {
231 string_view::size_type pos1 = sr1.find_last_of(*p);
232 string_view::size_type pos2 = sr1.find_last_not_of(*p);
233 BOOST_TEST ( pos1 != string_view::npos && pos1 < sr1.size () && pos1 >= ptr_diff ( p, arg ));
234 BOOST_TEST ( pos2 == string_view::npos || pos1 < sr1.size ());
235 if ( pos2 != string_view::npos ) {
236 for ( size_t i = sr1.size () -1 ; i > pos2; --i )
237 BOOST_TEST ( sr1[i] == *p );
238 BOOST_TEST ( sr1 [ pos2 ] != *p );
239 }
240
241 BOOST_TEST ( pos2 != pos1 );
242 ++p;
243 }
244
245 }
246
247 template <typename T>
248 class custom_allocator {
249 public:
250 typedef T value_type;
251 typedef T* pointer;
252 typedef const T* const_pointer;
253 typedef void* void_pointer;
254 typedef const void* const_void_pointer;
255 typedef std::size_t size_type;
256 typedef std::ptrdiff_t difference_type;
257 typedef T& reference;
258 typedef const T& const_reference;
259
260 template<class U>
261 struct rebind {
262 typedef custom_allocator<U> other;
263 };
264
265 custom_allocator() BOOST_NOEXCEPT {}
266 template <typename U>
267 custom_allocator(custom_allocator<U> const&) BOOST_NOEXCEPT {}
268
269 pointer allocate(size_type n) const {
270 return static_cast<pointer>(std::malloc(sizeof(value_type) * n));
271 }
272 void deallocate(pointer p, size_type) const BOOST_NOEXCEPT {
273 std::free(p);
274 }
275
276 pointer address(reference value) const BOOST_NOEXCEPT {
277 return &value;
278 }
279
280 const_pointer address(const_reference value) const BOOST_NOEXCEPT {
281 return &value;
282 }
283
284 BOOST_CONSTEXPR size_type max_size() const BOOST_NOEXCEPT {
285 return (~(size_type)0u) / sizeof(value_type);
286 }
287
288 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
289 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
290 template <class U, class... Args>
291 void construct(U* ptr, Args&&... args) const {
292 ::new((void*)ptr) U(static_cast<Args&&>(args)...);
293 }
294 #else
295 template <class U, class V>
296 void construct(U* ptr, V&& value) const {
297 ::new((void*)ptr) U(static_cast<V&&>(value));
298 }
299 #endif
300 #else
301 template <class U, class V>
302 void construct(U* ptr, const V& value) const {
303 ::new((void*)ptr) U(value);
304 }
305 #endif
306
307 template <class U>
308 void construct(U* ptr) const {
309 ::new((void*)ptr) U();
310 }
311
312 template <class U>
313 void destroy(U* ptr) const {
314 (void)ptr;
315 ptr->~U();
316 }
317 };
318
319 template <typename T, typename U>
320 BOOST_CONSTEXPR bool operator==(const custom_allocator<T> &, const custom_allocator<U> &) BOOST_NOEXCEPT {
321 return true;
322 }
323 template <typename T, typename U>
324 BOOST_CONSTEXPR bool operator!=(const custom_allocator<T> &, const custom_allocator<U> &) BOOST_NOEXCEPT {
325 return false;
326 }
327
328 void to_string ( const char *arg ) {
329 string_view sr1;
330 std::string str1;
331 std::string str2;
332
333 str1.assign ( arg );
334 sr1 = arg;
335 // str2 = sr1.to_string<std::allocator<char> > ();
336 str2 = sr1.to_string ();
337 BOOST_TEST ( str1 == str2 );
338
339 std::basic_string<char, std::char_traits<char>, custom_allocator<char> > str3 = sr1.to_string(custom_allocator<char>());
340 BOOST_TEST ( std::strcmp(str1.c_str(), str3.c_str()) == 0 );
341
342 #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
343 std::string str4 = static_cast<std::string> ( sr1 );
344 BOOST_TEST ( str1 == str4 );
345 #endif
346 }
347
348 void compare ( const char *arg ) {
349 string_view sr1;
350 std::string str1;
351 std::string str2 = str1;
352
353 str1.assign ( arg );
354 sr1 = arg;
355 BOOST_TEST ( sr1 == sr1); // compare string_view and string_view
356 BOOST_TEST ( sr1 == str1); // compare string and string_view
357 BOOST_TEST ( str1 == sr1 ); // compare string_view and string
358 BOOST_TEST ( sr1 == arg ); // compare string_view and pointer
359 BOOST_TEST ( arg == sr1 ); // compare pointer and string_view
360
361 if ( sr1.size () > 0 ) {
362 (*str1.rbegin())++;
363 BOOST_TEST ( sr1 != str1 );
364 BOOST_TEST ( str1 != sr1 );
365 BOOST_TEST ( sr1 < str1 );
366 BOOST_TEST ( sr1 <= str1 );
367 BOOST_TEST ( str1 > sr1 );
368 BOOST_TEST ( str1 >= sr1 );
369
370 (*str1.rbegin()) -= 2;
371 BOOST_TEST ( sr1 != str1 );
372 BOOST_TEST ( str1 != sr1 );
373 BOOST_TEST ( sr1 > str1 );
374 BOOST_TEST ( sr1 >= str1 );
375 BOOST_TEST ( str1 < sr1 );
376 BOOST_TEST ( str1 <= sr1 );
377 }
378 }
379
380 const char *test_strings [] = {
381 "",
382 "0",
383 "abc",
384 "AAA", // all the same
385 "adsfadadiaef;alkdg;aljt;j agl;sjrl;tjs;lga;lretj;srg[w349u5209dsfadfasdfasdfadsf",
386 "abc\0asdfadsfasf",
387 NULL
388 };
389
390 int main()
391 {
392 const char **p = &test_strings[0];
393
394 while ( *p != NULL ) {
395 starts_with ( *p );
396 ends_with ( *p );
397 reverse ( *p );
398 find ( *p );
399 to_string ( *p );
400 compare ( *p );
401
402 p++;
403 }
404
405 return boost::report_errors();
406 }