Debian Codegraph @

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