1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <istream>
11
12 // template <class charT, class traits = char_traits<charT> >
13 // class basic_istream::sentry;
14
15 // explicit sentry(basic_istream<charT,traits>& is, bool noskipws = false);
16
17 #include <istream>
18 #include <cassert>
19
20 int sync_called = 0;
21
22 template <class CharT>
23 struct testbuf
24 : public std::basic_streambuf<CharT>
25 {
26 typedef std::basic_string<CharT> string_type;
27 typedef std::basic_streambuf<CharT> base;
28 private:
29 string_type str_;
30 public:
31
testbuftestbuf32 testbuf() {}
testbuftestbuf33 testbuf(const string_type& str)
34 : str_(str)
35 {
36 base::setg(const_cast<CharT*>(str_.data()),
37 const_cast<CharT*>(str_.data()),
38 const_cast<CharT*>(str_.data()) + str_.size());
39 }
40
ebacktestbuf41 CharT* eback() const {return base::eback();}
gptrtestbuf42 CharT* gptr() const {return base::gptr();}
egptrtestbuf43 CharT* egptr() const {return base::egptr();}
44 protected:
45
synctestbuf46 int virtual sync()
47 {
48 ++sync_called;
49 return 1;
50 }
51 };
52
main()53 int main()
54 {
55 {
56 std::istream is((testbuf<char>*)0);
57 std::istream::sentry sen(is, true);
58 assert(!(bool)sen);
59 assert(!is.good());
60 assert(is.gcount() == 0);
61 assert(sync_called == 0);
62 }
63 {
64 std::wistream is((testbuf<wchar_t>*)0);
65 std::wistream::sentry sen(is, true);
66 assert(!(bool)sen);
67 assert(!is.good());
68 assert(is.gcount() == 0);
69 assert(sync_called == 0);
70 }
71 {
72 testbuf<char> sb(" 123");
73 std::istream is(&sb);
74 std::istream::sentry sen(is, true);
75 assert((bool)sen);
76 assert(is.good());
77 assert(is.gcount() == 0);
78 assert(sync_called == 0);
79 assert(sb.gptr() == sb.eback());
80 }
81 {
82 testbuf<wchar_t> sb(L" 123");
83 std::wistream is(&sb);
84 std::wistream::sentry sen(is, true);
85 assert((bool)sen);
86 assert(is.good());
87 assert(is.gcount() == 0);
88 assert(sync_called == 0);
89 assert(sb.gptr() == sb.eback());
90 }
91 {
92 testbuf<char> sb(" 123");
93 std::istream is(&sb);
94 std::istream::sentry sen(is);
95 assert((bool)sen);
96 assert(is.good());
97 assert(sync_called == 0);
98 assert(sb.gptr() == sb.eback() + 3);
99 }
100 {
101 testbuf<wchar_t> sb(L" 123");
102 std::wistream is(&sb);
103 std::wistream::sentry sen(is);
104 assert((bool)sen);
105 assert(is.good());
106 assert(sync_called == 0);
107 assert(sb.gptr() == sb.eback() + 3);
108 }
109 {
110 testbuf<char> sb(" ");
111 std::istream is(&sb);
112 std::istream::sentry sen(is);
113 assert(!(bool)sen);
114 assert(is.fail());
115 assert(is.eof());
116 assert(sync_called == 0);
117 assert(sb.gptr() == sb.eback() + 6);
118 }
119 {
120 testbuf<char> sb(" ");
121 std::istream is(&sb);
122 std::istream::sentry sen(is, true);
123 assert((bool)sen);
124 assert(is.good());
125 assert(sync_called == 0);
126 assert(sb.gptr() == sb.eback());
127 }
128 }
129