]>
Commit | Line | Data |
---|---|---|
b2dad0e3 BK |
1 | // 1999-06-04 bkoz |
2 | ||
48fe3de0 | 3 | // Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. |
b2dad0e3 BK |
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 <stdexcept> | |
fe413112 | 26 | #include <testsuite_hooks.h> |
b2dad0e3 BK |
27 | |
28 | int test01(void) | |
29 | { | |
30 | bool test = true; | |
31 | typedef std::string::size_type csize_type; | |
32 | typedef std::string::iterator citerator; | |
33 | csize_type npos = std::string::npos; | |
34 | csize_type csz01, csz02; | |
35 | ||
36 | const char str_lit01[] = "rodeo beach, marin"; | |
37 | const std::string str01(str_lit01); | |
38 | const std::string str02("baker beach, san francisco"); | |
39 | ||
40 | // basic_string(const string&, size_type pos = 0, siz_type n = npos, alloc) | |
41 | csz01 = str01.size(); | |
42 | try { | |
43 | std::string str03(str01, csz01 + 1); | |
aa1b2f7d | 44 | VERIFY( false ); |
b2dad0e3 BK |
45 | } |
46 | catch(std::out_of_range& fail) { | |
aa1b2f7d | 47 | VERIFY( true ); |
b2dad0e3 BK |
48 | } |
49 | catch(...) { | |
aa1b2f7d | 50 | VERIFY( false ); |
b2dad0e3 BK |
51 | } |
52 | ||
53 | try { | |
54 | std::string str03(str01, csz01); | |
aa1b2f7d BV |
55 | VERIFY( str03.size() == 0 ); |
56 | VERIFY( str03.size() <= str03.capacity() ); | |
b2dad0e3 BK |
57 | } |
58 | catch(...) { | |
aa1b2f7d | 59 | VERIFY( false ); |
b2dad0e3 BK |
60 | } |
61 | ||
6b76f569 BK |
62 | #if 0 |
63 | // XXX These tests have been temporarily disabled. | |
a9ab8db1 | 64 | //http://gcc.gnu.org/ml/libstdc++/2000-10/msg00033.html |
b2dad0e3 BK |
65 | // basic_string(const char* s, size_type n, alloc) |
66 | csz01 = str01.max_size(); | |
67 | // NB: As strlen(str_lit01) != csz01, this test is undefined. It | |
68 | // should not crash, but what gets constructed is a bit arbitrary. | |
69 | try { | |
70 | std::string str03(str_lit01, csz01 + 1); | |
aa1b2f7d | 71 | VERIFY( true ); |
b2dad0e3 BK |
72 | } |
73 | catch(std::length_error& fail) { | |
aa1b2f7d | 74 | VERIFY( true ); |
b2dad0e3 BK |
75 | } |
76 | catch(...) { | |
aa1b2f7d | 77 | VERIFY( false ); |
b2dad0e3 BK |
78 | } |
79 | ||
80 | // NB: As strlen(str_lit01) != csz01, this test is undefined. It | |
81 | // should not crash, but what gets constructed is a bit arbitrary. | |
644638bc | 82 | // The "maverick's" of all string objects. |
b2dad0e3 | 83 | try { |
644638bc | 84 | std::string str04(str_lit01, npos); |
aa1b2f7d | 85 | VERIFY( true ); |
b2dad0e3 BK |
86 | } |
87 | catch(std::length_error& fail) { | |
aa1b2f7d | 88 | VERIFY( true ); |
b2dad0e3 BK |
89 | } |
90 | catch(...) { | |
aa1b2f7d | 91 | VERIFY( false ); |
b2dad0e3 BK |
92 | } |
93 | ||
6b76f569 | 94 | // Build a maxsize - 1 lengthed string consisting of all A's |
b2dad0e3 | 95 | try { |
644638bc | 96 | std::string str03(csz01 - 1, 'A'); |
aa1b2f7d BV |
97 | VERIFY( str03.size() == csz01 - 1 ); |
98 | VERIFY( str03.size() <= str03.capacity() ); | |
b2dad0e3 BK |
99 | } |
100 | // NB: bad_alloc is regrettable but entirely kosher for | |
101 | // out-of-memory situations. | |
102 | catch(std::bad_alloc& fail) { | |
aa1b2f7d | 103 | VERIFY( true ); |
b2dad0e3 BK |
104 | } |
105 | catch(...) { | |
aa1b2f7d | 106 | VERIFY( false ); |
b2dad0e3 | 107 | } |
6b76f569 | 108 | #endif |
b2dad0e3 BK |
109 | |
110 | // basic_string(const char* s, const allocator& a = allocator()) | |
111 | std::string str04(str_lit01); | |
aa1b2f7d | 112 | VERIFY( str01 == str04 ); |
b2dad0e3 BK |
113 | |
114 | ||
115 | // basic_string(size_type n, char c, const allocator& a = allocator()) | |
116 | csz01 = str01.max_size(); | |
117 | try { | |
118 | std::string str03(csz01 + 1, 'z'); | |
aa1b2f7d | 119 | VERIFY( false ); |
b2dad0e3 BK |
120 | } |
121 | catch(std::length_error& fail) { | |
aa1b2f7d | 122 | VERIFY( true ); |
b2dad0e3 BK |
123 | } |
124 | catch(...) { | |
aa1b2f7d | 125 | VERIFY( false ); |
b2dad0e3 BK |
126 | } |
127 | ||
128 | try { | |
129 | std::string str04(npos, 'b'); // the "maverick's" of all string objects. | |
aa1b2f7d | 130 | VERIFY( false ); |
b2dad0e3 BK |
131 | } |
132 | catch(std::length_error& fail) { | |
aa1b2f7d | 133 | VERIFY( true ); |
b2dad0e3 BK |
134 | } |
135 | catch(...) { | |
aa1b2f7d | 136 | VERIFY( false ); |
b2dad0e3 BK |
137 | } |
138 | ||
139 | try { | |
140 | std::string str03(csz01 - 1, 'z'); | |
aa1b2f7d BV |
141 | VERIFY( str03.size() != 0 ); |
142 | VERIFY( str03.size() <= str03.capacity() ); | |
b2dad0e3 BK |
143 | } |
144 | // NB: bad_alloc is regrettable but entirely kosher for | |
145 | // out-of-memory situations. | |
146 | catch(std::bad_alloc& fail) { | |
aa1b2f7d | 147 | VERIFY( true ); |
b2dad0e3 BK |
148 | } |
149 | catch(...) { | |
aa1b2f7d | 150 | VERIFY( false ); |
b2dad0e3 BK |
151 | } |
152 | ||
153 | ||
154 | // template<typename _InputIter> | |
155 | // basic_string(_InputIter begin, _InputIter end, const allocator& a) | |
156 | std::string str06(str01.begin(), str01.end()); | |
aa1b2f7d | 157 | VERIFY( str06 == str01 ); |
b2dad0e3 BK |
158 | |
159 | #ifdef DEBUG_ASSERT | |
160 | assert(test); | |
161 | #endif | |
162 | return test; | |
163 | } | |
164 | ||
165 | void test02() | |
166 | { | |
167 | bool test = true; | |
168 | ||
169 | // template<typename _InputIter> | |
170 | // basic_string(_InputIter begin, _InputIter end, const allocator& a) | |
171 | // where _InputIter is integral [21.3.1 para 15] | |
172 | std::string s(10,0); | |
aa1b2f7d | 173 | VERIFY( s.size() == 10 ); |
b2dad0e3 BK |
174 | #ifdef DEBUG_ASSERT |
175 | assert(test); | |
176 | #endif | |
177 | } | |
178 | ||
48fe3de0 PE |
179 | void test03() |
180 | { | |
181 | bool test = true; | |
182 | const char* with_nulls = "This contains \0 a zero byte."; | |
183 | ||
184 | // These are tests to see how basic_string handles data with NUL | |
185 | // bytes. Obviously basic_string(char*) will halt at the first one, but | |
186 | // nothing else should. | |
187 | std::string s1 (with_nulls, 28); | |
188 | VERIFY( s1.size() == 28 ); | |
189 | std::string s2 (s1); | |
190 | VERIFY( s2.size() == 28 ); | |
191 | ||
192 | #ifdef DEBUG_ASSERT | |
193 | assert(test); | |
194 | #endif | |
195 | } | |
196 | ||
b2dad0e3 BK |
197 | int main() |
198 | { | |
fe413112 | 199 | __set_testsuite_memlimit(); |
b2dad0e3 BK |
200 | test01(); |
201 | test02(); | |
48fe3de0 | 202 | test03(); |
04d930e6 | 203 | return 0; |
b2dad0e3 | 204 | } |