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 // basic_istream<charT,traits>& get(char_type& c);
13 
14 #include <istream>
15 #include <cassert>
16 
17 template <class CharT>
18 struct testbuf
19     : public std::basic_streambuf<CharT>
20 {
21     typedef std::basic_string<CharT> string_type;
22     typedef std::basic_streambuf<CharT> base;
23 private:
24     string_type str_;
25 public:
26 
testbuftestbuf27     testbuf() {}
testbuftestbuf28     testbuf(const string_type& str)
29         : str_(str)
30     {
31         base::setg(const_cast<CharT*>(str_.data()),
32                    const_cast<CharT*>(str_.data()),
33                    const_cast<CharT*>(str_.data()) + str_.size());
34     }
35 
ebacktestbuf36     CharT* eback() const {return base::eback();}
gptrtestbuf37     CharT* gptr() const {return base::gptr();}
egptrtestbuf38     CharT* egptr() const {return base::egptr();}
39 };
40 
main()41 int main()
42 {
43     {
44         testbuf<char> sb("          ");
45         std::istream is(&sb);
46         char c;
47         is.get(c);
48         assert(!is.eof());
49         assert(!is.fail());
50         assert(c == ' ');
51         assert(is.gcount() == 1);
52     }
53     {
54         testbuf<char> sb(" abc");
55         std::istream is(&sb);
56         char c;
57         is.get(c);
58         assert(!is.eof());
59         assert(!is.fail());
60         assert(c == ' ');
61         assert(is.gcount() == 1);
62         is.get(c);
63         assert(!is.eof());
64         assert(!is.fail());
65         assert(c == 'a');
66         assert(is.gcount() == 1);
67         is.get(c);
68         assert(!is.eof());
69         assert(!is.fail());
70         assert(c == 'b');
71         assert(is.gcount() == 1);
72         is.get(c);
73         assert(!is.eof());
74         assert(!is.fail());
75         assert(c == 'c');
76         assert(is.gcount() == 1);
77     }
78     {
79         testbuf<wchar_t> sb(L" abc");
80         std::wistream is(&sb);
81         wchar_t c;
82         is.get(c);
83         assert(!is.eof());
84         assert(!is.fail());
85         assert(c == L' ');
86         assert(is.gcount() == 1);
87         is.get(c);
88         assert(!is.eof());
89         assert(!is.fail());
90         assert(c == L'a');
91         assert(is.gcount() == 1);
92         is.get(c);
93         assert(!is.eof());
94         assert(!is.fail());
95         assert(c == L'b');
96         assert(is.gcount() == 1);
97         is.get(c);
98         assert(!is.eof());
99         assert(!is.fail());
100         assert(c == L'c');
101         assert(is.gcount() == 1);
102     }
103 }
104