1 // (C) Copyright Gennadiy Rozental 2001-2005.
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_THROW( cs1.at( cs1.length() ), std::out_of_range );
73 BOOST_CHECK_EQUAL( common_layer::first_char()( cs1 ), 't' );
74 BOOST_CHECK_EQUAL( common_layer::last_char()( cs1 ) , 'g' );
78 BOOST_AUTO_TEST_CASE( length_test )
82 BOOST_CHECK_EQUAL( cs1.length(), (size_t)0 );
83 BOOST_CHECK( cs1.is_empty() );
86 BOOST_CHECK_EQUAL( cs1.length(), (size_t)0 );
87 BOOST_CHECK( cs1.is_empty() );
90 BOOST_CHECK_EQUAL( cs1.length(), (size_t)11 );
93 BOOST_CHECK_EQUAL( cs1.length(), (size_t)0 );
94 BOOST_CHECK_EQUAL( cs1.data(), "" );
96 cs1 = const_string( "test_string", 4 );
97 BOOST_CHECK_EQUAL( cs1.length(), (size_t)4 );
100 BOOST_CHECK_EQUAL( cs1.length(), (size_t)4 );
103 BOOST_CHECK_EQUAL( cs1.length(), (size_t)3 );
106 BOOST_CHECK_EQUAL( cs1.length(), (size_t)2 );
107 BOOST_CHECK_EQUAL( cs1[(size_t)0], 't' );
110 BOOST_CHECK_EQUAL( cs1.length(), (size_t)1 );
111 BOOST_CHECK_EQUAL( cs1[(size_t)0], 'e' );
114 BOOST_CHECK( cs1.is_empty() );
115 BOOST_CHECK_EQUAL( cs1.data(), "" );
119 BOOST_CHECK( cs1.is_empty() );
120 BOOST_CHECK_EQUAL( cs1.data(), "" );
123 BOOST_AUTO_TEST_CASE( asignment_test )
126 std::string s( "test_string" );
129 BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test" ), 0 );
132 BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test_string" ), 0 );
134 cs1.assign( "test" );
135 BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test" ), 0 );
137 const_string cs2( "test_string" );
140 BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test_string" ), 0 );
141 BOOST_CHECK_EQUAL( std::strcmp( cs2.data(), "test" ), 0 );
144 BOOST_AUTO_TEST_CASE( comparison_test )
146 const_string cs1( "test_string" );
147 const_string cs2( "test_string" );
148 std::string s( "test_string" );
150 BOOST_CHECK_EQUAL( cs1, "test_string" );
151 BOOST_CHECK_EQUAL( "test_string", cs1 );
152 BOOST_CHECK_EQUAL( cs1, cs2 );
153 BOOST_CHECK_EQUAL( cs1, s );
154 BOOST_CHECK_EQUAL( s , cs1 );
158 BOOST_CHECK( cs1 != "test_string" );
159 BOOST_CHECK( "test_string" != cs1 );
160 BOOST_CHECK( cs1 != cs2 );
161 BOOST_CHECK( cs1 != s );
162 BOOST_CHECK( s != cs1 );
164 BOOST_CHECK_EQUAL( cs1, "test" );
167 BOOST_AUTO_TEST_CASE( iterators_test )
169 const_string cs1( "test_string" );
172 std::copy( cs1.begin(), cs1.end(), std::back_inserter( s ) );
173 BOOST_CHECK_EQUAL( cs1, s );
177 std::copy( cs1.rbegin(), cs1.rend(), std::back_inserter( s ) );
178 BOOST_CHECK_EQUAL( const_string( s ), "gnirts_tset" );