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