Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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, |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 43 | std::ios_base::seekdir way, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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); |
Marshall Clow | 76a8670 | 2013-10-31 22:20:45 +0000 | [diff] [blame] | 60 | is.seekg(-1, std::ios_base::beg); |
| 61 | assert(is.fail()); |
| 62 | assert(seekoff_called == 2); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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()); |
Marshall Clow | 76a8670 | 2013-10-31 22:20:45 +0000 | [diff] [blame] | 69 | assert(seekoff_called == 3); |
| 70 | is.seekg(-1, std::ios_base::beg); |
| 71 | assert(is.fail()); |
| 72 | assert(seekoff_called == 4); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 73 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 74 | } |