]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/test/doc/snippet/const_string_test.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / test / doc / snippet / const_string_test.cpp
1 // (C) Copyright Gennadiy Rozental 2001-2014.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 //
6 // See http://www.boost.org/libs/test for the library home page.
7 //
8 // Description : simple string class test
9 // ***************************************************************************
10
11 #define BOOST_TEST_MODULE const_string test
12 #include <boost/test/unit_test.hpp>
13
14 #include <const_string.hpp>
15 using common_layer::const_string;
16
17 BOOST_AUTO_TEST_CASE( constructors_test )
18 {
19 const_string cs0( "" );
20 BOOST_CHECK_EQUAL( cs0.length(), (size_t)0 );
21 BOOST_CHECK_EQUAL( cs0.begin(), "" );
22 BOOST_CHECK_EQUAL( cs0.end(), "" );
23 BOOST_CHECK( cs0.is_empty() );
24
25 const_string cs01( NULL );
26 BOOST_CHECK_EQUAL( cs01.length(), (size_t)0 );
27 BOOST_CHECK_EQUAL( cs01.begin(), "" );
28 BOOST_CHECK_EQUAL( cs01.end(), "" );
29 BOOST_CHECK( cs01.is_empty() );
30
31 const_string cs1( "test_string" );
32 BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test_string" ), 0 );
33 BOOST_CHECK_EQUAL( cs1.length(), std::strlen("test_string") );
34
35 std::string s( "test_string" );
36 const_string cs2( s );
37 BOOST_CHECK_EQUAL( std::strcmp( cs2.data(), "test_string" ), 0 );
38
39 const_string cs3( cs1 );
40 BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test_string" ), 0 );
41
42 const_string cs4( "test_string", 4 );
43 BOOST_CHECK_EQUAL( std::strncmp( cs4.data(), "test", cs4.length() ), 0 );
44
45 const_string cs5( s.data(), s.data() + s.length() );
46 BOOST_CHECK_EQUAL( std::strncmp( cs5.data(), "test_string", cs5.length() ), 0 );
47
48 const_string cs_array[] = { "str1", "str2" };
49
50 BOOST_CHECK_EQUAL( cs_array[0], "str1" );
51 BOOST_CHECK_EQUAL( cs_array[1], "str2" );
52 }
53
54 BOOST_AUTO_TEST_CASE( data_access_test )
55 {
56 const_string cs1( "test_string" );
57 BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test_string" ), 0 );
58 BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), cs1 ), 0 );
59
60 BOOST_CHECK_EQUAL( cs1[(size_t)0], 't' );
61 BOOST_CHECK_EQUAL( cs1[(size_t)4], '_' );
62 BOOST_CHECK_EQUAL( cs1[cs1.length()-1], 'g' );
63
64 BOOST_CHECK_EQUAL( cs1[(size_t)0], cs1.at( 0 ) );
65 BOOST_CHECK_EQUAL( cs1[(size_t)2], cs1.at( 5 ) );
66 BOOST_CHECK_EQUAL( cs1.at( cs1.length() - 1 ), 'g' );
67
68 BOOST_CHECK_THROW( cs1.at( cs1.length() ), std::out_of_range );
69
70 BOOST_CHECK_EQUAL( common_layer::first_char()( cs1 ), 't' );
71 BOOST_CHECK_EQUAL( common_layer::last_char()( cs1 ) , 'g' );
72 }
73
74
75 BOOST_AUTO_TEST_CASE( length_test )
76 {
77 const_string cs1;
78
79 BOOST_CHECK_EQUAL( cs1.length(), (size_t)0 );
80 BOOST_CHECK( cs1.is_empty() );
81
82 cs1 = "";
83 BOOST_CHECK_EQUAL( cs1.length(), (size_t)0 );
84 BOOST_CHECK( cs1.is_empty() );
85
86 cs1 = "test_string";
87 BOOST_CHECK_EQUAL( cs1.length(), (size_t)11 );
88
89 cs1.erase();
90 BOOST_CHECK_EQUAL( cs1.length(), (size_t)0 );
91 BOOST_CHECK_EQUAL( cs1.data(), "" );
92
93 cs1 = const_string( "test_string", 4 );
94 BOOST_CHECK_EQUAL( cs1.length(), (size_t)4 );
95
96 cs1.resize( 5 );
97 BOOST_CHECK_EQUAL( cs1.length(), (size_t)4 );
98
99 cs1.resize( 3 );
100 BOOST_CHECK_EQUAL( cs1.length(), (size_t)3 );
101
102 cs1.rshorten();
103 BOOST_CHECK_EQUAL( cs1.length(), (size_t)2 );
104 BOOST_CHECK_EQUAL( cs1[(size_t)0], 't' );
105
106 cs1.lshorten();
107 BOOST_CHECK_EQUAL( cs1.length(), (size_t)1 );
108 BOOST_CHECK_EQUAL( cs1[(size_t)0], 'e' );
109
110 cs1.lshorten();
111 BOOST_CHECK( cs1.is_empty() );
112 BOOST_CHECK_EQUAL( cs1.data(), "" );
113
114 cs1 = "test_string";
115 cs1.lshorten( 11 );
116 BOOST_CHECK( cs1.is_empty() );
117 BOOST_CHECK_EQUAL( cs1.data(), "" );
118 }
119
120 BOOST_AUTO_TEST_CASE( asignment_test )
121 {
122 const_string cs1;
123 std::string s( "test_string" );
124
125 cs1 = "test";
126 BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test" ), 0 );
127
128 cs1 = s;
129 BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test_string" ), 0 );
130
131 cs1.assign( "test" );
132 BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test" ), 0 );
133
134 const_string cs2( "test_string" );
135
136 cs1.swap( cs2 );
137 BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test_string" ), 0 );
138 BOOST_CHECK_EQUAL( std::strcmp( cs2.data(), "test" ), 0 );
139 }
140
141 BOOST_AUTO_TEST_CASE( comparison_test )
142 {
143 const_string cs1( "test_string" );
144 const_string cs2( "test_string" );
145 std::string s( "test_string" );
146
147 BOOST_CHECK_EQUAL( cs1, "test_string" );
148 BOOST_CHECK_EQUAL( "test_string", cs1 );
149 BOOST_CHECK_EQUAL( cs1, cs2 );
150 BOOST_CHECK_EQUAL( cs1, s );
151 BOOST_CHECK_EQUAL( s , cs1 );
152
153 cs1.resize( 4 );
154
155 BOOST_CHECK( cs1 != "test_string" );
156 BOOST_CHECK( "test_string" != cs1 );
157 BOOST_CHECK( cs1 != cs2 );
158 BOOST_CHECK( cs1 != s );
159 BOOST_CHECK( s != cs1 );
160
161 BOOST_CHECK_EQUAL( cs1, "test" );
162 }
163
164 BOOST_AUTO_TEST_CASE( iterators_test )
165 {
166 const_string cs1( "test_string" );
167 std::string s;
168
169 std::copy( cs1.begin(), cs1.end(), std::back_inserter( s ) );
170 BOOST_CHECK_EQUAL( cs1, s );
171
172 s.erase();
173
174 std::copy( cs1.rbegin(), cs1.rend(), std::back_inserter( s ) );
175 BOOST_CHECK_EQUAL( const_string( s ), "gnirts_tset" );
176 }
177
178 BOOST_AUTO_TEST_CASE( search_test )
179 {
180 const_string cs( "test_string" );
181
182 BOOST_CHECK_EQUAL( cs.find_first_of( 't' ), cs.begin() );
183 BOOST_CHECK_EQUAL( cs.find_last_of( 't' ), cs.begin() + 6 );
184
185 BOOST_CHECK_EQUAL( cs.find_first_of( "st" ), cs.begin() + 2 );
186 BOOST_CHECK_EQUAL( cs.find_last_of( "st" ), cs.begin() + 5 );
187 }
188