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