Home | History | Annotate | Download | only in filebuf.virtuals
      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 // <fstream>
     11 
     12 // int_type pbackfail(int_type c = traits::eof());
     13 
     14 // This test is not entirely portable
     15 
     16 #include <fstream>
     17 #include <cassert>
     18 
     19 template <class CharT>
     20 struct test_buf
     21     : public std::basic_filebuf<CharT>
     22 {
     23     typedef std::basic_filebuf<CharT>  base;
     24     typedef typename base::char_type   char_type;
     25     typedef typename base::int_type    int_type;
     26     typedef typename base::traits_type traits_type;
     27 
     28     char_type* eback() const {return base::eback();}
     29     char_type* gptr()  const {return base::gptr();}
     30     char_type* egptr() const {return base::egptr();}
     31     void gbump(int n) {base::gbump(n);}
     32 
     33     virtual int_type pbackfail(int_type c = traits_type::eof()) {return base::pbackfail(c);}
     34 };
     35 
     36 int main()
     37 {
     38     {
     39         test_buf<char> f;
     40         assert(f.open("underflow.dat", std::ios_base::in) != 0);
     41         assert(f.is_open());
     42         assert(f.sbumpc() == '1');
     43         assert(f.sgetc() == '2');
     44         assert(f.pbackfail('a') == -1);
     45     }
     46     {
     47         test_buf<char> f;
     48         assert(f.open("underflow.dat", std::ios_base::in | std::ios_base::out) != 0);
     49         assert(f.is_open());
     50         assert(f.sbumpc() == '1');
     51         assert(f.sgetc() == '2');
     52         assert(f.pbackfail('a') == 'a');
     53         assert(f.sbumpc() == 'a');
     54         assert(f.sgetc() == '2');
     55     }
     56 }
     57