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