2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / testsuite / 21_strings / basic_string / cons / char / 1.cc
blob996f8ad0d32bffa9178988284a895f5ee8ca525a
1 // 1999-06-04 bkoz
3 // Copyright (C) 1999, 2000, 2001, 2002, 2003 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 2, or (at your option)
9 // any later version.
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.
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.
21 // 21.3.1 basic_string constructors.
23 #include <new>
24 #include <string>
25 #include <stdexcept>
26 #include <testsuite_hooks.h>
28 void test01(void)
30 bool test __attribute__((unused)) = 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;
36 const char str_lit01[] = "rodeo beach, marin";
37 const std::string str01(str_lit01);
38 const std::string str02("baker beach, san francisco");
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);
44 VERIFY( false );
46 catch(std::out_of_range& fail) {
47 VERIFY( true );
49 catch(...) {
50 VERIFY( false );
53 try {
54 std::string str03(str01, csz01);
55 VERIFY( str03.size() == 0 );
56 VERIFY( str03.size() <= str03.capacity() );
58 catch(...) {
59 VERIFY( false );
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);
68 VERIFY( true );
70 catch(std::length_error& fail) {
71 VERIFY( true );
73 catch(...) {
74 VERIFY( false );
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::string str04(str_lit01, npos);
82 VERIFY( true );
84 catch(std::length_error& fail) {
85 VERIFY( true );
87 catch(...) {
88 VERIFY( false );
91 // Build a maxsize - 1 lengthed string consisting of all A's
92 try {
93 std::string str03(csz01 - 1, 'A');
94 VERIFY( str03.size() == csz01 - 1 );
95 VERIFY( str03.size() <= str03.capacity() );
97 // NB: bad_alloc is regrettable but entirely kosher for
98 // out-of-memory situations.
99 catch(std::bad_alloc& fail) {
100 VERIFY( true );
102 catch(...) {
103 VERIFY( false );
106 // basic_string(const char* s, const allocator& a = allocator())
107 std::string str04(str_lit01);
108 VERIFY( str01 == str04 );
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');
115 VERIFY( false );
117 catch(std::length_error& fail) {
118 VERIFY( true );
120 catch(...) {
121 VERIFY( false );
124 try {
125 std::string str04(npos, 'b'); // the "maverick's" of all string objects.
126 VERIFY( false );
128 catch(std::length_error& fail) {
129 VERIFY( true );
131 catch(...) {
132 VERIFY( false );
135 try {
136 std::string str03(csz01 - 1, 'z');
137 VERIFY( str03.size() != 0 );
138 VERIFY( str03.size() <= str03.capacity() );
140 // NB: bad_alloc is regrettable but entirely kosher for
141 // out-of-memory situations.
142 catch(std::bad_alloc& fail) {
143 VERIFY( true );
145 catch(...) {
146 VERIFY( false );
150 // template<typename _InputIter>
151 // basic_string(_InputIter begin, _InputIter end, const allocator& a)
152 std::string str06(str01.begin(), str01.end());
153 VERIFY( str06 == str01 );
156 int main()
158 __gnu_test::set_memory_limits();
159 test01();
160 return 0;