]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/utility/test/string_ref_test1.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / utility / test / string_ref_test1.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 <iostream>
11 #include <algorithm>
12 #include <string>
13
14 #include <boost/utility/string_ref.hpp>
15
16 #define BOOST_TEST_MAIN
17 #include <boost/test/unit_test.hpp>
18
19 typedef boost::string_ref string_ref;
20
21 // Should be equal
22 void interop ( const std::string &str, string_ref ref ) {
23 // BOOST_CHECK ( str == ref );
24 BOOST_CHECK ( str.size () == ref.size ());
25 BOOST_CHECK ( std::equal ( str.begin (), str.end (), ref.begin ()));
26 BOOST_CHECK ( std::equal ( str.rbegin (), str.rend (), ref.rbegin ()));
27 }
28
29 void null_tests ( const char *p ) {
30 // All zero-length string-refs should be equal
31 string_ref sr1; // NULL, 0
32 string_ref sr2 ( NULL, 0 );
33 string_ref sr3 ( p, 0 );
34 string_ref sr4 ( p );
35 sr4.clear ();
36
37 BOOST_CHECK ( sr1 == sr2 );
38 BOOST_CHECK ( sr1 == sr3 );
39 BOOST_CHECK ( sr2 == sr3 );
40 BOOST_CHECK ( sr1 == sr4 );
41 }
42
43 // make sure that substrings work just like strings
44 void test_substr ( const std::string &str ) {
45 const size_t sz = str.size ();
46 string_ref ref ( str );
47
48 // Substrings at the end
49 for ( size_t i = 0; i <= sz; ++ i )
50 interop ( str.substr ( i ), ref.substr ( i ));
51
52 // Substrings at the beginning
53 for ( size_t i = 0; i <= sz; ++ i )
54 interop ( str.substr ( 0, i ), ref.substr ( 0, i ));
55
56 // All possible substrings
57 for ( size_t i = 0; i < sz; ++i )
58 for ( size_t j = i; j < sz; ++j )
59 interop ( str.substr ( i, j ), ref.substr ( i, j ));
60 }
61
62 // make sure that removing prefixes and suffixes work just like strings
63 void test_remove ( const std::string &str ) {
64 const size_t sz = str.size ();
65 std::string work;
66 string_ref ref;
67
68 for ( size_t i = 1; i <= sz; ++i ) {
69 work = str;
70 ref = str;
71 while ( ref.size () >= i ) {
72 interop ( work, ref );
73 work.erase ( 0, i );
74 ref.remove_prefix (i);
75 }
76 }
77
78 for ( size_t i = 1; i < sz; ++ i ) {
79 work = str;
80 ref = str;
81 while ( ref.size () >= i ) {
82 interop ( work, ref );
83 work.erase ( work.size () - i, i );
84 ref.remove_suffix (i);
85 }
86 }
87 }
88
89 const char *test_strings [] = {
90 "",
91 "1",
92 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
93 "0123456789",
94 NULL
95 };
96
97 BOOST_AUTO_TEST_CASE( test_main )
98 {
99 const char **p = &test_strings[0];
100
101 while ( *p != NULL ) {
102 interop ( *p, *p );
103 test_substr ( *p );
104 test_remove ( *p );
105 null_tests ( *p );
106
107 p++;
108 }
109 }