Debian Codegraph @

libcxx/test/std/input.output/iostream.format/input.streams/istream.unformatted/unget.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 // basic_istream<charT,traits>& unget();
12
13 #include <istream>
14 #include <cassert>
15
16 #include "test_macros.h"
17
18 template <class CharT>
19 struct testbuf
20 : public std::basic_streambuf<CharT>
21 {
22 typedef std::basic_string<CharT> string_type;
23 typedef std::basic_streambuf<CharT> base;
24 private:
25 string_type str_;
26 public:
27
28 testbuf() {}
29 testbuf(const string_type& str)
30 : str_(str)
31 {
32 base::setg(const_cast<CharT*>(str_.data()),
33 const_cast<CharT*>(str_.data()),
34 const_cast<CharT*>(str_.data()) + str_.size());
35 }
36
37 CharT* eback() const {return base::eback();}
38 CharT* gptr() const {return base::gptr();}
39 CharT* egptr() const {return base::egptr();}
40 };
41
42 int main(int, char**)
43 {
44 {
45 testbuf<char> sb(" 123456789");
46 std::istream is(&sb);
47 is.get();
48 is.get();
49 is.get();
50 is.unget();
51 assert(is.good());
52 assert(is.gcount() == 0);
53 is.unget();
54 assert(is.good());
55 assert(is.gcount() == 0);
56 is.unget();
57 assert(is.good());
58 assert(is.gcount() == 0);
59 is.unget();
60 assert(is.bad());
61 assert(is.gcount() == 0);
62 }
63 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
64 {
65 testbuf<wchar_t> sb(L" 123456789");
66 std::wistream is(&sb);
67 is.get();
68 is.get();
69 is.get();
70 is.unget();
71 assert(is.good());
72 assert(is.gcount() == 0);
73 is.unget();
74 assert(is.good());
75 assert(is.gcount() == 0);
76 is.unget();
77 assert(is.good());
78 assert(is.gcount() == 0);
79 is.unget();
80 assert(is.bad());
81 assert(is.gcount() == 0);
82 }
83 #endif
84 #ifndef TEST_HAS_NO_EXCEPTIONS
85 {
86 testbuf<char> sb;
87 std::basic_istream<char> is(&sb);
88 is.exceptions(std::ios_base::badbit);
89 bool threw = false;
90 try {
91 is.unget();
92 } catch (std::ios_base::failure&) {
93 threw = true;
94 }
95 assert(threw);
96 assert( is.bad());
97 assert(!is.eof());
98 assert( is.fail());
99 }
100 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
101 {
102 testbuf<wchar_t> sb;
103 std::basic_istream<wchar_t> is(&sb);
104 is.exceptions(std::ios_base::badbit);
105 bool threw = false;
106 try {
107 is.unget();
108 } catch (std::ios_base::failure&) {
109 threw = true;
110 }
111 assert(threw);
112 assert( is.bad());
113 assert(!is.eof());
114 assert( is.fail());
115 }
116 #endif
117 #endif // TEST_HAS_NO_EXCEPTIONS
118
119 return 0;
120 }