2333cc3dd8755f2efcd1b9326b3db4d42e1d137b
[gcc.git] / libstdc++-v3 / testsuite / 21_strings / append.cc
1 // 1999-07-08 bkoz
2
3 // Copyright (C) 1999 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.
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.5.3 basic_string::assign
22
23 #include <string>
24 #include <stdexcept>
25 #include <debug_assert.h>
26
27 bool test01(void)
28 {
29 bool test = true;
30 typedef std::string::size_type csize_type;
31 typedef std::string::const_reference cref;
32 typedef std::string::reference ref;
33 csize_type npos = std::string::npos;
34 csize_type csz01, csz02;
35
36 const char str_lit01[] = "point bolivar, texas";
37 const std::string str01(str_lit01);
38 const std::string str02("corpus, ");
39 const std::string str03;
40 std::string str05;
41
42
43 // string& append(const string&)
44 str05 = str02;
45 str05.append(str05);
46 VERIFY( str05 == "corpus, corpus, " );
47 str05.append(str01);
48 VERIFY( str05 == "corpus, corpus, point bolivar, texas" );
49 str05.append(str03);
50 VERIFY( str05 == "corpus, corpus, point bolivar, texas" );
51 std::string str06;
52 str06.append(str05);
53 VERIFY( str06 == str05 );
54
55
56 // string& append(const string&, size_type pos, size_type n)
57 str05.erase();
58 str06.erase();
59 csz01 = str03.size();
60 try {
61 str06.append(str03, csz01 + 1, 0);
62 VERIFY( false );
63 }
64 catch(std::out_of_range& fail) {
65 VERIFY( true );
66 }
67 catch(...) {
68 VERIFY( false );
69 }
70
71 csz01 = str01.size();
72 try {
73 str06.append(str01, csz01 + 1, 0);
74 VERIFY( false );
75 }
76 catch(std::out_of_range& fail) {
77 VERIFY( true );
78 }
79 catch(...) {
80 VERIFY( false );
81 }
82
83 str05 = str02;
84 str05.append(str01, 0, std::string::npos);
85 VERIFY( str05 == "corpus, point bolivar, texas" );
86 VERIFY( str05 != str02 );
87
88 str06 = str02;
89 str06.append(str01, 15, std::string::npos);
90 VERIFY( str06 == "corpus, texas" );
91 VERIFY( str02 != str06 );
92
93
94 // string& append(const char* s)
95 str05.erase();
96 str06.erase();
97 str05.append("");
98 VERIFY( str05 == str03 );
99
100 str05.append(str_lit01);
101 VERIFY( str05 == str01 );
102
103 str06 = str02;
104 str06.append("corpus, ");
105 VERIFY( str06 == "corpus, corpus, " );
106
107
108 // string& append(const char* s, size_type n)
109 str05.erase();
110 str06.erase();
111 str05.append("", 0);
112 VERIFY( str05.size() == 0 );
113 VERIFY( str05 == str03 );
114
115 str05.append(str_lit01, sizeof(str_lit01) - 1);
116 VERIFY( str05 == str01 );
117
118 str06 = str02;
119 str06.append("corpus, ", 6);
120 VERIFY( str06 == "corpus, corpus" );
121
122 str06 = str02;
123 str06.append("corpus, ", 12);
124 VERIFY( str06 != "corpus, corpus, " );
125
126
127 // string& append(size_type n, char c)
128 str05.erase();
129 str06.erase();
130 str05.append(0, 'a');
131 VERIFY( str05 == str03 );
132 str06.append(8, '.');
133 VERIFY( str06 == "........" );
134
135
136 // template<typename InputIter>
137 // string& append(InputIter first, InputIter last)
138 str05.erase();
139 str06.erase();
140 str05.append(str03.begin(), str03.end());
141 VERIFY( str05 == str03 );
142
143 str06 = str02;
144 str06.append(str01.begin(), str01.begin() + str01.find('r'));
145 VERIFY( str06 == "corpus, point boliva" );
146 VERIFY( str06 != str01 );
147 VERIFY( str06 != str02 );
148
149 str05 = str01;
150 str05.append(str05.begin(), str05.begin() + str05.find('r'));
151 VERIFY( str05 == "point bolivar, texaspoint boliva" );
152 VERIFY( str05 != str01 );
153
154 #ifdef DEBUG_ASSERT
155 assert(test);
156 #endif
157 return test;
158 }
159
160 int main()
161 {
162 test01();
163 return 0;
164 }