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