📄 fstream_test.cpp
字号:
ostringstream ostr; ostr << in.rdbuf(); CPPUNIT_ASSERT( ostr ); CPPUNIT_ASSERT( in ); CPPUNIT_ASSERT( ostr.str() == "0123456789" ); }# if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) { //If the output stream buffer throws: ifstream in("test_file.txt", ios_base::binary); CPPUNIT_ASSERT( in ); auto_ptr<full_streambuf> pfull_buf(_CHECK_PTR(new full_streambuf(10, true))); ostream out(pfull_buf.get()); CPPUNIT_ASSERT( out ); out << in.rdbuf(); CPPUNIT_ASSERT( out.bad() ); CPPUNIT_ASSERT( in ); //out is bad we have no guaranty on what has been extracted: //CPPUNIT_ASSERT( pfull_buf->str() == "0123456789" ); out.clear(); out << in.rdbuf(); CPPUNIT_ASSERT( out.fail() && out.bad() ); CPPUNIT_ASSERT( in ); ostringstream ostr; ostr << in.rdbuf(); CPPUNIT_ASSERT( ostr ); CPPUNIT_ASSERT( in ); CPPUNIT_ASSERT( ostr.str() == "0123456789" ); }# endif}void FstreamTest::win32_file_format(){ const char* file_name = "win32_file_format.tmp"; const size_t nb_lines = 2049; { ofstream out(file_name); CPPUNIT_ASSERT( out.good() ); out << 'a'; for (size_t i = 0; i < nb_lines - 1; ++i) { out << '\n'; } out << '\r'; CPPUNIT_ASSERT( out.good() ); } { ifstream in(file_name); CPPUNIT_ASSERT( in.good() ); string line, last_line; size_t nb_read_lines = 0; while (getline(in, line)) { ++nb_read_lines; last_line = line; } CPPUNIT_ASSERT( in.eof() ); CPPUNIT_ASSERT( nb_read_lines == nb_lines ); CPPUNIT_ASSERT( !last_line.empty() && (last_line[0] == '\r') ); }}void FstreamTest::null_buf(){ /* ********************************************************************************** testcase for bug #1830513: in _istream.c template < class _CharT, class _Traits, class _Is_Delim> streamsize _STLP_CALL __read_unbuffered(basic_istream<_CharT, _Traits>* __that, basic_streambuf<_CharT, _Traits>* __buf, streamsize _Num, _CharT* __s, _Is_Delim __is_delim, bool __extract_delim, bool __append_null, bool __is_getline) can't accept _Num == 0; this is legal case, and may happen from template <class _CharT, class _Traits> basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::getline(_CharT* __s, streamsize __n, _CharT __delim) *********************************************************************************** */ fstream f( "test.txt", ios_base::in | ios_base::out | ios_base::trunc ); // string line; for ( int i = 0; i < 0x200; ++i ) { f.put( ' ' ); } // const streambuf *b = f.rdbuf(); // string s; char buf[1024]; buf[0] = 'a'; buf[1] = 'b'; buf[2] = 'c'; // getline( f, s ); // cerr << f.good() << endl; f.seekg( 0, ios_base::beg ); // f.seekg( 0, ios_base::end ); // buf[0] = f.get(); // cerr << (void *)(b->_M_gptr()) << " " << (void *)(b->_M_egptr()) << endl; // cerr << f.good() << endl; // getline( f, s ); f.getline( buf, 1 ); // <-- key line CPPUNIT_CHECK( buf[0] == 0 ); CPPUNIT_CHECK( f.fail() ); // due to delimiter not found while buffer was exhausted}#if defined (DO_CUSTOM_FACET_TEST)struct my_state { char dummy;};struct my_traits : public char_traits<char> { typedef my_state state_type; typedef fpos<state_type> pos_type;};class my_codecvt# if defined (STLPORT) : public codecvt<char, char, my_state> {# else : public locale::facet, public codecvt_base { //STLport grant the same default implementation, other Standard libs implementation //do not necessarily do the same: public: typedef char intern_type; typedef char extern_type; typedef my_state state_type; explicit my_codecvt(size_t __refs = 0) : locale::facet(__refs) {} result out(state_type&, const intern_type* __from, const intern_type*, const intern_type*& __from_next, extern_type* __to, extern_type*, extern_type*& __to_next) const { __from_next = __from; __to_next = __to; return noconv; } result in (state_type&, const extern_type* __from, const extern_type*, const extern_type*& __from_next, intern_type* __to, intern_type*, intern_type*& __to_next) const { __from_next = __from; __to_next = __to; return noconv; } result unshift(state_type&, extern_type* __to, extern_type*, extern_type*& __to_next) const { __to_next = __to; return noconv; } int encoding() const throw() { return 1; } bool always_noconv() const throw() { return true; } int length(const state_type&, const extern_type* __from, const extern_type* __end, size_t __max) const { return (int)min(static_cast<size_t>(__end - __from), __max); } int max_length() const throw() { return 1; } static locale::id id;# endif};# if !defined (STLPORT)locale::id my_codecvt::id;# else# if defined (__BORLANDC__)template <>locale::id codecvt<char, char, my_state>::id;# endif# endif#endifvoid FstreamTest::custom_facet(){#if defined (DO_CUSTOM_FACET_TEST) const char* fileName = "test_file.txt"; //File preparation: { ofstream ofstr(fileName, ios_base::binary); ofstr << "0123456789"; CPPUNIT_ASSERT( ofstr ); } { typedef basic_ifstream<char, my_traits> my_ifstream; typedef basic_string<char, my_traits> my_string; my_ifstream ifstr(fileName); CPPUNIT_ASSERT( ifstr );# if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) ifstr.imbue(locale::classic()); CPPUNIT_ASSERT( ifstr.fail() && !ifstr.bad() ); ifstr.clear();# endif locale my_loc(locale::classic(), _CHECK_PTR(new my_codecvt())); ifstr.imbue(my_loc); CPPUNIT_ASSERT( ifstr.good() ); /* my_string res; ifstr >> res; CPPUNIT_ASSERT( !ifstr.fail() ); CPPUNIT_ASSERT( !ifstr.bad() ); CPPUNIT_ASSERT( ifstr.eof() ); CPPUNIT_ASSERT( res == "0123456789" ); */ }#endif}# if defined (CHECK_BIG_FILE)void FstreamTest::big_file(){ vector<pair<streamsize, streamoff> > file_pos; //Big file creation: { ofstream out("big_file.txt"); CPPUNIT_ASSERT( out ); //We are going to generate a file with the following schema for the content: //0(1019 times)0000 //1023 characters + 1 charater for \n (for some platforms it will be a 1 ko line) //0(1019 times)0001 //... //0(1019 times)1234 //... //Generation of the number of loop: streamoff nb = 1; for (int i = 0; i < 20; ++i) { //This assertion check that the streamoff can at least represent the necessary integers values //for this test: CPPUNIT_ASSERT( (nb << 1) > nb ); nb <<= 1; } CPPUNIT_ASSERT( nb * CHECK_BIG_FILE >= nb ); nb *= CHECK_BIG_FILE; //Preparation of the ouput stream state: out << setiosflags(ios_base::right) << setfill('*'); for (streamoff index = 0; index < nb; ++index) { if (index % 1024 == 0) { file_pos.push_back(make_pair(out.tellp(), index)); CPPUNIT_ASSERT( file_pos.back().first != streamsize(-1) ); if (file_pos.size() > 1) { CPPUNIT_ASSERT( file_pos[file_pos.size() - 1].first > file_pos[file_pos.size() - 2].first ); } } out << setw(1023) << index << '\n'; } } { ifstream in("big_file.txt"); CPPUNIT_ASSERT( in ); string line; vector<pair<streamsize, streamsize> >::const_iterator pit(file_pos.begin()), pitEnd(file_pos.end()); for (; pit != pitEnd; ++pit) { in.seekg((*pit).first); CPPUNIT_ASSERT( in ); in >> line; size_t lastStarPos = line.rfind('*'); CPPUNIT_ASSERT( atoi(line.substr(lastStarPos + 1).c_str()) == (*pit).second ); } } /* The following test has been used to check that STLport do not generate an infinite loop when the file size is larger than the streamsize and streamoff representation (32 bits or 64 bits). { ifstream in("big_file.txt"); CPPUNIT_ASSERT( in ); char tmp[4096]; streamsize nb_reads = 0; while ((!in.eof()) && in.good()){ in.read(tmp, 4096); nb_reads += in.gcount(); } } */}# endif# if !defined (STLPORT) || !defined (_STLP_WIN32)void FstreamTest::offset(){# if (defined(_LARGEFILE_SOURCE) || defined(_LARGEFILE64_SOURCE)) && !defined(_STLP_USE_DEFAULT_FILE_OFFSET) CPPUNIT_CHECK( sizeof(streamoff) == 8 );# else CPPUNIT_CHECK( sizeof(streamoff) == sizeof(off_t) );# endif}# endif#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -