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

source code of libcxx/test/std/input.output/iostream.format/input.streams/istream.unformatted/sync.pass.cpp