Debian Codegraph @

libcxx/test/std/input.output/iostream.format/input.streams/istream.unformatted/sync.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 // <istream>
10
11 // int sync();
12
13 // The fix for bug 51497 and bug 51499 require and updated dylib due to
14 // explicit instantiations. That means Apple backdeployment targets remain
15 // broken.
16 // XFAIL: using-built-library-before-llvm-19
17
18 #include <istream>
19 #include <cassert>
20
21 #include "test_macros.h"
22
23 int sync_called = 0;
24
25 template <class CharT>
26 struct testbuf
27 : public std::basic_streambuf<CharT>
28 {
29 typedef std::basic_string<CharT> string_type;
30 typedef std::basic_streambuf<CharT> base;
31 private:
32 string_type str_;
33 public:
34
35 testbuf() {}
36 testbuf(const string_type& str)
37 : str_(str)
38 {
39 base::setg(const_cast<CharT*>(str_.data()),
40 const_cast<CharT*>(str_.data()),
41 const_cast<CharT*>(str_.data()) + str_.size());
42 }
43
44 CharT* eback() const {return base::eback();}
45 CharT* gptr() const {return base::gptr();}
46 CharT* egptr() const {return base::egptr();}
47
48 protected:
49 int sync()
50 {
51 ++sync_called;
52 return 5;
53 }
54 };
55
56 template <class CharT>
57 struct testbuf_pubsync_error
58 : public std::basic_streambuf<CharT>
59 {
60 public:
61
62 testbuf_pubsync_error() {}
63 protected:
64 virtual int sync() { return -1; }
65 };
66
67
68 #ifndef TEST_HAS_NO_EXCEPTIONS
69 struct testbuf_exception { };
70
71 template <class CharT>
72 struct throwing_testbuf
73 : public std::basic_streambuf<CharT>
74 {
75 typedef std::basic_string<CharT> string_type;
76 typedef std::basic_streambuf<CharT> base;
77 private:
78 string_type str_;
79 public:
80
81 throwing_testbuf() {}
82 throwing_testbuf(const string_type& str)
83 : str_(str)
84 {
85 base::setg(const_cast<CharT*>(str_.data()),
86 const_cast<CharT*>(str_.data()),
87 const_cast<CharT*>(str_.data()) + str_.size());
88 }
89
90 CharT* eback() const {return base::eback();}
91 CharT* gptr() const {return base::gptr();}
92 CharT* egptr() const {return base::egptr();}
93
94 protected:
95 virtual int sync()
96 {
97 throw testbuf_exception();
98 return 5;
99 }
100 };
101 #endif // TEST_HAS_NO_EXCEPTIONS
102
103 int main(int, char**)
104 {
105 {
106 std::istream is(nullptr);
107 assert(is.sync() == -1);
108 }
109 {
110 testbuf<char> sb(" 123456789");
111 std::istream is(&sb);
112 assert(is.sync() == 0);
113 assert(sync_called == 1);
114 }
115 {
116 testbuf_pubsync_error<char> sb;
117 std::istream is(&sb);
118 is.exceptions(std::ios_base::failbit | std::ios_base::eofbit);
119 assert(is.sync() == -1);
120 assert( is.bad());
121 assert(!is.eof());
122 assert( is.fail());
123 }
124 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
125 {
126 std::wistream is(nullptr);
127 assert(is.sync() == -1);
128 }
129 {
130 testbuf<wchar_t> sb(L" 123456789");
131 std::wistream is(&sb);
132 assert(is.sync() == 0);
133 assert(sync_called == 2);
134 }
135 {
136 testbuf_pubsync_error<wchar_t> sb;
137 std::wistream is(&sb);
138 is.exceptions(std::ios_base::failbit | std::ios_base::eofbit);
139 assert(is.sync() == -1);
140 assert( is.bad());
141 assert(!is.eof());
142 assert( is.fail());
143 }
144 #endif
145 #ifndef TEST_HAS_NO_EXCEPTIONS
146 {
147 testbuf_pubsync_error<char> sb;
148 std::istream is(&sb);
149 is.exceptions(std::ios_base::badbit);
150 bool threw = false;
151 try {
152 is.sync();
153 } catch (std::ios_base::failure const&) {
154 threw = true;
155 }
156 assert( is.bad());
157 assert(!is.eof());
158 assert( is.fail());
159 assert(threw);
160 }
161 {
162 throwing_testbuf<char> sb(" 123456789");
163 std::basic_istream<char> is(&sb);
164 is.exceptions(std::ios_base::badbit);
165 bool threw = false;
166 try {
167 is.sync();
168 } catch (testbuf_exception const&) {
169 threw = true;
170 }
171 assert( is.bad());
172 assert(!is.eof());
173 assert( is.fail());
174 assert(threw);
175 }
176 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
177 {
178 testbuf_pubsync_error<wchar_t> sb;
179 std::wistream is(&sb);
180 is.exceptions(std::ios_base::badbit);
181 bool threw = false;
182 try {
183 is.sync();
184 } catch (std::ios_base::failure const&) {
185 threw = true;
186 }
187 assert( is.bad());
188 assert(!is.eof());
189 assert( is.fail());
190 assert(threw);
191 }
192 {
193 throwing_testbuf<wchar_t> sb(L" 123456789");
194 std::basic_istream<wchar_t> is(&sb);
195 is.exceptions(std::ios_base::badbit);
196 bool threw = false;
197 try {
198 is.sync();
199 } catch (testbuf_exception const&) {
200 threw = true;
201 }
202 assert( is.bad());
203 assert(!is.eof());
204 assert( is.fail());
205 assert(threw);
206 }
207 #endif // TEST_HAS_NO_WIDE_CHARACTERS
208 #endif // TEST_HAS_NO_EXCEPTIONS
209
210 return 0;
211 }