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>>(bool& 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         bool n = 0;
54         is >> n;
55         assert(is.fail());
56     }
57     {
58         testbuf<char> sb("0");
59         std::istream is(&sb);
60         bool n = true;
61         is >> n;
62         assert(n == false);
63         assert( is.eof());
64         assert(!is.fail());
65     }
66     {
67         testbuf<char> sb(" 1 ");
68         std::istream is(&sb);
69         bool n = 0;
70         is >> n;
71         assert(n == true);
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         bool n = 0;
80         is >> n;
81         assert(n == true);
82         assert(!is.eof());
83         assert(!is.fail());
84     }
85 #endif
86 #ifndef TEST_HAS_NO_EXCEPTIONS
87     {
88         testbuf<char> sb;
89         std::basic_istream<char> is(&sb);
90         is.exceptions(std::ios_base::failbit);
91 
92         bool threw = false;
93         try {
94             bool n = 0;
95             is >> n;
96         } catch (std::ios_base::failure const&) {
97             threw = true;
98         }
99 
100         assert(!is.bad());
101         assert(is.fail());
102         assert(is.eof());
103         assert(threw);
104     }
105     {
106         testbuf<char> sb;
107         std::basic_istream<char> is(&sb);
108         is.exceptions(std::ios_base::eofbit);
109 
110         bool threw = false;
111         try {
112             bool 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 #endif // TEST_HAS_NO_EXCEPTIONS
124 
125     return 0;
126 }
127