1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <sstream>
11 
12 // template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
13 // class basic_stringbuf
14 
15 // int_type pbackfail(int_type c = traits::eof());
16 
17 #include <sstream>
18 #include <cassert>
19 
20 template <class CharT>
21 struct testbuf
22     : public std::basic_stringbuf<CharT>
23 {
24     typedef std::basic_stringbuf<CharT> base;
testbuftestbuf25     explicit testbuf(const std::basic_string<CharT>& str,
26                      std::ios_base::openmode which = std::ios_base::in | std::ios_base::out)
27         : base(str, which) {}
28 
29     typename base::int_type
pbackfailtestbuf30         pbackfail(typename base::int_type c = base::type_traits::eof())
31         {return base::pbackfail(c);}
32 
pbumptestbuf33     void pbump(int n) {base::pbump(n);}
34 };
35 
main()36 int main()
37 {
38     {
39         testbuf<char> sb("123", std::ios_base::in);
40         assert(sb.sgetc() == '1');
41         assert(sb.snextc() == '2');
42         assert(sb.snextc() == '3');
43         assert(sb.sgetc() == '3');
44         assert(sb.snextc() == std::char_traits<char>::eof());
45         assert(sb.pbackfail('3') == '3');
46         assert(sb.pbackfail('3') == std::char_traits<char>::eof());
47         assert(sb.pbackfail('2') == '2');
48         assert(sb.pbackfail(std::char_traits<char>::eof()) != std::char_traits<char>::eof());
49         assert(sb.pbackfail(std::char_traits<char>::eof()) == std::char_traits<char>::eof());
50         assert(sb.str() == "123");
51     }
52     {
53         testbuf<char> sb("123");
54         assert(sb.sgetc() == '1');
55         assert(sb.snextc() == '2');
56         assert(sb.snextc() == '3');
57         assert(sb.sgetc() == '3');
58         assert(sb.snextc() == std::char_traits<char>::eof());
59         assert(sb.pbackfail('3') == '3');
60         assert(sb.pbackfail('3') == '3');
61         assert(sb.pbackfail(std::char_traits<char>::eof()) != std::char_traits<char>::eof());
62         assert(sb.pbackfail(std::char_traits<char>::eof()) == std::char_traits<char>::eof());
63         assert(sb.str() == "133");
64     }
65     {
66         testbuf<wchar_t> sb(L"123", std::ios_base::in);
67         assert(sb.sgetc() == L'1');
68         assert(sb.snextc() == L'2');
69         assert(sb.snextc() == L'3');
70         assert(sb.sgetc() == L'3');
71         assert(sb.snextc() == std::char_traits<wchar_t>::eof());
72         assert(sb.pbackfail(L'3') == L'3');
73         assert(sb.pbackfail(L'3') == std::char_traits<wchar_t>::eof());
74         assert(sb.pbackfail(L'2') == L'2');
75         assert(sb.pbackfail(std::char_traits<wchar_t>::eof()) != std::char_traits<wchar_t>::eof());
76         assert(sb.pbackfail(std::char_traits<wchar_t>::eof()) == std::char_traits<wchar_t>::eof());
77         assert(sb.str() == L"123");
78     }
79     {
80         testbuf<wchar_t> sb(L"123");
81         assert(sb.sgetc() == L'1');
82         assert(sb.snextc() == L'2');
83         assert(sb.snextc() == L'3');
84         assert(sb.sgetc() == L'3');
85         assert(sb.snextc() == std::char_traits<wchar_t>::eof());
86         assert(sb.pbackfail(L'3') == L'3');
87         assert(sb.pbackfail(L'3') == L'3');
88         assert(sb.pbackfail(std::char_traits<wchar_t>::eof()) != std::char_traits<wchar_t>::eof());
89         assert(sb.pbackfail(std::char_traits<wchar_t>::eof()) == std::char_traits<wchar_t>::eof());
90         assert(sb.str() == L"133");
91     }
92 }
93