]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/21_strings/basic_string/cons/wchar_t/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / basic_string / cons / wchar_t / 1.cc
CommitLineData
b2dad0e3
BK
1// 1999-06-04 bkoz
2
99dee823 3// Copyright (C) 1999-2021 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
22#include <new>
23#include <string>
24#include <stdexcept>
fe413112 25#include <testsuite_hooks.h>
b2dad0e3 26
fcaa8101 27void test01(void)
b2dad0e3 28{
61f1ed59
PC
29 typedef std::wstring::size_type csize_type;
30 typedef std::wstring::iterator citerator;
31 csize_type npos = std::wstring::npos;
11f10e6b 32 csize_type csz01;
b2dad0e3 33
61f1ed59
PC
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");
b2dad0e3 37
61f1ed59 38 // basic_string(const wstring&, size_type pos = 0, siz_type n = npos, alloc)
b2dad0e3
BK
39 csz01 = str01.size();
40 try {
61f1ed59 41 std::wstring str03(str01, csz01 + 1);
aa1b2f7d 42 VERIFY( false );
b2dad0e3
BK
43 }
44 catch(std::out_of_range& fail) {
aa1b2f7d 45 VERIFY( true );
b2dad0e3
BK
46 }
47 catch(...) {
aa1b2f7d 48 VERIFY( false );
b2dad0e3
BK
49 }
50
51 try {
61f1ed59 52 std::wstring str03(str01, csz01);
aa1b2f7d
BV
53 VERIFY( str03.size() == 0 );
54 VERIFY( str03.size() <= str03.capacity() );
b2dad0e3
BK
55 }
56 catch(...) {
aa1b2f7d 57 VERIFY( false );
b2dad0e3
BK
58 }
59
61f1ed59 60 // basic_string(const wchar_t* s, size_type n, alloc)
b2dad0e3
BK
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 {
61f1ed59 65 std::wstring str03(str_lit01, csz01 + 1);
aa1b2f7d 66 VERIFY( true );
b2dad0e3
BK
67 }
68 catch(std::length_error& fail) {
aa1b2f7d 69 VERIFY( true );
b2dad0e3
BK
70 }
71 catch(...) {
aa1b2f7d 72 VERIFY( false );
b2dad0e3
BK
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.
644638bc 77 // The "maverick's" of all string objects.
b2dad0e3 78 try {
61f1ed59 79 std::wstring str04(str_lit01, npos);
aa1b2f7d 80 VERIFY( true );
b2dad0e3
BK
81 }
82 catch(std::length_error& fail) {
aa1b2f7d 83 VERIFY( true );
b2dad0e3
BK
84 }
85 catch(...) {
aa1b2f7d 86 VERIFY( false );
b2dad0e3
BK
87 }
88
6b76f569 89 // Build a maxsize - 1 lengthed string consisting of all A's
b2dad0e3 90 try {
61f1ed59 91 std::wstring str03(csz01 - 1, 'A');
aa1b2f7d
BV
92 VERIFY( str03.size() == csz01 - 1 );
93 VERIFY( str03.size() <= str03.capacity() );
b2dad0e3
BK
94 }
95 // NB: bad_alloc is regrettable but entirely kosher for
96 // out-of-memory situations.
97 catch(std::bad_alloc& fail) {
aa1b2f7d 98 VERIFY( true );
b2dad0e3
BK
99 }
100 catch(...) {
aa1b2f7d 101 VERIFY( false );
b2dad0e3 102 }
b2dad0e3 103
61f1ed59
PC
104 // basic_string(const wchar_t* s, const allocator& a = allocator())
105 std::wstring str04(str_lit01);
aa1b2f7d 106 VERIFY( str01 == str04 );
b2dad0e3
BK
107
108
109 // basic_string(size_type n, char c, const allocator& a = allocator())
110 csz01 = str01.max_size();
111 try {
61f1ed59 112 std::wstring str03(csz01 + 1, L'z');
aa1b2f7d 113 VERIFY( false );
b2dad0e3
BK
114 }
115 catch(std::length_error& fail) {
aa1b2f7d 116 VERIFY( true );
b2dad0e3
BK
117 }
118 catch(...) {
aa1b2f7d 119 VERIFY( false );
b2dad0e3
BK
120 }
121
122 try {
61f1ed59 123 std::wstring str04(npos, L'b'); // the "maverick's" of all string objects.
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 {
61f1ed59 134 std::wstring str03(csz01 - 1, L'z');
aa1b2f7d
BV
135 VERIFY( str03.size() != 0 );
136 VERIFY( str03.size() <= str03.capacity() );
b2dad0e3
BK
137 }
138 // NB: bad_alloc is regrettable but entirely kosher for
139 // out-of-memory situations.
140 catch(std::bad_alloc& fail) {
aa1b2f7d 141 VERIFY( true );
b2dad0e3
BK
142 }
143 catch(...) {
aa1b2f7d 144 VERIFY( false );
b2dad0e3
BK
145 }
146
147
148 // template<typename _InputIter>
149 // basic_string(_InputIter begin, _InputIter end, const allocator& a)
61f1ed59 150 std::wstring str06(str01.begin(), str01.end());
aa1b2f7d 151 VERIFY( str06 == str01 );
b2dad0e3
BK
152}
153
b2dad0e3
BK
154int main()
155{
aecf642c 156 __gnu_test::set_memory_limits();
b2dad0e3 157 test01();
04d930e6 158 return 0;
b2dad0e3 159}