libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

seekg_off.pass.cpp (2419B)


      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // XFAIL: with_system_cxx_lib=macosx10.11
     11 // XFAIL: with_system_cxx_lib=macosx10.10
     12 // XFAIL: with_system_cxx_lib=macosx10.9
     13 
     14 // <istream>
     15 
     16 // basic_istream<charT,traits>& seekg(off_type off, ios_base::seekdir dir);
     17 
     18 #include <istream>
     19 #include <cassert>
     20 
     21 int seekoff_called = 0;
     22 
     23 template <class CharT>
     24 struct testbuf
     25     : public std::basic_streambuf<CharT>
     26 {
     27     typedef std::basic_string<CharT> string_type;
     28     typedef std::basic_streambuf<CharT> base;
     29 private:
     30     string_type str_;
     31 public:
     32 
     33     testbuf() {}
     34     testbuf(const string_type& str)
     35         : str_(str)
     36     {
     37         base::setg(const_cast<CharT*>(str_.data()),
     38                    const_cast<CharT*>(str_.data()),
     39                    const_cast<CharT*>(str_.data()) + str_.size());
     40     }
     41 
     42     CharT* eback() const {return base::eback();}
     43     CharT* gptr() const {return base::gptr();}
     44     CharT* egptr() const {return base::egptr();}
     45 protected:
     46     typename base::pos_type seekoff(typename base::off_type off,
     47                                     std::ios_base::seekdir,
     48                                     std::ios_base::openmode which)
     49     {
     50         assert(which == std::ios_base::in);
     51         ++seekoff_called;
     52         return off;
     53     }
     54 };
     55 
     56 int main()
     57 {
     58     {
     59         testbuf<char> sb(" 123456789");
     60         std::istream is(&sb);
     61         is.seekg(5, std::ios_base::cur);
     62         assert(is.good());
     63         assert(seekoff_called == 1);
     64         is.seekg(-1, std::ios_base::beg);
     65         assert(is.fail());
     66         assert(seekoff_called == 2);
     67     }
     68     {
     69         testbuf<wchar_t> sb(L" 123456789");
     70         std::wistream is(&sb);
     71         is.seekg(5, std::ios_base::cur);
     72         assert(is.good());
     73         assert(seekoff_called == 3);
     74         is.seekg(-1, std::ios_base::beg);
     75         assert(is.fail());
     76         assert(seekoff_called == 4);
     77     }
     78     {
     79         testbuf<char> sb(" 123456789");
     80         std::istream is(&sb);
     81         is.setstate(std::ios_base::eofbit);
     82         assert(is.eof());
     83         is.seekg(5, std::ios_base::beg);
     84         assert(is.good());
     85         assert(!is.eof());
     86     }
     87 }