File: const_string_test.cpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (188 lines) | stat: -rw-r--r-- 5,364 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//  (C) Copyright Gennadiy Rozental 2001-2014.
//  Distributed under the Boost Software License, Version 1.0.
//  (See accompanying file LICENSE_1_0.txt or copy at 
//  http://www.boost.org/LICENSE_1_0.txt)
//
//  See http://www.boost.org/libs/test for the library home page.
//
//  Description : simple string class test
// ***************************************************************************

#define BOOST_TEST_MODULE const_string test
#include <boost/test/unit_test.hpp>

#include <const_string.hpp>
using common_layer::const_string;

BOOST_AUTO_TEST_CASE( constructors_test )
{
  const_string cs0( "" );
  BOOST_CHECK_EQUAL( cs0.length(), (size_t)0 );
  BOOST_CHECK_EQUAL( cs0.begin(), "" );
  BOOST_CHECK_EQUAL( cs0.end(), "" );
  BOOST_CHECK( cs0.is_empty() );
  
  const_string cs01( NULL );
  BOOST_CHECK_EQUAL( cs01.length(), (size_t)0 );
  BOOST_CHECK_EQUAL( cs01.begin(), "" );
  BOOST_CHECK_EQUAL( cs01.end(), "" );
  BOOST_CHECK( cs01.is_empty() );
  
  const_string cs1( "test_string" );
  BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test_string" ), 0 );
  BOOST_CHECK_EQUAL( cs1.length(), std::strlen("test_string") );
  
  std::string  s( "test_string" );
  const_string cs2( s );
  BOOST_CHECK_EQUAL( std::strcmp( cs2.data(), "test_string" ), 0 );
  
  const_string cs3( cs1 );
  BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test_string" ), 0 );
  
  const_string cs4( "test_string", 4 );
  BOOST_CHECK_EQUAL( std::strncmp( cs4.data(), "test", cs4.length() ), 0 );
  
  const_string cs5( s.data(), s.data() + s.length() );
  BOOST_CHECK_EQUAL( std::strncmp( cs5.data(), "test_string", cs5.length() ), 0 );
  
  const_string cs_array[] = { "str1", "str2" };
  
  BOOST_CHECK_EQUAL( cs_array[0], "str1" );
  BOOST_CHECK_EQUAL( cs_array[1], "str2" );
}

BOOST_AUTO_TEST_CASE( data_access_test )
{
  const_string cs1( "test_string" );
  BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test_string" ), 0 );
  BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), cs1 ), 0 );
  
  BOOST_CHECK_EQUAL( cs1[(size_t)0], 't' );
  BOOST_CHECK_EQUAL( cs1[(size_t)4], '_' );
  BOOST_CHECK_EQUAL( cs1[cs1.length()-1], 'g' );
  
  BOOST_CHECK_EQUAL( cs1[(size_t)0], cs1.at( 0 ) );
  BOOST_CHECK_EQUAL( cs1[(size_t)2], cs1.at( 5 ) );
  BOOST_CHECK_EQUAL( cs1.at( cs1.length() - 1 ), 'g' );
  
  BOOST_CHECK_THROW( cs1.at( cs1.length() ), std::out_of_range );
  
  BOOST_CHECK_EQUAL( common_layer::first_char()( cs1  ), 't' );
  BOOST_CHECK_EQUAL( common_layer::last_char()( cs1  ) , 'g' );
}


