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