1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12|13|14}}
10 
11 // <istream>
12 
13 // basic_istream<charT,traits>& get(char_type& c);
14 
15 #include <istream>
16 #include <cassert>
17 #include "test_macros.h"
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 
testbuftestbuf29     testbuf() {}
testbuftestbuf30     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 
ebacktestbuf38     CharT* eback() const {return base::eback();}
gptrtestbuf39     CharT* gptr() const {return base::gptr();}
egptrtestbuf40     CharT* egptr() const {return base::egptr();}
41 };
42 
main(int,char **)43 int main(int, char**)
44 {
45     {
46         testbuf<char> sb("          ");
47         std::istream is(&sb);
48         char c;
49         is.get(c);
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;
59         is.get(c);
60         assert(!is.eof());
61         assert(!is.fail());
62         assert(c == ' ');
63         assert(is.gcount() == 1);
64         is.get(c);
65         assert(!is.eof());
66         assert(!is.fail());
67         assert(c == 'a');
68         assert(is.gcount() == 1);
69         is.get(c);
70         assert(!is.eof());
71         assert(!is.fail());
72         assert(c == 'b');
73         assert(is.gcount() == 1);
74         is.get(c);
75         assert(!is.eof());
76         assert(!is.fail());
77         assert(c == 'c');
78         assert(is.gcount() == 1);
79     }
80 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
81     {
82         testbuf<wchar_t> sb(L" abc");
83         std::wistream is(&sb);
84         wchar_t c;
85         is.get(c);
86         assert(!is.eof());
87         assert(!is.fail());
88         assert(c == L' ');
89         assert(is.gcount() == 1);
90         is.get(c);
91         assert(!is.eof());
92         assert(!is.fail());
93         assert(c == L'a');
94         assert(is.gcount() == 1);
95         is.get(c);
96         assert(!is.eof());
97         assert(!is.fail());
98         assert(c == L'b');
99         assert(is.gcount() == 1);
100         is.get(c);
101         assert(!is.eof());
102         assert(!is.fail());
103         assert(c == L'c');
104         assert(is.gcount() == 1);
105     }
106 #endif // TEST_HAS_NO_WIDE_CHARACTERS
107 #ifndef TEST_HAS_NO_EXCEPTIONS
108     {
109         testbuf<char> sb("rrrrrrrrr");
110         std::basic_istream<char> is(&sb);
111         is.exceptions(std::ios_base::eofbit);
112 
113         bool threw = false;
114         try {
115             while (true) {
116                 char c;
117                 is.get(c);
118                 if (is.eof())
119                     break;
120             }
121         } catch (std::ios_base::failure const&) {
122             threw = true;
123         }
124 
125         assert(!is.bad());
126         assert( is.fail());
127         assert( is.eof());
128         assert(threw);
129     }
130 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
131     {
132         testbuf<wchar_t> sb(L"rrrrrrrrr");
133         std::basic_istream<wchar_t> is(&sb);
134         is.exceptions(std::ios_base::eofbit);
135 
136         bool threw = false;
137         try {
138             while (true) {
139                 wchar_t c;
140                 is.get(c);
141                 if (is.eof())
142                     break;
143             }
144         } catch (std::ios_base::failure const&) {
145             threw = true;
146         }
147 
148         assert(!is.bad());
149         assert( is.fail());
150         assert( is.eof());
151         assert(threw);
152     }
153 #endif // TEST_HAS_NO_WIDE_CHARACTERS
154 #endif // TEST_HAS_NO_EXCEPTIONS
155 
156     return 0;
157 }
158