Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / input.output / iostream.format / input.streams / istream.unformatted / sync.pass.cpp
blobec1195d6b32bd98d03035fdd462cf7c721e24f1a
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 //===----------------------------------------------------------------------===//
9 // <istream>
11 // int sync();
13 #include <istream>
14 #include <cassert>
16 #include "test_macros.h"
18 int sync_called = 0;
20 template <class CharT>
21 struct testbuf
22 : public std::basic_streambuf<CharT>
24 typedef std::basic_string<CharT> string_type;
25 typedef std::basic_streambuf<CharT> base;
26 private:
27 string_type str_;
28 public:
30 testbuf() {}
31 testbuf(const string_type& str)
32 : str_(str)
34 base::setg(const_cast<CharT*>(str_.data()),
35 const_cast<CharT*>(str_.data()),
36 const_cast<CharT*>(str_.data()) + str_.size());
39 CharT* eback() const {return base::eback();}
40 CharT* gptr() const {return base::gptr();}
41 CharT* egptr() const {return base::egptr();}
43 protected:
44 int sync()
46 ++sync_called;
47 return 5;
51 #ifndef TEST_HAS_NO_EXCEPTIONS
52 struct testbuf_exception { };
54 template <class CharT>
55 struct throwing_testbuf
56 : public std::basic_streambuf<CharT>
58 typedef std::basic_string<CharT> string_type;
59 typedef std::basic_streambuf<CharT> base;
60 private:
61 string_type str_;
62 public:
64 throwing_testbuf() {}
65 throwing_testbuf(const string_type& str)
66 : str_(str)
68 base::setg(const_cast<CharT*>(str_.data()),
69 const_cast<CharT*>(str_.data()),
70 const_cast<CharT*>(str_.data()) + str_.size());
73 CharT* eback() const {return base::eback();}
74 CharT* gptr() const {return base::gptr();}
75 CharT* egptr() const {return base::egptr();}
77 protected:
78 virtual int sync()
80 throw testbuf_exception();
81 return 5;
84 #endif // TEST_HAS_NO_EXCEPTIONS
86 int main(int, char**)
89 testbuf<char> sb(" 123456789");
90 std::istream is(&sb);
91 assert(is.sync() == 0);
92 assert(sync_called == 1);
94 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
96 testbuf<wchar_t> sb(L" 123456789");
97 std::wistream is(&sb);
98 assert(is.sync() == 0);
99 assert(sync_called == 2);
101 #endif
102 #ifndef TEST_HAS_NO_EXCEPTIONS
104 throwing_testbuf<char> sb(" 123456789");
105 std::basic_istream<char> is(&sb);
106 is.exceptions(std::ios_base::badbit);
107 bool threw = false;
108 try {
109 is.sync();
110 } catch (testbuf_exception const&) {
111 threw = true;
113 assert( is.bad());
114 assert(!is.eof());
115 assert( is.fail());
116 assert(threw);
118 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
120 throwing_testbuf<wchar_t> sb(L" 123456789");
121 std::basic_istream<wchar_t> is(&sb);
122 is.exceptions(std::ios_base::badbit);
123 bool threw = false;
124 try {
125 is.sync();
126 } catch (testbuf_exception const&) {
127 threw = true;
129 assert( is.bad());
130 assert(!is.eof());
131 assert( is.fail());
132 assert(threw);
134 #endif
135 #endif // TEST_HAS_NO_EXCEPTIONS
137 return 0;