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(pos_type pos); |
| 13 | |
| 14 | #include <istream> |
| 15 | #include <cassert> |
| 16 | |
| 17 | template <class CharT> |
| 18 | struct testbuf |
| 19 | : public std::basic_streambuf<CharT> |
| 20 | { |
| 21 | typedef std::basic_string<CharT> string_type; |
| 22 | typedef std::basic_streambuf<CharT> base; |
| 23 | private: |
| 24 | string_type str_; |
| 25 | public: |
| 26 | |
| 27 | testbuf() {} |
| 28 | testbuf(const string_type& str) |
| 29 | : str_(str) |
| 30 | { |
| 31 | base::setg(const_cast<CharT*>(str_.data()), |
| 32 | const_cast<CharT*>(str_.data()), |
| 33 | const_cast<CharT*>(str_.data()) + str_.size()); |
| 34 | } |
| 35 | |
| 36 | CharT* eback() const {return base::eback();} |
| 37 | CharT* gptr() const {return base::gptr();} |
| 38 | CharT* egptr() const {return base::egptr();} |
| 39 | protected: |
| 40 | typename base::pos_type seekpos(typename base::pos_type sp, |
| 41 | std::ios_base::openmode which) |
| 42 | { |
| 43 | assert(which == std::ios_base::in); |
| 44 | return sp; |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | int main() |
| 49 | { |
| 50 | { |
| 51 | testbuf<char> sb(" 123456789"); |
| 52 | std::istream is(&sb); |
| 53 | is.seekg(5); |
| 54 | assert(is.good()); |
| 55 | is.seekg(-1); |
| 56 | assert(is.fail()); |
| 57 | } |
| 58 | { |
| 59 | testbuf<wchar_t> sb(L" 123456789"); |
| 60 | std::wistream is(&sb); |
| 61 | is.seekg(5); |
| 62 | assert(is.good()); |
| 63 | is.seekg(-1); |
| 64 | assert(is.fail()); |
| 65 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 66 | } |