]>
Commit | Line | Data |
---|---|---|
b2dad0e3 BK |
1 | // 1999-06-04 bkoz |
2 | ||
3 | // Copyright (C) 1999 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 <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. | |
82 | try { | |
83 | std::string str04(str_lit01, npos); // the "maverick's" of all string objects. | |
84 | test &= true; | |
85 | } | |
86 | catch(std::length_error& fail) { | |
87 | test &= true; | |
88 | } | |
89 | catch(...) { | |
90 | test &= false; | |
91 | } | |
92 | ||
93 | try { | |
94 | std::string str03(str_lit01, csz01 - 1); | |
95 | test &= str03.size() != 0; | |
96 | test &= 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 | test &= true; | |
102 | } | |
103 | catch(...) { | |
104 | test &= false; | |
105 | } | |
106 | ||
107 | ||
108 | // basic_string(const char* s, const allocator& a = allocator()) | |
109 | std::string str04(str_lit01); | |
110 | test &= str01 == str04; | |
111 | ||
112 | ||
113 | // basic_string(size_type n, char c, const allocator& a = allocator()) | |
114 | csz01 = str01.max_size(); | |
115 | try { | |
116 | std::string str03(csz01 + 1, 'z'); | |
117 | test &= false; | |
118 | } | |
119 | catch(std::length_error& fail) { | |
120 | test &= true; | |
121 | } | |
122 | catch(...) { | |
123 | test &= false; | |
124 | } | |
125 | ||
126 | try { | |
127 | std::string str04(npos, 'b'); // the "maverick's" of all string objects. | |
128 | test &= false; | |
129 | } | |
130 | catch(std::length_error& fail) { | |
131 | test &= true; | |
132 | } | |
133 | catch(...) { | |
134 | test &= false; | |
135 | } | |
136 | ||
137 | try { | |
138 | std::string str03(csz01 - 1, 'z'); | |
139 | test &= str03.size() != 0; | |
140 | test &= str03.size() <= str03.capacity(); | |
141 | } | |
142 | // NB: bad_alloc is regrettable but entirely kosher for | |
143 | // out-of-memory situations. | |
144 | catch(std::bad_alloc& fail) { | |
145 | test &= true; | |
146 | } | |
147 | catch(...) { | |
148 | test &= false; | |
149 | } | |
150 | ||
151 | ||
152 | // template<typename _InputIter> | |
153 | // basic_string(_InputIter begin, _InputIter end, const allocator& a) | |
154 | std::string str06(str01.begin(), str01.end()); | |
155 | test &= str06 == str01; | |
156 | ||
157 | #ifdef DEBUG_ASSERT | |
158 | assert(test); | |
159 | #endif | |
160 | return test; | |
161 | } | |
162 | ||
163 | void test02() | |
164 | { | |
165 | bool test = true; | |
166 | ||
167 | // template<typename _InputIter> | |
168 | // basic_string(_InputIter begin, _InputIter end, const allocator& a) | |
169 | // where _InputIter is integral [21.3.1 para 15] | |
170 | std::string s(10,0); | |
171 | test &= s.size() == 10; | |
172 | #ifdef DEBUG_ASSERT | |
173 | assert(test); | |
174 | #endif | |
175 | } | |
176 | ||
177 | int main() | |
178 | { | |
179 | test01(); | |
180 | test02(); | |
181 | } | |
182 | ||
183 | ||
184 | ||
185 | ||
186 | ||
187 | ||
188 | ||
189 |