sl@0: // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include sl@0: #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS) sl@0: # include sl@0: # include sl@0: # include sl@0: # include sl@0: # include sl@0: # include sl@0: #include sl@0: sl@0: # include "full_streambuf.h" sl@0: # include "cppunit/cppunit_proxy.h" sl@0: sl@0: # if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) sl@0: using namespace std; sl@0: # endif sl@0: sl@0: //The macro value gives approximately the generated file sl@0: //size in Go sl@0: //#define CHECK_BIG_FILE 4 sl@0: sl@0: # if !defined (STLPORT) || !defined (_STLP_NO_CUSTOM_IO) && !defined (_STLP_NO_MEMBER_TEMPLATES) && \ sl@0: !((defined (_STLP_MSVC) && (_STLP_MSVC < 1300)) || \ sl@0: (defined (__GNUC__) && (__GNUC__ < 3)) || \ sl@0: (defined (__SUNPRO_CC)) || \ sl@0: (defined (__DMC__) && defined (_DLL))) sl@0: # define DO_CUSTOM_FACET_TEST sl@0: # endif sl@0: sl@0: // sl@0: // TestCase class sl@0: // sl@0: class FstreamTest : public CPPUNIT_NS::TestCase sl@0: { sl@0: CPPUNIT_TEST_SUITE(FstreamTest); sl@0: CPPUNIT_TEST(output); sl@0: CPPUNIT_TEST(input); sl@0: CPPUNIT_TEST(input_char); sl@0: CPPUNIT_TEST(io); sl@0: CPPUNIT_TEST(err); sl@0: CPPUNIT_TEST(tellg); sl@0: CPPUNIT_TEST(buf); sl@0: CPPUNIT_TEST(rdbuf); sl@0: #if !defined (STLPORT) || !defined (_STLP_WIN32) sl@0: CPPUNIT_TEST(offset); sl@0: #endif sl@0: # if defined (__DMC__) sl@0: CPPUNIT_IGNORE; sl@0: # endif sl@0: CPPUNIT_TEST(streambuf_output); sl@0: CPPUNIT_STOP_IGNORE; sl@0: CPPUNIT_TEST(win32_file_format); sl@0: # if defined (CHECK_BIG_FILE) sl@0: CPPUNIT_TEST(big_file); sl@0: # endif sl@0: # if !defined (DO_CUSTOM_FACET_TEST) sl@0: CPPUNIT_IGNORE; sl@0: #endif sl@0: CPPUNIT_TEST(custom_facet); sl@0: CPPUNIT_TEST(fstream_cov1); sl@0: CPPUNIT_TEST(fstream_cov2); sl@0: CPPUNIT_TEST(fstream_cov3); sl@0: CPPUNIT_TEST(fstream_cov4); sl@0: CPPUNIT_TEST(fstream_cov5); sl@0: CPPUNIT_TEST_SUITE_END(); sl@0: sl@0: protected: sl@0: void output(); sl@0: void input(); sl@0: void input_char(); sl@0: void io(); sl@0: void err(); sl@0: void tellg(); sl@0: void buf(); sl@0: void rdbuf(); sl@0: void streambuf_output(); sl@0: void win32_file_format(); sl@0: void custom_facet(); sl@0: void fstream_cov1(); sl@0: void fstream_cov2(); sl@0: void fstream_cov3(); sl@0: void fstream_cov4(); sl@0: void fstream_cov5(); sl@0: # if !defined (STLPORT) || !defined (_STLP_WIN32) sl@0: void offset(); sl@0: # endif sl@0: # if defined (CHECK_BIG_FILE) sl@0: void big_file(); sl@0: # endif sl@0: }; sl@0: sl@0: CPPUNIT_TEST_SUITE_REGISTRATION(FstreamTest); sl@0: sl@0: // sl@0: // tests implementation sl@0: // sl@0: void FstreamTest::output() sl@0: { sl@0: ofstream f( "c:\\private\\test_file.txt" ); sl@0: sl@0: f << 1 << '\n' << 2.0 << '\n' << "abcd\n" << "ghk lm\n" << "abcd ef"; sl@0: CPPUNIT_ASSERT (f.good()); sl@0: // CPPUNIT_ASSERT( s.str() == "1\n2\nabcd\nghk lm\nabcd ef" ); sl@0: } sl@0: sl@0: void FstreamTest::input() sl@0: { sl@0: ifstream f( "c:\\private\\test_file.txt" ); sl@0: int i = 0; sl@0: f >> i; sl@0: CPPUNIT_ASSERT( f.good() ); sl@0: CPPUNIT_ASSERT( i == 1 ); sl@0: double d = 0.0; sl@0: f >> d; sl@0: CPPUNIT_ASSERT( f.good() ); sl@0: CPPUNIT_ASSERT( d == 2.0 ); sl@0: string str; sl@0: f >> str; sl@0: CPPUNIT_ASSERT( f.good() ); sl@0: CPPUNIT_ASSERT( str == "abcd" ); sl@0: char c; sl@0: f.get(c); // extract newline, that not extracted by operator >> sl@0: CPPUNIT_ASSERT( f.good() ); sl@0: CPPUNIT_ASSERT( c == '\n' ); sl@0: getline( f, str ); sl@0: CPPUNIT_ASSERT( f.good() ); sl@0: CPPUNIT_ASSERT( str == "ghk lm" ); sl@0: getline( f, str ); sl@0: CPPUNIT_ASSERT( f.eof() ); sl@0: CPPUNIT_ASSERT( str == "abcd ef" ); sl@0: } sl@0: sl@0: void FstreamTest::input_char() sl@0: { sl@0: char buf[16] = { 0, '1', '2', '3' }; sl@0: ifstream s( "c:\\private\\test_file.txt" ); sl@0: s >> buf; sl@0: sl@0: CPPUNIT_ASSERT( buf[0] == '1' ); sl@0: CPPUNIT_ASSERT( buf[1] == 0 ); sl@0: CPPUNIT_ASSERT( buf[2] == '2' ); sl@0: } sl@0: sl@0: void FstreamTest::io() sl@0: { sl@0: basic_fstream > f( "c:\\private\\test_file.txt", ios_base::in | ios_base::out | ios_base::trunc ); sl@0: sl@0: CPPUNIT_ASSERT( f.is_open() ); sl@0: sl@0: f << 1 << '\n' << 2.0 << '\n' << "abcd\n" << "ghk lm\n" << "abcd ef"; sl@0: sl@0: // f.flush(); sl@0: f.seekg( 0, ios_base::beg ); sl@0: sl@0: int i = 0; sl@0: f >> i; sl@0: CPPUNIT_ASSERT( f.good() ); sl@0: CPPUNIT_ASSERT( i == 1 ); sl@0: double d = 0.0; sl@0: f >> d; sl@0: CPPUNIT_ASSERT( d == 2.0 ); sl@0: string s; sl@0: f >> s; sl@0: CPPUNIT_ASSERT( f.good() ); sl@0: CPPUNIT_ASSERT( s == "abcd" ); sl@0: char c; sl@0: f.get(c); // extract newline, that not extracted by operator >> sl@0: CPPUNIT_ASSERT( f.good() ); sl@0: CPPUNIT_ASSERT( c == '\n' ); sl@0: getline( f, s ); sl@0: CPPUNIT_ASSERT( f.good() ); sl@0: CPPUNIT_ASSERT( s == "ghk lm" ); sl@0: getline( f, s ); sl@0: CPPUNIT_ASSERT( !f.fail() ); sl@0: CPPUNIT_ASSERT( s == "abcd ef" ); sl@0: CPPUNIT_ASSERT( f.eof() ); sl@0: } sl@0: sl@0: void FstreamTest::err() sl@0: { sl@0: basic_fstream > f( "c:\\private\\test_file.txt", ios_base::in | ios_base::out | ios_base::trunc ); sl@0: sl@0: CPPUNIT_ASSERT( f.is_open() ); sl@0: sl@0: int i = 9; sl@0: f << i; sl@0: CPPUNIT_ASSERT( f.good() ); sl@0: i = 0; sl@0: f.seekg( 0, ios_base::beg ); sl@0: f >> i; sl@0: CPPUNIT_ASSERT( !f.fail() ); sl@0: CPPUNIT_ASSERT( i == 9 ); sl@0: f >> i; sl@0: CPPUNIT_ASSERT( f.fail() ); sl@0: CPPUNIT_ASSERT( f.eof() ); sl@0: CPPUNIT_ASSERT( i == 9 ); sl@0: } sl@0: sl@0: void FstreamTest::tellg() sl@0: { sl@0: { sl@0: // bogus ios_base::binary is for Wins sl@0: ofstream of("c:\\private\\test_file.txt", ios_base::out | ios_base::binary | ios_base::trunc); sl@0: CPPUNIT_ASSERT( of.is_open() ); sl@0: sl@0: for (int i = 0; i < 50; ++i) { sl@0: of << "line " << setiosflags(ios_base::right) << setfill('0') << setw(2) << i << "\n"; sl@0: CPPUNIT_ASSERT( !of.fail() ); sl@0: } sl@0: of.close(); sl@0: } sl@0: sl@0: { sl@0: // bogus ios_base::binary is for Wins sl@0: ifstream is("c:\\private\\test_file.txt", ios_base::in | ios_base::binary); sl@0: CPPUNIT_ASSERT( is.is_open() ); sl@0: char buf[64]; sl@0: sl@0: // CPPUNIT_ASSERT( is.tellg() == 0 ); sl@0: streampos p = 0; sl@0: for (int i = 0; i < 50; ++i) { sl@0: CPPUNIT_ASSERT( is.tellg() == p ); sl@0: is.read( buf, 8 ); sl@0: CPPUNIT_ASSERT( !is.fail() ); sl@0: p += 8; sl@0: } sl@0: } sl@0: sl@0: { sl@0: // bogus ios_base::binary is for Wins sl@0: ifstream is("c:\\private\\test_file.txt", ios_base::in | ios_base::binary); sl@0: CPPUNIT_ASSERT( is.is_open() ); sl@0: sl@0: streampos p = 0; sl@0: for (int i = 0; i < 50; ++i) { sl@0: CPPUNIT_ASSERT( !is.fail() ); sl@0: is.tellg(); sl@0: CPPUNIT_ASSERT( is.tellg() == p ); sl@0: p += 8; sl@0: is.seekg( p, ios_base::beg ); sl@0: CPPUNIT_ASSERT( !is.fail() ); sl@0: } sl@0: } sl@0: sl@0: { sl@0: // bogus ios_base::binary is for Wins sl@0: ifstream is("c:\\private\\test_file.txt", ios_base::in | ios_base::binary); sl@0: CPPUNIT_ASSERT( is.is_open() ); sl@0: sl@0: streampos p = 0; sl@0: for (int i = 0; i < 50; ++i) { sl@0: CPPUNIT_ASSERT( is.tellg() == p ); sl@0: p += 8; sl@0: is.seekg( 8, ios_base::cur ); sl@0: CPPUNIT_ASSERT( !is.fail() ); sl@0: } sl@0: } sl@0: } sl@0: sl@0: void FstreamTest::buf() sl@0: { sl@0: fstream ss( "c:\\private\\test_file.txt", ios_base::in | ios_base::out | ios_base::binary | ios_base::trunc ); sl@0: sl@0: ss << "1234567\n89\n"; sl@0: ss.seekg( 0, ios_base::beg ); sl@0: char buf[10]; sl@0: buf[7] = 'x'; sl@0: ss.get( buf, 10 ); sl@0: CPPUNIT_ASSERT( !ss.fail() ); sl@0: CPPUNIT_ASSERT( buf[0] == '1' ); sl@0: CPPUNIT_ASSERT( buf[1] == '2' ); sl@0: CPPUNIT_ASSERT( buf[2] == '3' ); sl@0: CPPUNIT_ASSERT( buf[3] == '4' ); sl@0: CPPUNIT_ASSERT( buf[4] == '5' ); sl@0: CPPUNIT_ASSERT( buf[5] == '6' ); sl@0: CPPUNIT_ASSERT( buf[6] == '7' ); // 27.6.1.3 paragraph 10, paragraph 7 sl@0: CPPUNIT_ASSERT( buf[7] == 0 ); // 27.6.1.3 paragraph 8 sl@0: char c; sl@0: ss.get(c); sl@0: CPPUNIT_ASSERT( !ss.fail() ); sl@0: CPPUNIT_ASSERT( c == '\n' ); // 27.6.1.3 paragraph 10, paragraph 7 sl@0: ss.get(c); sl@0: CPPUNIT_ASSERT( !ss.fail() ); sl@0: CPPUNIT_ASSERT( c == '8' ); sl@0: } sl@0: sl@0: void FstreamTest::rdbuf() sl@0: { sl@0: fstream ss( "c:\\private\\test_file.txt", ios_base::in | ios_base::out | ios_base::binary | ios_base::trunc ); sl@0: sl@0: ss << "1234567\n89\n"; sl@0: ss.seekg( 0, ios_base::beg ); sl@0: sl@0: ostringstream os; sl@0: ss.get( *os.rdbuf(), '\n' ); sl@0: CPPUNIT_ASSERT( !ss.fail() ); sl@0: char c; sl@0: ss.get(c); sl@0: CPPUNIT_ASSERT( !ss.fail() ); sl@0: CPPUNIT_ASSERT( c == '\n' ); // 27.6.1.3 paragraph 12 sl@0: CPPUNIT_ASSERT( os.str() == "1234567" ); sl@0: } sl@0: sl@0: void FstreamTest::streambuf_output() sl@0: { sl@0: { sl@0: ofstream ofstr("c:\\private\\test_file.txt", ios_base::binary); sl@0: if (!ofstr) sl@0: //No test if we cannot create the file sl@0: return; sl@0: ofstr << "01234567890123456789"; sl@0: CPPUNIT_ASSERT( ofstr ); sl@0: } sl@0: sl@0: { sl@0: ifstream in("c:\\private\\test_file.txt", ios_base::binary); sl@0: CPPUNIT_ASSERT( in ); sl@0: sl@0: auto_ptr pfull_buf(new full_streambuf(10)); sl@0: ostream out(pfull_buf.get()); sl@0: CPPUNIT_ASSERT( out ); sl@0: sl@0: out << in.rdbuf(); sl@0: CPPUNIT_ASSERT( out ); sl@0: CPPUNIT_ASSERT( in ); sl@0: CPPUNIT_ASSERT( pfull_buf->str() == "0123456789" ); sl@0: sl@0: out << in.rdbuf(); sl@0: CPPUNIT_ASSERT( out.fail() ); sl@0: CPPUNIT_ASSERT( in ); sl@0: sl@0: ostringstream ostr; sl@0: ostr << in.rdbuf(); sl@0: CPPUNIT_ASSERT( ostr ); sl@0: CPPUNIT_ASSERT( in ); sl@0: CPPUNIT_ASSERT( ostr.str() == "0123456789" ); sl@0: } sl@0: sl@0: # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) sl@0: { sl@0: //If the output stream buffer throws: sl@0: ifstream in("c:\\private\\test_file.txt", ios_base::binary); sl@0: CPPUNIT_ASSERT( in ); sl@0: sl@0: auto_ptr pfull_buf(new full_streambuf(10, true)); sl@0: ostream out(pfull_buf.get()); sl@0: CPPUNIT_ASSERT( out ); sl@0: sl@0: out << in.rdbuf(); sl@0: CPPUNIT_ASSERT( out.bad() ); sl@0: CPPUNIT_ASSERT( in ); sl@0: //out is bad we have no guaranty on what has been extracted: sl@0: //CPPUNIT_ASSERT( pfull_buf->str() == "0123456789" ); sl@0: sl@0: out.clear(); sl@0: out << in.rdbuf(); sl@0: CPPUNIT_ASSERT( out.fail() && out.bad() ); sl@0: CPPUNIT_ASSERT( in ); sl@0: sl@0: ostringstream ostr; sl@0: ostr << in.rdbuf(); sl@0: CPPUNIT_ASSERT( ostr ); sl@0: CPPUNIT_ASSERT( in ); sl@0: CPPUNIT_ASSERT( ostr.str() == "0123456789" ); sl@0: } sl@0: # endif sl@0: } sl@0: sl@0: void FstreamTest::win32_file_format() sl@0: { sl@0: const char* file_name = "c:\\private\\win32_file_format.tmp"; sl@0: const size_t nb_lines = 2049; sl@0: { sl@0: ofstream out(file_name); sl@0: CPPUNIT_ASSERT( out.good() ); sl@0: out << 'a'; sl@0: for (size_t i = 0; i < nb_lines - 1; ++i) { sl@0: out << '\n'; sl@0: } sl@0: out << '\r'; sl@0: CPPUNIT_ASSERT( out.good() ); sl@0: } sl@0: { sl@0: ifstream in(file_name); sl@0: CPPUNIT_ASSERT( in.good() ); sl@0: string line, last_line; sl@0: size_t nb_read_lines = 0; sl@0: while (getline(in, line)) { sl@0: ++nb_read_lines; sl@0: last_line = line; sl@0: } sl@0: CPPUNIT_ASSERT( in.eof() ); sl@0: CPPUNIT_ASSERT( nb_read_lines == nb_lines ); sl@0: CPPUNIT_ASSERT( !last_line.empty() && (last_line[0] == '\r') ); sl@0: } sl@0: } sl@0: sl@0: #if defined (DO_CUSTOM_FACET_TEST) sl@0: struct my_state { sl@0: char dummy; sl@0: }; sl@0: sl@0: struct my_traits : public char_traits { sl@0: typedef my_state state_type; sl@0: typedef fpos pos_type; sl@0: }; sl@0: sl@0: class my_codecvt sl@0: # if defined (STLPORT) sl@0: : public codecvt { sl@0: sl@0: # else sl@0: : public locale::facet, public codecvt_base { sl@0: //STLport grant the same default implementation, other Standard libs implementation sl@0: //do not necessarily do the same: sl@0: public: sl@0: typedef char intern_type; sl@0: typedef char extern_type; sl@0: typedef my_state state_type; sl@0: sl@0: explicit my_codecvt(size_t __refs = 0) : locale::facet(__refs) {} sl@0: result out(state_type&, sl@0: const intern_type* __from, sl@0: const intern_type*, sl@0: const intern_type*& __from_next, sl@0: extern_type* __to, sl@0: extern_type*, sl@0: extern_type*& __to_next) const sl@0: { __from_next = __from; __to_next = __to; return noconv; } sl@0: sl@0: result in (state_type&, sl@0: const extern_type* __from, sl@0: const extern_type*, sl@0: const extern_type*& __from_next, sl@0: intern_type* __to, sl@0: intern_type*, sl@0: intern_type*& __to_next) const sl@0: { __from_next = __from; __to_next = __to; return noconv; } sl@0: sl@0: result unshift(state_type&, sl@0: extern_type* __to, sl@0: extern_type*, sl@0: extern_type*& __to_next) const sl@0: { __to_next = __to; return noconv; } sl@0: sl@0: int encoding() const throw() sl@0: { return 1; } sl@0: sl@0: bool always_noconv() const throw() sl@0: { return true; } sl@0: sl@0: int length(const state_type&, sl@0: const extern_type* __from, sl@0: const extern_type* __end, sl@0: size_t __max) const sl@0: { return (int)min(static_cast(__end - __from), __max); } sl@0: sl@0: int max_length() const throw() sl@0: { return 1; } sl@0: sl@0: static locale::id id; sl@0: # endif sl@0: sl@0: }; sl@0: sl@0: # if !defined (STLPORT) sl@0: locale::id my_codecvt::id; sl@0: # else sl@0: # if defined (__BORLANDC__) sl@0: template <> sl@0: locale::id codecvt::id; sl@0: # endif sl@0: # endif sl@0: #endif sl@0: sl@0: #if defined (__SYMBIAN32__WSD__) sl@0: locale::id& codecvt::GetFacetLocaleId() sl@0: { sl@0: static locale::id aId = {41}; sl@0: return aId; sl@0: } sl@0: #endif sl@0: sl@0: void FstreamTest::custom_facet() sl@0: { sl@0: #if defined (DO_CUSTOM_FACET_TEST) && !defined (__SYMBIAN32__) sl@0: sl@0: const char* fileName = "c:\\private\\test_file.txt"; sl@0: //File preparation: sl@0: { sl@0: ofstream ofstr(fileName, ios_base::binary); sl@0: ofstr << "0123456789"; sl@0: CPPUNIT_ASSERT( ofstr ); sl@0: } sl@0: sl@0: { sl@0: typedef basic_ifstream my_ifstream; sl@0: typedef basic_string my_string; sl@0: sl@0: my_ifstream ifstr(fileName); sl@0: CPPUNIT_ASSERT( ifstr ); sl@0: sl@0: # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) sl@0: ifstr.imbue(locale::classic()); sl@0: CPPUNIT_ASSERT( ifstr.fail() && !ifstr.bad() ); sl@0: ifstr.clear(); sl@0: # endif sl@0: locale my_loc(locale::classic(), new my_codecvt()); sl@0: ifstr.imbue(my_loc); sl@0: CPPUNIT_ASSERT( ifstr.good() ); sl@0: /* sl@0: my_string res; sl@0: ifstr >> res; sl@0: CPPUNIT_ASSERT( !ifstr.fail() ); sl@0: CPPUNIT_ASSERT( !ifstr.bad() ); sl@0: CPPUNIT_ASSERT( ifstr.eof() ); sl@0: CPPUNIT_ASSERT( res == "0123456789" ); sl@0: */ sl@0: } sl@0: #endif sl@0: } sl@0: sl@0: # if defined (CHECK_BIG_FILE) sl@0: void FstreamTest::big_file() sl@0: { sl@0: vector > file_pos; sl@0: sl@0: //Big file creation: sl@0: { sl@0: ofstream out("big_file.txt"); sl@0: CPPUNIT_ASSERT( out ); sl@0: sl@0: //We are going to generate a file with the following schema for the content: sl@0: //0(1019 times)0000 //1023 characters + 1 charater for \n (for some platforms it will be a 1 ko line) sl@0: //0(1019 times)0001 sl@0: //... sl@0: //0(1019 times)1234 sl@0: //... sl@0: sl@0: //Generation of the number of loop: sl@0: streamoff nb = 1; sl@0: for (int i = 0; i < 20; ++i) { sl@0: //This assertion check that the streamoff can at least represent the necessary integers values sl@0: //for this test: sl@0: CPPUNIT_ASSERT( (nb << 1) > nb ); sl@0: nb <<= 1; sl@0: } sl@0: CPPUNIT_ASSERT( nb * CHECK_BIG_FILE >= nb ); sl@0: nb *= CHECK_BIG_FILE; sl@0: sl@0: //Preparation of the ouput stream state: sl@0: out << setiosflags(ios_base::right) << setfill('*'); sl@0: for (streamoff index = 0; index < nb; ++index) { sl@0: if (index % 1024 == 0) { sl@0: file_pos.push_back(make_pair(out.tellp(), index)); sl@0: CPPUNIT_ASSERT( file_pos.back().first != streamsize(-1) ); sl@0: if (file_pos.size() > 1) { sl@0: CPPUNIT_ASSERT( file_pos[file_pos.size() - 1].first > file_pos[file_pos.size() - 2].first ); sl@0: } sl@0: } sl@0: out << setw(1023) << index << '\n'; sl@0: } sl@0: } sl@0: sl@0: { sl@0: ifstream in("big_file.txt"); sl@0: CPPUNIT_ASSERT( in ); sl@0: sl@0: string line; sl@0: vector >::const_iterator pit(file_pos.begin()), sl@0: pitEnd(file_pos.end()); sl@0: for (; pit != pitEnd; ++pit) { sl@0: in.seekg((*pit).first); sl@0: CPPUNIT_ASSERT( in ); sl@0: in >> line; sl@0: size_t lastStarPos = line.rfind('*'); sl@0: CPPUNIT_ASSERT( atoi(line.substr(lastStarPos + 1).c_str()) == (*pit).second ); sl@0: } sl@0: } sl@0: sl@0: /* sl@0: The following test has been used to check that STLport do not generate sl@0: an infinite loop when the file size is larger than the streamsize and sl@0: streamoff representation (32 bits or 64 bits). sl@0: { sl@0: ifstream in("big_file.txt"); sl@0: CPPUNIT_ASSERT( in ); sl@0: char tmp[4096]; sl@0: streamsize nb_reads = 0; sl@0: while ((!in.eof()) && in.good()){ sl@0: in.read(tmp, 4096); sl@0: nb_reads += in.gcount(); sl@0: } sl@0: } sl@0: */ sl@0: } sl@0: # endif sl@0: sl@0: # if !defined (STLPORT) || !defined (_STLP_WIN32) sl@0: void FstreamTest::offset() sl@0: { sl@0: # if (defined(_LARGEFILE_SOURCE) || defined(_LARGEFILE64_SOURCE)) && !defined(_STLP_USE_DEFAULT_FILE_OFFSET) sl@0: CPPUNIT_CHECK( sizeof(streamoff) == 8 ); sl@0: # else sl@0: CPPUNIT_CHECK( sizeof(streamoff) == sizeof(off_t) ); sl@0: # endif sl@0: } sl@0: # endif sl@0: sl@0: #endif sl@0: void FstreamTest::fstream_cov1() sl@0: { sl@0: __UHEAP_MARK; sl@0: { sl@0: ofstream ofs; sl@0: int x = 0; sl@0: char buf[12]; sl@0: if (!ofs.bad()) sl@0: { sl@0: ofs.open("c:\\test_cpp.txt",ios::out); sl@0: ofs << "example"; sl@0: x = 1; sl@0: ofs.close(); sl@0: } sl@0: CPPUNIT_ASSERT( x == 1 ); sl@0: sl@0: ifstream ifs; sl@0: x = 0; sl@0: if (!ifs.bad()) sl@0: { sl@0: ifs.open("c:\\test_cpp.txt",ios::in); sl@0: ifs >> buf; sl@0: x = 1; sl@0: ifs.close(); sl@0: } sl@0: CPPUNIT_ASSERT( x == 1 ); sl@0: CPPUNIT_ASSERT( !strcmp(buf,"example")); sl@0: } sl@0: { sl@0: fstream fs; sl@0: int x = 0; sl@0: sl@0: if (!fs.bad()) sl@0: { sl@0: fs.open("c:\\test_cpp1.txt",ios::out); sl@0: fs << "example"; sl@0: x = 1; sl@0: fs.close(); sl@0: } sl@0: CPPUNIT_ASSERT( x == 1 ); sl@0: } sl@0: __UHEAP_MARKEND; sl@0: } sl@0: void FstreamTest::fstream_cov2() sl@0: { sl@0: __UHEAP_MARK; sl@0: {/* sl@0: ofstream ofs; sl@0: int x = 0; sl@0: char buf[12]; sl@0: if (!ofs.bad()) sl@0: { sl@0: ofs.open("c:\\test_cpp.txt",ios::out,(long)0666); sl@0: ofs << "example"; sl@0: x = 1; sl@0: ofs.close(); sl@0: } sl@0: CPPUNIT_ASSERT( x == 1 ); sl@0: sl@0: ifstream ifs; sl@0: x = 0; sl@0: if (!ifs.bad()) sl@0: { sl@0: ifs.open("c:\\test_cpp.txt",ios::in,0666); sl@0: ifs >> buf; sl@0: x = 1; sl@0: ifs.close(); sl@0: } sl@0: CPPUNIT_ASSERT( x == 1 ); sl@0: CPPUNIT_ASSERT( !strcmp(buf,"example"));*/ sl@0: } sl@0: {/* sl@0: fstream fs; sl@0: int x = 0; sl@0: char buf[12]; sl@0: if (!fs.bad()) sl@0: { sl@0: fs.open("c:\\test_cpp.txt",ios::in | ios::out,(long)0666); sl@0: fs << "example"; sl@0: fs >> buf; sl@0: x = 1; sl@0: fs.close(); sl@0: } sl@0: CPPUNIT_ASSERT( x == 1 ); sl@0: CPPUNIT_ASSERT( !strcmp(buf,"example"));*/ sl@0: } sl@0: __UHEAP_MARKEND; sl@0: } sl@0: void FstreamTest::fstream_cov3() sl@0: { sl@0: __UHEAP_MARK; sl@0: { sl@0: long pos; sl@0: char buf[20]; sl@0: ofstream outfile; sl@0: outfile.open ("c:\\test_cpp12.txt"); sl@0: outfile.write ("Thisisanapple",14); sl@0: pos=outfile.tellp(); sl@0: outfile.seekp (pos-7); sl@0: outfile.write ("sam",3); sl@0: outfile.flush(); sl@0: outfile.close(); sl@0: sl@0: ifstream ifs; sl@0: if (!ifs.bad()) sl@0: { sl@0: ifs.open("c:\\test_cpp12.txt",ios::in); sl@0: ifs >> buf; sl@0: ifs.close(); sl@0: } sl@0: sl@0: CPPUNIT_ASSERT( !strcmp(buf,"Thisisasample")); sl@0: } sl@0: { sl@0: long pos; sl@0: ofstream outfile; sl@0: outfile.open ("c:\\test_cpp12.txt"); sl@0: outfile.write ("Thisisanapple",14); sl@0: pos=outfile.tellp(); sl@0: // seekp beyond the file sl@0: outfile.seekp (pos - (pos + 1) ); sl@0: CPPUNIT_ASSERT( ios::failbit ); sl@0: outfile.flush(); sl@0: outfile.close(); sl@0: } sl@0: { sl@0: ofstream ofs; sl@0: int x = 0; sl@0: if (!ofs.bad()) sl@0: { sl@0: ofs.open("c:\\test_cpp.txt",ios::out); sl@0: ofs << "testing"; sl@0: x = 1; sl@0: ofs.close(); sl@0: } sl@0: CPPUNIT_ASSERT( x == 1 ); sl@0: ifstream myfile( "c:\\test_cpp.txt", ios::in ); sl@0: sl@0: myfile.rdbuf( )->stossc( ); sl@0: char i = myfile.rdbuf( )->sgetc( ); sl@0: CPPUNIT_ASSERT( i == 'e' ); sl@0: } sl@0: { sl@0: ofstream ofs; sl@0: int x = 0; sl@0: if (!ofs.bad()) sl@0: { sl@0: ofs.open("c:\\test_cpp.txt",ios::out); sl@0: ofs << "testing"; sl@0: x = 1; sl@0: ofs.close(); sl@0: } sl@0: CPPUNIT_ASSERT( x == 1 ); sl@0: ifstream myfile( "c:\\test_cpp.txt", ios::in ); sl@0: sl@0: int i; sl@0: i = myfile.rdbuf( )->sbumpc( ); sl@0: CPPUNIT_ASSERT( (char)i == 't' ); sl@0: i = myfile.rdbuf( )->sbumpc( ); sl@0: CPPUNIT_ASSERT( (char)i == 'e' ); sl@0: i = myfile.rdbuf( )->sungetc( ); sl@0: CPPUNIT_ASSERT( (char)i == 'e' ); sl@0: i = myfile.rdbuf( )->sungetc( ); sl@0: CPPUNIT_ASSERT( (char)i == 't' ); sl@0: i = myfile.rdbuf( )->sbumpc( ); sl@0: CPPUNIT_ASSERT( (char)i == 't' ); sl@0: i = myfile.rdbuf( )->sbumpc( ); sl@0: i = myfile.rdbuf( )->sbumpc( ); sl@0: i = myfile.rdbuf( )->sbumpc( ); sl@0: i = myfile.rdbuf( )->sbumpc( ); sl@0: i = myfile.rdbuf( )->sbumpc( ); sl@0: i = myfile.rdbuf( )->sbumpc( ); sl@0: i = myfile.rdbuf( )->sbumpc( ); sl@0: i = myfile.rdbuf( )->sbumpc( ); sl@0: //CPPUNIT_ASSERT(myfile.eof()); sl@0: } sl@0: __UHEAP_MARKEND; sl@0: } sl@0: void FstreamTest::fstream_cov4() sl@0: { sl@0: __UHEAP_MARK; sl@0: { sl@0: ofstream ofs; sl@0: int x = 0; sl@0: if (!ofs.bad()) sl@0: { sl@0: ofs.open("c:\\test_cpp.txt",ios::out); sl@0: ofs << "testing"; sl@0: x = 1; sl@0: ofs.close(); sl@0: } sl@0: CPPUNIT_ASSERT( x == 1 ); sl@0: ifstream myfile( "c:\\test_cpp.txt", ios::in ); sl@0: sl@0: int i; sl@0: i = myfile.rdbuf( )->snextc( ); sl@0: CPPUNIT_ASSERT( (char)i == 'e' ); sl@0: } sl@0: { sl@0: ofstream ofs; sl@0: int x = 0; sl@0: char c[10]; sl@0: if (!ofs.bad()) sl@0: { sl@0: ofs.open("c:\\test_cpp.txt",ios::out); sl@0: ofs << "testing"; sl@0: x = 1; sl@0: ofs.close(); sl@0: } sl@0: CPPUNIT_ASSERT( x == 1 ); sl@0: ifstream myfile( "c:\\test_cpp.txt", ios::in ); sl@0: sl@0: int i; sl@0: i = myfile.rdbuf( )->in_avail( ); sl@0: CPPUNIT_ASSERT( i == 7 ); sl@0: myfile.readsome(&c[0],5); sl@0: c[5]='\0'; sl@0: CPPUNIT_ASSERT( !strcmp(c,"testi") ); sl@0: } sl@0: { sl@0: ofstream ofs; sl@0: char c; sl@0: if (!ofs.bad()) sl@0: { sl@0: ofs.open("c:\\test_cpp.txt",ios::out); sl@0: ofs << "9876543210"; sl@0: ofs.close(); sl@0: } sl@0: int pos1; sl@0: ifstream file; sl@0: sl@0: file.open( "c:\\test_cpp.txt" ); sl@0: if (!file.good()) sl@0: CPPUNIT_ASSERT( 0 ); sl@0: file.seekg( 0 ); // Goes to a zero-based position in the file sl@0: pos1 = file.tellg( ); sl@0: CPPUNIT_ASSERT( pos1 == 0 ); sl@0: file.get( c); sl@0: CPPUNIT_ASSERT( c == '9' ); sl@0: CPPUNIT_ASSERT( (int)file.tellg( ) == 1 ); sl@0: pos1 += 1; sl@0: file.get( c ); sl@0: CPPUNIT_ASSERT( c == '8' ); sl@0: CPPUNIT_ASSERT( (int)file.tellg( ) == 2 ); sl@0: pos1 -= 1; sl@0: file.seekg( pos1 ); sl@0: file.get( c ); sl@0: CPPUNIT_ASSERT( c == '9' ); sl@0: CPPUNIT_ASSERT( (int)file.tellg( ) == 1 ); sl@0: file.seekg( pos1+2 ); sl@0: file.get( c ); sl@0: CPPUNIT_ASSERT( c == '7' ); sl@0: CPPUNIT_ASSERT( (int)file.tellg( ) == 3 ); sl@0: } sl@0: __UHEAP_MARKEND; sl@0: } sl@0: void FstreamTest::fstream_cov5() sl@0: { sl@0: __UHEAP_MARK; sl@0: { sl@0: ofstream ofs; sl@0: int x = 0; sl@0: char c; sl@0: if (!ofs.bad()) sl@0: { sl@0: ofs.open("c:\\test_cpp.txt",ios::out); sl@0: ofs << "0123456789"; sl@0: x = 1; sl@0: ofs.close(); sl@0: } sl@0: CPPUNIT_ASSERT( x == 1 ); sl@0: ifstream file; sl@0: file.open( "c:\\test_cpp.txt" ); sl@0: file.seekg(2); sl@0: file >> c; sl@0: CPPUNIT_ASSERT( c == '2' ); sl@0: file.seekg( 0,ios_base::beg); sl@0: file >> c; sl@0: CPPUNIT_ASSERT( c == '0' ); sl@0: file.seekg( -1, ios_base::end ); sl@0: file >> c; sl@0: CPPUNIT_ASSERT( c == '9' ); sl@0: } sl@0: { sl@0: ofstream ofs; sl@0: int x = 0; sl@0: sl@0: if (!ofs.bad()) sl@0: { sl@0: ofs.open("c:\\test_cpp.txt",ios::out); sl@0: ofs << "example"; sl@0: x = 1; sl@0: ofs.close(); sl@0: } sl@0: CPPUNIT_ASSERT( x == 1 ); sl@0: sl@0: ifstream ifs; sl@0: x = 0; sl@0: if (!ifs.bad()) sl@0: { sl@0: ifs.open("c:\\test_cpp.txt"); sl@0: ostringstream os; sl@0: ifs.get( *os.rdbuf()); sl@0: x = 1; sl@0: ifs.close(); sl@0: } sl@0: CPPUNIT_ASSERT( x == 1 ); sl@0: } sl@0: { sl@0: ofstream ofs; sl@0: int x = 0; sl@0: sl@0: if (!ofs.bad()) sl@0: { sl@0: ofs.open("c:\\test_cpp.txt",ios::out); sl@0: ofs << "testing"; sl@0: x = 1; sl@0: ofs.close(); sl@0: } sl@0: CPPUNIT_ASSERT( x == 1 ); sl@0: sl@0: ifstream myfile("c:\\test_cpp.txt", ios::in); sl@0: char a[15]; sl@0: sl@0: streamsize i = myfile.rdbuf()->sgetn(a, 3); sl@0: CPPUNIT_ASSERT( i == 3 ); sl@0: a[i] = myfile.widen('\0'); sl@0: CPPUNIT_ASSERT( !strcmp(a,"tes") ); sl@0: } sl@0: { sl@0: ofstream ofs; sl@0: int x = 0; sl@0: char buf[20]; sl@0: if (!ofs.bad()) sl@0: { sl@0: ofs.open("c:\\test_cpp.txt",ios::out); sl@0: ofs << "stdndardcpp"; sl@0: x = 1; sl@0: ofs.seekp(2); sl@0: ofs << "a"; sl@0: ofs.seekp(0,ios::end); sl@0: ofs << "onpips"; sl@0: ofs.close(); sl@0: } sl@0: CPPUNIT_ASSERT( x == 1 ); sl@0: ifstream ifs; sl@0: if (!ifs.bad()) sl@0: { sl@0: ifs.open("c:\\test_cpp.txt",ios::in); sl@0: ifs >> buf; sl@0: ifs.close(); sl@0: } sl@0: sl@0: CPPUNIT_ASSERT( !strcmp(buf,"standardcpponpips")); sl@0: } sl@0: __UHEAP_MARKEND; sl@0: }