]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/test/example/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)
6 // See http://www.boost.org/libs/test for the library home page.
8 // File : $RCSfile: const_string_test.cpp,v $
10 // Version : $Revision$
12 // Description : simple string class test
13 // ***************************************************************************
15 #define BOOST_TEST_MODULE const_string test
16 #include <boost/test/included/unit_test.hpp>
18 #include "const_string.hpp"
19 using common_layer::const_string
;
21 BOOST_AUTO_TEST_CASE( constructors_test
)
23 const_string
cs0( "" );
24 BOOST_CHECK_EQUAL( cs0
.length(), (size_t)0 );
25 BOOST_CHECK_EQUAL( cs0
.begin(), "" );
26 BOOST_CHECK_EQUAL( cs0
.end(), "" );
27 BOOST_CHECK( cs0
.is_empty() );
29 const_string
cs01( NULL
);
30 BOOST_CHECK_EQUAL( cs01
.length(), (size_t)0 );
31 BOOST_CHECK_EQUAL( cs01
.begin(), "" );
32 BOOST_CHECK_EQUAL( cs01
.end(), "" );
33 BOOST_CHECK( cs01
.is_empty() );
35 const_string
cs1( "test_string" );
36 BOOST_CHECK_EQUAL( std::strcmp( cs1
.data(), "test_string" ), 0 );
37 BOOST_CHECK_EQUAL( cs1
.length(), std::strlen("test_string") );
39 std::string
s( "test_string" );
40 const_string
cs2( s
);
41 BOOST_CHECK_EQUAL( std::strcmp( cs2
.data(), "test_string" ), 0 );
43 const_string
cs3( cs1
);
44 BOOST_CHECK_EQUAL( std::strcmp( cs1
.data(), "test_string" ), 0 );
46 const_string
cs4( "test_string", 4 );
47 BOOST_CHECK_EQUAL( std::strncmp( cs4
.data(), "test", cs4
.length() ), 0 );
49 const_string
cs5( s
.data(), s
.data() + s
.length() );
50 BOOST_CHECK_EQUAL( std::strncmp( cs5
.data(), "test_string", cs5
.length() ), 0 );
52 const_string cs_array
[] = { "str1", "str2" };
54 BOOST_CHECK_EQUAL( cs_array
[0], "str1" );
55 BOOST_CHECK_EQUAL( cs_array
[1], "str2" );
58 BOOST_AUTO_TEST_CASE( data_access_test
)
60 const_string
cs1( "test_string" );
61 BOOST_CHECK_EQUAL( std::strcmp( cs1
.data(), "test_string" ), 0 );
63 BOOST_CHECK_EQUAL( cs1
[(size_t)0], 't' );
64 BOOST_CHECK_EQUAL( cs1
[(size_t)4], '_' );
65 BOOST_CHECK_EQUAL( cs1
[cs1
.length()-1], 'g' );
67 BOOST_CHECK_EQUAL( cs1
[(size_t)0], cs1
.at( 0 ) );
68 BOOST_CHECK_EQUAL( cs1
[(size_t)2], cs1
.at( 5 ) );
69 BOOST_CHECK_EQUAL( cs1
.at( cs1
.length() - 1 ), 'g' );
71 BOOST_CHECK_EQUAL( common_layer::first_char()( cs1
), 't' );
72 BOOST_CHECK_EQUAL( common_layer::last_char()( cs1
) , 'g' );
76 BOOST_AUTO_TEST_CASE( length_test
)
80 BOOST_CHECK_EQUAL( cs1
.length(), (size_t)0 );
81 BOOST_CHECK( cs1
.is_empty() );
84 BOOST_CHECK_EQUAL( cs1
.length(), (size_t)0 );
85 BOOST_CHECK( cs1
.is_empty() );
88 BOOST_CHECK_EQUAL( cs1
.length(), (size_t)11 );
91 BOOST_CHECK_EQUAL( cs1
.length(), (size_t)0 );
92 BOOST_CHECK_EQUAL( cs1
.data(), "" );
94 cs1
= const_string( "test_string", 4 );
95 BOOST_CHECK_EQUAL( cs1
.length(), (size_t)4 );
98 BOOST_CHECK_EQUAL( cs1
.length(), (size_t)4 );
101 BOOST_CHECK_EQUAL( cs1
.length(), (size_t)3 );
104 BOOST_CHECK_EQUAL( cs1
.length(), (size_t)2 );
105 BOOST_CHECK_EQUAL( cs1
[(size_t)0], 't' );
108 BOOST_CHECK_EQUAL( cs1
.length(), (size_t)1 );
109 BOOST_CHECK_EQUAL( cs1
[(size_t)0], 'e' );
112 BOOST_CHECK( cs1
.is_empty() );
113 BOOST_CHECK_EQUAL( cs1
.data(), "" );
117 BOOST_CHECK( cs1
.is_empty() );
118 BOOST_CHECK_EQUAL( cs1
.data(), "" );
121 BOOST_AUTO_TEST_CASE( asignment_test
)
124 std::string
s( "test_string" );
127 BOOST_CHECK_EQUAL( std::strcmp( cs1
.data(), "test" ), 0 );
130 BOOST_CHECK_EQUAL( std::strcmp( cs1
.data(), "test_string" ), 0 );
132 cs1
.assign( "test" );
133 BOOST_CHECK_EQUAL( std::strcmp( cs1
.data(), "test" ), 0 );
135 const_string
cs2( "test_string" );
138 BOOST_CHECK_EQUAL( std::strcmp( cs1
.data(), "test_string" ), 0 );
139 BOOST_CHECK_EQUAL( std::strcmp( cs2
.data(), "test" ), 0 );
142 BOOST_AUTO_TEST_CASE( comparison_test
)
144 const_string
cs1( "test_string" );
145 const_string
cs2( "test_string" );
146 std::string
s( "test_string" );
148 BOOST_CHECK_EQUAL( cs1
, "test_string" );
149 BOOST_CHECK_EQUAL( "test_string", cs1
);
150 BOOST_CHECK_EQUAL( cs1
, cs2
);
151 BOOST_CHECK_EQUAL( cs1
, s
);
152 BOOST_CHECK_EQUAL( s
, cs1
);
156 BOOST_CHECK( cs1
!= "test_string" );
157 BOOST_CHECK( "test_string" != cs1
);
158 BOOST_CHECK( cs1
!= cs2
);
159 BOOST_CHECK( cs1
!= s
);
160 BOOST_CHECK( s
!= cs1
);
162 BOOST_CHECK_EQUAL( cs1
, "test" );
165 BOOST_AUTO_TEST_CASE( iterators_test
)
167 const_string
cs1( "test_string" );
170 std::copy( cs1
.begin(), cs1
.end(), std::back_inserter( s
) );
171 BOOST_CHECK_EQUAL( cs1
, s
);
175 std::copy( cs1
.rbegin(), cs1
.rend(), std::back_inserter( s
) );
176 BOOST_CHECK_EQUAL( const_string( s
), "gnirts_tset" );