Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
17 #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
26 # include "full_streambuf.h"
27 # include "cppunit/cppunit_proxy.h"
29 # if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
33 //The macro value gives approximately the generated file
35 //#define CHECK_BIG_FILE 4
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
48 class FstreamTest : public CPPUNIT_NS::TestCase
50 CPPUNIT_TEST_SUITE(FstreamTest);
53 CPPUNIT_TEST(input_char);
59 #if !defined (STLPORT) || !defined (_STLP_WIN32)
62 # if defined (__DMC__)
65 CPPUNIT_TEST(streambuf_output);
67 CPPUNIT_TEST(win32_file_format);
68 # if defined (CHECK_BIG_FILE)
69 CPPUNIT_TEST(big_file);
71 # if !defined (DO_CUSTOM_FACET_TEST)
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();
91 void streambuf_output();
92 void win32_file_format();
99 # if !defined (STLPORT) || !defined (_STLP_WIN32)
102 # if defined (CHECK_BIG_FILE)
107 CPPUNIT_TEST_SUITE_REGISTRATION(FstreamTest);
110 // tests implementation
112 void FstreamTest::output()
114 ofstream f( "c:\\private\\test_file.txt" );
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" );
121 void FstreamTest::input()
123 ifstream f( "c:\\private\\test_file.txt" );
126 CPPUNIT_ASSERT( f.good() );
127 CPPUNIT_ASSERT( i == 1 );
130 CPPUNIT_ASSERT( f.good() );
131 CPPUNIT_ASSERT( d == 2.0 );
134 CPPUNIT_ASSERT( f.good() );
135 CPPUNIT_ASSERT( str == "abcd" );
137 f.get(c); // extract newline, that not extracted by operator >>
138 CPPUNIT_ASSERT( f.good() );
139 CPPUNIT_ASSERT( c == '\n' );
141 CPPUNIT_ASSERT( f.good() );
142 CPPUNIT_ASSERT( str == "ghk lm" );
144 CPPUNIT_ASSERT( f.eof() );
145 CPPUNIT_ASSERT( str == "abcd ef" );
148 void FstreamTest::input_char()
150 char buf[16] = { 0, '1', '2', '3' };
151 ifstream s( "c:\\private\\test_file.txt" );
154 CPPUNIT_ASSERT( buf[0] == '1' );
155 CPPUNIT_ASSERT( buf[1] == 0 );
156 CPPUNIT_ASSERT( buf[2] == '2' );
159 void FstreamTest::io()
161 basic_fstream<char,char_traits<char> > f( "c:\\private\\test_file.txt", ios_base::in | ios_base::out | ios_base::trunc );
163 CPPUNIT_ASSERT( f.is_open() );
165 f << 1 << '\n' << 2.0 << '\n' << "abcd\n" << "ghk lm\n" << "abcd ef";
168 f.seekg( 0, ios_base::beg );
172 CPPUNIT_ASSERT( f.good() );
173 CPPUNIT_ASSERT( i == 1 );
176 CPPUNIT_ASSERT( d == 2.0 );
179 CPPUNIT_ASSERT( f.good() );
180 CPPUNIT_ASSERT( s == "abcd" );
182 f.get(c); // extract newline, that not extracted by operator >>
183 CPPUNIT_ASSERT( f.good() );
184 CPPUNIT_ASSERT( c == '\n' );
186 CPPUNIT_ASSERT( f.good() );
187 CPPUNIT_ASSERT( s == "ghk lm" );
189 CPPUNIT_ASSERT( !f.fail() );
190 CPPUNIT_ASSERT( s == "abcd ef" );
191 CPPUNIT_ASSERT( f.eof() );
194 void FstreamTest::err()
196 basic_fstream<char,char_traits<char> > f( "c:\\private\\test_file.txt", ios_base::in | ios_base::out | ios_base::trunc );
198 CPPUNIT_ASSERT( f.is_open() );
202 CPPUNIT_ASSERT( f.good() );
204 f.seekg( 0, ios_base::beg );
206 CPPUNIT_ASSERT( !f.fail() );
207 CPPUNIT_ASSERT( i == 9 );
209 CPPUNIT_ASSERT( f.fail() );
210 CPPUNIT_ASSERT( f.eof() );
211 CPPUNIT_ASSERT( i == 9 );
214 void FstreamTest::tellg()
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() );
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() );
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() );
234 // CPPUNIT_ASSERT( is.tellg() == 0 );
236 for (int i = 0; i < 50; ++i) {
237 CPPUNIT_ASSERT( is.tellg() == p );
239 CPPUNIT_ASSERT( !is.fail() );
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() );
250 for (int i = 0; i < 50; ++i) {
251 CPPUNIT_ASSERT( !is.fail() );
253 CPPUNIT_ASSERT( is.tellg() == p );
255 is.seekg( p, ios_base::beg );
256 CPPUNIT_ASSERT( !is.fail() );
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() );
266 for (int i = 0; i < 50; ++i) {
267 CPPUNIT_ASSERT( is.tellg() == p );
269 is.seekg( 8, ios_base::cur );
270 CPPUNIT_ASSERT( !is.fail() );
275 void FstreamTest::buf()
277 fstream ss( "c:\\private\\test_file.txt", ios_base::in | ios_base::out | ios_base::binary | ios_base::trunc );
279 ss << "1234567\n89\n";
280 ss.seekg( 0, ios_base::beg );
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
295 CPPUNIT_ASSERT( !ss.fail() );
296 CPPUNIT_ASSERT( c == '\n' ); // 27.6.1.3 paragraph 10, paragraph 7
298 CPPUNIT_ASSERT( !ss.fail() );
299 CPPUNIT_ASSERT( c == '8' );
302 void FstreamTest::rdbuf()
304 fstream ss( "c:\\private\\test_file.txt", ios_base::in | ios_base::out | ios_base::binary | ios_base::trunc );
306 ss << "1234567\n89\n";
307 ss.seekg( 0, ios_base::beg );
310 ss.get( *os.rdbuf(), '\n' );
311 CPPUNIT_ASSERT( !ss.fail() );
314 CPPUNIT_ASSERT( !ss.fail() );
315 CPPUNIT_ASSERT( c == '\n' ); // 27.6.1.3 paragraph 12
316 CPPUNIT_ASSERT( os.str() == "1234567" );
319 void FstreamTest::streambuf_output()
322 ofstream ofstr("c:\\private\\test_file.txt", ios_base::binary);
324 //No test if we cannot create the file
326 ofstr << "01234567890123456789";
327 CPPUNIT_ASSERT( ofstr );
331 ifstream in("c:\\private\\test_file.txt", ios_base::binary);
332 CPPUNIT_ASSERT( in );
334 auto_ptr<full_streambuf> pfull_buf(new full_streambuf(10));
335 ostream out(pfull_buf.get());
336 CPPUNIT_ASSERT( out );
339 CPPUNIT_ASSERT( out );
340 CPPUNIT_ASSERT( in );
341 CPPUNIT_ASSERT( pfull_buf->str() == "0123456789" );
344 CPPUNIT_ASSERT( out.fail() );
345 CPPUNIT_ASSERT( in );
349 CPPUNIT_ASSERT( ostr );
350 CPPUNIT_ASSERT( in );
351 CPPUNIT_ASSERT( ostr.str() == "0123456789" );
354 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
356 //If the output stream buffer throws:
357 ifstream in("c:\\private\\test_file.txt", ios_base::binary);
358 CPPUNIT_ASSERT( in );
360 auto_ptr<full_streambuf> pfull_buf(new full_streambuf(10, true));
361 ostream out(pfull_buf.get());
362 CPPUNIT_ASSERT( out );
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" );
372 CPPUNIT_ASSERT( out.fail() && out.bad() );
373 CPPUNIT_ASSERT( in );
377 CPPUNIT_ASSERT( ostr );
378 CPPUNIT_ASSERT( in );
379 CPPUNIT_ASSERT( ostr.str() == "0123456789" );
384 void FstreamTest::win32_file_format()
386 const char* file_name = "c:\\private\\win32_file_format.tmp";
387 const size_t nb_lines = 2049;
389 ofstream out(file_name);
390 CPPUNIT_ASSERT( out.good() );
392 for (size_t i = 0; i < nb_lines - 1; ++i) {
396 CPPUNIT_ASSERT( out.good() );
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)) {
407 CPPUNIT_ASSERT( in.eof() );
408 CPPUNIT_ASSERT( nb_read_lines == nb_lines );
409 CPPUNIT_ASSERT( !last_line.empty() && (last_line[0] == '\r') );
413 #if defined (DO_CUSTOM_FACET_TEST)
418 struct my_traits : public char_traits<char> {
419 typedef my_state state_type;
420 typedef fpos<state_type> pos_type;
424 # if defined (STLPORT)
425 : public codecvt<char, char, my_state> {
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:
432 typedef char intern_type;
433 typedef char extern_type;
434 typedef my_state state_type;
436 explicit my_codecvt(size_t __refs = 0) : locale::facet(__refs) {}
437 result out(state_type&,
438 const intern_type* __from,
440 const intern_type*& __from_next,
443 extern_type*& __to_next) const
444 { __from_next = __from; __to_next = __to; return noconv; }
446 result in (state_type&,
447 const extern_type* __from,
449 const extern_type*& __from_next,
452 intern_type*& __to_next) const
453 { __from_next = __from; __to_next = __to; return noconv; }
455 result unshift(state_type&,
458 extern_type*& __to_next) const
459 { __to_next = __to; return noconv; }
461 int encoding() const throw()
464 bool always_noconv() const throw()
467 int length(const state_type&,
468 const extern_type* __from,
469 const extern_type* __end,
471 { return (int)min(static_cast<size_t>(__end - __from), __max); }
473 int max_length() const throw()
476 static locale::id id;
481 # if !defined (STLPORT)
482 locale::id my_codecvt::id;
484 # if defined (__BORLANDC__)
486 locale::id codecvt<char, char, my_state>::id;
491 #if defined (__SYMBIAN32__WSD__)
492 locale::id& codecvt<char, char, my_state>::GetFacetLocaleId()
494 static locale::id aId = {41};
499 void FstreamTest::custom_facet()
501 #if defined (DO_CUSTOM_FACET_TEST) && !defined (__SYMBIAN32__)
503 const char* fileName = "c:\\private\\test_file.txt";
506 ofstream ofstr(fileName, ios_base::binary);
507 ofstr << "0123456789";
508 CPPUNIT_ASSERT( ofstr );
512 typedef basic_ifstream<char, my_traits> my_ifstream;
513 typedef basic_string<char, my_traits> my_string;
515 my_ifstream ifstr(fileName);
516 CPPUNIT_ASSERT( ifstr );
518 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
519 ifstr.imbue(locale::classic());
520 CPPUNIT_ASSERT( ifstr.fail() && !ifstr.bad() );
523 locale my_loc(locale::classic(), new my_codecvt());
525 CPPUNIT_ASSERT( ifstr.good() );
529 CPPUNIT_ASSERT( !ifstr.fail() );
530 CPPUNIT_ASSERT( !ifstr.bad() );
531 CPPUNIT_ASSERT( ifstr.eof() );
532 CPPUNIT_ASSERT( res == "0123456789" );
538 # if defined (CHECK_BIG_FILE)
539 void FstreamTest::big_file()
541 vector<pair<streamsize, streamoff> > file_pos;
545 ofstream out("big_file.txt");
546 CPPUNIT_ASSERT( out );
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)
555 //Generation of the number of loop:
557 for (int i = 0; i < 20; ++i) {
558 //This assertion check that the streamoff can at least represent the necessary integers values
560 CPPUNIT_ASSERT( (nb << 1) > nb );
563 CPPUNIT_ASSERT( nb * CHECK_BIG_FILE >= nb );
564 nb *= CHECK_BIG_FILE;
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 );
576 out << setw(1023) << index << '\n';
581 ifstream in("big_file.txt");
582 CPPUNIT_ASSERT( in );
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 );
591 size_t lastStarPos = line.rfind('*');
592 CPPUNIT_ASSERT( atoi(line.substr(lastStarPos + 1).c_str()) == (*pit).second );
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).
601 ifstream in("big_file.txt");
602 CPPUNIT_ASSERT( in );
604 streamsize nb_reads = 0;
605 while ((!in.eof()) && in.good()){
607 nb_reads += in.gcount();
614 # if !defined (STLPORT) || !defined (_STLP_WIN32)
615 void FstreamTest::offset()
617 # if (defined(_LARGEFILE_SOURCE) || defined(_LARGEFILE64_SOURCE)) && !defined(_STLP_USE_DEFAULT_FILE_OFFSET)
618 CPPUNIT_CHECK( sizeof(streamoff) == 8 );
620 CPPUNIT_CHECK( sizeof(streamoff) == sizeof(off_t) );
626 void FstreamTest::fstream_cov1()
635 ofs.open("c:\\test_cpp.txt",ios::out);
640 CPPUNIT_ASSERT( x == 1 );
646 ifs.open("c:\\test_cpp.txt",ios::in);
651 CPPUNIT_ASSERT( x == 1 );
652 CPPUNIT_ASSERT( !strcmp(buf,"example"));
660 fs.open("c:\\test_cpp1.txt",ios::out);
665 CPPUNIT_ASSERT( x == 1 );
669 void FstreamTest::fstream_cov2()
678 ofs.open("c:\\test_cpp.txt",ios::out,(long)0666);
683 CPPUNIT_ASSERT( x == 1 );
689 ifs.open("c:\\test_cpp.txt",ios::in,0666);
694 CPPUNIT_ASSERT( x == 1 );
695 CPPUNIT_ASSERT( !strcmp(buf,"example"));*/
703 fs.open("c:\\test_cpp.txt",ios::in | ios::out,(long)0666);
709 CPPUNIT_ASSERT( x == 1 );
710 CPPUNIT_ASSERT( !strcmp(buf,"example"));*/
714 void FstreamTest::fstream_cov3()
721 outfile.open ("c:\\test_cpp12.txt");
722 outfile.write ("Thisisanapple",14);
724 outfile.seekp (pos-7);
725 outfile.write ("sam",3);
732 ifs.open("c:\\test_cpp12.txt",ios::in);
737 CPPUNIT_ASSERT( !strcmp(buf,"Thisisasample"));
742 outfile.open ("c:\\test_cpp12.txt");
743 outfile.write ("Thisisanapple",14);
745 // seekp beyond the file
746 outfile.seekp (pos - (pos + 1) );
747 CPPUNIT_ASSERT( ios::failbit );
756 ofs.open("c:\\test_cpp.txt",ios::out);
761 CPPUNIT_ASSERT( x == 1 );
762 ifstream myfile( "c:\\test_cpp.txt", ios::in );
764 myfile.rdbuf( )->stossc( );
765 char i = myfile.rdbuf( )->sgetc( );
766 CPPUNIT_ASSERT( i == 'e' );
773 ofs.open("c:\\test_cpp.txt",ios::out);
778 CPPUNIT_ASSERT( x == 1 );
779 ifstream myfile( "c:\\test_cpp.txt", ios::in );
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());
804 void FstreamTest::fstream_cov4()
812 ofs.open("c:\\test_cpp.txt",ios::out);
817 CPPUNIT_ASSERT( x == 1 );
818 ifstream myfile( "c:\\test_cpp.txt", ios::in );
821 i = myfile.rdbuf( )->snextc( );
822 CPPUNIT_ASSERT( (char)i == 'e' );
830 ofs.open("c:\\test_cpp.txt",ios::out);
835 CPPUNIT_ASSERT( x == 1 );
836 ifstream myfile( "c:\\test_cpp.txt", ios::in );
839 i = myfile.rdbuf( )->in_avail( );
840 CPPUNIT_ASSERT( i == 7 );
841 myfile.readsome(&c[0],5);
843 CPPUNIT_ASSERT( !strcmp(c,"testi") );
850 ofs.open("c:\\test_cpp.txt",ios::out);
857 file.open( "c:\\test_cpp.txt" );
860 file.seekg( 0 ); // Goes to a zero-based position in the file
861 pos1 = file.tellg( );
862 CPPUNIT_ASSERT( pos1 == 0 );
864 CPPUNIT_ASSERT( c == '9' );
865 CPPUNIT_ASSERT( (int)file.tellg( ) == 1 );
868 CPPUNIT_ASSERT( c == '8' );
869 CPPUNIT_ASSERT( (int)file.tellg( ) == 2 );
873 CPPUNIT_ASSERT( c == '9' );
874 CPPUNIT_ASSERT( (int)file.tellg( ) == 1 );
875 file.seekg( pos1+2 );
877 CPPUNIT_ASSERT( c == '7' );
878 CPPUNIT_ASSERT( (int)file.tellg( ) == 3 );
882 void FstreamTest::fstream_cov5()
891 ofs.open("c:\\test_cpp.txt",ios::out);
896 CPPUNIT_ASSERT( x == 1 );
898 file.open( "c:\\test_cpp.txt" );
901 CPPUNIT_ASSERT( c == '2' );
902 file.seekg( 0,ios_base::beg);
904 CPPUNIT_ASSERT( c == '0' );
905 file.seekg( -1, ios_base::end );
907 CPPUNIT_ASSERT( c == '9' );
915 ofs.open("c:\\test_cpp.txt",ios::out);
920 CPPUNIT_ASSERT( x == 1 );
926 ifs.open("c:\\test_cpp.txt");
928 ifs.get( *os.rdbuf());
932 CPPUNIT_ASSERT( x == 1 );
940 ofs.open("c:\\test_cpp.txt",ios::out);
945 CPPUNIT_ASSERT( x == 1 );
947 ifstream myfile("c:\\test_cpp.txt", ios::in);
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") );
961 ofs.open("c:\\test_cpp.txt",ios::out);
962 ofs << "stdndardcpp";
966 ofs.seekp(0,ios::end);
970 CPPUNIT_ASSERT( x == 1 );
974 ifs.open("c:\\test_cpp.txt",ios::in);
979 CPPUNIT_ASSERT( !strcmp(buf,"standardcpponpips"));