1 // 1999-05-11 bkoz
2
3 // Copyright (C) 1999, 2002 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // 21.3.3 string capacity
22
23 #include <string>
24 #include <testsuite_hooks.h>
25
26 template<typename T>
27 struct A { };
28
29 template<typename T>
30 bool
operator ==(const A<T> & a,const A<T> & b)31 operator==(const A<T>& a, const A<T>& b) { return true; }
32
33 template<typename T>
34 bool
operator <(const A<T> & a,const A<T> & b)35 operator<(const A<T>& a, const A<T>& b) { return true; }
36
37 struct B { };
38
39 // char_traits specialization
40 namespace std
41 {
42 template<>
43 struct char_traits<A<B> >
44 {
45 typedef A<B> char_type;
46 // Unsigned as wint_t in unsigned.
47 typedef unsigned long int_type;
48 typedef streampos pos_type;
49 typedef streamoff off_type;
50 typedef mbstate_t state_type;
51
52 static void
assignstd::char_traits53 assign(char_type& __c1, const char_type& __c2)
54 { __c1 = __c2; }
55
56 static bool
eqstd::char_traits57 eq(const char_type& __c1, const char_type& __c2)
58 { return __c1 == __c2; }
59
60 static bool
ltstd::char_traits61 lt(const char_type& __c1, const char_type& __c2)
62 { return __c1 < __c2; }
63
64 static int
comparestd::char_traits65 compare(const char_type* __s1, const char_type* __s2, size_t __n)
66 {
67 for (size_t __i = 0; __i < __n; ++__i)
68 if (!eq(__s1[__i], __s2[__i]))
69 return lt(__s1[__i], __s2[__i]) ? -1 : 1;
70 return 0;
71 }
72
73 static size_t
lengthstd::char_traits74 length(const char_type* __s)
75 {
76 const char_type* __p = __s;
77 while (__p)
78 ++__p;
79 return (__p - __s);
80 }
81
82 static const char_type*
findstd::char_traits83 find(const char_type* __s, size_t __n, const char_type& __a)
84 {
85 for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
86 if (*__p == __a) return __p;
87 return 0;
88 }
89
90 static char_type*
movestd::char_traits91 move(char_type* __s1, const char_type* __s2, size_t __n)
92 { return (char_type*) memmove(__s1, __s2, __n * sizeof(char_type)); }
93
94 static char_type*
copystd::char_traits95 copy(char_type* __s1, const char_type* __s2, size_t __n)
96 { return (char_type*) memcpy(__s1, __s2, __n * sizeof(char_type)); }
97
98 static char_type*
assignstd::char_traits99 assign(char_type* __s, size_t __n, char_type __a)
100 {
101 for (char_type* __p = __s; __p < __s + __n; ++__p)
102 assign(*__p, __a);
103 return __s;
104 }
105
106 static char_type
to_char_typestd::char_traits107 to_char_type(const int_type& __c)
108 { return char_type(); }
109
110 static int_type
to_int_typestd::char_traits111 to_int_type(const char_type& __c) { return int_type(); }
112
113 static bool
eq_int_typestd::char_traits114 eq_int_type(const int_type& __c1, const int_type& __c2)
115 { return __c1 == __c2; }
116
117 static int_type
eofstd::char_traits118 eof() { return static_cast<int_type>(-1); }
119
120 static int_type
not_eofstd::char_traits121 not_eof(const int_type& __c)
122 { return eq_int_type(__c, eof()) ? int_type(0) : __c; }
123 };
124 } // namespace std
125
test01()126 void test01()
127 {
128 // 1 POD types : resize, capacity, reserve
129 bool test = true;
130 std::string str01;
131 typedef std::string::size_type size_type_s;
132
133 size_type_s sz01 = str01.capacity();
134 str01.reserve(100);
135 size_type_s sz02 = str01.capacity();
136 VERIFY( sz02 >= sz01 );
137 VERIFY( sz02 >= 100 );
138 str01.reserve();
139 sz01 = str01.capacity();
140 VERIFY( sz01 >= 0 );
141
142 sz01 = str01.size() + 5;
143 str01.resize(sz01);
144 sz02 = str01.size();
145 VERIFY( sz01 == sz02 );
146
147 sz01 = str01.size() - 5;
148 str01.resize(sz01);
149 sz02 = str01.size();
150 VERIFY( sz01 == sz02 );
151
152 std::string str05(30, 'q');
153 std::string str06 = str05;
154 str05 = str06 + str05;
155 VERIFY( str05.capacity() >= str05.size() );
156 VERIFY( str06.capacity() >= str06.size() );
157
158 // 2 non POD types : resize, capacity, reserve
159 std::basic_string< A<B> > str02;
160 typedef std::basic_string< A<B> >::size_type size_type_o;
161 size_type_o sz03;
162 size_type_o sz04;
163
164 sz03 = str02.capacity();
165 str02.reserve(100);
166 sz04 = str02.capacity();
167 VERIFY( sz04 >= sz03 );
168 VERIFY( sz04 >= 100 );
169 str02.reserve();
170 sz03 = str02.capacity();
171 VERIFY( sz03 >= 0 );
172
173 sz03 = str02.size() + 5;
174 str02.resize(sz03);
175 sz04 = str02.size();
176 VERIFY( sz03 == sz04 );
177
178 sz03 = str02.size() - 5;
179 str02.resize(sz03);
180 sz04 = str02.size();
181 VERIFY( sz03 == sz04 );
182
183 A<B> inst_obj;
184 std::basic_string<A<B> > str07(30, inst_obj);
185 std::basic_string<A<B> > str08 = str07;
186 str07 = str08 + str07;
187 VERIFY( str07.capacity() >= str07.size() );
188 VERIFY( str08.capacity() >= str08.size() );
189
190 // 3 POD types: size, length, max_size, clear(), empty()
191 bool b01;
192 std::string str011;
193 b01 = str01.empty();
194 VERIFY( b01 == true );
195 sz01 = str01.size();
196 sz02 = str01.length();
197 VERIFY( sz01 == sz02 );
198 str01.c_str();
199 sz01 = str01.size();
200 sz02 = str01.length();
201 VERIFY( sz01 == sz02 );
202
203 sz01 = str01.length();
204 str01.c_str();
205 str011 = str01 + "_addendum_";
206 str01.c_str();
207 sz02 = str01.length();
208 VERIFY( sz01 == sz02 );
209 sz02 = str011.length();
210 VERIFY( sz02 > sz01 );
211
212 // trickster allocator issues involved with these:
213 std::string str3 = "8-chars_8-chars_";
214 const char* p3 = str3.c_str();
215 std::string str4 = str3 + "7-chars";
216 const char* p4 = str3.c_str();
217
218 sz01 = str01.size();
219 sz02 = str01.max_size();
220 VERIFY( sz02 >= sz01 );
221
222 sz01 = str01.size();
223 str01.clear();
224 b01 = str01.empty();
225 VERIFY( b01 == true );
226 sz02 = str01.size();
227 VERIFY( sz01 >= sz02 );
228
229
230 // 4 non-POD types: size, length, max_size, clear(), empty()
231 b01 = str02.empty();
232 VERIFY( b01 == true );
233 sz03 = str02.size();
234 sz04 = str02.length();
235 VERIFY( sz03 == sz04 );
236 str02.c_str();
237 sz03 = str02.size();
238 sz04 = str02.length();
239 VERIFY( sz03 == sz04 );
240
241 sz03 = str02.max_size();
242 VERIFY( sz03 >= sz04 );
243
244 sz03 = str02.size();
245 str02.clear();
246 b01 = str02.empty();
247 VERIFY( b01 == true );
248 sz04 = str02.size();
249 VERIFY( sz03 >= sz04 );
250 }
251
252 // libstdc++/4548
253 // http://gcc.gnu.org/ml/libstdc++/2001-11/msg00150.html
test02()254 void test02()
255 {
256 bool test = true;
257
258 std::string str01 = "twelve chars";
259 // str01 becomes shared
260 std::string str02 = str01;
261 str01.reserve(1);
262 VERIFY( str01.capacity() == 12 );
263 }
264
265 #if !__GXX_WEAK__
266 // Explicitly instantiate for systems with no COMDAT or weak support.
267 template
268 std::basic_string< A<B> >::size_type
269 std::basic_string< A<B> >::_Rep::_S_max_size;
270
271 template
272 A<B>
273 std::basic_string< A<B> >::_Rep::_S_terminal;
274 #endif
275
main()276 int main()
277 {
278 test01();
279 test02();
280
281 return 0;
282 }
283