Debian Codegraph @

libcxx/test/std/input.output/iostream.format/input.streams/istream.unformatted/get_chart.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 // basic_istream<charT,traits>& get(char_type& c);
15
16 #include <istream>
17 #include <cassert>
18 #include "test_macros.h"
19
20 template <class CharT>
21 struct testbuf
22 : public std::basic_streambuf<CharT>
23 {
24 typedef std::basic_string<CharT> string_type;
25 typedef std::basic_streambuf<CharT> base;
26 private:
27 string_type str_;
28 public:
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
44 int main(int, char**)
45 {
46 {
47 testbuf<char> sb(" ");
48 std::istream is(&sb);
49 char c;
50 is.get(c);
51 assert(!is.eof());
52 assert(!is.fail());
53 assert(c == ' ');
54 assert(is.gcount() == 1);
55 }
56 {
57 testbuf<char> sb(" abc");
58 std::istream is(&sb);
59 char c;
60 is.get(c);
61 assert(!is.eof());
62 assert(!is.fail());
63 assert(c == ' ');
64 assert(is.gcount() == 1);
65 is.get(c);
66 assert(!is.eof());
67 assert(!is.fail());
68 assert(c == 'a');
69 assert(is.gcount() == 1);
70 is.get(c);
71 assert(!is.eof());
72 assert(!is.fail());
73 assert(c == 'b');
74 assert(is.gcount() == 1);
75 is.get(c);
76 assert(!is.eof());
77 assert(!is.fail());
78 assert(c == 'c');
79 assert(is.gcount() == 1);
80 }
81 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
82 {
83 testbuf<wchar_t> sb(L" abc");
84 std::wistream is(&sb);
85 wchar_t c;
86 is.get(c);
87 assert(!is.eof());
88 assert(!is.fail());
89 assert(c == L' ');
90 assert(is.gcount() == 1);
91 is.get(c);
92 assert(!is.eof());
93 assert(!is.fail());
94 assert(c == L'a');
95 assert(is.gcount() == 1);
96 is.get(c);
97 assert(!is.eof());
98 assert(!is.fail());
99 assert(c == L'b');
100 assert(is.gcount() == 1);
101 is.get(c);
102 assert(!is.eof());
103 assert(!is.fail());
104 assert(c == L'c');
105 assert(is.gcount() == 1);
106 }
107 #endif // TEST_HAS_NO_WIDE_CHARACTERS
108 #ifndef TEST_HAS_NO_EXCEPTIONS
109 {
110 testbuf<char> sb("rrrrrrrrr");
111 std::basic_istream<char> is(&sb);
112 is.exceptions(std::ios_base::eofbit);
113
114 bool threw = false;
115 try {
116 while (true) {
117 char c;
118 is.get(c);
119 if (is.eof())
120 break;
121 }
122 } catch (std::ios_base::failure const&) {
123 threw = true;
124 }
125
126 assert(!is.bad());
127 assert( is.fail());
128 assert( is.eof());
129 assert(threw);
130 }
131 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
132 {
133 testbuf<wchar_t> sb(L"rrrrrrrrr");
134 std::basic_istream<wchar_t> is(&sb);
135 is.exceptions(std::ios_base::eofbit);
136
137 bool threw = false;
138 try {
139 while (true) {
140 wchar_t c;
141 is.get(c);
142 if (is.eof())
143 break;
144 }
145 } catch (std::ios_base::failure const&) {
146 threw = true;
147 }
148
149 assert(!is.bad());
150 assert( is.fail());
151 assert( is.eof());
152 assert(threw);
153 }
154 #endif // TEST_HAS_NO_WIDE_CHARACTERS
155 #endif // TEST_HAS_NO_EXCEPTIONS
156
157 return 0;
158 }