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>& putback(char_type c);
12 
13 #include <istream>
14 #include <cassert>
15 
16 template <class CharT>
17 struct testbuf
18     : public std::basic_streambuf<CharT>
19 {
20     typedef std::basic_string<CharT> string_type;
21     typedef std::basic_streambuf<CharT> base;
22 private:
23     string_type str_;
24 public:
25 
26     testbuf() {}
27     testbuf(const string_type& str)
28         : str_(str)
29     {
30         base::setg(const_cast<CharT*>(str_.data()),
31                    const_cast<CharT*>(str_.data()),
32                    const_cast<CharT*>(str_.data()) + str_.size());
33     }
34 
35     CharT* eback() const {return base::eback();}
36     CharT* gptr() const {return base::gptr();}
37     CharT* egptr() const {return base::egptr();}
38 };
39 
40 int main(int, char**)
41 {
42     {
43         testbuf<char> sb(" 123456789");
44         std::istream is(&sb);
45         is.get();
46         is.get();
47         is.get();
48         is.putback('a');
49         assert(is.bad());
50         assert(is.gcount() == 0);
51         is.clear();
52         is.putback('2');
53         assert(is.good());
54         assert(is.gcount() == 0);
55         is.putback('1');
56         assert(is.good());
57         assert(is.gcount() == 0);
58         is.putback(' ');
59         assert(is.good());
60         assert(is.gcount() == 0);
61         is.putback(' ');
62         assert(is.bad());
63         assert(is.gcount() == 0);
64     }
65     {
66         testbuf<wchar_t> sb(L" 123456789");
67         std::wistream is(&sb);
68         is.get();
69         is.get();
70         is.get();
71         is.putback(L'a');
72         assert(is.bad());
73         assert(is.gcount() == 0);
74         is.clear();
75         is.putback(L'2');
76         assert(is.good());
77         assert(is.gcount() == 0);
78         is.putback(L'1');
79         assert(is.good());
80         assert(is.gcount() == 0);
81         is.putback(L' ');
82         assert(is.good());
83         assert(is.gcount() == 0);
84         is.putback(L' ');
85         assert(is.bad());
86         assert(is.gcount() == 0);
87     }
88 
89   return 0;
90 }
91