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 // template <class charT, class traits = char_traits<charT> >
14 //   class basic_istream;
15 
16 // operator>>(void*& val);
17 
18 #include <istream>
19 #include <cassert>
20 #include "test_macros.h"
21 
22 template <class CharT>
23 struct testbuf
24     : public std::basic_streambuf<CharT>
25 {
26     typedef std::basic_string<CharT> string_type;
27     typedef std::basic_streambuf<CharT> base;
28 private:
29     string_type str_;
30 public:
31 
testbuftestbuf32     testbuf() {}
testbuftestbuf33     testbuf(const string_type& str)
34         : str_(str)
35     {
36         base::setg(const_cast<CharT*>(str_.data()),
37                    const_cast<CharT*>(str_.data()),
38                    const_cast<CharT*>(str_.data()) + str_.size());
39     }
40 
ebacktestbuf41     CharT* eback() const {return base::eback();}
gptrtestbuf42     CharT* gptr() const {return base::gptr();}
egptrtestbuf43     CharT* egptr() const {return base::egptr();}
44 };
45 
main(int,char **)46 int main(int, char**)
47 {
48     {
49         std::istream is((std::streambuf*)0);
50         void* n = 0;
51         is >> n;
52         assert(is.fail());
53     }
54     {
55         testbuf<char> sb("0");
56         std::istream is(&sb);
57         void* n = (void*)1;
58         is >> n;
59         assert(n == 0);
60         assert( is.eof());
61         assert(!is.fail());
62     }
63     {
64         testbuf<char> sb(" 1 ");
65         std::istream is(&sb);
66         void* n = 0;
67         is >> n;
68         assert(n == (void*)1);
69         assert(!is.eof());
70         assert(!is.fail());
71     }
72     {
73         testbuf<wchar_t> sb(L" 1 ");
74         std::wistream is(&sb);
75         void* n = 0;
76         is >> n;
77         assert(n == (void*)1);
78         assert(!is.eof());
79         assert(!is.fail());
80     }
81     {
82         testbuf<char> sb("12345678");
83         std::istream is(&sb);
84         void* n = 0;
85         is >> n;
86         assert(n == (void*)0x12345678);
87         assert( is.eof());
88         assert(!is.fail());
89     }
90     {
91         testbuf<wchar_t> sb(L"12345678");
92         std::wistream is(&sb);
93         void* n = 0;
94         is >> n;
95         assert(n == (void*)0x12345678);
96         assert( is.eof());
97         assert(!is.fail());
98     }
99 #ifndef TEST_HAS_NO_EXCEPTIONS
100     {
101         testbuf<char> sb;
102         std::basic_istream<char> is(&sb);
103         is.exceptions(std::ios_base::failbit);
104 
105         bool threw = false;
106         try {
107             void* n = 0;
108             is >> n;
109         } catch (std::ios_base::failure const&) {
110             threw = true;
111         }
112 
113         assert(!is.bad());
114         assert(is.fail());
115         assert(is.eof());
116         assert(threw);
117     }
118     {
119         testbuf<char> sb;
120         std::basic_istream<char> is(&sb);
121         is.exceptions(std::ios_base::eofbit);
122 
123         bool threw = false;
124         try {
125             void* n = 0;
126             is >> n;
127         } catch (std::ios_base::failure const&) {
128             threw = true;
129         }
130 
131         assert(!is.bad());
132         assert(is.fail());
133         assert(is.eof());
134         assert(threw);
135     }
136 #endif // TEST_HAS_NO_EXCEPTIONS
137 
138     return 0;
139 }
140