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 && x86_64-apple-macosx10.14
10 // XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.13
11 // XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.12
12 // XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.11
13 // XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.10
14 // XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.9
15 
16 // <istream>
17 
18 // int_type get();
19 
20 #include <istream>
21 #include <cassert>
22 #include "test_macros.h"
23 
24 template <class CharT>
25 struct testbuf
26     : public std::basic_streambuf<CharT>
27 {
28     typedef std::basic_string<CharT> string_type;
29     typedef std::basic_streambuf<CharT> base;
30 private:
31     string_type str_;
32 public:
33 
34     testbuf() {}
35     testbuf(const string_type& str)
36         : str_(str)
37     {
38         base::setg(const_cast<CharT*>(str_.data()),
39                    const_cast<CharT*>(str_.data()),
40                    const_cast<CharT*>(str_.data()) + str_.size());
41     }
42 
43     CharT* eback() const {return base::eback();}
44     CharT* gptr() const {return base::gptr();}
45     CharT* egptr() const {return base::egptr();}
46 };
47 
48 int main(int, char**)
49 {
50     {
51         testbuf<char> sb("          ");
52         std::istream is(&sb);
53         char c = static_cast<char>(is.get());
54         assert(!is.eof());
55         assert(!is.fail());
56         assert(c == ' ');
57         assert(is.gcount() == 1);
58     }
59     {
60         testbuf<char> sb(" abc");
61         std::istream is(&sb);
62         char c = static_cast<char>(is.get());
63         assert(!is.eof());
64         assert(!is.fail());
65         assert(c == ' ');
66         assert(is.gcount() == 1);
67         c = static_cast<char>(is.get());
68         assert(!is.eof());
69         assert(!is.fail());
70         assert(c == 'a');
71         assert(is.gcount() == 1);
72         c = static_cast<char>(is.get());
73         assert(!is.eof());
74         assert(!is.fail());
75         assert(c == 'b');
76         assert(is.gcount() == 1);
77         c = static_cast<char>(is.get());
78         assert(!is.eof());
79         assert(!is.fail());
80         assert(c == 'c');
81         assert(is.gcount() == 1);
82     }
83     {
84         testbuf<wchar_t> sb(L" abc");
85         std::wistream is(&sb);
86         wchar_t c = is.get();
87         assert(!is.eof());
88         assert(!is.fail());
89         assert(c == L' ');
90         assert(is.gcount() == 1);
91         c = is.get();
92         assert(!is.eof());
93         assert(!is.fail());
94         assert(c == L'a');
95         assert(is.gcount() == 1);
96         c = is.get();
97         assert(!is.eof());
98         assert(!is.fail());
99         assert(c == L'b');
100         assert(is.gcount() == 1);
101         c = is.get();
102         assert(!is.eof());
103         assert(!is.fail());
104         assert(c == L'c');
105         assert(is.gcount() == 1);
106     }
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                 is.get();
117                 if (is.eof())
118                     break;
119             }
120         } catch (std::ios_base::failure const&) {
121             threw = true;
122         }
123 
124         assert(!is.bad());
125         assert( is.fail());
126         assert( is.eof());
127         assert(threw);
128     }
129     {
130         testbuf<wchar_t> sb(L"rrrrrrrrr");
131         std::basic_istream<wchar_t> is(&sb);
132         is.exceptions(std::ios_base::eofbit);
133 
134         bool threw = false;
135         try {
136             while (true) {
137                 is.get();
138                 if (is.eof())
139                     break;
140             }
141         } catch (std::ios_base::failure const&) {
142             threw = true;
143         }
144 
145         assert(!is.bad());
146         assert( is.fail());
147         assert( is.eof());
148         assert(threw);
149     }
150 #endif
151 
152     return 0;
153 }
154