]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/21_strings/basic_string/cons/char/1.cc
*: Change __gnu_cxx_test to __gnu_test.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / basic_string / cons / char / 1.cc
CommitLineData
b2dad0e3
BK
1// 1999-06-04 bkoz
2
8d59b230 3// Copyright (C) 1999, 2000, 2001, 2002, 2003 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
8// Free Software Foundation; either version 2, 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 COPYING. If not, write to the Free
18// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19// USA.
20
21// 21.3.1 basic_string constructors.
22
23#include <new>
24#include <string>
25#include <stdexcept>
fe413112 26#include <testsuite_hooks.h>
b2dad0e3 27
fcaa8101 28void test01(void)
b2dad0e3
BK
29{
30 bool test = true;
31 typedef std::string::size_type csize_type;
32 typedef std::string::iterator citerator;
33 csize_type npos = std::string::npos;
34 csize_type csz01, csz02;
35
36 const char str_lit01[] = "rodeo beach, marin";
37 const std::string str01(str_lit01);
38 const std::string str02("baker beach, san francisco");
39
40 // basic_string(const string&, size_type pos = 0, siz_type n = npos, alloc)
41 csz01 = str01.size();
42 try {
43 std::string str03(str01, csz01 + 1);
aa1b2f7d 44 VERIFY( false );
b2dad0e3
BK
45 }
46 catch(std::out_of_range& fail) {
aa1b2f7d 47 VERIFY( true );
b2dad0e3
BK
48 }
49 catch(...) {
aa1b2f7d 50 VERIFY( false );
b2dad0e3
BK
51 }
52
53 try {
54 std::string str03(str01, csz01);
aa1b2f7d
BV
55 VERIFY( str03.size() == 0 );
56 VERIFY( str03.size() <= str03.capacity() );
b2dad0e3
BK
57 }
58 catch(...) {
aa1b2f7d 59 VERIFY( false );
b2dad0e3
BK
60 }
61
b2dad0e3
BK
62 // basic_string(const char* 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::string str03(str_lit01, csz01 + 1);
aa1b2f7d 68 VERIFY( true );
b2dad0e3
BK
69 }
70 catch(std::length_error& fail) {
aa1b2f7d 71 VERIFY( true );
b2dad0e3
BK
72 }
73 catch(...) {
aa1b2f7d 74 VERIFY( false );
b2dad0e3
BK
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.
644638bc 79 // The "maverick's" of all string objects.
b2dad0e3 80 try {
644638bc 81 std::string str04(str_lit01, npos);
aa1b2f7d 82 VERIFY( true );
b2dad0e3
BK
83 }
84 catch(std::length_error& fail) {
aa1b2f7d 85 VERIFY( true );
b2dad0e3
BK
86 }
87 catch(...) {
aa1b2f7d 88 VERIFY( false );
b2dad0e3
BK
89 }
90
6b76f569 91 // Build a maxsize - 1 lengthed string consisting of all A's
b2dad0e3 92 try {
644638bc 93 std::string str03(csz01 - 1, 'A');
aa1b2f7d
BV
94 VERIFY( str03.size() == csz01 - 1 );
95 VERIFY( str03.size() <= str03.capacity() );
b2dad0e3
BK
96 }
97 // NB: bad_alloc is regrettable but entirely kosher for
98 // out-of-memory situations.
99 catch(std::bad_alloc& fail) {
aa1b2f7d 100 VERIFY( true );
b2dad0e3
BK
101 }
102 catch(...) {
aa1b2f7d 103 VERIFY( false );
b2dad0e3 104 }
b2dad0e3
BK
105
106 // basic_string(const char* s, const allocator& a = allocator())
107 std::string str04(str_lit01);
aa1b2f7d 108 VERIFY( str01 == str04 );
b2dad0e3
BK
109
110
111 // basic_string(size_type n, char c, const allocator& a = allocator())
112 csz01 = str01.max_size();
113 try {
114 std::string str03(csz01 + 1, 'z');
aa1b2f7d 115 VERIFY( false );
b2dad0e3
BK
116 }
117 catch(std::length_error& fail) {
aa1b2f7d 118 VERIFY( true );
b2dad0e3
BK
119 }
120 catch(...) {
aa1b2f7d 121 VERIFY( false );
b2dad0e3
BK
122 }
123
124 try {
125 std::string str04(npos, 'b'); // the "maverick's" of all string objects.
aa1b2f7d 126 VERIFY( false );
b2dad0e3
BK
127 }
128 catch(std::length_error& fail) {
aa1b2f7d 129 VERIFY( true );
b2dad0e3
BK
130 }
131 catch(...) {
aa1b2f7d 132 VERIFY( false );
b2dad0e3
BK
133 }
134
135 try {
136 std::string str03(csz01 - 1, 'z');
aa1b2f7d
BV
137 VERIFY( str03.size() != 0 );
138 VERIFY( str03.size() <= str03.capacity() );
b2dad0e3
BK
139 }
140 // NB: bad_alloc is regrettable but entirely kosher for
141 // out-of-memory situations.
142 catch(std::bad_alloc& fail) {
aa1b2f7d 143 VERIFY( true );
b2dad0e3
BK
144 }
145 catch(...) {
aa1b2f7d 146 VERIFY( false );
b2dad0e3
BK
147 }
148
149
150 // template<typename _InputIter>
151 // basic_string(_InputIter begin, _InputIter end, const allocator& a)
152 std::string str06(str01.begin(), str01.end());
aa1b2f7d 153 VERIFY( str06 == str01 );
b2dad0e3
BK
154}
155
b2dad0e3
BK
156int main()
157{
aecf642c 158 __gnu_test::set_memory_limits();
b2dad0e3 159 test01();
04d930e6 160 return 0;
b2dad0e3 161}