Debian Codegraph @

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

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