Debian Codegraph @

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