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 // <fstream>
10 
11 // int_type pbackfail(int_type c = traits::eof());
12 
13 #include <fstream>
14 #include <cassert>
15 
16 #include "test_macros.h"
17 
18 template <class CharT>
19 struct test_buf
20     : public std::basic_filebuf<CharT>
21 {
22     typedef std::basic_filebuf<CharT>  base;
23     typedef typename base::char_type   char_type;
24     typedef typename base::int_type    int_type;
25     typedef typename base::traits_type traits_type;
26 
ebacktest_buf27     char_type* eback() const {return base::eback();}
gptrtest_buf28     char_type* gptr()  const {return base::gptr();}
egptrtest_buf29     char_type* egptr() const {return base::egptr();}
gbumptest_buf30     void gbump(int n) {base::gbump(n);}
31 
pbackfailtest_buf32     virtual int_type pbackfail(int_type c = traits_type::eof()) {return base::pbackfail(c);}
33 };
34 
main(int,char **)35 int main(int, char**)
36 {
37     {
38         test_buf<char> f;
39         assert(f.open("underflow.dat", std::ios_base::in) != 0);
40         assert(f.is_open());
41         assert(f.sbumpc() == '1');
42         assert(f.sgetc() == '2');
43         typename test_buf<char>::int_type pbackResult = f.pbackfail('a');
44         LIBCPP_ASSERT(pbackResult == -1);
45         if (pbackResult != -1) {
46             assert(f.sbumpc() == 'a');
47             assert(f.sgetc() == '2');
48         }
49     }
50     {
51         test_buf<char> f;
52         assert(f.open("underflow.dat", std::ios_base::in | std::ios_base::out) != 0);
53         assert(f.is_open());
54         assert(f.sbumpc() == '1');
55         assert(f.sgetc() == '2');
56         typename test_buf<char>::int_type pbackResult = f.pbackfail('a');
57         LIBCPP_ASSERT(pbackResult == 'a');
58         if (pbackResult != -1) {
59             assert(f.sbumpc() == 'a');
60             assert(f.sgetc() == '2');
61         }
62     }
63 
64   return 0;
65 }
66