| 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 |
#include <new> |
| 23 |
#include <string> |
| 24 |
#include <stdexcept> |
| 25 |
#include <testsuite_hooks.h> |
| 26 |
|
| 27 |
void test01(void) |
| 28 |
{ |
| 29 |
typedef std::wstring::size_type csize_type; |
| 30 |
typedef std::wstring::iterator citerator; |
| 31 |
csize_type npos = std::wstring::npos; |
| 32 |
csize_type csz01; |
| 33 |
|
| 34 |
const wchar_t str_lit01[] = L"rodeo beach, marin"; |
| 35 |
const std::wstring str01(str_lit01); |
| 36 |
const std::wstring str02(L"baker beach, san francisco"); |
| 37 |
|
| 38 |
// basic_string(const wstring&, size_type pos = 0, siz_type n = npos, alloc) |
| 39 |
csz01 = str01.size(); |
| 40 |
try { |
| 41 |
std::wstring str03(str01, csz01 + 1); |
| 42 |
VERIFY( false ); |
| 43 |
} |
| 44 |
catch(std::out_of_range& fail) { |
| 45 |
VERIFY( true ); |
| 46 |
} |
| 47 |
catch(...) { |
| 48 |
VERIFY( false ); |
| 49 |
} |
| 50 |
|
| 51 |
try { |
| 52 |
std::wstring str03(str01, csz01); |
| 53 |
VERIFY( str03.size() == 0 ); |
| 54 |
VERIFY( str03.size() <= str03.capacity() ); |
| 55 |
} |
| 56 |
catch(...) { |
| 57 |
VERIFY( false ); |
| 58 |
} |
| 59 |
|
| 60 |
// basic_string(const wchar_t* s, size_type n, alloc) |
| 61 |
csz01 = str01.max_size(); |
| 62 |
// NB: As strlen(str_lit01) != csz01, this test is undefined. It |
| 63 |
// should not crash, but what gets constructed is a bit arbitrary. |
| 64 |
try { |
| 65 |
std::wstring str03(str_lit01, csz01 + 1); |
| 66 |
VERIFY( true ); |
| 67 |
} |
| 68 |
catch(std::length_error& fail) { |
| 69 |
VERIFY( true ); |
| 70 |
} |
| 71 |
catch(...) { |
| 72 |
VERIFY( false ); |
| 73 |
} |
| 74 |
|
| 75 |
// NB: As strlen(str_lit01) != csz01, this test is undefined. It |
| 76 |
// should not crash, but what gets constructed is a bit arbitrary. |
| 77 |
// The "maverick's" of all string objects. |
| 78 |
try { |
| 79 |
std::wstring str04(str_lit01, npos); |
| 80 |
VERIFY( true ); |
| 81 |
} |
| 82 |
catch(std::length_error& fail) { |
| 83 |
VERIFY( true ); |
| 84 |
} |
| 85 |
catch(...) { |
| 86 |
VERIFY( false ); |
| 87 |
} |
| 88 |
|
| 89 |
// Build a maxsize - 1 lengthed string consisting of all A's |
| 90 |
try { |
| 91 |
std::wstring str03(csz01 - 1, 'A'); |
| 92 |
VERIFY( str03.size() == csz01 - 1 ); |
| 93 |
VERIFY( str03.size() <= str03.capacity() ); |
| 94 |
} |
| 95 |
// NB: bad_alloc is regrettable but entirely kosher for |
| 96 |
// out-of-memory situations. |
| 97 |
catch(std::bad_alloc& fail) { |
| 98 |
VERIFY( true ); |
| 99 |
} |
| 100 |
catch(...) { |
| 101 |
VERIFY( false ); |
| 102 |
} |
| 103 |
|
| 104 |
// basic_string(const wchar_t* s, const allocator& a = allocator()) |
| 105 |
std::wstring str04(str_lit01); |
| 106 |
VERIFY( str01 == str04 ); |
| 107 |
|
| 108 |
|
| 109 |
// basic_string(size_type n, char c, const allocator& a = allocator()) |
| 110 |
csz01 = str01.max_size(); |
| 111 |
try { |
| 112 |
std::wstring str03(csz01 + 1, L'z'); |
| 113 |
VERIFY( false ); |
| 114 |
} |
| 115 |
catch(std::length_error& fail) { |
| 116 |
VERIFY( true ); |
| 117 |
} |
| 118 |
catch(...) { |
| 119 |
VERIFY( false ); |
| 120 |
} |
| 121 |
|
| 122 |
try { |
| 123 |
std::wstring str04(npos, L'b'); // the "maverick's" of all string objects. |
| 124 |
VERIFY( false ); |
| 125 |
} |
| 126 |
catch(std::length_error& fail) { |
| 127 |
VERIFY( true ); |
| 128 |
} |
| 129 |
catch(...) { |
| 130 |
VERIFY( false ); |
| 131 |
} |
| 132 |
|
| 133 |
try { |
| 134 |
std::wstring str03(csz01 - 1, L'z'); |
| 135 |
VERIFY( str03.size() != 0 ); |
| 136 |
VERIFY( str03.size() <= str03.capacity() ); |
| 137 |
} |
| 138 |
// NB: bad_alloc is regrettable but entirely kosher for |
| 139 |
// out-of-memory situations. |
| 140 |
catch(std::bad_alloc& fail) { |
| 141 |
VERIFY( true ); |
| 142 |
} |
| 143 |
catch(...) { |
| 144 |
VERIFY( false ); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
// template<typename _InputIter> |
| 149 |
// basic_string(_InputIter begin, _InputIter end, const allocator& a) |
| 150 |
std::wstring str06(str01.begin(), str01.end()); |
| 151 |
VERIFY( str06 == str01 ); |
| 152 |
} |
| 153 |
|
| 154 |
int main() |
| 155 |
{ |
| 156 |
__gnu_test::set_memory_limits(); |
| 157 |
test01(); |
| 158 |
return 0; |
| 159 |
} |