|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include <string> |
|
17 #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS) |
|
18 # include <fstream> |
|
19 # include <iostream> |
|
20 # include <iomanip> |
|
21 # include <sstream> |
|
22 # include <vector> |
|
23 # include <memory> |
|
24 #include <e32std.h> |
|
25 |
|
26 # include "full_streambuf.h" |
|
27 # include "cppunit/cppunit_proxy.h" |
|
28 |
|
29 # if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) |
|
30 using namespace std; |
|
31 # endif |
|
32 |
|
33 //The macro value gives approximately the generated file |
|
34 //size in Go |
|
35 //#define CHECK_BIG_FILE 4 |
|
36 |
|
37 # if !defined (STLPORT) || !defined (_STLP_NO_CUSTOM_IO) && !defined (_STLP_NO_MEMBER_TEMPLATES) && \ |
|
38 !((defined (_STLP_MSVC) && (_STLP_MSVC < 1300)) || \ |
|
39 (defined (__GNUC__) && (__GNUC__ < 3)) || \ |
|
40 (defined (__SUNPRO_CC)) || \ |
|
41 (defined (__DMC__) && defined (_DLL))) |
|
42 # define DO_CUSTOM_FACET_TEST |
|
43 # endif |
|
44 |
|
45 // |
|
46 // TestCase class |
|
47 // |
|
48 class FstreamTest : public CPPUNIT_NS::TestCase |
|
49 { |
|
50 CPPUNIT_TEST_SUITE(FstreamTest); |
|
51 CPPUNIT_TEST(output); |
|
52 CPPUNIT_TEST(input); |
|
53 CPPUNIT_TEST(input_char); |
|
54 CPPUNIT_TEST(io); |
|
55 CPPUNIT_TEST(err); |
|
56 CPPUNIT_TEST(tellg); |
|
57 CPPUNIT_TEST(buf); |
|
58 CPPUNIT_TEST(rdbuf); |
|
59 #if !defined (STLPORT) || !defined (_STLP_WIN32) |
|
60 CPPUNIT_TEST(offset); |
|
61 #endif |
|
62 # if defined (__DMC__) |
|
63 CPPUNIT_IGNORE; |
|
64 # endif |
|
65 CPPUNIT_TEST(streambuf_output); |
|
66 CPPUNIT_STOP_IGNORE; |
|
67 CPPUNIT_TEST(win32_file_format); |
|
68 # if defined (CHECK_BIG_FILE) |
|
69 CPPUNIT_TEST(big_file); |
|
70 # endif |
|
71 # if !defined (DO_CUSTOM_FACET_TEST) |
|
72 CPPUNIT_IGNORE; |
|
73 #endif |
|
74 CPPUNIT_TEST(custom_facet); |
|
75 CPPUNIT_TEST(fstream_cov1); |
|
76 CPPUNIT_TEST(fstream_cov2); |
|
77 CPPUNIT_TEST(fstream_cov3); |
|
78 CPPUNIT_TEST(fstream_cov4); |
|
79 CPPUNIT_TEST(fstream_cov5); |
|
80 CPPUNIT_TEST_SUITE_END(); |
|
81 |
|
82 protected: |
|
83 void output(); |
|
84 void input(); |
|
85 void input_char(); |
|
86 void io(); |
|
87 void err(); |
|
88 void tellg(); |
|
89 void buf(); |
|
90 void rdbuf(); |
|
91 void streambuf_output(); |
|
92 void win32_file_format(); |
|
93 void custom_facet(); |
|
94 void fstream_cov1(); |
|
95 void fstream_cov2(); |
|
96 void fstream_cov3(); |
|
97 void fstream_cov4(); |
|
98 void fstream_cov5(); |
|
99 # if !defined (STLPORT) || !defined (_STLP_WIN32) |
|
100 void offset(); |
|
101 # endif |
|
102 # if defined (CHECK_BIG_FILE) |
|
103 void big_file(); |
|
104 # endif |
|
105 }; |
|
106 |
|
107 CPPUNIT_TEST_SUITE_REGISTRATION(FstreamTest); |
|
108 |
|
109 // |
|
110 // tests implementation |
|
111 // |
|
112 void FstreamTest::output() |
|
113 { |
|
114 ofstream f( "c:\\private\\test_file.txt" ); |
|
115 |
|
116 f << 1 << '\n' << 2.0 << '\n' << "abcd\n" << "ghk lm\n" << "abcd ef"; |
|
117 CPPUNIT_ASSERT (f.good()); |
|
118 // CPPUNIT_ASSERT( s.str() == "1\n2\nabcd\nghk lm\nabcd ef" ); |
|
119 } |
|
120 |
|
121 void FstreamTest::input() |
|
122 { |
|
123 ifstream f( "c:\\private\\test_file.txt" ); |
|
124 int i = 0; |
|
125 f >> i; |
|
126 CPPUNIT_ASSERT( f.good() ); |
|
127 CPPUNIT_ASSERT( i == 1 ); |
|
128 double d = 0.0; |
|
129 f >> d; |
|
130 CPPUNIT_ASSERT( f.good() ); |
|
131 CPPUNIT_ASSERT( d == 2.0 ); |
|
132 string str; |
|
133 f >> str; |
|
134 CPPUNIT_ASSERT( f.good() ); |
|
135 CPPUNIT_ASSERT( str == "abcd" ); |
|
136 char c; |
|
137 f.get(c); // extract newline, that not extracted by operator >> |
|
138 CPPUNIT_ASSERT( f.good() ); |
|
139 CPPUNIT_ASSERT( c == '\n' ); |
|
140 getline( f, str ); |
|
141 CPPUNIT_ASSERT( f.good() ); |
|
142 CPPUNIT_ASSERT( str == "ghk lm" ); |
|
143 getline( f, str ); |
|
144 CPPUNIT_ASSERT( f.eof() ); |
|
145 CPPUNIT_ASSERT( str == "abcd ef" ); |
|
146 } |
|
147 |
|
148 void FstreamTest::input_char() |
|
149 { |
|
150 char buf[16] = { 0, '1', '2', '3' }; |
|
151 ifstream s( "c:\\private\\test_file.txt" ); |
|
152 s >> buf; |
|
153 |
|
154 CPPUNIT_ASSERT( buf[0] == '1' ); |
|
155 CPPUNIT_ASSERT( buf[1] == 0 ); |
|
156 CPPUNIT_ASSERT( buf[2] == '2' ); |
|
157 } |
|
158 |
|
159 void FstreamTest::io() |
|
160 { |
|
161 basic_fstream<char,char_traits<char> > f( "c:\\private\\test_file.txt", ios_base::in | ios_base::out | ios_base::trunc ); |
|
162 |
|
163 CPPUNIT_ASSERT( f.is_open() ); |
|
164 |
|
165 f << 1 << '\n' << 2.0 << '\n' << "abcd\n" << "ghk lm\n" << "abcd ef"; |
|
166 |
|
167 // f.flush(); |
|
168 f.seekg( 0, ios_base::beg ); |
|
169 |
|
170 int i = 0; |
|
171 f >> i; |
|
172 CPPUNIT_ASSERT( f.good() ); |
|
173 CPPUNIT_ASSERT( i == 1 ); |
|
174 double d = 0.0; |
|
175 f >> d; |
|
176 CPPUNIT_ASSERT( d == 2.0 ); |
|
177 string s; |
|
178 f >> s; |
|
179 CPPUNIT_ASSERT( f.good() ); |
|
180 CPPUNIT_ASSERT( s == "abcd" ); |
|
181 char c; |
|
182 f.get(c); // extract newline, that not extracted by operator >> |
|
183 CPPUNIT_ASSERT( f.good() ); |
|
184 CPPUNIT_ASSERT( c == '\n' ); |
|
185 getline( f, s ); |
|
186 CPPUNIT_ASSERT( f.good() ); |
|
187 CPPUNIT_ASSERT( s == "ghk lm" ); |
|
188 getline( f, s ); |
|
189 CPPUNIT_ASSERT( !f.fail() ); |
|
190 CPPUNIT_ASSERT( s == "abcd ef" ); |
|
191 CPPUNIT_ASSERT( f.eof() ); |
|
192 } |
|
193 |
|
194 void FstreamTest::err() |
|
195 { |
|
196 basic_fstream<char,char_traits<char> > f( "c:\\private\\test_file.txt", ios_base::in | ios_base::out | ios_base::trunc ); |
|
197 |
|
198 CPPUNIT_ASSERT( f.is_open() ); |
|
199 |
|
200 int i = 9; |
|
201 f << i; |
|
202 CPPUNIT_ASSERT( f.good() ); |
|
203 i = 0; |
|
204 f.seekg( 0, ios_base::beg ); |
|
205 f >> i; |
|
206 CPPUNIT_ASSERT( !f.fail() ); |
|
207 CPPUNIT_ASSERT( i == 9 ); |
|
208 f >> i; |
|
209 CPPUNIT_ASSERT( f.fail() ); |
|
210 CPPUNIT_ASSERT( f.eof() ); |
|
211 CPPUNIT_ASSERT( i == 9 ); |
|
212 } |
|
213 |
|
214 void FstreamTest::tellg() |
|
215 { |
|
216 { |
|
217 // bogus ios_base::binary is for Wins |
|
218 ofstream of("c:\\private\\test_file.txt", ios_base::out | ios_base::binary | ios_base::trunc); |
|
219 CPPUNIT_ASSERT( of.is_open() ); |
|
220 |
|
221 for (int i = 0; i < 50; ++i) { |
|
222 of << "line " << setiosflags(ios_base::right) << setfill('0') << setw(2) << i << "\n"; |
|
223 CPPUNIT_ASSERT( !of.fail() ); |
|
224 } |
|
225 of.close(); |
|
226 } |
|
227 |
|
228 { |
|
229 // bogus ios_base::binary is for Wins |
|
230 ifstream is("c:\\private\\test_file.txt", ios_base::in | ios_base::binary); |
|
231 CPPUNIT_ASSERT( is.is_open() ); |
|
232 char buf[64]; |
|
233 |
|
234 // CPPUNIT_ASSERT( is.tellg() == 0 ); |
|
235 streampos p = 0; |
|
236 for (int i = 0; i < 50; ++i) { |
|
237 CPPUNIT_ASSERT( is.tellg() == p ); |
|
238 is.read( buf, 8 ); |
|
239 CPPUNIT_ASSERT( !is.fail() ); |
|
240 p += 8; |
|
241 } |
|
242 } |
|
243 |
|
244 { |
|
245 // bogus ios_base::binary is for Wins |
|
246 ifstream is("c:\\private\\test_file.txt", ios_base::in | ios_base::binary); |
|
247 CPPUNIT_ASSERT( is.is_open() ); |
|
248 |
|
249 streampos p = 0; |
|
250 for (int i = 0; i < 50; ++i) { |
|
251 CPPUNIT_ASSERT( !is.fail() ); |
|
252 is.tellg(); |
|
253 CPPUNIT_ASSERT( is.tellg() == p ); |
|
254 p += 8; |
|
255 is.seekg( p, ios_base::beg ); |
|
256 CPPUNIT_ASSERT( !is.fail() ); |
|
257 } |
|
258 } |
|
259 |
|
260 { |
|
261 // bogus ios_base::binary is for Wins |
|
262 ifstream is("c:\\private\\test_file.txt", ios_base::in | ios_base::binary); |
|
263 CPPUNIT_ASSERT( is.is_open() ); |
|
264 |
|
265 streampos p = 0; |
|
266 for (int i = 0; i < 50; ++i) { |
|
267 CPPUNIT_ASSERT( is.tellg() == p ); |
|
268 p += 8; |
|
269 is.seekg( 8, ios_base::cur ); |
|
270 CPPUNIT_ASSERT( !is.fail() ); |
|
271 } |
|
272 } |
|
273 } |
|
274 |
|
275 void FstreamTest::buf() |
|
276 { |
|
277 fstream ss( "c:\\private\\test_file.txt", ios_base::in | ios_base::out | ios_base::binary | ios_base::trunc ); |
|
278 |
|
279 ss << "1234567\n89\n"; |
|
280 ss.seekg( 0, ios_base::beg ); |
|
281 char buf[10]; |
|
282 buf[7] = 'x'; |
|
283 ss.get( buf, 10 ); |
|
284 CPPUNIT_ASSERT( !ss.fail() ); |
|
285 CPPUNIT_ASSERT( buf[0] == '1' ); |
|
286 CPPUNIT_ASSERT( buf[1] == '2' ); |
|
287 CPPUNIT_ASSERT( buf[2] == '3' ); |
|
288 CPPUNIT_ASSERT( buf[3] == '4' ); |
|
289 CPPUNIT_ASSERT( buf[4] == '5' ); |
|
290 CPPUNIT_ASSERT( buf[5] == '6' ); |
|
291 CPPUNIT_ASSERT( buf[6] == '7' ); // 27.6.1.3 paragraph 10, paragraph 7 |
|
292 CPPUNIT_ASSERT( buf[7] == 0 ); // 27.6.1.3 paragraph 8 |
|
293 char c; |
|
294 ss.get(c); |
|
295 CPPUNIT_ASSERT( !ss.fail() ); |
|
296 CPPUNIT_ASSERT( c == '\n' ); // 27.6.1.3 paragraph 10, paragraph 7 |
|
297 ss.get(c); |
|
298 CPPUNIT_ASSERT( !ss.fail() ); |
|
299 CPPUNIT_ASSERT( c == '8' ); |
|
300 } |
|
301 |
|
302 void FstreamTest::rdbuf() |
|
303 { |
|
304 fstream ss( "c:\\private\\test_file.txt", ios_base::in | ios_base::out | ios_base::binary | ios_base::trunc ); |
|
305 |
|
306 ss << "1234567\n89\n"; |
|
307 ss.seekg( 0, ios_base::beg ); |
|
308 |
|
309 ostringstream os; |
|
310 ss.get( *os.rdbuf(), '\n' ); |
|
311 CPPUNIT_ASSERT( !ss.fail() ); |
|
312 char c; |
|
313 ss.get(c); |
|
314 CPPUNIT_ASSERT( !ss.fail() ); |
|
315 CPPUNIT_ASSERT( c == '\n' ); // 27.6.1.3 paragraph 12 |
|
316 CPPUNIT_ASSERT( os.str() == "1234567" ); |
|
317 } |
|
318 |
|
319 void FstreamTest::streambuf_output() |
|
320 { |
|
321 { |
|
322 ofstream ofstr("c:\\private\\test_file.txt", ios_base::binary); |
|
323 if (!ofstr) |
|
324 //No test if we cannot create the file |
|
325 return; |
|
326 ofstr << "01234567890123456789"; |
|
327 CPPUNIT_ASSERT( ofstr ); |
|
328 } |
|
329 |
|
330 { |
|
331 ifstream in("c:\\private\\test_file.txt", ios_base::binary); |
|
332 CPPUNIT_ASSERT( in ); |
|
333 |
|
334 auto_ptr<full_streambuf> pfull_buf(new full_streambuf(10)); |
|
335 ostream out(pfull_buf.get()); |
|
336 CPPUNIT_ASSERT( out ); |
|
337 |
|
338 out << in.rdbuf(); |
|
339 CPPUNIT_ASSERT( out ); |
|
340 CPPUNIT_ASSERT( in ); |
|
341 CPPUNIT_ASSERT( pfull_buf->str() == "0123456789" ); |
|
342 |
|
343 out << in.rdbuf(); |
|
344 CPPUNIT_ASSERT( out.fail() ); |
|
345 CPPUNIT_ASSERT( in ); |
|
346 |
|
347 ostringstream ostr; |
|
348 ostr << in.rdbuf(); |
|
349 CPPUNIT_ASSERT( ostr ); |
|
350 CPPUNIT_ASSERT( in ); |
|
351 CPPUNIT_ASSERT( ostr.str() == "0123456789" ); |
|
352 } |
|
353 |
|
354 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) |
|
355 { |
|
356 //If the output stream buffer throws: |
|
357 ifstream in("c:\\private\\test_file.txt", ios_base::binary); |
|
358 CPPUNIT_ASSERT( in ); |
|
359 |
|
360 auto_ptr<full_streambuf> pfull_buf(new full_streambuf(10, true)); |
|
361 ostream out(pfull_buf.get()); |
|
362 CPPUNIT_ASSERT( out ); |
|
363 |
|
364 out << in.rdbuf(); |
|
365 CPPUNIT_ASSERT( out.bad() ); |
|
366 CPPUNIT_ASSERT( in ); |
|
367 //out is bad we have no guaranty on what has been extracted: |
|
368 //CPPUNIT_ASSERT( pfull_buf->str() == "0123456789" ); |
|
369 |
|
370 out.clear(); |
|
371 out << in.rdbuf(); |
|
372 CPPUNIT_ASSERT( out.fail() && out.bad() ); |
|
373 CPPUNIT_ASSERT( in ); |
|
374 |
|
375 ostringstream ostr; |
|
376 ostr << in.rdbuf(); |
|
377 CPPUNIT_ASSERT( ostr ); |
|
378 CPPUNIT_ASSERT( in ); |
|
379 CPPUNIT_ASSERT( ostr.str() == "0123456789" ); |
|
380 } |
|
381 # endif |
|
382 } |
|
383 |
|
384 void FstreamTest::win32_file_format() |
|
385 { |
|
386 const char* file_name = "c:\\private\\win32_file_format.tmp"; |
|
387 const size_t nb_lines = 2049; |
|
388 { |
|
389 ofstream out(file_name); |
|
390 CPPUNIT_ASSERT( out.good() ); |
|
391 out << 'a'; |
|
392 for (size_t i = 0; i < nb_lines - 1; ++i) { |
|
393 out << '\n'; |
|
394 } |
|
395 out << '\r'; |
|
396 CPPUNIT_ASSERT( out.good() ); |
|
397 } |
|
398 { |
|
399 ifstream in(file_name); |
|
400 CPPUNIT_ASSERT( in.good() ); |
|
401 string line, last_line; |
|
402 size_t nb_read_lines = 0; |
|
403 while (getline(in, line)) { |
|
404 ++nb_read_lines; |
|
405 last_line = line; |
|
406 } |
|
407 CPPUNIT_ASSERT( in.eof() ); |
|
408 CPPUNIT_ASSERT( nb_read_lines == nb_lines ); |
|
409 CPPUNIT_ASSERT( !last_line.empty() && (last_line[0] == '\r') ); |
|
410 } |
|
411 } |
|
412 |
|
413 #if defined (DO_CUSTOM_FACET_TEST) |
|
414 struct my_state { |
|
415 char dummy; |
|
416 }; |
|
417 |
|
418 struct my_traits : public char_traits<char> { |
|
419 typedef my_state state_type; |
|
420 typedef fpos<state_type> pos_type; |
|
421 }; |
|
422 |
|
423 class my_codecvt |
|
424 # if defined (STLPORT) |
|
425 : public codecvt<char, char, my_state> { |
|
426 |
|
427 # else |
|
428 : public locale::facet, public codecvt_base { |
|
429 //STLport grant the same default implementation, other Standard libs implementation |
|
430 //do not necessarily do the same: |
|
431 public: |
|
432 typedef char intern_type; |
|
433 typedef char extern_type; |
|
434 typedef my_state state_type; |
|
435 |
|
436 explicit my_codecvt(size_t __refs = 0) : locale::facet(__refs) {} |
|
437 result out(state_type&, |
|
438 const intern_type* __from, |
|
439 const intern_type*, |
|
440 const intern_type*& __from_next, |
|
441 extern_type* __to, |
|
442 extern_type*, |
|
443 extern_type*& __to_next) const |
|
444 { __from_next = __from; __to_next = __to; return noconv; } |
|
445 |
|
446 result in (state_type&, |
|
447 const extern_type* __from, |
|
448 const extern_type*, |
|
449 const extern_type*& __from_next, |
|
450 intern_type* __to, |
|
451 intern_type*, |
|
452 intern_type*& __to_next) const |
|
453 { __from_next = __from; __to_next = __to; return noconv; } |
|
454 |
|
455 result unshift(state_type&, |
|
456 extern_type* __to, |
|
457 extern_type*, |
|
458 extern_type*& __to_next) const |
|
459 { __to_next = __to; return noconv; } |
|
460 |
|
461 int encoding() const throw() |
|
462 { return 1; } |
|
463 |
|
464 bool always_noconv() const throw() |
|
465 { return true; } |
|
466 |
|
467 int length(const state_type&, |
|
468 const extern_type* __from, |
|
469 const extern_type* __end, |
|
470 size_t __max) const |
|
471 { return (int)min(static_cast<size_t>(__end - __from), __max); } |
|
472 |
|
473 int max_length() const throw() |
|
474 { return 1; } |
|
475 |
|
476 static locale::id id; |
|
477 # endif |
|
478 |
|
479 }; |
|
480 |
|
481 # if !defined (STLPORT) |
|
482 locale::id my_codecvt::id; |
|
483 # else |
|
484 # if defined (__BORLANDC__) |
|
485 template <> |
|
486 locale::id codecvt<char, char, my_state>::id; |
|
487 # endif |
|
488 # endif |
|
489 #endif |
|
490 |
|
491 #if defined (__SYMBIAN32__WSD__) |
|
492 locale::id& codecvt<char, char, my_state>::GetFacetLocaleId() |
|
493 { |
|
494 static locale::id aId = {41}; |
|
495 return aId; |
|
496 } |
|
497 #endif |
|
498 |
|
499 void FstreamTest::custom_facet() |
|
500 { |
|
501 #if defined (DO_CUSTOM_FACET_TEST) && !defined (__SYMBIAN32__) |
|
502 |
|
503 const char* fileName = "c:\\private\\test_file.txt"; |
|
504 //File preparation: |
|
505 { |
|
506 ofstream ofstr(fileName, ios_base::binary); |
|
507 ofstr << "0123456789"; |
|
508 CPPUNIT_ASSERT( ofstr ); |
|
509 } |
|
510 |
|
511 { |
|
512 typedef basic_ifstream<char, my_traits> my_ifstream; |
|
513 typedef basic_string<char, my_traits> my_string; |
|
514 |
|
515 my_ifstream ifstr(fileName); |
|
516 CPPUNIT_ASSERT( ifstr ); |
|
517 |
|
518 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) |
|
519 ifstr.imbue(locale::classic()); |
|
520 CPPUNIT_ASSERT( ifstr.fail() && !ifstr.bad() ); |
|
521 ifstr.clear(); |
|
522 # endif |
|
523 locale my_loc(locale::classic(), new my_codecvt()); |
|
524 ifstr.imbue(my_loc); |
|
525 CPPUNIT_ASSERT( ifstr.good() ); |
|
526 /* |
|
527 my_string res; |
|
528 ifstr >> res; |
|
529 CPPUNIT_ASSERT( !ifstr.fail() ); |
|
530 CPPUNIT_ASSERT( !ifstr.bad() ); |
|
531 CPPUNIT_ASSERT( ifstr.eof() ); |
|
532 CPPUNIT_ASSERT( res == "0123456789" ); |
|
533 */ |
|
534 } |
|
535 #endif |
|
536 } |
|
537 |
|
538 # if defined (CHECK_BIG_FILE) |
|
539 void FstreamTest::big_file() |
|
540 { |
|
541 vector<pair<streamsize, streamoff> > file_pos; |
|
542 |
|
543 //Big file creation: |
|
544 { |
|
545 ofstream out("big_file.txt"); |
|
546 CPPUNIT_ASSERT( out ); |
|
547 |
|
548 //We are going to generate a file with the following schema for the content: |
|
549 //0(1019 times)0000 //1023 characters + 1 charater for \n (for some platforms it will be a 1 ko line) |
|
550 //0(1019 times)0001 |
|
551 //... |
|
552 //0(1019 times)1234 |
|
553 //... |
|
554 |
|
555 //Generation of the number of loop: |
|
556 streamoff nb = 1; |
|
557 for (int i = 0; i < 20; ++i) { |
|
558 //This assertion check that the streamoff can at least represent the necessary integers values |
|
559 //for this test: |
|
560 CPPUNIT_ASSERT( (nb << 1) > nb ); |
|
561 nb <<= 1; |
|
562 } |
|
563 CPPUNIT_ASSERT( nb * CHECK_BIG_FILE >= nb ); |
|
564 nb *= CHECK_BIG_FILE; |
|
565 |
|
566 //Preparation of the ouput stream state: |
|
567 out << setiosflags(ios_base::right) << setfill('*'); |
|
568 for (streamoff index = 0; index < nb; ++index) { |
|
569 if (index % 1024 == 0) { |
|
570 file_pos.push_back(make_pair(out.tellp(), index)); |
|
571 CPPUNIT_ASSERT( file_pos.back().first != streamsize(-1) ); |
|
572 if (file_pos.size() > 1) { |
|
573 CPPUNIT_ASSERT( file_pos[file_pos.size() - 1].first > file_pos[file_pos.size() - 2].first ); |
|
574 } |
|
575 } |
|
576 out << setw(1023) << index << '\n'; |
|
577 } |
|
578 } |
|
579 |
|
580 { |
|
581 ifstream in("big_file.txt"); |
|
582 CPPUNIT_ASSERT( in ); |
|
583 |
|
584 string line; |
|
585 vector<pair<streamsize, streamsize> >::const_iterator pit(file_pos.begin()), |
|
586 pitEnd(file_pos.end()); |
|
587 for (; pit != pitEnd; ++pit) { |
|
588 in.seekg((*pit).first); |
|
589 CPPUNIT_ASSERT( in ); |
|
590 in >> line; |
|
591 size_t lastStarPos = line.rfind('*'); |
|
592 CPPUNIT_ASSERT( atoi(line.substr(lastStarPos + 1).c_str()) == (*pit).second ); |
|
593 } |
|
594 } |
|
595 |
|
596 /* |
|
597 The following test has been used to check that STLport do not generate |
|
598 an infinite loop when the file size is larger than the streamsize and |
|
599 streamoff representation (32 bits or 64 bits). |
|
600 { |
|
601 ifstream in("big_file.txt"); |
|
602 CPPUNIT_ASSERT( in ); |
|
603 char tmp[4096]; |
|
604 streamsize nb_reads = 0; |
|
605 while ((!in.eof()) && in.good()){ |
|
606 in.read(tmp, 4096); |
|
607 nb_reads += in.gcount(); |
|
608 } |
|
609 } |
|
610 */ |
|
611 } |
|
612 # endif |
|
613 |
|
614 # if !defined (STLPORT) || !defined (_STLP_WIN32) |
|
615 void FstreamTest::offset() |
|
616 { |
|
617 # if (defined(_LARGEFILE_SOURCE) || defined(_LARGEFILE64_SOURCE)) && !defined(_STLP_USE_DEFAULT_FILE_OFFSET) |
|
618 CPPUNIT_CHECK( sizeof(streamoff) == 8 ); |
|
619 # else |
|
620 CPPUNIT_CHECK( sizeof(streamoff) == sizeof(off_t) ); |
|
621 # endif |
|
622 } |
|
623 # endif |
|
624 |
|
625 #endif |
|
626 void FstreamTest::fstream_cov1() |
|
627 { |
|
628 __UHEAP_MARK; |
|
629 { |
|
630 ofstream ofs; |
|
631 int x = 0; |
|
632 char buf[12]; |
|
633 if (!ofs.bad()) |
|
634 { |
|
635 ofs.open("c:\\test_cpp.txt",ios::out); |
|
636 ofs << "example"; |
|
637 x = 1; |
|
638 ofs.close(); |
|
639 } |
|
640 CPPUNIT_ASSERT( x == 1 ); |
|
641 |
|
642 ifstream ifs; |
|
643 x = 0; |
|
644 if (!ifs.bad()) |
|
645 { |
|
646 ifs.open("c:\\test_cpp.txt",ios::in); |
|
647 ifs >> buf; |
|
648 x = 1; |
|
649 ifs.close(); |
|
650 } |
|
651 CPPUNIT_ASSERT( x == 1 ); |
|
652 CPPUNIT_ASSERT( !strcmp(buf,"example")); |
|
653 } |
|
654 { |
|
655 fstream fs; |
|
656 int x = 0; |
|
657 |
|
658 if (!fs.bad()) |
|
659 { |
|
660 fs.open("c:\\test_cpp1.txt",ios::out); |
|
661 fs << "example"; |
|
662 x = 1; |
|
663 fs.close(); |
|
664 } |
|
665 CPPUNIT_ASSERT( x == 1 ); |
|
666 } |
|
667 __UHEAP_MARKEND; |
|
668 } |
|
669 void FstreamTest::fstream_cov2() |
|
670 { |
|
671 __UHEAP_MARK; |
|
672 {/* |
|
673 ofstream ofs; |
|
674 int x = 0; |
|
675 char buf[12]; |
|
676 if (!ofs.bad()) |
|
677 { |
|
678 ofs.open("c:\\test_cpp.txt",ios::out,(long)0666); |
|
679 ofs << "example"; |
|
680 x = 1; |
|
681 ofs.close(); |
|
682 } |
|
683 CPPUNIT_ASSERT( x == 1 ); |
|
684 |
|
685 ifstream ifs; |
|
686 x = 0; |
|
687 if (!ifs.bad()) |
|
688 { |
|
689 ifs.open("c:\\test_cpp.txt",ios::in,0666); |
|
690 ifs >> buf; |
|
691 x = 1; |
|
692 ifs.close(); |
|
693 } |
|
694 CPPUNIT_ASSERT( x == 1 ); |
|
695 CPPUNIT_ASSERT( !strcmp(buf,"example"));*/ |
|
696 } |
|
697 {/* |
|
698 fstream fs; |
|
699 int x = 0; |
|
700 char buf[12]; |
|
701 if (!fs.bad()) |
|
702 { |
|
703 fs.open("c:\\test_cpp.txt",ios::in | ios::out,(long)0666); |
|
704 fs << "example"; |
|
705 fs >> buf; |
|
706 x = 1; |
|
707 fs.close(); |
|
708 } |
|
709 CPPUNIT_ASSERT( x == 1 ); |
|
710 CPPUNIT_ASSERT( !strcmp(buf,"example"));*/ |
|
711 } |
|
712 __UHEAP_MARKEND; |
|
713 } |
|
714 void FstreamTest::fstream_cov3() |
|
715 { |
|
716 __UHEAP_MARK; |
|
717 { |
|
718 long pos; |
|
719 char buf[20]; |
|
720 ofstream outfile; |
|
721 outfile.open ("c:\\test_cpp12.txt"); |
|
722 outfile.write ("Thisisanapple",14); |
|
723 pos=outfile.tellp(); |
|
724 outfile.seekp (pos-7); |
|
725 outfile.write ("sam",3); |
|
726 outfile.flush(); |
|
727 outfile.close(); |
|
728 |
|
729 ifstream ifs; |
|
730 if (!ifs.bad()) |
|
731 { |
|
732 ifs.open("c:\\test_cpp12.txt",ios::in); |
|
733 ifs >> buf; |
|
734 ifs.close(); |
|
735 } |
|
736 |
|
737 CPPUNIT_ASSERT( !strcmp(buf,"Thisisasample")); |
|
738 } |
|
739 { |
|
740 long pos; |
|
741 ofstream outfile; |
|
742 outfile.open ("c:\\test_cpp12.txt"); |
|
743 outfile.write ("Thisisanapple",14); |
|
744 pos=outfile.tellp(); |
|
745 // seekp beyond the file |
|
746 outfile.seekp (pos - (pos + 1) ); |
|
747 CPPUNIT_ASSERT( ios::failbit ); |
|
748 outfile.flush(); |
|
749 outfile.close(); |
|
750 } |
|
751 { |
|
752 ofstream ofs; |
|
753 int x = 0; |
|
754 if (!ofs.bad()) |
|
755 { |
|
756 ofs.open("c:\\test_cpp.txt",ios::out); |
|
757 ofs << "testing"; |
|
758 x = 1; |
|
759 ofs.close(); |
|
760 } |
|
761 CPPUNIT_ASSERT( x == 1 ); |
|
762 ifstream myfile( "c:\\test_cpp.txt", ios::in ); |
|
763 |
|
764 myfile.rdbuf( )->stossc( ); |
|
765 char i = myfile.rdbuf( )->sgetc( ); |
|
766 CPPUNIT_ASSERT( i == 'e' ); |
|
767 } |
|
768 { |
|
769 ofstream ofs; |
|
770 int x = 0; |
|
771 if (!ofs.bad()) |
|
772 { |
|
773 ofs.open("c:\\test_cpp.txt",ios::out); |
|
774 ofs << "testing"; |
|
775 x = 1; |
|
776 ofs.close(); |
|
777 } |
|
778 CPPUNIT_ASSERT( x == 1 ); |
|
779 ifstream myfile( "c:\\test_cpp.txt", ios::in ); |
|
780 |
|
781 int i; |
|
782 i = myfile.rdbuf( )->sbumpc( ); |
|
783 CPPUNIT_ASSERT( (char)i == 't' ); |
|
784 i = myfile.rdbuf( )->sbumpc( ); |
|
785 CPPUNIT_ASSERT( (char)i == 'e' ); |
|
786 i = myfile.rdbuf( )->sungetc( ); |
|
787 CPPUNIT_ASSERT( (char)i == 'e' ); |
|
788 i = myfile.rdbuf( )->sungetc( ); |
|
789 CPPUNIT_ASSERT( (char)i == 't' ); |
|
790 i = myfile.rdbuf( )->sbumpc( ); |
|
791 CPPUNIT_ASSERT( (char)i == 't' ); |
|
792 i = myfile.rdbuf( )->sbumpc( ); |
|
793 i = myfile.rdbuf( )->sbumpc( ); |
|
794 i = myfile.rdbuf( )->sbumpc( ); |
|
795 i = myfile.rdbuf( )->sbumpc( ); |
|
796 i = myfile.rdbuf( )->sbumpc( ); |
|
797 i = myfile.rdbuf( )->sbumpc( ); |
|
798 i = myfile.rdbuf( )->sbumpc( ); |
|
799 i = myfile.rdbuf( )->sbumpc( ); |
|
800 //CPPUNIT_ASSERT(myfile.eof()); |
|
801 } |
|
802 __UHEAP_MARKEND; |
|
803 } |
|
804 void FstreamTest::fstream_cov4() |
|
805 { |
|
806 __UHEAP_MARK; |
|
807 { |
|
808 ofstream ofs; |
|
809 int x = 0; |
|
810 if (!ofs.bad()) |
|
811 { |
|
812 ofs.open("c:\\test_cpp.txt",ios::out); |
|
813 ofs << "testing"; |
|
814 x = 1; |
|
815 ofs.close(); |
|
816 } |
|
817 CPPUNIT_ASSERT( x == 1 ); |
|
818 ifstream myfile( "c:\\test_cpp.txt", ios::in ); |
|
819 |
|
820 int i; |
|
821 i = myfile.rdbuf( )->snextc( ); |
|
822 CPPUNIT_ASSERT( (char)i == 'e' ); |
|
823 } |
|
824 { |
|
825 ofstream ofs; |
|
826 int x = 0; |
|
827 char c[10]; |
|
828 if (!ofs.bad()) |
|
829 { |
|
830 ofs.open("c:\\test_cpp.txt",ios::out); |
|
831 ofs << "testing"; |
|
832 x = 1; |
|
833 ofs.close(); |
|
834 } |
|
835 CPPUNIT_ASSERT( x == 1 ); |
|
836 ifstream myfile( "c:\\test_cpp.txt", ios::in ); |
|
837 |
|
838 int i; |
|
839 i = myfile.rdbuf( )->in_avail( ); |
|
840 CPPUNIT_ASSERT( i == 7 ); |
|
841 myfile.readsome(&c[0],5); |
|
842 c[5]='\0'; |
|
843 CPPUNIT_ASSERT( !strcmp(c,"testi") ); |
|
844 } |
|
845 { |
|
846 ofstream ofs; |
|
847 char c; |
|
848 if (!ofs.bad()) |
|
849 { |
|
850 ofs.open("c:\\test_cpp.txt",ios::out); |
|
851 ofs << "9876543210"; |
|
852 ofs.close(); |
|
853 } |
|
854 int pos1; |
|
855 ifstream file; |
|
856 |
|
857 file.open( "c:\\test_cpp.txt" ); |
|
858 if (!file.good()) |
|
859 CPPUNIT_ASSERT( 0 ); |
|
860 file.seekg( 0 ); // Goes to a zero-based position in the file |
|
861 pos1 = file.tellg( ); |
|
862 CPPUNIT_ASSERT( pos1 == 0 ); |
|
863 file.get( c); |
|
864 CPPUNIT_ASSERT( c == '9' ); |
|
865 CPPUNIT_ASSERT( (int)file.tellg( ) == 1 ); |
|
866 pos1 += 1; |
|
867 file.get( c ); |
|
868 CPPUNIT_ASSERT( c == '8' ); |
|
869 CPPUNIT_ASSERT( (int)file.tellg( ) == 2 ); |
|
870 pos1 -= 1; |
|
871 file.seekg( pos1 ); |
|
872 file.get( c ); |
|
873 CPPUNIT_ASSERT( c == '9' ); |
|
874 CPPUNIT_ASSERT( (int)file.tellg( ) == 1 ); |
|
875 file.seekg( pos1+2 ); |
|
876 file.get( c ); |
|
877 CPPUNIT_ASSERT( c == '7' ); |
|
878 CPPUNIT_ASSERT( (int)file.tellg( ) == 3 ); |
|
879 } |
|
880 __UHEAP_MARKEND; |
|
881 } |
|
882 void FstreamTest::fstream_cov5() |
|
883 { |
|
884 __UHEAP_MARK; |
|
885 { |
|
886 ofstream ofs; |
|
887 int x = 0; |
|
888 char c; |
|
889 if (!ofs.bad()) |
|
890 { |
|
891 ofs.open("c:\\test_cpp.txt",ios::out); |
|
892 ofs << "0123456789"; |
|
893 x = 1; |
|
894 ofs.close(); |
|
895 } |
|
896 CPPUNIT_ASSERT( x == 1 ); |
|
897 ifstream file; |
|
898 file.open( "c:\\test_cpp.txt" ); |
|
899 file.seekg(2); |
|
900 file >> c; |
|
901 CPPUNIT_ASSERT( c == '2' ); |
|
902 file.seekg( 0,ios_base::beg); |
|
903 file >> c; |
|
904 CPPUNIT_ASSERT( c == '0' ); |
|
905 file.seekg( -1, ios_base::end ); |
|
906 file >> c; |
|
907 CPPUNIT_ASSERT( c == '9' ); |
|
908 } |
|
909 { |
|
910 ofstream ofs; |
|
911 int x = 0; |
|
912 |
|
913 if (!ofs.bad()) |
|
914 { |
|
915 ofs.open("c:\\test_cpp.txt",ios::out); |
|
916 ofs << "example"; |
|
917 x = 1; |
|
918 ofs.close(); |
|
919 } |
|
920 CPPUNIT_ASSERT( x == 1 ); |
|
921 |
|
922 ifstream ifs; |
|
923 x = 0; |
|
924 if (!ifs.bad()) |
|
925 { |
|
926 ifs.open("c:\\test_cpp.txt"); |
|
927 ostringstream os; |
|
928 ifs.get( *os.rdbuf()); |
|
929 x = 1; |
|
930 ifs.close(); |
|
931 } |
|
932 CPPUNIT_ASSERT( x == 1 ); |
|
933 } |
|
934 { |
|
935 ofstream ofs; |
|
936 int x = 0; |
|
937 |
|
938 if (!ofs.bad()) |
|
939 { |
|
940 ofs.open("c:\\test_cpp.txt",ios::out); |
|
941 ofs << "testing"; |
|
942 x = 1; |
|
943 ofs.close(); |
|
944 } |
|
945 CPPUNIT_ASSERT( x == 1 ); |
|
946 |
|
947 ifstream myfile("c:\\test_cpp.txt", ios::in); |
|
948 char a[15]; |
|
949 |
|
950 streamsize i = myfile.rdbuf()->sgetn(a, 3); |
|
951 CPPUNIT_ASSERT( i == 3 ); |
|
952 a[i] = myfile.widen('\0'); |
|
953 CPPUNIT_ASSERT( !strcmp(a,"tes") ); |
|
954 } |
|
955 { |
|
956 ofstream ofs; |
|
957 int x = 0; |
|
958 char buf[20]; |
|
959 if (!ofs.bad()) |
|
960 { |
|
961 ofs.open("c:\\test_cpp.txt",ios::out); |
|
962 ofs << "stdndardcpp"; |
|
963 x = 1; |
|
964 ofs.seekp(2); |
|
965 ofs << "a"; |
|
966 ofs.seekp(0,ios::end); |
|
967 ofs << "onpips"; |
|
968 ofs.close(); |
|
969 } |
|
970 CPPUNIT_ASSERT( x == 1 ); |
|
971 ifstream ifs; |
|
972 if (!ifs.bad()) |
|
973 { |
|
974 ifs.open("c:\\test_cpp.txt",ios::in); |
|
975 ifs >> buf; |
|
976 ifs.close(); |
|
977 } |
|
978 |
|
979 CPPUNIT_ASSERT( !strcmp(buf,"standardcpponpips")); |
|
980 } |
|
981 __UHEAP_MARKEND; |
|
982 } |