]>
Commit | Line | Data |
---|---|---|
b2dad0e3 BK |
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> | |
aa1b2f7d | 25 | #include <debug_assert.h> |
b2dad0e3 BK |
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); | |
aa1b2f7d | 46 | VERIFY( str05 == "corpus, corpus, " ); |
b2dad0e3 | 47 | str05.append(str01); |
aa1b2f7d | 48 | VERIFY( str05 == "corpus, corpus, point bolivar, texas" ); |
b2dad0e3 | 49 | str05.append(str03); |
aa1b2f7d | 50 | VERIFY( str05 == "corpus, corpus, point bolivar, texas" ); |
b2dad0e3 BK |
51 | std::string str06; |
52 | str06.append(str05); | |
aa1b2f7d | 53 | VERIFY( str06 == str05 ); |
b2dad0e3 BK |
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); | |
aa1b2f7d | 62 | VERIFY( false ); |
b2dad0e3 BK |
63 | } |
64 | catch(std::out_of_range& fail) { | |
aa1b2f7d | 65 | VERIFY( true ); |
b2dad0e3 BK |
66 | } |
67 | catch(...) { | |
aa1b2f7d | 68 | VERIFY( false ); |
b2dad0e3 BK |
69 | } |
70 | ||
71 | csz01 = str01.size(); | |
72 | try { | |
73 | str06.append(str01, csz01 + 1, 0); | |
aa1b2f7d | 74 | VERIFY( false ); |
b2dad0e3 BK |
75 | } |
76 | catch(std::out_of_range& fail) { | |
aa1b2f7d | 77 | VERIFY( true ); |
b2dad0e3 BK |
78 | } |
79 | catch(...) { | |
aa1b2f7d | 80 | VERIFY( false ); |
b2dad0e3 BK |
81 | } |
82 | ||
83 | str05 = str02; | |
84 | str05.append(str01, 0, std::string::npos); | |
aa1b2f7d BV |
85 | VERIFY( str05 == "corpus, point bolivar, texas" ); |
86 | VERIFY( str05 != str02 ); | |
b2dad0e3 BK |
87 | |
88 | str06 = str02; | |
89 | str06.append(str01, 15, std::string::npos); | |
aa1b2f7d BV |
90 | VERIFY( str06 == "corpus, texas" ); |
91 | VERIFY( str02 != str06 ); | |
b2dad0e3 BK |
92 | |
93 | ||
94 | // string& append(const char* s) | |
95 | str05.erase(); | |
96 | str06.erase(); | |
97 | str05.append(""); | |
aa1b2f7d | 98 | VERIFY( str05 == str03 ); |
b2dad0e3 BK |
99 | |
100 | str05.append(str_lit01); | |
aa1b2f7d | 101 | VERIFY( str05 == str01 ); |
b2dad0e3 BK |
102 | |
103 | str06 = str02; | |
104 | str06.append("corpus, "); | |
aa1b2f7d | 105 | VERIFY( str06 == "corpus, corpus, " ); |
b2dad0e3 BK |
106 | |
107 | ||
108 | // string& append(const char* s, size_type n) | |
109 | str05.erase(); | |
110 | str06.erase(); | |
111 | str05.append("", 0); | |
aa1b2f7d BV |
112 | VERIFY( str05.size() == 0 ); |
113 | VERIFY( str05 == str03 ); | |
b2dad0e3 BK |
114 | |
115 | str05.append(str_lit01, sizeof(str_lit01) - 1); | |
aa1b2f7d | 116 | VERIFY( str05 == str01 ); |
b2dad0e3 BK |
117 | |
118 | str06 = str02; | |
119 | str06.append("corpus, ", 6); | |
aa1b2f7d | 120 | VERIFY( str06 == "corpus, corpus" ); |
b2dad0e3 BK |
121 | |
122 | str06 = str02; | |
123 | str06.append("corpus, ", 12); | |
aa1b2f7d | 124 | VERIFY( str06 != "corpus, corpus, " ); |
b2dad0e3 BK |
125 | |
126 | ||
127 | // string& append(size_type n, char c) | |
128 | str05.erase(); | |
129 | str06.erase(); | |
130 | str05.append(0, 'a'); | |
aa1b2f7d | 131 | VERIFY( str05 == str03 ); |
b2dad0e3 | 132 | str06.append(8, '.'); |
aa1b2f7d | 133 | VERIFY( str06 == "........" ); |
b2dad0e3 BK |
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()); | |
aa1b2f7d | 141 | VERIFY( str05 == str03 ); |
b2dad0e3 BK |
142 | |
143 | str06 = str02; | |
144 | str06.append(str01.begin(), str01.begin() + str01.find('r')); | |
aa1b2f7d BV |
145 | VERIFY( str06 == "corpus, point boliva" ); |
146 | VERIFY( str06 != str01 ); | |
147 | VERIFY( str06 != str02 ); | |
b2dad0e3 BK |
148 | |
149 | str05 = str01; | |
150 | str05.append(str05.begin(), str05.begin() + str05.find('r')); | |
aa1b2f7d BV |
151 | VERIFY( str05 == "point bolivar, texaspoint boliva" ); |
152 | VERIFY( str05 != str01 ); | |
b2dad0e3 BK |
153 | |
154 | #ifdef DEBUG_ASSERT | |
155 | assert(test); | |
156 | #endif | |
157 | return test; | |
158 | } | |
159 | ||
160 | int main() | |
161 | { | |
162 | test01(); | |
04d930e6 | 163 | return 0; |
b2dad0e3 | 164 | } |