1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// <istream>
10
11// basic_istream<charT,traits>& seekg(off_type off, ios_base::seekdir dir);
12
13#include <istream>
14#include <cassert>
15#include <streambuf>
16
17#include "test_macros.h"
18
19int seekoff_called = 0;
20
21template <class CharT>
22struct testbuf
23 : public std::basic_streambuf<CharT>
24{
25 typedef std::basic_string<CharT> string_type;
26 typedef std::basic_streambuf<CharT> base;
27private:
28 string_type str_;
29public:
30
31 testbuf() {}
32 testbuf(const string_type& str)
33 : str_(str)
34 {
35 base::setg(const_cast<CharT*>(str_.data()),
36 const_cast<CharT*>(str_.data()),
37 const_cast<CharT*>(str_.data()) + str_.size());
38 }
39
40 CharT* eback() const {return base::eback();}
41 CharT* gptr() const {return base::gptr();}
42 CharT* egptr() const {return base::egptr();}
43protected:
44 typename base::pos_type seekoff(typename base::off_type off,
45 std::ios_base::seekdir,
46 std::ios_base::openmode which)
47 {
48 assert(which == std::ios_base::in);
49 ++seekoff_called;
50 return off;
51 }
52};
53
54int main(int, char**)
55{
56 {
57 testbuf<char> sb(" 123456789");
58 std::istream is(&sb);
59 is.seekg(5, std::ios_base::cur);
60 assert(is.good());
61 assert(seekoff_called == 1);
62 is.seekg(-1, std::ios_base::beg);
63 assert(is.fail());
64 assert(seekoff_called == 2);
65 }
66#ifndef TEST_HAS_NO_WIDE_CHARACTERS
67 {
68 testbuf<wchar_t> sb(L" 123456789");
69 std::wistream is(&sb);
70 is.seekg(5, std::ios_base::cur);
71 assert(is.good());
72 assert(seekoff_called == 3);
73 is.seekg(-1, std::ios_base::beg);
74 assert(is.fail());
75 assert(seekoff_called == 4);
76 }
77#endif
78 {
79 testbuf<char> sb(" 123456789");
80 std::istream is(&sb);
81 is.setstate(std::ios_base::eofbit);
82 assert(is.eof());
83 is.seekg(5, std::ios_base::beg);
84 assert(is.good());
85 assert(!is.eof());
86 }
87
88 return 0;
89}
90

source code of libcxx/test/std/input.output/iostream.format/input.streams/istream.unformatted/seekg_off.pass.cpp