]>
Commit | Line | Data |
---|---|---|
b2dad0e3 BK |
1 | // 1999-08-16 bkoz |
2 | ||
3 | // Copyright (C) 2000, 1999 Free Software Foundation | |
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 | // 27.6.2.5.4 basic_ostream character inserters | |
22 | ||
23 | #include <string> | |
24 | #include <ostream> | |
25 | #include <sstream> | |
26 | #include <fstream> | |
aa1b2f7d | 27 | #include <debug_assert.h> |
b2dad0e3 BK |
28 | |
29 | // ofstream | |
30 | bool test01() | |
31 | { | |
32 | bool test = true; | |
33 | std::string str01; | |
34 | const int size = 1000; | |
db353c2c | 35 | const char name_02[] = "ostream_inserter_char-1.txt"; |
b2dad0e3 BK |
36 | |
37 | // initialize string | |
38 | for(int i=0 ; i < size; i++) { | |
39 | str01 += '1'; | |
40 | str01 += '2'; | |
41 | str01 += '3'; | |
42 | str01 += '4'; | |
43 | str01 += '5'; | |
44 | str01 += '6'; | |
45 | str01 += '7'; | |
46 | str01 += '8'; | |
47 | str01 += '9'; | |
48 | str01 += '\n'; | |
49 | } | |
50 | std::ofstream f(name_02); | |
51 | ||
52 | f << str01; | |
53 | f.close(); | |
54 | ||
55 | #ifdef DEBUG_ASSERT | |
56 | assert(test); | |
57 | #endif | |
58 | ||
59 | return test; | |
60 | } | |
61 | ||
62 | // ostringstream width() != zero | |
63 | // left | |
64 | bool test02(void) | |
65 | { | |
66 | bool test = true; | |
67 | std::string tmp; | |
68 | ||
69 | std::string str01 = ""; | |
70 | std::ostringstream oss01; | |
71 | oss01.width(5); | |
72 | oss01.fill('0'); | |
73 | oss01.flags(std::ios_base::left); | |
74 | oss01 << str01; | |
75 | tmp = oss01.str(); | |
aa1b2f7d | 76 | VERIFY( tmp == "00000" ); |
b2dad0e3 BK |
77 | |
78 | std::string str02 = "1"; | |
79 | std::ostringstream oss02; | |
80 | oss02.width(5); | |
81 | oss02.fill('0'); | |
82 | oss02.flags(std::ios_base::left); | |
83 | oss02 << str02; | |
84 | tmp = oss02.str(); | |
aa1b2f7d | 85 | VERIFY( tmp == "10000" ); |
b2dad0e3 BK |
86 | |
87 | std::string str03 = "909909"; | |
88 | std::ostringstream oss03; | |
89 | oss03.width(5); | |
90 | oss03.fill('0'); | |
91 | oss03.flags(std::ios_base::left); | |
92 | oss03 << str03; | |
93 | tmp = oss03.str(); | |
aa1b2f7d | 94 | VERIFY( tmp == "909909" ); |
b2dad0e3 BK |
95 | |
96 | #ifdef DEBUG_ASSERT | |
97 | assert(test); | |
98 | #endif | |
99 | ||
100 | return test; | |
101 | } | |
102 | ||
103 | // width() != zero | |
104 | // right | |
105 | bool test03(void) | |
106 | { | |
107 | bool test = true; | |
108 | std::string tmp; | |
109 | ||
110 | std::string str01 = ""; | |
111 | std::ostringstream oss01; | |
112 | oss01.width(5); | |
113 | oss01.fill('0'); | |
114 | oss01.flags(std::ios_base::right); | |
115 | oss01 << str01; | |
116 | tmp = oss01.str(); | |
aa1b2f7d | 117 | VERIFY( tmp == "00000" ); |
b2dad0e3 BK |
118 | |
119 | std::string str02 = "1"; | |
120 | std::ostringstream oss02; | |
121 | oss02.width(5); | |
122 | oss02.fill('0'); | |
123 | oss02.flags(std::ios_base::right); | |
124 | oss02 << str02; | |
125 | tmp = oss02.str(); | |
aa1b2f7d | 126 | VERIFY( tmp == "00001" ); |
b2dad0e3 BK |
127 | |
128 | std::string str03 = "909909"; | |
129 | std::ostringstream oss03; | |
130 | oss03.width(5); | |
131 | oss03.fill('0'); | |
132 | oss03.flags(std::ios_base::right); | |
133 | oss03 << str03; | |
134 | tmp = oss03.str(); | |
aa1b2f7d | 135 | VERIFY( tmp == "909909" ); |
b2dad0e3 BK |
136 | |
137 | #ifdef DEBUG_ASSERT | |
138 | assert(test); | |
139 | #endif | |
140 | ||
141 | return test; | |
142 | } | |
143 | ||
144 | // stringstream and large strings | |
145 | bool test04() { | |
146 | ||
147 | bool test = true; | |
148 | std::string str_01; | |
149 | const std::string str_02("coltrane playing 'softly as a morning sunrise'"); | |
150 | const std::string str_03("coltrane"); | |
151 | std::string str_tmp; | |
152 | const int i_max=250; | |
153 | ||
154 | std::ostringstream oss_01(std::ios_base::out); | |
155 | std::ostringstream oss_02(str_01, std::ios_base::out); | |
156 | ||
157 | std::ios_base::iostate state1, state2, statefail; | |
158 | statefail = std::ios_base::failbit; | |
159 | ||
160 | // template<_CharT, _Traits> | |
161 | // basic_ostream& operator<<(ostream&, const char*) | |
162 | for (int i = 0; i < i_max; ++i) | |
163 | oss_02 << "Test: " << i << std::endl; | |
164 | str_tmp = oss_02.str(); | |
aa1b2f7d BV |
165 | VERIFY( !oss_02.bad() ); |
166 | VERIFY( oss_02.good() ); | |
167 | VERIFY( str_tmp != str_01 ); | |
168 | VERIFY( str_tmp.size() == 2390 ); | |
b2dad0e3 BK |
169 | |
170 | #ifdef DEBUG_ASSERT | |
171 | assert(test); | |
172 | #endif | |
173 | ||
174 | return test; | |
175 | } | |
176 | ||
177 | // ostringstream and large strings number 2 | |
178 | bool test05() | |
179 | { | |
180 | bool test = true; | |
181 | std::string str05, str10; | |
182 | ||
183 | typedef std::ostream::pos_type pos_type; | |
184 | typedef std::ostream::off_type off_type; | |
185 | std::string str01; | |
186 | const int size = 1000; | |
187 | ||
188 | // initialize string | |
189 | for(int i=0 ; i < size; i++) { | |
190 | str01 += '1'; | |
191 | str01 += '2'; | |
192 | str01 += '3'; | |
193 | str01 += '4'; | |
194 | str01 += '5'; | |
195 | str01 += '6'; | |
196 | str01 += '7'; | |
197 | str01 += '8'; | |
198 | str01 += '9'; | |
199 | str01 += '\n'; | |
200 | } | |
201 | ||
202 | // test 1: out | |
203 | std::ostringstream sstr01(str01, std::ios_base::out); | |
204 | std::ostringstream sstr02; | |
205 | sstr02 << str01; | |
206 | str05 = sstr01.str(); | |
207 | str10 = sstr02.str(); | |
aa1b2f7d BV |
208 | VERIFY( str05 == str01 ); |
209 | VERIFY( str10 == str01 ); | |
b2dad0e3 BK |
210 | |
211 | // test 2: in | out | |
212 | std::ostringstream sstr04(str01, std::ios_base::out | std::ios_base::in); | |
213 | std::ostringstream sstr05(std::ios_base::in | std::ios_base::out); | |
214 | sstr05 << str01; | |
215 | str05 = sstr04.str(); | |
216 | str10 = sstr05.str(); | |
aa1b2f7d BV |
217 | VERIFY( str05 == str01 ); |
218 | VERIFY( str10 == str01 ); | |
b2dad0e3 BK |
219 | |
220 | #ifdef DEBUG_ASSERT | |
221 | assert(test); | |
222 | #endif | |
223 | ||
224 | return test; | |
225 | } | |
226 | ||
227 | ||
228 | // ostringstream and positioning, multiple writes | |
a9ab8db1 | 229 | // http://gcc.gnu.org/ml/libstdc++/2000-q1/msg00326.html |
b2dad0e3 BK |
230 | void test06() |
231 | { | |
232 | bool test = true; | |
233 | const char carray01[] = "mos def & talib kweli are black star"; | |
234 | ||
235 | // normal | |
236 | std::ostringstream ostr1("mos def"); | |
aa1b2f7d | 237 | VERIFY( ostr1.str() == "mos def" ); |
b2dad0e3 | 238 | ostr1 << " & talib kweli"; // should overwrite first part of buffer |
aa1b2f7d | 239 | VERIFY( ostr1.str() == " & talib kweli" ); |
b2dad0e3 | 240 | ostr1 << " are black star"; // should append to string from above |
aa1b2f7d BV |
241 | VERIFY( ostr1.str() != carray01 ); |
242 | VERIFY( ostr1.str() == " & talib kweli are black star" ); | |
b2dad0e3 BK |
243 | |
244 | // appending | |
245 | std::ostringstream ostr2("blackalicious", | |
246 | std::ios_base::out | std::ios_base::ate); | |
aa1b2f7d | 247 | VERIFY( ostr2.str() == "blackalicious" ); |
b2dad0e3 | 248 | ostr2 << " NIA "; // should not overwrite first part of buffer |
aa1b2f7d | 249 | VERIFY( ostr2.str() == "blackalicious NIA " ); |
b2dad0e3 | 250 | ostr2 << "4: deception (5:19)"; // should append to full string from above |
aa1b2f7d | 251 | VERIFY( ostr2.str() == "blackalicious NIA 4: deception (5:19)" ); |
b2dad0e3 BK |
252 | |
253 | #ifdef DEBUG_ASSERT | |
254 | assert(test); | |
255 | #endif | |
256 | } | |
257 | ||
258 | ||
259 | int main() | |
260 | { | |
261 | ||
262 | test02(); | |
263 | test03(); | |
264 | test04(); | |
265 | test05(); | |
266 | test06(); | |
267 | return 0; | |
268 | } |