]>
Commit | Line | Data |
---|---|---|
b2dad0e3 BK |
1 | // 1999-06-04 bkoz |
2 | ||
a945c346 | 3 | // Copyright (C) 1999-2024 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 | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
b2dad0e3 BK |
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 | |
748086b7 JJ |
17 | // with this library; see the file COPYING3. If not see |
18 | // <http://www.gnu.org/licenses/>. | |
b2dad0e3 BK |
19 | |
20 | // 21.3.1 basic_string constructors. | |
21 | ||
86e2dc22 JW |
22 | // { dg-options "-Wno-stringop-overflow" } |
23 | ||
b2dad0e3 | 24 | #include <new> |
b2dad0e3 | 25 | #include <stdexcept> |
fe413112 | 26 | #include <testsuite_hooks.h> |
b2dad0e3 | 27 | |
96eb9df6 FD |
28 | #ifdef _GLIBCXX_DEBUG |
29 | # include <debug/string> | |
30 | using namespace __gnu_debug; | |
31 | #else | |
32 | # include <string> | |
33 | using namespace std; | |
34 | #endif | |
35 | ||
fcaa8101 | 36 | void test01(void) |
b2dad0e3 | 37 | { |
96eb9df6 | 38 | typedef string::size_type csize_type; |
96eb9df6 | 39 | csize_type npos = string::npos; |
11f10e6b | 40 | csize_type csz01; |
b2dad0e3 BK |
41 | |
42 | const char str_lit01[] = "rodeo beach, marin"; | |
96eb9df6 FD |
43 | const string str01(str_lit01); |
44 | const string str02("baker beach, san francisco"); | |
b2dad0e3 BK |
45 | |
46 | // basic_string(const string&, size_type pos = 0, siz_type n = npos, alloc) | |
47 | csz01 = str01.size(); | |
48 | try { | |
96eb9df6 | 49 | string str03(str01, csz01 + 1); |
aa1b2f7d | 50 | VERIFY( false ); |
b2dad0e3 BK |
51 | } |
52 | catch(std::out_of_range& fail) { | |
aa1b2f7d | 53 | VERIFY( true ); |
b2dad0e3 BK |
54 | } |
55 | catch(...) { | |
aa1b2f7d | 56 | VERIFY( false ); |
b2dad0e3 BK |
57 | } |
58 | ||
59 | try { | |
96eb9df6 | 60 | string str03(str01, csz01); |
aa1b2f7d BV |
61 | VERIFY( str03.size() == 0 ); |
62 | VERIFY( str03.size() <= str03.capacity() ); | |
b2dad0e3 BK |
63 | } |
64 | catch(...) { | |
aa1b2f7d | 65 | VERIFY( false ); |
b2dad0e3 BK |
66 | } |
67 | ||
b2dad0e3 BK |
68 | // basic_string(const char* s, size_type n, alloc) |
69 | csz01 = str01.max_size(); | |
92edc4a7 JW |
70 | #pragma GCC diagnostic push |
71 | #pragma GCC diagnostic ignored "-Wstringop-overread" | |
b2dad0e3 BK |
72 | // NB: As strlen(str_lit01) != csz01, this test is undefined. It |
73 | // should not crash, but what gets constructed is a bit arbitrary. | |
74 | try { | |
96eb9df6 | 75 | string str03(str_lit01, csz01 + 1); |
aa1b2f7d | 76 | VERIFY( true ); |
b2dad0e3 BK |
77 | } |
78 | catch(std::length_error& fail) { | |
aa1b2f7d | 79 | VERIFY( true ); |
b2dad0e3 BK |
80 | } |
81 | catch(...) { | |
aa1b2f7d | 82 | VERIFY( false ); |
b2dad0e3 BK |
83 | } |
84 | ||
85 | // NB: As strlen(str_lit01) != csz01, this test is undefined. It | |
86 | // should not crash, but what gets constructed is a bit arbitrary. | |
644638bc | 87 | // The "maverick's" of all string objects. |
b2dad0e3 | 88 | try { |
96eb9df6 | 89 | string str04(str_lit01, npos); |
aa1b2f7d | 90 | VERIFY( true ); |
b2dad0e3 BK |
91 | } |
92 | catch(std::length_error& fail) { | |
aa1b2f7d | 93 | VERIFY( true ); |
b2dad0e3 BK |
94 | } |
95 | catch(...) { | |
aa1b2f7d | 96 | VERIFY( false ); |
b2dad0e3 | 97 | } |
92edc4a7 | 98 | #pragma GCC diagnostic pop |
b2dad0e3 | 99 | |
6b76f569 | 100 | // Build a maxsize - 1 lengthed string consisting of all A's |
b2dad0e3 | 101 | try { |
96eb9df6 | 102 | string str03(csz01 - 1, 'A'); |
aa1b2f7d BV |
103 | VERIFY( str03.size() == csz01 - 1 ); |
104 | VERIFY( str03.size() <= str03.capacity() ); | |
b2dad0e3 BK |
105 | } |
106 | // NB: bad_alloc is regrettable but entirely kosher for | |
107 | // out-of-memory situations. | |
108 | catch(std::bad_alloc& fail) { | |
aa1b2f7d | 109 | VERIFY( true ); |
b2dad0e3 BK |
110 | } |
111 | catch(...) { | |
aa1b2f7d | 112 | VERIFY( false ); |
b2dad0e3 | 113 | } |
b2dad0e3 BK |
114 | |
115 | // basic_string(const char* s, const allocator& a = allocator()) | |
96eb9df6 | 116 | string str04(str_lit01); |
aa1b2f7d | 117 | VERIFY( str01 == str04 ); |
b2dad0e3 BK |
118 | |
119 | ||
120 | // basic_string(size_type n, char c, const allocator& a = allocator()) | |
121 | csz01 = str01.max_size(); | |
122 | try { | |
96eb9df6 | 123 | string str03(csz01 + 1, 'z'); |
aa1b2f7d | 124 | VERIFY( false ); |
b2dad0e3 BK |
125 | } |
126 | catch(std::length_error& fail) { | |
aa1b2f7d | 127 | VERIFY( true ); |
b2dad0e3 BK |
128 | } |
129 | catch(...) { | |
aa1b2f7d | 130 | VERIFY( false ); |
b2dad0e3 BK |
131 | } |
132 | ||
133 | try { | |
96eb9df6 | 134 | string str04(npos, 'b'); // the "maverick's" of all string objects. |
aa1b2f7d | 135 | VERIFY( false ); |
b2dad0e3 BK |
136 | } |
137 | catch(std::length_error& fail) { | |
aa1b2f7d | 138 | VERIFY( true ); |
b2dad0e3 BK |
139 | } |
140 | catch(...) { | |
aa1b2f7d | 141 | VERIFY( false ); |
b2dad0e3 BK |
142 | } |
143 | ||
144 | try { | |
96eb9df6 | 145 | string str03(csz01 - 1, 'z'); |
aa1b2f7d BV |
146 | VERIFY( str03.size() != 0 ); |
147 | VERIFY( str03.size() <= str03.capacity() ); | |
b2dad0e3 BK |
148 | } |
149 | // NB: bad_alloc is regrettable but entirely kosher for | |
150 | // out-of-memory situations. | |
151 | catch(std::bad_alloc& fail) { | |
aa1b2f7d | 152 | VERIFY( true ); |
b2dad0e3 BK |
153 | } |
154 | catch(...) { | |
aa1b2f7d | 155 | VERIFY( false ); |
b2dad0e3 BK |
156 | } |
157 | ||
b2dad0e3 BK |
158 | // template<typename _InputIter> |
159 | // basic_string(_InputIter begin, _InputIter end, const allocator& a) | |
96eb9df6 | 160 | string str06(str01.begin(), str01.end()); |
aa1b2f7d | 161 | VERIFY( str06 == str01 ); |
b2dad0e3 BK |
162 | } |
163 | ||
b2dad0e3 BK |
164 | int main() |
165 | { | |
aecf642c | 166 | __gnu_test::set_memory_limits(); |
b2dad0e3 | 167 | test01(); |
04d930e6 | 168 | return 0; |
b2dad0e3 | 169 | } |