fstream_test.cpp

来自「stl的源码」· C++ 代码 · 共 916 行 · 第 1/2 页

CPP
916
字号
    wchar_t b1[] = { L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x', L'x' };    wfstream s( "test_file.txt", ios_base::in | ios_base::out );    CPPUNIT_ASSERT( s );    s.imbue(loc);    CPPUNIT_ASSERT( s );    int chars_read = (int)s.rdbuf()->sgetn( b1, sizeof(b1) / sizeof(wchar_t) );    CPPUNIT_CHECK( chars_read == 11 );    CPPUNIT_CHECK( b1[9] == L'0' );    fstream::pos_type pos = s.rdbuf()->pubseekoff(0, ios_base::cur);    // Depending on how '\n' is written in file, file position can be greater or equal to the number of chars_read read.    streamoff off = pos;    CPPUNIT_ASSERT( off >= chars_read );    off = s.rdbuf()->pubseekoff(-off, ios_base::cur);    CPPUNIT_ASSERT( off == -1 );    off = s.rdbuf()->pubseekoff(0, ios_base::beg);    CPPUNIT_ASSERT( off == 0 );    wchar_t b2[10] = { L'y', L'y', L'y', L'y', L'y', L'y', L'y', L'y', L'y', L'y' };    CPPUNIT_ASSERT( s.rdbuf()->sgetn( b2, 5 ) == 5 );    CPPUNIT_CHECK( b2[4] == L'5' );    pos = s.rdbuf()->pubseekoff(0, ios_base::cur);    CPPUNIT_ASSERT( pos == fstream::pos_type(5) );    //CPPUNIT_ASSERT( s.rdbuf()->pubseekoff(-5, ios_base::cur) == fstream::pos_type(0) );  }#endif}void FstreamTest::rdbuf(){  fstream ss( "test_file.txt", ios_base::in | ios_base::out | ios_base::binary | ios_base::trunc );  ss << "1234567\n89\n";  ss.seekg( 0, ios_base::beg );  ostringstream os;  ss.get( *os.rdbuf(), '\n' );  CPPUNIT_ASSERT( !ss.fail() );  char c;  ss.get(c);  CPPUNIT_ASSERT( !ss.fail() );  CPPUNIT_ASSERT( c == '\n' ); // 27.6.1.3 paragraph 12  CPPUNIT_ASSERT( os.str() == "1234567" );}void FstreamTest::streambuf_output(){  {    ofstream ofstr("test_file.txt", ios_base::binary);    if (!ofstr)      //No test if we cannot create the file      return;    ofstr << "01234567890123456789";    CPPUNIT_ASSERT( ofstr );  }  {    ifstream in("test_file.txt", ios_base::binary);    CPPUNIT_ASSERT( in );    full_streambuf full_buf(10);    ostream out(&full_buf);    CPPUNIT_ASSERT( out );    out << in.rdbuf();    CPPUNIT_ASSERT( out );    CPPUNIT_ASSERT( in );    CPPUNIT_ASSERT( full_buf.str() == "0123456789" );    out << in.rdbuf();    CPPUNIT_ASSERT( out.fail() );    CPPUNIT_ASSERT( in );    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 );    full_streambuf full_buf(10, true);    ostream out(&full_buf);    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( full_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') );  }}#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;};#if !defined (STLPORT)//STLport grant a default implementation, other Standard libs implementation//do not necessarily do the same:namespace std {  template <>  class codecvt<char, char, my_state>    : public locale::facet, public codecvt_base {  public:    typedef char intern_type;    typedef char extern_type;    typedef my_state state_type;    explicit 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;  };  locale::id codecvt<char, char, my_state>::id;}#  else#    if defined (__BORLANDC__) && (__BORLANDC__ < 0x590)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    typedef codecvt<char, char, my_state> my_codecvt;    locale my_loc(locale::classic(), new my_codecvt());    // Check that my_codecvt has not replace default codecvt:    CPPUNIT_ASSERT( (has_facet<my_codecvt>(my_loc)) );    CPPUNIT_ASSERT( (has_facet<codecvt<char, char, mbstate_t> >(my_loc)) );#  if !defined (STLPORT) || !defined (_STLP_NO_WCHAR_T)    CPPUNIT_ASSERT( (has_facet<codecvt<wchar_t, char, mbstate_t> >(my_loc)) );#  endif    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();    }  }  */}#  endifvoid FstreamTest::null_stream(){#  if (defined (STLPORT) && defined (_STLP_USE_WIN32_IO)) || \      (!defined (STLPORT) && (defined (WIN32) || defined (_WIN32)))  const char* nullStreamName = "NUL";#  else  const char* nullStreamName = "/dev/null";#  endif  {    ofstream nullStream(nullStreamName);    CPPUNIT_CHECK( nullStream );  }  {    ofstream nullStream(nullStreamName, ios_base::ate);    CPPUNIT_CHECK( nullStream );  }  {    ofstream nullStream(nullStreamName, ios_base::trunc);    CPPUNIT_CHECK( nullStream );  }  {    ofstream nullStream(nullStreamName, ios_base::app);    CPPUNIT_CHECK( nullStream );  }  {    ifstream nullStream(nullStreamName);    CPPUNIT_CHECK( nullStream );  }  {    ifstream nullStream(nullStreamName, ios_base::ate);    CPPUNIT_CHECK( nullStream );  }  {    fstream nullStream(nullStreamName);    CPPUNIT_CHECK( nullStream );  }  {    fstream nullStream(nullStreamName, ios_base::in | ios_base::out | ios_base::ate);    CPPUNIT_CHECK( nullStream );  }  {    fstream nullStream(nullStreamName, ios_base::in | ios_base::out | ios_base::trunc);    CPPUNIT_CHECK( nullStream );  }}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 (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 + =
减小字号Ctrl + -
显示快捷键?