1// 1999-06-04 bkoz 2 3// Copyright (C) 1999, 2000, 2001, 2002, 2003, 2009 4// Free Software Foundation, Inc. 5// 6// This file is part of the GNU ISO C++ Library. This library is free 7// software; you can redistribute it and/or modify it under the 8// terms of the GNU General Public License as published by the 9// Free Software Foundation; either version 3, or (at your option) 10// any later version. 11 12// This library is distributed in the hope that it will be useful, 13// but WITHOUT ANY WARRANTY; without even the implied warranty of 14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15// GNU General Public License for more details. 16 17// You should have received a copy of the GNU General Public License along 18// with this library; see the file COPYING3. If not see 19// <http://www.gnu.org/licenses/>. 20 21// 21.3.1 basic_string constructors. 22 23#include <new> 24#include <string> 25#include <stdexcept> 26#include <testsuite_hooks.h> 27 28void test01(void) 29{ 30 bool test __attribute__((unused)) = true; 31 typedef std::wstring::size_type csize_type; 32 typedef std::wstring::iterator citerator; 33 csize_type npos = std::wstring::npos; 34 csize_type csz01; 35 36 const wchar_t str_lit01[] = L"rodeo beach, marin"; 37 const std::wstring str01(str_lit01); 38 const std::wstring str02(L"baker beach, san francisco"); 39 40 // basic_string(const wstring&, size_type pos = 0, siz_type n = npos, alloc) 41 csz01 = str01.size(); 42 try { 43 std::wstring str03(str01, csz01 + 1); 44 VERIFY( false ); 45 } 46 catch(std::out_of_range& fail) { 47 VERIFY( true ); 48 } 49 catch(...) { 50 VERIFY( false ); 51 } 52 53 try { 54 std::wstring str03(str01, csz01); 55 VERIFY( str03.size() == 0 ); 56 VERIFY( str03.size() <= str03.capacity() ); 57 } 58 catch(...) { 59 VERIFY( false ); 60 } 61 62 // basic_string(const wchar_t* s, size_type n, alloc) 63 csz01 = str01.max_size(); 64 // NB: As strlen(str_lit01) != csz01, this test is undefined. It 65 // should not crash, but what gets constructed is a bit arbitrary. 66 try { 67 std::wstring str03(str_lit01, csz01 + 1); 68 VERIFY( true ); 69 } 70 catch(std::length_error& fail) { 71 VERIFY( true ); 72 } 73 catch(...) { 74 VERIFY( false ); 75 } 76 77 // NB: As strlen(str_lit01) != csz01, this test is undefined. It 78 // should not crash, but what gets constructed is a bit arbitrary. 79 // The "maverick's" of all string objects. 80 try { 81 std::wstring str04(str_lit01, npos); 82 VERIFY( true ); 83 } 84 catch(std::length_error& fail) { 85 VERIFY( true ); 86 } 87 catch(...) { 88 VERIFY( false ); 89 } 90 91 // Build a maxsize - 1 lengthed string consisting of all A's 92 try { 93 std::wstring str03(csz01 - 1, 'A'); 94 VERIFY( str03.size() == csz01 - 1 ); 95 VERIFY( str03.size() <= str03.capacity() ); 96 } 97 // NB: bad_alloc is regrettable but entirely kosher for 98 // out-of-memory situations. 99 catch(std::bad_alloc& fail) { 100 VERIFY( true ); 101 } 102 catch(...) { 103 VERIFY( false ); 104 } 105 106 // basic_string(const wchar_t* s, const allocator& a = allocator()) 107 std::wstring str04(str_lit01); 108 VERIFY( str01 == str04 ); 109 110 111 // basic_string(size_type n, char c, const allocator& a = allocator()) 112 csz01 = str01.max_size(); 113 try { 114 std::wstring str03(csz01 + 1, L'z'); 115 VERIFY( false ); 116 } 117 catch(std::length_error& fail) { 118 VERIFY( true ); 119 } 120 catch(...) { 121 VERIFY( false ); 122 } 123 124 try { 125 std::wstring str04(npos, L'b'); // the "maverick's" of all string objects. 126 VERIFY( false ); 127 } 128 catch(std::length_error& fail) { 129 VERIFY( true ); 130 } 131 catch(...) { 132 VERIFY( false ); 133 } 134 135 try { 136 std::wstring str03(csz01 - 1, L'z'); 137 VERIFY( str03.size() != 0 ); 138 VERIFY( str03.size() <= str03.capacity() ); 139 } 140 // NB: bad_alloc is regrettable but entirely kosher for 141 // out-of-memory situations. 142 catch(std::bad_alloc& fail) { 143 VERIFY( true ); 144 } 145 catch(...) { 146 VERIFY( false ); 147 } 148 149 150 // template<typename _InputIter> 151 // basic_string(_InputIter begin, _InputIter end, const allocator& a) 152 std::wstring str06(str01.begin(), str01.end()); 153 VERIFY( str06 == str01 ); 154} 155 156int main() 157{ 158 __gnu_test::set_memory_limits(); 159 test01(); 160 return 0; 161} 162