xref: /llvm-project/libcxx/test/std/input.output/file.streams/fstreams/filebuf.virtuals/pbackfail.pass.cpp (revision df88d80337d5e0ee0e50ca599d6436854951d8fa)
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 // FILE_DEPENDENCIES: underflow.dat
14 
15 #include <fstream>
16 #include <cassert>
17 
18 #include "test_macros.h"
19 
20 template <class CharT>
21 struct test_buf
22     : public std::basic_filebuf<CharT>
23 {
24     typedef std::basic_filebuf<CharT>  base;
25     typedef typename base::char_type   char_type;
26     typedef typename base::int_type    int_type;
27     typedef typename base::traits_type traits_type;
28 
ebacktest_buf29     char_type* eback() const {return base::eback();}
gptrtest_buf30     char_type* gptr()  const {return base::gptr();}
egptrtest_buf31     char_type* egptr() const {return base::egptr();}
gbumptest_buf32     void gbump(int n) {base::gbump(n);}
33 
pbackfailtest_buf34     virtual int_type pbackfail(int_type c = traits_type::eof()) {return base::pbackfail(c);}
35 };
36 
main(int,char **)37 int main(int, char**)
38 {
39     {
40         test_buf<char> f;
41         assert(f.open("underflow.dat", std::ios_base::in) != 0);
42         assert(f.is_open());
43         assert(f.sbumpc() == '1');
44         assert(f.sgetc() == '2');
45         typename test_buf<char>::int_type pbackResult = f.pbackfail('a');
46         LIBCPP_ASSERT(pbackResult == -1);
47         if (pbackResult != -1) {
48             assert(f.sbumpc() == 'a');
49             assert(f.sgetc() == '2');
50         }
51     }
52     {
53         test_buf<char> f;
54         assert(f.open("underflow.dat", std::ios_base::in | std::ios_base::out) != 0);
55         assert(f.is_open());
56         assert(f.sbumpc() == '1');
57         assert(f.sgetc() == '2');
58         typename test_buf<char>::int_type pbackResult = f.pbackfail('a');
59         LIBCPP_ASSERT(pbackResult == 'a');
60         if (pbackResult != -1) {
61             assert(f.sbumpc() == 'a');
62             assert(f.sgetc() == '2');
63         }
64     }
65 
66   return 0;
67 }
68