| 1 | // 1999-06-04 bkoz |
| 2 | |
| 3 | // Copyright (C) 1999-2025 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 3, 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 COPYING3. If not see |
| 18 | // <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | // 21.3.1 basic_string constructors. |
| 21 | |
| 22 | // { dg-options "-Wno-stringop-overflow" } |
| 23 | |
| 24 | #include <new> |
| 25 | #include <stdexcept> |
| 26 | #include <testsuite_hooks.h> |
| 27 | |
| 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 | |
| 36 | void test01(void) |
| 37 | { |
| 38 | typedef string::size_type csize_type; |
| 39 | csize_type npos = string::npos; |
| 40 | csize_type csz01; |
| 41 | |
| 42 | const char str_lit01[] = "rodeo beach, marin"; |
| 43 | const string str01(str_lit01); |
| 44 | const string str02("baker beach, san francisco"); |
| 45 | |
| 46 | // basic_string(const string&, size_type pos = 0, siz_type n = npos, alloc) |
| 47 | csz01 = str01.size(); |
| 48 | try { |
| 49 | string str03(str01, csz01 + 1); |
| 50 | VERIFY( false ); |
| 51 | } |
| 52 | catch(std::out_of_range& fail) { |
| 53 | VERIFY( true ); |
| 54 | } |
| 55 | catch(...) { |
| 56 | VERIFY( false ); |
| 57 | } |
| 58 | |
| 59 | try { |
| 60 | string str03(str01, csz01); |
| 61 | VERIFY( str03.size() == 0 ); |
| 62 | VERIFY( str03.size() <= str03.capacity() ); |
| 63 | } |
| 64 | catch(...) { |
| 65 | VERIFY( false ); |
| 66 | } |
| 67 | |
| 68 | // basic_string(const char* s, size_type n, alloc) |
| 69 | csz01 = str01.max_size(); |
| 70 | #pragma GCC diagnostic push |
| 71 | #pragma GCC diagnostic ignored "-Wstringop-overread" |
| 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 { |
| 75 | string str03(str_lit01, csz01 + 1); |
| 76 | VERIFY( true ); |
| 77 | } |
| 78 | catch(std::length_error& fail) { |
| 79 | VERIFY( true ); |
| 80 | } |
| 81 | catch(...) { |
| 82 | VERIFY( false ); |
| 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. |
| 87 | // The "maverick's" of all string objects. |
| 88 | try { |
| 89 | string str04(str_lit01, npos); |
| 90 | VERIFY( true ); |
| 91 | } |
| 92 | catch(std::length_error& fail) { |
| 93 | VERIFY( true ); |
| 94 | } |
| 95 | catch(...) { |
| 96 | VERIFY( false ); |
| 97 | } |
| 98 | #pragma GCC diagnostic pop |
| 99 | |
| 100 | // Build a maxsize - 1 lengthed string consisting of all A's |
| 101 | try { |
| 102 | string str03(csz01 - 1, 'A'); |
| 103 | VERIFY( str03.size() == csz01 - 1 ); |
| 104 | VERIFY( str03.size() <= str03.capacity() ); |
| 105 | } |
| 106 | // NB: bad_alloc is regrettable but entirely kosher for |
| 107 | // out-of-memory situations. |
| 108 | catch(std::bad_alloc& fail) { |
| 109 | VERIFY( true ); |
| 110 | } |
| 111 | catch(...) { |
| 112 | VERIFY( false ); |
| 113 | } |
| 114 | |
| 115 | // basic_string(const char* s, const allocator& a = allocator()) |
| 116 | string str04(str_lit01); |
| 117 | VERIFY( str01 == str04 ); |
| 118 | |
| 119 | |
| 120 | // basic_string(size_type n, char c, const allocator& a = allocator()) |
| 121 | csz01 = str01.max_size(); |
| 122 | try { |
| 123 | string str03(csz01 + 1, 'z'); |
| 124 | VERIFY( false ); |
| 125 | } |
| 126 | catch(std::length_error& fail) { |
| 127 | VERIFY( true ); |
| 128 | } |
| 129 | catch(...) { |
| 130 | VERIFY( false ); |
| 131 | } |
| 132 | |
| 133 | try { |
| 134 | string str04(npos, 'b'); // the "maverick's" of all string objects. |
| 135 | VERIFY( false ); |
| 136 | } |
| 137 | catch(std::length_error& fail) { |
| 138 | VERIFY( true ); |
| 139 | } |
| 140 | catch(...) { |
| 141 | VERIFY( false ); |
| 142 | } |
| 143 | |
| 144 | try { |
| 145 | string str03(csz01 - 1, 'z'); |
| 146 | VERIFY( str03.size() != 0 ); |
| 147 | VERIFY( str03.size() <= str03.capacity() ); |
| 148 | } |
| 149 | // NB: bad_alloc is regrettable but entirely kosher for |
| 150 | // out-of-memory situations. |
| 151 | catch(std::bad_alloc& fail) { |
| 152 | VERIFY( true ); |
| 153 | } |
| 154 | catch(...) { |
| 155 | VERIFY( false ); |
| 156 | } |
| 157 | |
| 158 | // template<typename _InputIter> |
| 159 | // basic_string(_InputIter begin, _InputIter end, const allocator& a) |
| 160 | string str06(str01.begin(), str01.end()); |
| 161 | VERIFY( str06 == str01 ); |
| 162 | } |
| 163 | |
| 164 | int main() |
| 165 | { |
| 166 | __gnu_test::set_memory_limits(); |
| 167 | test01(); |
| 168 | return 0; |
| 169 | } |