Debian Codegraph @

libcxx/test/std/input.output/iostream.format/input.streams/istream.formatted/istream.formatted.arithmetic/float.pass.cpp

raw
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>>(float& val);
18
19 #include <istream>
20 #include <cassert>
21 #include "test_macros.h"
22
23 template <class CharT>
24 struct testbuf
25 : public std::basic_streambuf<CharT>
26 {
27 typedef std::basic_string<CharT> string_type;
28 typedef std::basic_streambuf<CharT> base;
29 private:
30 string_type str_;
31 public:
32
33 testbuf() {}
34 testbuf(const string_type& str)
35 : str_(str)
36 {
37 base::setg(const_cast<CharT*>(str_.data()),
38 const_cast<CharT*>(str_.data()),
39 const_cast<CharT*>(str_.data()) + str_.size());
40 }
41
42 CharT* eback() const {return base::eback();}
43 CharT* gptr() const {return base::gptr();}
44 CharT* egptr() const {return base::egptr();}
45 };
46
47 int main(int, char**)
48 {
49 {
50 std::istream is((std::streambuf*)0);
51 float n = 0;
52 is >> n;
53 assert(is.fail());
54 }
55 {
56 testbuf<char> sb("0");
57 std::istream is(&sb);
58 float n = 10;
59 is >> n;
60 assert(n == 0);
61 assert( is.eof());
62 assert(!is.fail());
63 }
64 {
65 testbuf<char> sb(" 123 ");
66 std::istream is(&sb);
67 float n = 10;
68 is >> n;
69 assert(n == 123);
70 assert(!is.eof());
71 assert(!is.fail());
72 }
73 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
74 {
75 testbuf<wchar_t> sb(L" -123.5 ");
76 std::wistream is(&sb);
77 float n = 10;
78 is >> n;
79 assert(n == -123.5);
80 assert(!is.eof());
81 assert(!is.fail());
82 }
83 #endif
84 #ifndef TEST_HAS_NO_EXCEPTIONS
85 {
86 testbuf<char> sb;
87 std::basic_istream<char> is(&sb);
88 is.exceptions(std::ios_base::failbit);
89
90 bool threw = false;
91 try {
92 float n = 0;
93 is >> n;
94 } catch (std::ios_base::failure const&) {
95 threw = true;
96 }
97
98 assert(!is.bad());
99 assert(is.fail());
100 assert(is.eof());
101 assert(threw);
102 }
103 {
104 testbuf<char> sb;
105 std::basic_istream<char> is(&sb);
106 is.exceptions(std::ios_base::eofbit);
107
108 bool threw = false;
109 try {
110 float n = 0;
111 is >> n;
112 } catch (std::ios_base::failure const&) {
113 threw = true;
114 }
115
116 assert(!is.bad());
117 assert(is.fail());
118 assert(is.eof());
119 assert(threw);
120 }
121 #endif // TEST_HAS_NO_EXCEPTIONS
122
123 return 0;
124 }