Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / input.output / iostream.format / input.streams / istream.unformatted / seekg_off.pass.cpp
blob8b43c31e9f2ba612229c4acb8d0aa3f4cac7b033
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 //===----------------------------------------------------------------------===//
9 // XFAIL: stdlib=apple-libc++ && target={{.+}}-apple-macosx10.{{9|10|11}}
11 // <istream>
13 // basic_istream<charT,traits>& seekg(off_type off, ios_base::seekdir dir);
15 #include <istream>
16 #include <cassert>
18 #include "test_macros.h"
20 int seekoff_called = 0;
22 template <class CharT>
23 struct testbuf
24 : public std::basic_streambuf<CharT>
26 typedef std::basic_string<CharT> string_type;
27 typedef std::basic_streambuf<CharT> base;
28 private:
29 string_type str_;
30 public:
32 testbuf() {}
33 testbuf(const string_type& str)
34 : str_(str)
36 base::setg(const_cast<CharT*>(str_.data()),
37 const_cast<CharT*>(str_.data()),
38 const_cast<CharT*>(str_.data()) + str_.size());
41 CharT* eback() const {return base::eback();}
42 CharT* gptr() const {return base::gptr();}
43 CharT* egptr() const {return base::egptr();}
44 protected:
45 typename base::pos_type seekoff(typename base::off_type off,
46 std::ios_base::seekdir,
47 std::ios_base::openmode which)
49 assert(which == std::ios_base::in);
50 ++seekoff_called;
51 return off;
55 int main(int, char**)
58 testbuf<char> sb(" 123456789");
59 std::istream is(&sb);
60 is.seekg(5, std::ios_base::cur);
61 assert(is.good());
62 assert(seekoff_called == 1);
63 is.seekg(-1, std::ios_base::beg);
64 assert(is.fail());
65 assert(seekoff_called == 2);
67 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
69 testbuf<wchar_t> sb(L" 123456789");
70 std::wistream is(&sb);
71 is.seekg(5, std::ios_base::cur);
72 assert(is.good());
73 assert(seekoff_called == 3);
74 is.seekg(-1, std::ios_base::beg);
75 assert(is.fail());
76 assert(seekoff_called == 4);
78 #endif
80 testbuf<char> sb(" 123456789");
81 std::istream is(&sb);
82 is.setstate(std::ios_base::eofbit);
83 assert(is.eof());
84 is.seekg(5, std::ios_base::beg);
85 assert(is.good());
86 assert(!is.eof());
89 return 0;