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