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 // int_type get();
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 = is.get();
47         assert(!is.eof());
48         assert(!is.fail());
49         assert(c == ' ');
50         assert(is.gcount() == 1);
51     }
52     {
53         testbuf<char> sb(" abc");
54         std::istream is(&sb);
55         char c = is.get();
56         assert(!is.eof());
57         assert(!is.fail());
58         assert(c == ' ');
59         assert(is.gcount() == 1);
60         c = is.get();
61         assert(!is.eof());
62         assert(!is.fail());
63         assert(c == 'a');
64         assert(is.gcount() == 1);
65         c = is.get();
66         assert(!is.eof());
67         assert(!is.fail());
68         assert(c == 'b');
69         assert(is.gcount() == 1);
70         c = is.get();
71         assert(!is.eof());
72         assert(!is.fail());
73         assert(c == 'c');
74         assert(is.gcount() == 1);
75     }
76     {
77         testbuf<wchar_t> sb(L" abc");
78         std::wistream is(&sb);
79         wchar_t c = is.get();
80         assert(!is.eof());
81         assert(!is.fail());
82         assert(c == L' ');
83         assert(is.gcount() == 1);
84         c = is.get();
85         assert(!is.eof());
86         assert(!is.fail());
87         assert(c == L'a');
88         assert(is.gcount() == 1);
89         c = is.get();
90         assert(!is.eof());
91         assert(!is.fail());
92         assert(c == L'b');
93         assert(is.gcount() == 1);
94         c = is.get();
95         assert(!is.eof());
96         assert(!is.fail());
97         assert(c == L'c');
98         assert(is.gcount() == 1);
99     }
100 }
101