1 // 1999-06-04 bkoz
2
3 // Copyright (C) 1999, 2000, 2001, 2002, 2003 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.1 basic_string constructors.
22
23 #include <new>
24 #include <string>
25 #include <vector>
26 #include <stdexcept>
27 #include <testsuite_hooks.h>
28
test01(void)29 void test01(void)
30 {
31 bool test = true;
32 typedef std::string::size_type csize_type;
33 typedef std::string::iterator citerator;
34 csize_type npos = std::string::npos;
35 csize_type csz01, csz02;
36
37 const char str_lit01[] = "rodeo beach, marin";
38 const std::string str01(str_lit01);
39 const std::string str02("baker beach, san francisco");
40
41 // basic_string(const string&, size_type pos = 0, siz_type n = npos, alloc)
42 csz01 = str01.size();
43 try {
44 std::string str03(str01, csz01 + 1);
45 VERIFY( false );
46 }
47 catch(std::out_of_range& fail) {
48 VERIFY( true );
49 }
50 catch(...) {
51 VERIFY( false );
52 }
53
54 try {
55 std::string str03(str01, csz01);
56 VERIFY( str03.size() == 0 );
57 VERIFY( str03.size() <= str03.capacity() );
58 }
59 catch(...) {
60 VERIFY( false );
61 }
62
63 // basic_string(const char* s, size_type n, alloc)
64 csz01 = str01.max_size();
65 // NB: As strlen(str_lit01) != csz01, this test is undefined. It
66 // should not crash, but what gets constructed is a bit arbitrary.
67 try {
68 std::string str03(str_lit01, csz01 + 1);
69 VERIFY( true );
70 }
71 catch(std::length_error& fail) {
72 VERIFY( true );
73 }
74 catch(...) {
75 VERIFY( false );
76 }
77
78 // NB: As strlen(str_lit01) != csz01, this test is undefined. It
79 // should not crash, but what gets constructed is a bit arbitrary.
80 // The "maverick's" of all string objects.
81 try {
82 std::string str04(str_lit01, npos);
83 VERIFY( true );
84 }
85 catch(std::length_error& fail) {
86 VERIFY( true );
87 }
88 catch(...) {
89 VERIFY( false );
90 }
91
92 // Build a maxsize - 1 lengthed string consisting of all A's
93 try {
94 std::string str03(csz01 - 1, 'A');
95 VERIFY( str03.size() == csz01 - 1 );
96 VERIFY( str03.size() <= str03.capacity() );
97 }
98 // NB: bad_alloc is regrettable but entirely kosher for
99 // out-of-memory situations.
100 catch(std::bad_alloc& fail) {
101 VERIFY( true );
102 }
103 catch(...) {
104 VERIFY( false );
105 }
106
107 // basic_string(const char* s, const allocator& a = allocator())
108 std::string str04(str_lit01);
109 VERIFY( str01 == str04 );
110
111
112 // basic_string(size_type n, char c, const allocator& a = allocator())
113 csz01 = str01.max_size();
114 try {
115 std::string str03(csz01 + 1, 'z');
116 VERIFY( false );
117 }
118 catch(std::length_error& fail) {
119 VERIFY( true );
120 }
121 catch(...) {
122 VERIFY( false );
123 }
124
125 try {
126 std::string str04(npos, 'b'); // the "maverick's" of all string objects.
127 VERIFY( false );
128 }
129 catch(std::length_error& fail) {
130 VERIFY( true );
131 }
132 catch(...) {
133 VERIFY( false );
134 }
135
136 try {
137 std::string str03(csz01 - 1, 'z');
138 VERIFY( str03.size() != 0 );
139 VERIFY( str03.size() <= str03.capacity() );
140 }
141 // NB: bad_alloc is regrettable but entirely kosher for
142 // out-of-memory situations.
143 catch(std::bad_alloc& fail) {
144 VERIFY( true );
145 }
146 catch(...) {
147 VERIFY( false );
148 }
149
150
151 // template<typename _InputIter>
152 // basic_string(_InputIter begin, _InputIter end, const allocator& a)
153 std::string str06(str01.begin(), str01.end());
154 VERIFY( str06 == str01 );
155 }
156
test02()157 void test02()
158 {
159 bool test = true;
160
161 // template<typename _InputIter>
162 // basic_string(_InputIter begin, _InputIter end, const allocator& a)
163 // where _InputIter is integral [21.3.1 para 15]
164 std::string s(10,0);
165 VERIFY( s.size() == 10 );
166 }
167
test03()168 void test03()
169 {
170 bool test = true;
171 const char* with_nulls = "This contains \0 a zero byte.";
172
173 // These are tests to see how basic_string handles data with NUL
174 // bytes. Obviously basic_string(char*) will halt at the first one, but
175 // nothing else should.
176 std::string s1 (with_nulls, 28);
177 VERIFY( s1.size() == 28 );
178 std::string s2 (s1);
179 VERIFY( s2.size() == 28 );
180
181 // Not defined, but libstdc++ throws an exception.
182 const char* bogus = 0;
183 try
184 {
185 std::string str1(bogus);
186 VERIFY( false );
187 }
188 catch(std::exception& fail)
189 {
190 VERIFY( true );
191 }
192
193 // Not defined, but libstdc++ throws an exception.
194 try
195 {
196 std::string str2(bogus, 5);
197 VERIFY( false );
198 }
199 catch(std::exception& fail)
200 {
201 VERIFY( true );
202 }
203 }
204
205 // http://gcc.gnu.org/ml/libstdc++/2002-06/msg00025.html
test04()206 void test04()
207 {
208 bool test = true;
209
210 std::string str01("portofino");
211
212 std::string::reverse_iterator i1 = str01.rbegin();
213 std::string::reverse_iterator i2 = str01.rend();
214 std::string str02(i1, i2);
215 VERIFY( str02 == "onifotrop" );
216 }
217
218 // libstdc++/8347
test05()219 void test05()
220 {
221 bool test = true;
222
223 std::vector<char> empty;
224 std::string empty2(empty.begin(), empty.end());
225
226 // libstdc++/8716 (same underlying situation, same fix)
227 char const * s = NULL;
228 std::string zero_length_built_with_NULL(s,0);
229 }
230
main()231 int main()
232 {
233 __gnu_cxx_test::set_memory_limits();
234 test01();
235 test02();
236 test03();
237 test04();
238 test05();
239 return 0;
240 }
241