BOOST_AUTO_TEST_CASE( length_test )
{
  const_string cs1;
  
  BOOST_CHECK_EQUAL( cs1.length(), (size_t)0 );
  BOOST_CHECK( cs1.is_empty() );
  
  cs1 = "";
  BOOST_CHECK_EQUAL( cs1.length(), (size_t)0 );
  BOOST_CHECK( cs1.is_empty() );
  
  cs1 = "test_string";
  BOOST_CHECK_EQUAL( cs1.length(), (size_t)11 );
  
  cs1.erase();
  BOOST_CHECK_EQUAL( cs1.length(), (size_t)0 );
  BOOST_CHECK_EQUAL( cs1.data(), "" );
  
  cs1 = const_string( "test_string", 4 );
  BOOST_CHECK_EQUAL( cs1.length(), (size_t)4 );
  
  cs1.resize( 5 );
  BOOST_CHECK_EQUAL( cs1.length(), (size_t)4 );
  
  cs1.resize( 3 );
  BOOST_CHECK_EQUAL( cs1.length(), (size_t)3 );
  
  cs1.rshorten();
  BOOST_CHECK_EQUAL( cs1.length(), (size_t)2 );
  BOOST_CHECK_EQUAL( cs1[(size_t)0], 't' );
  
  cs1.lshorten();
  BOOST_CHECK_EQUAL( cs1.length(), (size_t)1 );
  BOOST_CHECK_EQUAL( cs1[(size_t)0], 'e' );
  
  cs1.lshorten();
  BOOST_CHECK( cs1.is_empty() );
  BOOST_CHECK_EQUAL( cs1.data(), "" );
  
  cs1 = "test_string";
  cs1.lshorten( 11 );
  BOOST_CHECK( cs1.is_empty() );
  BOOST_CHECK_EQUAL( cs1.data(), "" );
}

BOOST_AUTO_TEST_CASE( asignment_test )
{
  const_string cs1;
  std::string  s( "test_string" );
  
  cs1 = "test";
  BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test" ), 0 );
  
  cs1 = s;
  BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test_string" ), 0 );
  
  cs1.assign( "test" );
  BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test" ), 0 );
  
  const_string cs2( "test_string" );
  
  cs1.swap( cs2 );
  BOOST_CHECK_EQUAL( std::strcmp( cs1.data(), "test_string" ), 0 );
  BOOST_CHECK_EQUAL( std::strcmp( cs2.data(), "test" ), 0 );
}

BOOST_AUTO_TEST_CASE( comparison_test )
{
  const_string cs1( "test_string" );
  const_string cs2( "test_string" );
  std::string  s( "test_string" );
  
  BOOST_CHECK_EQUAL( cs1, "test_string" );
  BOOST_CHECK_EQUAL( "test_string", cs1 );
  BOOST_CHECK_EQUAL( cs1, cs2 );
  BOOST_CHECK_EQUAL( cs1, s );
  BOOST_CHECK_EQUAL( s  , cs1 );
  
  cs1.resize( 4 );
  
  BOOST_CHECK( cs1 != "test_string" );
  BOOST_CHECK( "test_string" != cs1 );
  BOOST_CHECK( cs1 != cs2 );
  BOOST_CHECK( cs1 != s );
  BOOST_CHECK( s   != cs1 );
  
  BOOST_CHECK_EQUAL( cs1, "test" );
}

BOOST_AUTO_TEST_CASE( iterators_test )
{
  const_string cs1( "test_string" );
  std::string  s;
  
  std::copy( cs1.begin(), cs1.end(), std::back_inserter( s ) );
  BOOST_CHECK_EQUAL( cs1, s );
  
  s.erase();
  
  std::copy( cs1.rbegin(), cs1.rend(), std::back_inserter( s ) );
  BOOST_CHECK_EQUAL( const_string( s ), "gnirts_tset" );
}

BOOST_AUTO_TEST_CASE( search_test )
{
  const_string cs( "test_string" );
  
  BOOST_CHECK_EQUAL( cs.find_first_of( 't' ), cs.begin() );
  BOOST_CHECK_EQUAL( cs.find_last_of( 't' ), cs.begin() + 6 );
  
  BOOST_CHECK_EQUAL( cs.find_first_of( "st" ), cs.begin() + 2 );
  BOOST_CHECK_EQUAL( cs.find_last_of( "st" ), cs.begin() + 5 );
}