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>>(float& 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         float n = 0;
51         is >> n;
52         assert(is.fail());
53     }
54     {
55         testbuf<char> sb("0");
56         std::istream is(&sb);
57         float n = 10;
58         is >> n;
59         assert(n == 0);
60         assert( is.eof());
61         assert(!is.fail());
62     }
63     {
64         testbuf<char> sb(" 123 ");
65         std::istream is(&sb);
66         float n = 10;
67         is >> n;
68         assert(n == 123);
69         assert(!is.eof());
70         assert(!is.fail());
71     }
72 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
73     {
74         testbuf<wchar_t> sb(L" -123.5 ");
75         std::wistream is(&sb);
76         float n = 10;
77         is >> n;
78         assert(n == -123.5);
79         assert(!is.eof());
80         assert(!is.fail());
81     }
82 #endif
83 #ifndef TEST_HAS_NO_EXCEPTIONS
84     {
85         testbuf<char> sb;
86         std::basic_istream<char> is(&sb);
87         is.exceptions(std::ios_base::failbit);
88 
89         bool threw = false;
90         try {
91             float n = 0;
92             is >> n;
93         } catch (std::ios_base::failure const&) {
94             threw = true;
95         }
96 
97         assert(!is.bad());
98         assert(is.fail());
99         assert(is.eof());
100         assert(threw);
101     }
102     {
103         testbuf<char> sb;
104         std::basic_istream<char> is(&sb);
105         is.exceptions(std::ios_base::eofbit);
106 
107         bool threw = false;
108         try {
109             float n = 0;
110             is >> n;
111         } catch (std::ios_base::failure const&) {
112             threw = true;
113         }
114 
115         assert(!is.bad());
116         assert(is.fail());
117         assert(is.eof());
118         assert(threw);
119     }
120 #endif // TEST_HAS_NO_EXCEPTIONS
121 
122     return 0;
123 }
124