Debian Codegraph @

libcxx/test/std/input.output/iostream.format/input.streams/istream.unformatted/get_streambuf.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(basic_streambuf<char_type,traits>& sb);
15
16 #include <istream>
17 #include <cassert>
18
19 #include "test_macros.h"
20
21 template <class CharT>
22 class testbuf
23 : public std::basic_streambuf<CharT>
24 {
25 typedef std::basic_streambuf<CharT> base;
26 std::basic_string<CharT> str_;
27 public:
28 testbuf()
29 {
30 }
31 testbuf(const std::basic_string<CharT>& 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 std::basic_string<CharT> str() const
40 {return std::basic_string<CharT>(base::pbase(), base::pptr());}
41
42 protected:
43
44 virtual typename base::int_type
45 overflow(typename base::int_type ch = base::traits_type::eof())
46 {
47 if (ch != base::traits_type::eof())
48 {
49 int n = static_cast<int>(str_.size());
50 str_.push_back(static_cast<CharT>(ch));
51 str_.resize(str_.capacity());
52 base::setp(const_cast<CharT*>(str_.data()),
53 const_cast<CharT*>(str_.data() + str_.size()));
54 base::pbump(n+1);
55 }
56 return ch;
57 }
58 };
59
60 int main(int, char**)
61 {
62 {
63 testbuf<char> sb("testing\n...");
64 std::istream is(&sb);
65 testbuf<char> sb2;
66 is.get(sb2);
67 assert(sb2.str() == "testing");
68 assert(is.good());
69 assert(is.gcount() == 7);
70 assert(is.get() == '\n');
71 is.get(sb2);
72 assert(sb2.str() == "testing...");
73 assert(is.eof());
74 assert(!is.fail());
75 assert(is.gcount() == 3);
76 }
77 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
78 {
79 testbuf<wchar_t> sb(L"testing\n...");
80 std::wistream is(&sb);
81 testbuf<wchar_t> sb2;
82 is.get(sb2);
83 assert(sb2.str() == L"testing");
84 assert(is.good());
85 assert(is.gcount() == 7);
86 assert(is.get() == L'\n');
87 is.get(sb2);
88 assert(sb2.str() == L"testing...");
89 assert(is.eof());
90 assert(!is.fail());
91 assert(is.gcount() == 3);
92 }
93 #endif
94 #ifndef TEST_HAS_NO_EXCEPTIONS
95 {
96 testbuf<char> sb(" ");
97 std::basic_istream<char> is(&sb);
98 testbuf<char> sb2;
99 is.exceptions(std::ios_base::eofbit);
100 bool threw = false;
101 try {
102 is.get(sb2);
103 } catch (std::ios_base::failure&) {
104 threw = true;
105 }
106 assert(threw);
107 assert(!is.bad());
108 assert( is.eof());
109 assert(!is.fail());
110 }
111 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
112 {
113 testbuf<wchar_t> sb(L" ");
114 std::basic_istream<wchar_t> is(&sb);
115 testbuf<wchar_t> sb2;
116 is.exceptions(std::ios_base::eofbit);
117 bool threw = false;
118 try {
119 is.get(sb2);
120 } catch (std::ios_base::failure&) {
121 threw = true;
122 }
123 assert(threw);
124 assert(!is.bad());
125 assert( is.eof());
126 assert(!is.fail());
127 }
128 #endif
129
130 {
131 testbuf<char> sb;
132 std::basic_istream<char> is(&sb);
133 testbuf<char> sb2;
134 is.exceptions(std::ios_base::eofbit);
135 bool threw = false;
136 try {
137 is.get(sb2);
138 } catch (std::ios_base::failure&) {
139 threw = true;
140 }
141 assert(threw);
142 assert(!is.bad());
143 assert( is.eof());
144 assert( is.fail());
145 }
146 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
147 {
148 testbuf<wchar_t> sb;
149 std::basic_istream<wchar_t> is(&sb);
150 testbuf<wchar_t> sb2;
151 is.exceptions(std::ios_base::eofbit);
152 bool threw = false;
153 try {
154 is.get(sb2);
155 } catch (std::ios_base::failure&) {
156 threw = true;
157 }
158 assert(threw);
159 assert(!is.bad());
160 assert( is.eof());
161 assert( is.fail());
162 }
163 #endif
164 #endif // TEST_HAS_NO_EXCEPTIONS
165
166 return 0;
167 }