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// <istream>
10
11// template<class charT, class traits>
12// basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&& in, charT& c);
13
14#include <istream>
15#include <cassert>
16#include <streambuf>
17
18#include "test_macros.h"
19
20template <class CharT>
21struct testbuf
22 : public std::basic_streambuf<CharT>
23{
24 typedef std::basic_string<CharT> string_type;
25 typedef std::basic_streambuf<CharT> base;
26private:
27 string_type str_;
28public:
29
30 testbuf() {}
31 testbuf(const string_type& str)
32 : str_(str)
33 {
34 base::setg(const_cast<CharT*>(str_.data()),
35 const_cast<CharT*>(str_.data()),
36 const_cast<CharT*>(str_.data()) + str_.size());
37 }
38
39 CharT* eback() const {return base::eback();}
40 CharT* gptr() const {return base::gptr();}
41 CharT* egptr() const {return base::egptr();}
42};
43
44int main(int, char**)
45{
46 {
47 testbuf<char> sb(" ");
48 std::istream is(&sb);
49 char c = 'z';
50 is >> c;
51 assert( is.eof());
52 assert( is.fail());
53 assert(c == 'z');
54 }
55 {
56 testbuf<char> sb(" abcdefghijk ");
57 std::istream is(&sb);
58 char c;
59 is >> c;
60 assert(!is.eof());
61 assert(!is.fail());
62 assert(c == 'a');
63 is >> c;
64 assert(!is.eof());
65 assert(!is.fail());
66 assert(c == 'b');
67 is >> c;
68 assert(!is.eof());
69 assert(!is.fail());
70 assert(c == 'c');
71 }
72#ifndef TEST_HAS_NO_WIDE_CHARACTERS
73 {
74 testbuf<wchar_t> sb(L" abc");
75 std::wistream is(&sb);
76 wchar_t c;
77 is >> c;
78 assert(!is.eof());
79 assert(!is.fail());
80 assert(c == L'a');
81 is >> c;
82 assert(!is.eof());
83 assert(!is.fail());
84 assert(c == L'b');
85 is >> c;
86 assert(!is.eof());
87 assert(!is.fail());
88 assert(c == L'c');
89 }
90#endif
91#ifndef TEST_HAS_NO_EXCEPTIONS
92 {
93 testbuf<char> sb;
94 std::basic_istream<char> is(&sb);
95 is.exceptions(std::ios_base::failbit);
96
97 bool threw = false;
98 try {
99 char n = 0;
100 is >> n;
101 } catch (std::ios_base::failure const&) {
102 threw = true;
103 }
104
105 assert(!is.bad());
106 assert(is.fail());
107 assert(is.eof());
108 assert(threw);
109 }
110#ifndef TEST_HAS_NO_WIDE_CHARACTERS
111 {
112 testbuf<wchar_t> sb;
113 std::basic_istream<wchar_t> is(&sb);
114 is.exceptions(std::ios_base::failbit);
115
116 bool threw = false;
117 try {
118 wchar_t n = 0;
119 is >> n;
120 } catch (std::ios_base::failure const&) {
121 threw = true;
122 }
123
124 assert(!is.bad());
125 assert(is.fail());
126 assert(is.eof());
127 assert(threw);
128 }
129#endif
130 {
131 testbuf<char> sb;
132 std::basic_istream<char> is(&sb);
133 is.exceptions(std::ios_base::eofbit);
134
135 bool threw = false;
136 try {
137 char n = 0;
138 is >> n;
139 } catch (std::ios_base::failure const&) {
140 threw = true;
141 }
142
143 assert(!is.bad());
144 assert(is.fail());
145 assert(is.eof());
146 assert(threw);
147 }
148#ifndef TEST_HAS_NO_WIDE_CHARACTERS
149 {
150 testbuf<wchar_t> sb;
151 std::basic_istream<wchar_t> is(&sb);
152 is.exceptions(std::ios_base::eofbit);
153
154 bool threw = false;
155 try {
156 wchar_t n = 0;
157 is >> n;
158 } catch (std::ios_base::failure const&) {
159 threw = true;
160 }
161
162 assert(!is.bad());
163 assert(is.fail());
164 assert(is.eof());
165 assert(threw);
166 }
167#endif
168#endif // TEST_HAS_NO_EXCEPTIONS
169
170 return 0;
171}
172

source code of libcxx/test/std/input.output/iostream.format/input.streams/istream.formatted/istream_extractors/chart.pass.cpp