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