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