• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //  File        : $RCSfile: const_string_test.cpp,v $
9 //
10 //  Version     : $Revision$
11 //
12 //  Description : simple string class test
13 // ***************************************************************************
14 
15 #define BOOST_TEST_MODULE const_string test
16 #include <boost/test/included/unit_test.hpp>
17 
18 #include "const_string.hpp"
19 using common_layer::const_string;
20 
BOOST_AUTO_TEST_CASE(constructors_test)21 BOOST_AUTO_TEST_CASE( constructors_test )
22 {
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() );
28 
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() );
34 
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") );
38 
39     std::string  s( "test_string" );
40     const_string cs2( s );
41     BOOST_CHECK_EQUAL( std::strcmp( cs2.data(), "test_string" ), 0 );
42 
43     const_string cs3( cs1 );
44     BOOST_CHECK_EQUAL( std::strcmp( cs3.data(), "test_string" ), 0 );
45 
46     const_string cs4( "test_string", 4 );
47     BOOST_CHECK_EQUAL( std::strncmp( cs4.data(), "test", cs4.length() ), 0 );
48 
49     const_string cs5( s.data(), s.data() + s.length() );
50     BOOST_CHECK_EQUAL( std::strncmp( cs5.data(), "test_string", cs5.length() ), 0 );
51 
52     const_string cs_array[] = { "str1", "str2" };
53 
54     BOOST_CHECK_EQUAL( cs_array[0], "str1" );
55     BOOST_CHECK_EQUAL( cs_array[1], "str2" );
56 }
57 
BOOST_AUTO_TEST_CASE(data_access_test)58 BOOST_AUTO_TEST_CASE( data_access_test )
59 {
60     const_string cs1( "test_string" );
61     BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test_string" ), 0 );
62 
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' );
66 
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' );
70 
71     BOOST_CHECK_EQUAL( common_layer::first_char()( cs1  ), 't' );
72     BOOST_CHECK_EQUAL( common_layer::last_char()( cs1  ) , 'g' );
73 }
74 
75 
BOOST_AUTO_TEST_CASE(length_test)76 BOOST_AUTO_TEST_CASE( length_test )
77 {
78     const_string cs1;
79 
80     BOOST_CHECK_EQUAL( cs1.length(), (size_t)0 );
81     BOOST_CHECK( cs1.is_empty() );
82 
83     cs1 = "";
84     BOOST_CHECK_EQUAL( cs1.length(), (size_t)0 );
85     BOOST_CHECK( cs1.is_empty() );
86 
87     cs1 = "test_string";
88     BOOST_CHECK_EQUAL( cs1.length(), (size_t)11 );
89 
90     cs1.erase();
91     BOOST_CHECK_EQUAL( cs1.length(), (size_t)0 );
92     BOOST_CHECK_EQUAL( cs1.data(), "" );
93 
94     cs1 = const_string( "test_string", 4 );
95     BOOST_CHECK_EQUAL( cs1.length(), (size_t)4 );
96 
97     cs1.resize( 5 );
98     BOOST_CHECK_EQUAL( cs1.length(), (size_t)4 );
99 
100     cs1.resize( 3 );
101     BOOST_CHECK_EQUAL( cs1.length(), (size_t)3 );
102 
103     cs1.rshorten();
104     BOOST_CHECK_EQUAL( cs1.length(), (size_t)2 );
105     BOOST_CHECK_EQUAL( cs1[(size_t)0], 't' );
106 
107     cs1.lshorten();
108     BOOST_CHECK_EQUAL( cs1.length(), (size_t)1 );
109     BOOST_CHECK_EQUAL( cs1[(size_t)0], 'e' );
110 
111     cs1.lshorten();
112     BOOST_CHECK( cs1.is_empty() );
113     BOOST_CHECK_EQUAL( cs1.data(), "" );
114 
115     cs1 = "test_string";
116     cs1.lshorten( 11 );
117     BOOST_CHECK( cs1.is_empty() );
118     BOOST_CHECK_EQUAL( cs1.data(), "" );
119 }
120 
BOOST_AUTO_TEST_CASE(asignment_test)121 BOOST_AUTO_TEST_CASE( asignment_test )
122 {
123     const_string cs1;
124     std::string  s( "test_string" );
125 
126     cs1 = "test";
127     BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test" ), 0 );
128 
129     cs1 = s;
130     BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test_string" ), 0 );
131 
132     cs1.assign( "test" );
133     BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test" ), 0 );
134 
135     const_string cs2( "test_string" );
136 
137     cs1.swap( cs2 );
138     BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test_string" ), 0 );
139     BOOST_CHECK_EQUAL( std::strcmp( cs2.data(), "test" ), 0 );
140 }
141 
BOOST_AUTO_TEST_CASE(comparison_test)142 BOOST_AUTO_TEST_CASE( comparison_test )
143 {
144     const_string cs1( "test_string" );
145     const_string cs2( "test_string" );
146     std::string  s( "test_string" );
147 
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 );
153 
154     cs1.resize( 4 );
155 
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 );
161 
162     BOOST_CHECK_EQUAL( cs1, "test" );
163 }
164 
BOOST_AUTO_TEST_CASE(iterators_test)165 BOOST_AUTO_TEST_CASE( iterators_test )
166 {
167     const_string cs1( "test_string" );
168     std::string  s;
169 
170     std::copy( cs1.begin(), cs1.end(), std::back_inserter( s ) );
171     BOOST_CHECK_EQUAL( cs1, s );
172 
173     s.erase();
174 
175     std::copy( cs1.rbegin(), cs1.rend(), std::back_inserter( s ) );
176     BOOST_CHECK_EQUAL( const_string( s ), "gnirts_tset" );
177 }
178 
179 // EOF
180