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 #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 
testbuftestbuf28     testbuf() {}
testbuftestbuf29     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 
ebacktestbuf37     CharT* eback() const {return base::eback();}
gptrtestbuf38     CharT* gptr() const {return base::gptr();}
egptrtestbuf39     CharT* egptr() const {return base::egptr();}
40 };
41 
main(int,char **)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.putback('a');
51         assert(is.bad());
52         assert(is.gcount() == 0);
53         is.clear();
54         is.putback('2');
55         assert(is.good());
56         assert(is.gcount() == 0);
57         is.putback('1');
58         assert(is.good());
59         assert(is.gcount() == 0);
60         is.putback(' ');
61         assert(is.good());
62         assert(is.gcount() == 0);
63         is.putback(' ');
64         assert(is.bad());
65         assert(is.gcount() == 0);
66     }
67     {
68         testbuf<wchar_t> sb(L" 123456789");
69         std::wistream is(&sb);
70         is.get();
71         is.get();
72         is.get();
73         is.putback(L'a');
74         assert(is.bad());
75         assert(is.gcount() == 0);
76         is.clear();
77         is.putback(L'2');
78         assert(is.good());
79         assert(is.gcount() == 0);
80         is.putback(L'1');
81         assert(is.good());
82         assert(is.gcount() == 0);
83         is.putback(L' ');
84         assert(is.good());
85         assert(is.gcount() == 0);
86         is.putback(L' ');
87         assert(is.bad());
88         assert(is.gcount() == 0);
89     }
90 #ifndef TEST_HAS_NO_EXCEPTIONS
91     {
92         testbuf<char> sb;
93         std::basic_istream<char> is(&sb);
94         is.exceptions(std::ios_base::badbit);
95         bool threw = false;
96         try {
97             is.putback('x');
98         } catch (std::ios_base::failure&) {
99             threw = true;
100         }
101         assert(threw);
102         assert( is.bad());
103         assert(!is.eof());
104         assert( is.fail());
105     }
106     {
107         testbuf<wchar_t> sb;
108         std::basic_istream<wchar_t> is(&sb);
109         is.exceptions(std::ios_base::badbit);
110         bool threw = false;
111         try {
112             is.putback(L'x');
113         } catch (std::ios_base::failure&) {
114             threw = true;
115         }
116         assert(threw);
117         assert( is.bad());
118         assert(!is.eof());
119         assert( is.fail());
120     }
121 #endif
122 
123     return 0;
124 }
125