]>
Commit | Line | Data |
---|---|---|
b2dad0e3 BK |
1 | // 1999-06-04 bkoz |
2 | ||
644638bc | 3 | // Copyright (C) 1999, 2000 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> | |
26 | #ifdef DEBUG_ASSERT | |
27 | #include <assert.h> | |
28 | #endif | |
29 | ||
30 | int test01(void) | |
31 | { | |
32 | bool test = true; | |
33 | typedef std::string::size_type csize_type; | |
34 | typedef std::string::iterator citerator; | |
35 | csize_type npos = std::string::npos; | |
36 | csize_type csz01, csz02; | |
37 | ||
38 | const char str_lit01[] = "rodeo beach, marin"; | |
39 | const std::string str01(str_lit01); | |
40 | const std::string str02("baker beach, san francisco"); | |
41 | ||
42 | // basic_string(const string&, size_type pos = 0, siz_type n = npos, alloc) | |
43 | csz01 = str01.size(); | |
44 | try { | |
45 | std::string str03(str01, csz01 + 1); | |
46 | test &= false; | |
47 | } | |
48 | catch(std::out_of_range& fail) { | |
49 | test &= true; | |
50 | } | |
51 | catch(...) { | |
52 | test &= false; | |
53 | } | |
54 | ||
55 | try { | |
56 | std::string str03(str01, csz01); | |
57 | test &= str03.size() == 0; | |
58 | test &= str03.size() <= str03.capacity(); | |
59 | } | |
60 | catch(...) { | |
61 | test &= false; | |
62 | } | |
63 | ||
64 | ||
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); | |
71 | test &= true; | |
72 | } | |
73 | catch(std::length_error& fail) { | |
74 | test &= true; | |
75 | } | |
76 | catch(...) { | |
77 | test &= false; | |
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); |
b2dad0e3 BK |
85 | test &= true; |
86 | } | |
87 | catch(std::length_error& fail) { | |
88 | test &= true; | |
89 | } | |
90 | catch(...) { | |
91 | test &= false; | |
92 | } | |
93 | ||
644638bc | 94 | // Build a maxsize-1 lengthed string consisting of all A's |
b2dad0e3 | 95 | try { |
644638bc BK |
96 | std::string str03(csz01 - 1, 'A'); |
97 | test &= str03.size() == csz01 - 1; | |
b2dad0e3 BK |
98 | test &= str03.size() <= str03.capacity(); |
99 | } | |
100 | // NB: bad_alloc is regrettable but entirely kosher for | |
101 | // out-of-memory situations. | |
102 | catch(std::bad_alloc& fail) { | |
103 | test &= true; | |
104 | } | |
105 | catch(...) { | |
106 | test &= false; | |
107 | } | |
108 | ||
109 | ||
110 | // basic_string(const char* s, const allocator& a = allocator()) | |
111 | std::string str04(str_lit01); | |
112 | test &= str01 == str04; | |
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'); | |
119 | test &= false; | |
120 | } | |
121 | catch(std::length_error& fail) { | |
122 | test &= true; | |
123 | } | |
124 | catch(...) { | |
125 | test &= false; | |
126 | } | |
127 | ||
128 | try { | |
129 | std::string str04(npos, 'b'); // the "maverick's" of all string objects. | |
130 | test &= false; | |
131 | } | |
132 | catch(std::length_error& fail) { | |
133 | test &= true; | |
134 | } | |
135 | catch(...) { | |
136 | test &= false; | |
137 | } | |
138 | ||
139 | try { | |
140 | std::string str03(csz01 - 1, 'z'); | |
141 | test &= str03.size() != 0; | |
142 | test &= str03.size() <= str03.capacity(); | |
143 | } | |
144 | // NB: bad_alloc is regrettable but entirely kosher for | |
145 | // out-of-memory situations. | |
146 | catch(std::bad_alloc& fail) { | |
147 | test &= true; | |
148 | } | |
149 | catch(...) { | |
150 | test &= false; | |
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()); | |
157 | test &= str06 == str01; | |
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); | |
173 | test &= s.size() == 10; | |
174 | #ifdef DEBUG_ASSERT | |
175 | assert(test); | |
176 | #endif | |
177 | } | |
178 | ||
179 | int main() | |
180 | { | |
181 | test01(); | |
182 | test02(); | |
183 | } | |
184 | ||
185 | ||
186 | ||
187 | ||
188 | ||
189 | ||
190 | ||
191 |