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 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
73     {
74         testbuf<wchar_t> sb(L" 1 ");
75         std::wistream is(&sb);
76         void* n = 0;
77         is >> n;
78         assert(n == (void*)1);
79         assert(!is.eof());
80         assert(!is.fail());
81     }
82 #endif
83     {
84         testbuf<char> sb("12345678");
85         std::istream is(&sb);
86         void* n = 0;
87         is >> n;
88         assert(n == (void*)0x12345678);
89         assert( is.eof());
90         assert(!is.fail());
91     }
92 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
93     {
94         testbuf<wchar_t> sb(L"12345678");
95         std::wistream is(&sb);
96         void* n = 0;
97         is >> n;
98         assert(n == (void*)0x12345678);
99         assert( is.eof());
100         assert(!is.fail());
101     }
102 #endif
103 #ifndef TEST_HAS_NO_EXCEPTIONS
104     {
105         testbuf<char> sb;
106         std::basic_istream<char> is(&sb);
107         is.exceptions(std::ios_base::failbit);
108 
109         bool threw = false;
110         try {
111             void* n = 0;
112             is >> n;
113         } catch (std::ios_base::failure const&) {
114             threw = true;
115         }
116 
117         assert(!is.bad());
118         assert(is.fail());
119         assert(is.eof());
120         assert(threw);
121     }
122     {
123         testbuf<char> sb;
124         std::basic_istream<char> is(&sb);
125         is.exceptions(std::ios_base::eofbit);
126 
127         bool threw = false;
128         try {
129             void* n = 0;
130             is >> n;
131         } catch (std::ios_base::failure const&) {
132             threw = true;
133         }
134 
135         assert(!is.bad());
136         assert(is.fail());
137         assert(is.eof());
138         assert(threw);
139     }
140 #endif // TEST_HAS_NO_EXCEPTIONS
141 
142     return 0;
143 }
144