blob: 73f3da1c6bd1f94d24cb309dbebdba1ab7c47556 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
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
17int seekoff_called = 0;
18
19template <class CharT>
20struct testbuf
21 : public std::basic_streambuf<CharT>
22{
23 typedef std::basic_string<CharT> string_type;
24 typedef std::basic_streambuf<CharT> base;
25private:
26 string_type str_;
27public:
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();}
41protected:
42 typename base::pos_type seekoff(typename base::off_type off,
Dan Albert1d4a1ed2016-05-25 22:36:09 -070043 std::ios_base::seekdir way,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000044 std::ios_base::openmode which)
45 {
46 assert(which == std::ios_base::in);
47 ++seekoff_called;
48 return off;
49 }
50};
51
52int 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 Clow76a86702013-10-31 22:20:45 +000060 is.seekg(-1, std::ios_base::beg);
61 assert(is.fail());
62 assert(seekoff_called == 2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063 }
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 Clow76a86702013-10-31 22:20:45 +000069 assert(seekoff_called == 3);
70 is.seekg(-1, std::ios_base::beg);
71 assert(is.fail());
72 assert(seekoff_called == 4);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000073 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000074}